Example usage for com.amazonaws.services.route53.model CreateHealthCheckResult getHealthCheck

List of usage examples for com.amazonaws.services.route53.model CreateHealthCheckResult getHealthCheck

Introduction

In this page you can find the example usage for com.amazonaws.services.route53.model CreateHealthCheckResult getHealthCheck.

Prototype


public HealthCheck getHealthCheck() 

Source Link

Document

A complex type that contains identifying information about the health check.

Usage

From source file:com.deploymentio.ec2namer.helpers.DnsRegistrar.java

License:Apache License

protected Change createName(NamerRequest req, LambdaContext context, RequestedName additionalName,
        String assignedName) {//from   w ww  .j  ava2 s .  c om

    String fullAssignedName = req.createFqdn(assignedName);
    String fullAdditionalName = req.createFqdn(additionalName.getName());

    Instance instance = instanceLookup.lookup(context, req.getInstanceId());
    boolean usePublicHostname = req.isAlwaysUsePublicName() || StringUtils.isEmpty(instance.getVpcId());

    ResourceRecordSet set = new ResourceRecordSet(fullAdditionalName + ".", RRType.CNAME).withTTL(60l)
            .withResourceRecords(
                    new ResourceRecord(usePublicHostname ? instance.getPublicDnsName() : fullAssignedName))
            .withSetIdentifier(fullAssignedName).withWeight(Long.valueOf(additionalName.getWeight()));

    if (additionalName.isHealthChecked()) {

        String uuid = instance.getInstanceId() + "-" + UUID.randomUUID().toString();
        String hcId = null;

        for (int i = 0; i < 5; i++) {
            try {

                if (i > 0) {
                    Thread.sleep(5000);
                }

                CreateHealthCheckResult result = route53.createHealthCheck(
                        new CreateHealthCheckRequest().withCallerReference(uuid).withHealthCheckConfig(
                                new HealthCheckConfig().withFullyQualifiedDomainName(fullAssignedName)
                                        .withIPAddress(instance.getPublicIpAddress())
                                        .withPort(additionalName.getHealthCheckPort())
                                        .withType(additionalName.getHealthCheckType())
                                        .withResourcePath(additionalName.getHealthCheckUri())));

                hcId = result.getHealthCheck().getId();
                context.log("Created health-check: Name=" + fullAdditionalName + " Protocol="
                        + additionalName.getHealthCheckType() + " Port=" + additionalName.getHealthCheckPort()
                        + " Uri=" + additionalName.getHealthCheckUri() + " Id=" + hcId);

                // create tags on health-check
                route53.changeTagsForResource(new ChangeTagsForResourceRequest().withResourceId(hcId)
                        .withResourceType(TagResourceType.Healthcheck).withAddTags(
                                new Tag().withKey("Name").withValue(req.getEnvironment() + ":" + assignedName),
                                new Tag().withKey("InstanceId").withValue(req.getInstanceId())));

                break;

            } catch (Exception e) {
                context.log("Can't create health-check: Attempt= " + (i + 1) + " Name=" + fullAdditionalName
                        + " Protocol=" + additionalName.getHealthCheckType() + " Port="
                        + additionalName.getHealthCheckPort() + " Uri=" + additionalName.getHealthCheckUri()
                        + " Id=" + hcId + " Ref=" + uuid + " Error=" + e.getMessage());
            }
        }

        if (!StringUtils.isEmpty(hcId)) {
            set.setHealthCheckId(hcId);
        }
    }

    return new Change(ChangeAction.UPSERT, set);
}