Example usage for com.amazonaws.services.elasticloadbalancing.model CreateLoadBalancerPolicyRequest CreateLoadBalancerPolicyRequest

List of usage examples for com.amazonaws.services.elasticloadbalancing.model CreateLoadBalancerPolicyRequest CreateLoadBalancerPolicyRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.elasticloadbalancing.model CreateLoadBalancerPolicyRequest CreateLoadBalancerPolicyRequest.

Prototype

CreateLoadBalancerPolicyRequest

Source Link

Usage

From source file:com.netflix.spinnaker.clouddriver.aws.deploy.handlers.MigrateLoadBalancerStrategy.java

License:Apache License

/**
 * Applies any listener policies from the source load balancer to the target load balancer.
 *
 * Since policy names are unique to each load balancer, two policies with the same name in different load balancers
 * may contain different policy attributes. For the sake of simplicity, we assume that policies with the same name
 * are structurally the same, and do not attempt to reconcile any differences between attributes.
 *
 * We will, however, attempt to override the policies applied to a given listener if it's different, e.g., if the
 * source load balancer has policy "a" on port 7000, and the target load balancer has policy "b" on port 7000, we
 * will:/*from  w  w w.j a  v a  2 s  . co m*/
 *   1. create policy "a" if it doesn't exist on the target load balancer, then
 *   2. update the target load balancer so port 7000 will have only policy "a"
 */
public void applyListenerPolicies(AmazonElasticLoadBalancing sourceClient,
        AmazonElasticLoadBalancing targetClient, LoadBalancerDescription source, String loadBalancerName) {
    Set<String> policiesToRetrieve = new HashSet<>();
    Map<String, String> policyNameMap = new HashMap<>();
    source.getListenerDescriptions().forEach(d -> policiesToRetrieve.addAll(d.getPolicyNames()));
    List<PolicyDescription> sourcePolicies = sourceClient
            .describeLoadBalancerPolicies(new DescribeLoadBalancerPoliciesRequest()
                    .withLoadBalancerName(source.getLoadBalancerName()).withPolicyNames(policiesToRetrieve))
            .getPolicyDescriptions();
    List<PolicyDescription> targetPolicies = targetClient
            .describeLoadBalancerPolicies(
                    new DescribeLoadBalancerPoliciesRequest().withLoadBalancerName(loadBalancerName))
            .getPolicyDescriptions();

    sourcePolicies.forEach(p -> {
        Optional<PolicyDescription> match = targetPolicies.stream().filter(
                tp -> tp.getPolicyAttributeDescriptions().size() == p.getPolicyAttributeDescriptions().size()
                        && tp.getPolicyAttributeDescriptions().containsAll(p.getPolicyAttributeDescriptions()))
                .findFirst();

        if (match.isPresent()) {
            policyNameMap.put(p.getPolicyName(), match.get().getPolicyName());
        } else {
            String policyName = p.getPolicyName();
            if (policyName.startsWith("ELBSample-") || policyName.startsWith("ELBSecurityPolicy-")) {
                policyName = "migrated-" + policyName;
            }
            policyNameMap.put(p.getPolicyName(), policyName);
            CreateLoadBalancerPolicyRequest request = new CreateLoadBalancerPolicyRequest()
                    .withPolicyName(policyName).withLoadBalancerName(loadBalancerName)
                    .withPolicyTypeName(p.getPolicyTypeName());
            // only copy policy attributes if this is not a pre-defined policy
            // (as defined by the presence of 'Reference-Security-Policy'
            Optional<PolicyAttributeDescription> referencePolicy = p.getPolicyAttributeDescriptions().stream()
                    .filter(d -> d.getAttributeName().equals("Reference-Security-Policy")).findFirst();
            if (referencePolicy.isPresent()) {
                request.withPolicyAttributes(new PolicyAttribute(referencePolicy.get().getAttributeName(),
                        referencePolicy.get().getAttributeValue()));
            } else {
                request.withPolicyAttributes(p.getPolicyAttributeDescriptions().stream()
                        .map(d -> new PolicyAttribute(d.getAttributeName(), d.getAttributeValue()))
                        .collect(Collectors.toList()));
            }
            targetClient.createLoadBalancerPolicy(request);
        }
    });
    source.getListenerDescriptions().forEach(l -> targetClient.setLoadBalancerPoliciesOfListener(
            new SetLoadBalancerPoliciesOfListenerRequest().withLoadBalancerName(loadBalancerName)
                    .withLoadBalancerPort(l.getListener().getLoadBalancerPort()).withPolicyNames(
                            l.getPolicyNames().stream().map(policyNameMap::get).collect(Collectors.toList()))));
}