Example usage for com.amazonaws.services.ec2.model AttachClassicLinkVpcRequest AttachClassicLinkVpcRequest

List of usage examples for com.amazonaws.services.ec2.model AttachClassicLinkVpcRequest AttachClassicLinkVpcRequest

Introduction

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

Prototype

AttachClassicLinkVpcRequest

Source Link

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);
                    }//from   w w w  . j a v a 2s . co m
                }
                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);
    }
}