# -*- coding: UTF-8 -*- import sys from core import config_source_endpoint_args, validate_env, read_configuration, read_vpc_configuration, \ list_endpoints, list_subnets, create_endpoint, list_security_groups from core.tag import Tag def main(): args = config_source_endpoint_args() env = args['env'] validate_env(env) profile = args['profile'] config = read_configuration(env) vpc_config = read_vpc_configuration() endpoint_services = config["source_endpoint_services_config"]['endpoint_services'] region = config["source_endpoint_services_config"]["region"] vpc_id = vpc_config['main_vpc']['vpc-ids'][env] subnets = list_subnets(region, profile, vpc_id) if len(subnets) == 0: print("failed to retrieve subnets, please check") sys.exit(1) 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 endpoint_services} 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-cp-inter-region-{env}-{broker_id}"), Tag("environment", env), Tag("component", "cloud-platform-eventbus"), Tag("servicetype", "dedicated"), Tag("usedby", "dbaas-control-plane-base"), ] create_endpoint( region, profile, sv_name, vpc_id, selected_subnet_ids, security_group_id, tags ) if __name__ == '__main__': main()