<commit_before>package core.aws.task.elb;

import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.model.Subnet;
import com.amazonaws.services.elasticloadbalancing.model.AccessLog;
import com.amazonaws.services.elasticloadbalancing.model.ConfigureHealthCheckRequest;
import com.amazonaws.services.elasticloadbalancing.model.ConnectionDraining;
import com.amazonaws.services.elasticloadbalancing.model.CreateLoadBalancerRequest;
import com.amazonaws.services.elasticloadbalancing.model.CrossZoneLoadBalancing;
import com.amazonaws.services.elasticloadbalancing.model.HealthCheck;
import com.amazonaws.services.elasticloadbalancing.model.Listener;
import com.amazonaws.services.elasticloadbalancing.model.LoadBalancerAttributes;
import com.amazonaws.services.elasticloadbalancing.model.ModifyLoadBalancerAttributesRequest;
import com.amazonaws.services.elasticloadbalancing.model.Tag;
import com.amazonaws.services.s3.model.SetBucketPolicyRequest;
import core.aws.client.AWS;
import core.aws.env.Context;
import core.aws.resource.elb.ELB;
import core.aws.workflow.Action;
import core.aws.workflow.Task;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author neo
 */
@Action("create-elb")
public class CreateELBTask extends Task<ELB> {
    public CreateELBTask(ELB elb) {
        super(elb);
    }

    @Override
    public void execute(Context context) throws Exception {
        CreateLoadBalancerRequest request = new CreateLoadBalancerRequest()
            .withLoadBalancerName(resource.name)
            .withScheme(resource.scheme.orElse(null))
            .withTags(new Tag().withKey("cloud-manager:env").withValue(context.env.name));

        if (resource.subnet != null) {
            request.withSecurityGroups(resource.securityGroup.remoteSecurityGroup.getGroupId())
                .withSubnets(resource.subnet.remoteSubnets.stream().map(Subnet::getSubnetId).collect(Collectors.toList()));
        } else {
            List<String> zones = AWS.ec2.availabilityZones();
            request.withAvailabilityZones(zones.get(0));
        }

        if (resource.listenHTTP) {
            request.getListeners().add(new Listener("HTTP", 80, 80));
        }

        if (resource.listenHTTPS) {
            request.getListeners().add(new Listener()
                .withProtocol("HTTPS")
                .withLoadBalancerPort(443)
                .withInstanceProtocol("HTTP")
                .withInstancePort(80)
                .withSSLCertificateId(resource.cert.remoteCert.getServerCertificateMetadata().getArn()));
        }

        resource.remoteELB = AWS.elb.createELB(request);

        configureELB(context.env.region);

        configureHealthCheck();

        context.output(String.format("elb/%s/DNS", resource.id), resource.remoteELB.getDNSName());
    }

    private void configureELB(Regions region) {
        LoadBalancerAttributes attributes = new LoadBalancerAttributes()<commit_msg>fix create ELB task to handle Amazon cert<commit_after>package core.aws.task.elb;

import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.model.Subnet;
import com.amazonaws.services.elasticloadbalancing.model.AccessLog;
import com.amazonaws.services.elasticloadbalancing.model.ConfigureHealthCheckRequest;
import com.amazonaws.services.elasticloadbalancing.model.ConnectionDraining;
import com.amazonaws.services.elasticloadbalancing.model.CreateLoadBalancerRequest;
import com.amazonaws.services.elasticloadbalancing.model.CrossZoneLoadBalancing;
import com.amazonaws.services.elasticloadbalancing.model.HealthCheck;
import com.amazonaws.services.elasticloadbalancing.model.Listener;
import com.amazonaws.services.elasticloadbalancing.model.LoadBalancerAttributes;
import com.amazonaws.services.elasticloadbalancing.model.ModifyLoadBalancerAttributesRequest;
import com.amazonaws.services.elasticloadbalancing.model.Tag;
import com.amazonaws.services.s3.model.SetBucketPolicyRequest;
import core.aws.client.AWS;
import core.aws.env.Context;
import core.aws.resource.elb.ELB;
import core.aws.workflow.Action;
import core.aws.workflow.Task;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author neo
 */
@Action("create-elb")
public class CreateELBTask extends Task<ELB> {
    public CreateELBTask(ELB elb) {
        super(elb);
    }

    @Override
    public void execute(Context context) throws Exception {
        CreateLoadBalancerRequest request = new CreateLoadBalancerRequest()
            .withLoadBalancerName(resource.name)
            .withScheme(resource.scheme.orElse(null))
            .withTags(new Tag().withKey("cloud-manager:env").withValue(context.env.name));

        if (resource.subnet != null) {
            request.withSecurityGroups(resource.securityGroup.remoteSecurityGroup.getGroupId())
                .withSubnets(resource.subnet.remoteSubnets.stream().map(Subnet::getSubnetId).collect(Collectors.toList()));
        } else {
            List<String> zones = AWS.ec2.availabilityZones();
            request.withAvailabilityZones(zones.get(0));
        }

        if (resource.listenHTTP) {
            request.getListeners().add(new Listener("HTTP", 80, 80));
        }

        if (resource.listenHTTPS) {
            String certARN = resource.amazonCertARN != null ? resource.amazonCertARN : resource.cert.remoteCert.getServerCertificateMetadata().getArn();
            request.getListeners().add(new Listener()
                .withProtocol("HTTPS")
                .withLoadBalancerPort(443)
                .withInstanceProtocol("HTTP")
                .withInstancePort(80)
                .withSSLCertificateId(certARN));
        }

        resource.remoteELB = AWS.elb.createELB(request);

        configureELB(context.env.region);

        configureHealthCheck();

        context.output(String.format("elb/%s/DNS", resource.id), resource.remoteELB.getDNSName());
    }

    private void configureELB(Regions region) {
        LoadBalancerAttributes attributes = new LoadBalancerAttributes()
{'fraction_non_alphanumeric': 0.1084137013634852, 'fraction_numerical': 0.004323245759893582, 'mean_word_length': 3.7587025316455698, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5896400', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1685, 'n_words': 230}