# -*- coding: UTF-8 -*- import sys from core import config_eventbus_access_args,validate_env, read_configuration, \ list_endpoints, list_subnets, list_hosted_zones_by_vpc, \ create_hosted_zone, change_resource_record_sets, create_endpoint, list_security_groups from core.tag import Tag KAFKA_HOST_DOMAIN = "kafka.us-west-2.amazonaws.com" def main(): args = config_eventbus_access_args() env = args['env'] validate_env(env) profile = args['profile'] region = args['region'] vpc_id = args['vpc-id'] security_group_id = args["security-group-id"] config = read_configuration(env) if region not in config['endpoint_services']: print("unsupported region") sys.exit(1) subnets = list_subnets(region, profile, vpc_id) if len(subnets) == 0: print("failed to retrieve subnets, please check") sys.exit(1) if security_group_id is None: sg_list = list_security_groups(region, profile, vpc_id) if len(sg_list) == 0: print("There are no available security groups") sys.exit(1) security_group_id = sg_list[0]['GroupId'] service_names_map = {svc["service_name"]: svc for svc in config['endpoint_services'][region]} endpoints = list_endpoints(region, profile, vpc_id, service_names_map.keys()) if len(endpoints) != len(service_names_map.keys()): processed_services = {endpoint["ServiceName"] for endpoint in endpoints} for sv_name, sv in service_names_map.items(): if sv_name in processed_services: continue selected_subnet_ids = [] for subnet in subnets: if subnet['State'] == 'available' and subnet['AvailabilityZoneId'] in sv['az_ids']: selected_subnet_ids.append(subnet['SubnetId']) if len(selected_subnet_ids) == 0: print(f"There are no available vpc subnets to be use in az {sv['az_id']}") sys.exit(1) print(f"start to create endpoint for services {sv_name}") broker_id = sv['broker'].split('.')[0] tags = [ Tag("Name", f"endpoint-tieventbus-{env}-{broker_id}"), Tag("environment", env), Tag("component", "cloud-platform-eventbus"), ] create_endpoint( region, profile, sv_name, vpc_id, selected_subnet_ids, security_group_id, tags ) endpoints = list_endpoints(region, profile, vpc_id, service_names_map.keys()) record_pair_list = [] for endpoint in endpoints: record = service_names_map[endpoint["ServiceName"]]['broker'] target = endpoint["DnsEntries"][0]["DnsName"] record_pair_list.append((record, target)) hosted_zones = list_hosted_zones_by_vpc(region, profile, vpc_id) hosted_zones_name_id_map = { hz['Name'][:-1] if hz['Name'].endswith(".") else hz['Name']: hz['HostedZoneId'] for hz in hosted_zones } if KAFKA_HOST_DOMAIN not in hosted_zones_name_id_map: hz = create_hosted_zone(region, profile, KAFKA_HOST_DOMAIN, vpc_id) hosted_zones_name_id_map[KAFKA_HOST_DOMAIN] = hz['Id'] kafka_hosted_zone_id = hosted_zones_name_id_map[KAFKA_HOST_DOMAIN] change_resource_record_sets(profile, kafka_hosted_zone_id, record_pair_list) print(f"success to config eventbus access in {vpc_id} at {region}") if __name__ == '__main__': main()