Example usage for com.amazonaws.services.ec2 AmazonEC2 attachClassicLinkVpc

List of usage examples for com.amazonaws.services.ec2 AmazonEC2 attachClassicLinkVpc

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2 AmazonEC2 attachClassicLinkVpc.

Prototype

AttachClassicLinkVpcResult attachClassicLinkVpc(AttachClassicLinkVpcRequest attachClassicLinkVpcRequest);

Source Link

Document

Links an EC2-Classic instance to a ClassicLink-enabled VPC through one or more of the VPC's 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);
                    }//  ww  w.  ja va 2 s  .  c  o 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);
    }
}