Example usage for com.amazonaws.services.ec2.model ClassicLinkInstance getGroups

List of usage examples for com.amazonaws.services.ec2.model ClassicLinkInstance getGroups

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model ClassicLinkInstance getGroups.

Prototype


public java.util.List<GroupIdentifier> getGroups() 

Source Link

Document

A list of security groups.

Usage

From source file:com.netflix.spinnaker.clouddriver.aws.agent.ReconcileClassicLinkSecurityGroupsAgent.java

License:Apache License

void reconcileInstances(AmazonEC2 ec2, Map<String, String> groupNamesToIds,
        Collection<ClassicLinkInstance> instances) {
    RateLimiter apiRequestRateLimit = RateLimiter.create(5);
    StringBuilder report = new StringBuilder();
    for (ClassicLinkInstance i : instances) {
        List<String> existingClassicLinkGroups = i.getGroups().stream().map(GroupIdentifier::getGroupId)
                .collect(Collectors.toList());

        int maxNewGroups = deployDefaults.getMaxClassicLinkSecurityGroups() - existingClassicLinkGroups.size();
        if (maxNewGroups > 0) {
            String asgName = i.getTags().stream().filter(t -> AUTOSCALING_TAG.equals(t.getKey()))
                    .map(Tag::getValue).findFirst().orElse(null);

            List<String> candidateGroupNames = getSecurityGroupNames(asgName);

            List<String> missingGroupIds = candidateGroupNames.stream().map(groupNamesToIds::get)
                    .filter(name -> name != null && !existingClassicLinkGroups.contains(name))
                    .limit(maxNewGroups).collect(Collectors.toList());

            if (!missingGroupIds.isEmpty()) {
                List<String> groupIds = new ArrayList<>(existingClassicLinkGroups);
                groupIds.addAll(missingGroupIds);
                if (deployDefaults
                        .getReconcileClassicLinkSecurityGroups() == AwsConfiguration.DeployDefaults.ReconcileMode.MODIFY) {
                    apiRequestRateLimit.acquire();
                    try {
                        ec2.attachClassicLinkVpc(new AttachClassicLinkVpcRequest().withVpcId(i.getVpcId())
                                .withGroups(groupIds).withInstanceId(i.getInstanceId()));
                    } catch (AmazonServiceException ase) {
                        log.warn("Failed calling attachClassicLinkVpc", ase);
                    }/*  w w w .j  av a2s . c  om*/
                }
                report.append("\n\t").append(Strings.padStart(i.getInstanceId(), 24, ' '))
                        .append(missingGroupIds);
            }
        }
    }
    if (report.length() > 0) {
        log.info("Attach to classicLinkVpc: account: " + account.getName() + ", region: " + region + report);
    }
}