def test_create_health_check_fqdn(self): self.set_http_response(status_code=201) hc = HealthCheck(ip_addr='', port=443, hc_type='HTTPS', resource_path='/health_check', fqdn='example.com') hc_xml = hc.to_xml() self.assertTrue('<FullyQualifiedDomainName>' in hc_xml) self.assertFalse('<IPAddress>' in hc_xml) response = self.service_connection.create_health_check(hc) hc_resp = response['CreateHealthCheckResponse']['HealthCheck']['HealthCheckConfig'] self.assertEqual(hc_resp['FullyQualifiedDomainName'], 'example.com') self.assertEqual(hc_resp['ResourcePath'], '/health_check') self.assertEqual(hc_resp['Type'], 'HTTPS') self.assertEqual(hc_resp['Port'], '443') self.assertEqual(hc_resp['ResourcePath'], '/health_check') self.assertEqual(response['CreateHealthCheckResponse']['HealthCheck']['Id'], 'f9abfe10-8d2a-4bbd-8f35-796f0f8572f2')
def test_create_health_check_ip_address(self): self.set_http_response(status_code=201) hc = HealthCheck(ip_addr='74.125.228.81', port=443, hc_type='HTTPS_STR_MATCH', resource_path='/health_check', string_match='OK') hc_xml = hc.to_xml() self.assertFalse('<FullyQualifiedDomainName>' in hc_xml) self.assertTrue('<IPAddress>' in hc_xml) response = self.service_connection.create_health_check(hc) hc_resp = response['CreateHealthCheckResponse']['HealthCheck']['HealthCheckConfig'] self.assertEqual(hc_resp['IPAddress'], '74.125.228.81') self.assertEqual(hc_resp['ResourcePath'], '/health_check') self.assertEqual(hc_resp['Type'], 'HTTPS_STR_MATCH') self.assertEqual(hc_resp['Port'], '443') self.assertEqual(hc_resp['ResourcePath'], '/health_check') self.assertEqual(hc_resp['SearchString'], 'OK') self.assertEqual(response['CreateHealthCheckResponse']['HealthCheck']['Id'], '34778cf8-e31e-4974-bad0-b108bd1623d3')
def test_create_health_check_ip_address(self): self.set_http_response(status_code=201) hc = HealthCheck(ip_addr='74.125.228.81', port=443, hc_type='HTTPS_STR_MATCH', resource_path='/health_check', string_match='OK') hc_xml = hc.to_xml() self.assertFalse('<FullyQualifiedDomainName>' in hc_xml) self.assertTrue('<IPAddress>' in hc_xml) response = self.service_connection.create_health_check(hc) hc_resp = response['CreateHealthCheckResponse']['HealthCheck'][ 'HealthCheckConfig'] self.assertEqual(hc_resp['IPAddress'], '74.125.228.81') self.assertEqual(hc_resp['ResourcePath'], '/health_check') self.assertEqual(hc_resp['Type'], 'HTTPS_STR_MATCH') self.assertEqual(hc_resp['Port'], '443') self.assertEqual(hc_resp['ResourcePath'], '/health_check') self.assertEqual(hc_resp['SearchString'], 'OK') self.assertEqual( response['CreateHealthCheckResponse']['HealthCheck']['Id'], '34778cf8-e31e-4974-bad0-b108bd1623d3')
def test_delete_health_check(self): hc = HealthCheck(ip_addr="54.217.7.118", port=80, hc_type="HTTP", resource_path="/testing") result = self.conn.create_health_check(hc) hc_id = result['CreateHealthCheckResponse']['HealthCheck']['Id'] result = self.conn.get_list_health_checks() found = False for hc in result['ListHealthChecksResponse']['HealthChecks']: if hc['Id'] == hc_id: found = True break self.assertTrue(found) result = self.conn.delete_health_check(hc_id) result = self.conn.get_list_health_checks() for hc in result['ListHealthChecksResponse']['HealthChecks']: self.assertFalse(hc['Id'] == hc_id)
def test_create_resource_record_set(self): hc = HealthCheck(ip_addr="54.217.7.118", port=80, hc_type="HTTP", resource_path="/testing") result = self.conn.create_health_check(hc) records = ResourceRecordSets( connection=self.conn, hosted_zone_id=self.zone.id, comment='Create DNS entry for test') change = records.add_change('CREATE', 'unittest.%s.' % self.base_domain, 'A', ttl=30, identifier='test', weight=1, health_check=result['CreateHealthCheckResponse']['HealthCheck']['Id']) change.add_value("54.217.7.118") records.commit() records = ResourceRecordSets(self.conn, self.zone.id) deleted = records.add_change('DELETE', "unittest.%s." % self.base_domain, "A", ttl=30, identifier='test', weight=1, health_check=result['CreateHealthCheckResponse']['HealthCheck']['Id']) deleted.add_value('54.217.7.118') records.commit()
def create_health_check(self, ip, port, protocol, request_interval=30, failure_threshold=3, resource_path=None, fqdn=None, string_match=None): botoconn = self.__get_boto_conn() hc_type = None if protocol == 'tcp': hc_type = 'TCP' elif protocol == 'http': hc_type = 'HTTP' elif protocol == 'https': hc_type = 'HTTPS' if not hc_type: raise AttributeError("Protocol must be one of [tcp, http, https]") if not ip: raise AttributeError("ip must be provided for healthcheck") if not port: raise AttributeError("port must be provided for healthcheck") if string_match and hc_type in ('HTTP', 'HTTPS'): hc_type += '_STR_MATCH' hc = HealthCheck(ip, port, hc_type, resource_path, fqdn, string_match, request_interval, failure_threshold) self.logger.debug(hc) response = botoconn.create_health_check(hc) #TODO need to find out what an error response looks like hcid = None if 'CreateHealthCheckResponse' in response: if 'HealthCheck' in response['CreateHealthCheckResponse']: hcid = self.store_healthcheck( response['CreateHealthCheckResponse']['HealthCheck']) if hcid: conf = response['CreateHealthCheckResponse']['HealthCheck'][ 'HealthCheckConfig'] self.logger.info("Created healthcheck {0}: {1}://{2}:{3}".format( hcid, conf['Type'], conf['IPAddress'], conf['Port'])) return hcid
def test_delete_health_check(): conn = boto.connect_route53("the_key", "the_secret") check = HealthCheck(ip_addr="10.0.0.25", port=80, hc_type="HTTP", resource_path="/") conn.create_health_check(check) checks = conn.get_list_health_checks( )["ListHealthChecksResponse"]["HealthChecks"] list(checks).should.have.length_of(1) health_check_id = checks[0]["Id"] conn.delete_health_check(health_check_id) checks = conn.get_list_health_checks( )["ListHealthChecksResponse"]["HealthChecks"] list(checks).should.have.length_of(0)
def test_delete_health_check(): conn = boto.connect_route53('the_key', 'the_secret') check = HealthCheck( ip_addr="10.0.0.25", port=80, hc_type="HTTP", resource_path="/", ) conn.create_health_check(check) checks = conn.get_list_health_checks()['ListHealthChecksResponse']['HealthChecks'] list(checks).should.have.length_of(1) health_check_id = checks[0]['Id'] conn.delete_health_check(health_check_id) checks = conn.get_list_health_checks()['ListHealthChecksResponse']['HealthChecks'] list(checks).should.have.length_of(0)
def test_use_health_check_in_resource_record_set(): conn = boto.connect_route53("the_key", "the_secret") check = HealthCheck(ip_addr="10.0.0.25", port=80, hc_type="HTTP", resource_path="/") check = conn.create_health_check(check)["CreateHealthCheckResponse"]["HealthCheck"] check_id = check["Id"] zone = conn.create_hosted_zone("testdns.aws.com") zone_id = zone["CreateHostedZoneResponse"]["HostedZone"]["Id"].split("/")[-1] changes = ResourceRecordSets(conn, zone_id) change = changes.add_change( "CREATE", "foo.bar.testdns.aws.com", "A", health_check=check_id ) change.add_value("1.2.3.4") changes.commit() record_sets = conn.get_all_rrsets(zone_id) record_sets[0].health_check.should.equal(check_id)
def test_create_https_health_check(self): hc = HealthCheck(ip_addr="54.217.7.118", port=80, hc_type="HTTPS", resource_path="/testing") result = self.conn.create_health_check(hc) self.assertEquals( result[u'CreateHealthCheckResponse'][u'HealthCheck'] [u'HealthCheckConfig'][u'Type'], 'HTTPS') self.assertEquals( result[u'CreateHealthCheckResponse'][u'HealthCheck'] [u'HealthCheckConfig'][u'IPAddress'], '54.217.7.118') self.assertEquals( result[u'CreateHealthCheckResponse'][u'HealthCheck'] [u'HealthCheckConfig'][u'Port'], '80') self.assertEquals( result[u'CreateHealthCheckResponse'][u'HealthCheck'] [u'HealthCheckConfig'][u'ResourcePath'], '/testing') self.conn.delete_health_check( result['CreateHealthCheckResponse']['HealthCheck']['Id'])
def test_create_https_health_check_fqdn(self): hc = HealthCheck(ip_addr=None, port=443, hc_type="HTTPS", resource_path="/", fqdn="google.com") result = self.conn.create_health_check(hc) self.assertEquals( result[u'CreateHealthCheckResponse'][u'HealthCheck'] [u'HealthCheckConfig'][u'Type'], 'HTTPS') self.assertEquals( result[u'CreateHealthCheckResponse'][u'HealthCheck'] [u'HealthCheckConfig'][u'FullyQualifiedDomainName'], 'google.com') self.assertEquals( result[u'CreateHealthCheckResponse'][u'HealthCheck'] [u'HealthCheckConfig'][u'Port'], '443') self.assertEquals( result[u'CreateHealthCheckResponse'][u'HealthCheck'] [u'HealthCheckConfig'][u'ResourcePath'], '/') self.assertFalse('IPAddress' in result[u'CreateHealthCheckResponse'] [u'HealthCheck'][u'HealthCheckConfig']) self.conn.delete_health_check( result['CreateHealthCheckResponse']['HealthCheck']['Id'])
def create_healthcheck(route, url): # Parse the URL and get required params. result = urlparse(url) hc_type = result.scheme.upper() default_port = 443 if hc_type == 'HTTPS' else 80 port = result.port if result.port else default_port path = result.path if result.path else None domain = result.hostname if result.hostname else result.netloc # Create health check for the given URL. hc = HealthCheck(ip_addr="", port=port, hc_type=hc_type, resource_path=path, fqdn=domain, string_match=None, request_interval=30, failure_threshold=3) response = route.create_health_check(hc) print("Created healthcheck for: " + url + "; Id: " + response.CreateHealthCheckResponse.HealthCheck.Id) return response.CreateHealthCheckResponse.HealthCheck.Id
def test_create_health_check_string_match(self): hc = HealthCheck(ip_addr="54.217.7.118", port=80, hc_type="HTTP_STR_MATCH", resource_path="/testing", string_match="test") result = self.conn.create_health_check(hc) self.assertEquals( result[u'CreateHealthCheckResponse'][u'HealthCheck'] [u'HealthCheckConfig'][u'Type'], 'HTTP_STR_MATCH') self.assertEquals( result[u'CreateHealthCheckResponse'][u'HealthCheck'] [u'HealthCheckConfig'][u'IPAddress'], '54.217.7.118') self.assertEquals( result[u'CreateHealthCheckResponse'][u'HealthCheck'] [u'HealthCheckConfig'][u'Port'], '80') self.assertEquals( result[u'CreateHealthCheckResponse'][u'HealthCheck'] [u'HealthCheckConfig'][u'ResourcePath'], '/testing') self.assertEquals( result[u'CreateHealthCheckResponse'][u'HealthCheck'] [u'HealthCheckConfig'][u'SearchString'], 'test') self.conn.delete_health_check( result['CreateHealthCheckResponse']['HealthCheck']['Id'])
def main(): argument_spec = ec2_argument_spec() argument_spec.update( dict( state=dict(choices=['present', 'absent'], default='present'), ip_address=dict(), port=dict(type='int'), type=dict(required=True, choices=[ 'HTTP', 'HTTPS', 'HTTP_STR_MATCH', 'HTTPS_STR_MATCH', 'TCP' ]), resource_path=dict(), fqdn=dict(), string_match=dict(), request_interval=dict(type='int', choices=[10, 30], default=30), failure_threshold=dict(type='int', choices=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], default=3), )) module = AnsibleModule(argument_spec=argument_spec) if not HAS_BOTO: module.fail_json(msg='boto 2.27.0+ required for this module') state_in = module.params.get('state') ip_addr_in = module.params.get('ip_address') port_in = module.params.get('port') type_in = module.params.get('type') resource_path_in = module.params.get('resource_path') fqdn_in = module.params.get('fqdn') string_match_in = module.params.get('string_match') request_interval_in = module.params.get('request_interval') failure_threshold_in = module.params.get('failure_threshold') if ip_addr_in is None and fqdn_in is None: module.fail_json(msg="parameter 'ip_address' or 'fqdn' is required") # Default port if port_in is None: if type_in in ['HTTP', 'HTTP_STR_MATCH']: port_in = 80 elif type_in in ['HTTPS', 'HTTPS_STR_MATCH']: port_in = 443 else: module.fail_json(msg="parameter 'port' is required for 'type' TCP") # string_match in relation with type if type_in in ['HTTP_STR_MATCH', 'HTTPS_STR_MATCH']: if string_match_in is None: module.fail_json( msg= "parameter 'string_match' is required for the HTTP(S)_STR_MATCH types" ) elif len(string_match_in) > 255: module.fail_json( msg="parameter 'string_match' is limited to 255 characters max" ) elif string_match_in: module.fail_json( msg= "parameter 'string_match' argument is only for the HTTP(S)_STR_MATCH types" ) region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module) # connect to the route53 endpoint try: conn = Route53Connection(**aws_connect_kwargs) except boto.exception.BotoServerError as e: module.fail_json(msg=e.error_message) changed = False action = None check_id = None wanted_config = HealthCheck(ip_addr_in, port_in, type_in, resource_path_in, fqdn_in, string_match_in, request_interval_in, failure_threshold_in) existing_check = find_health_check(conn, wanted_config) if existing_check: check_id = existing_check.Id existing_config = to_health_check(existing_check.HealthCheckConfig) if state_in == 'present': if existing_check is None: action = "create" check_id = create_health_check(conn, wanted_config).HealthCheck.Id changed = True else: diff = health_check_diff(existing_config, wanted_config) if diff: action = "update" update_health_check(conn, existing_check.Id, int(existing_check.HealthCheckVersion), wanted_config) changed = True elif state_in == 'absent': if check_id: action = "delete" conn.delete_health_check(check_id) changed = True else: module.fail_json(msg="Logic Error: Unknown state") module.exit_json(changed=changed, health_check=dict(id=check_id), action=action)
msg= "parameter 'string_match' argument is only for the HTTP(S)_STR_MATCH types" ) region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module) # connect to the route53 endpoint try: conn = Route53Connection(**aws_connect_kwargs) except boto.exception.BotoServerError, e: module.fail_json(msg=e.error_message) changed = False action = None check_id = None wanted_config = HealthCheck(ip_addr_in, port_in, type_in, resource_path_in, fqdn_in, string_match_in, request_interval_in, failure_threshold_in) existing_check = find_health_check(conn, wanted_config) if existing_check: check_id = existing_check.Id existing_config = to_health_check(existing_check.HealthCheckConfig) if state_in == 'present': if existing_check is None: action = "create" check_id = create_health_check(conn, wanted_config).HealthCheck.Id changed = True else: diff = health_check_diff(existing_config, wanted_config) if not diff: action = "update"
def test_create_health_check_invalid_request_interval(self): """Test that health checks cannot be created with an invalid 'request_interval'. """ self.assertRaises(AttributeError, lambda: HealthCheck(**self.health_check_params(request_interval=5)))