def check_vcn_cidr_match(client, compartment_id, vcn_cidr_block): is_match_found = False try: getResponse = client.list_vcns(compartment_id=compartment_id) vcns = convert_response_to_dict(getResponse) for vcn in vcns: vcn_cidr_ip = vcn["cidr_block"].split("/")[0] input_cidr_ip = vcn_cidr_block.split("/")[0] if vcn["cidr_block"] == vcn_cidr_block or vcn_cidr_ip == input_cidr_ip: is_match_found = True break else: is_match_found = False if is_match_found: print_decorator("VCN CIDR ALREADY EXIST. SKIPPING VCN CREATION") else: print_decorator("NO VCN CIDR MATCH FOUND.") return is_match_found except Exception as inst: exception = inst if inst.status and inst.message: error_handle("VCN", inst.status, inst.message) else: error_handle("VCN", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return False
def check_vcn_ocid_is_available(client, compartment_id, vcn_ocid): try: vcnResponse = client.get_vcn(vcn_id=vcn_ocid) vcn = convert_response_to_dict(vcnResponse) return True if vcn["lifecycle_state"] == "AVAILABLE" else False except Exception as inst: exception = inst if inst.status and inst.message: error_handle("VCN", inst.status, inst.message) else: error_handle("VCN", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return False
def check_vcn_exist_by_ocid(client, compartment_id, vcn_ocid): try: vcnResponse = client.get_vcn(vcn_id=vcn_ocid) vcn = convert_response_to_dict(vcnResponse) return True if vcn["id"] == vcn_ocid else False except Exception as inst: exception = inst if inst.status and inst.message: error_handle("VCN", inst.status, inst.message) else: error_handle("VCN", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return False
def check_if_drg_exist_by_name(client, compartment_ocid, drg_name): try: listDRGs = client.list_drgs(compartment_id=compartment_ocid) drgs = convert_response_to_dict(listDRGs) drg_name_extract = extract_value_by_field(drgs, "display_name") return True if drg_name in drg_name_extract else False except Exception as inst: exception = inst if inst.status and inst.message: error_handle("DRG", inst.status, inst.message) else: error_handle("DRG", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def connect_local_peering_gateway(client, source_lpg_id, peer_lpg_id): try: connect_lpg_details = oci.core.models.ConnectLocalPeeringGatewaysDetails( peer_id=peer_lpg_id) connect_lpg = client.connect_local_peering_gateways( source_lpg_id, connect_local_peering_gateways_details=connect_lpg_details) print_decorator('LPG CONNECTED') return convert_response_to_dict(connect_lpg) except Exception as inst: if inst.status and inst.message: error_handle("LPG CONNECTION", inst.status, inst.message) else: error_handle("LPG CONNECTION", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def check_if_compartment_exist(client, compartment_ocid): try: compartmentResponse = client.get_compartment( compartment_id=compartment_ocid, retry_strategy=oci.retry.DEFAULT_RETRY_STRATEGY, ) compartment_detail = convert_response_to_dict(compartmentResponse) return True if compartment_detail["id"] == compartment_ocid else False except Exception as inst: exception = inst if inst.status and inst.message: error_handle("COMPARTMENT", inst.status, inst.message) else: error_handle("COMPARTMENT", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return False
def create_dhcp(client, compartment_id, vcn_ocid, dhcp_options, dhcp_name): try: dhcpDetails = oci.core.models.CreateDhcpDetails( compartment_id=compartment_id, options=dhcp_options, vcn_id=vcn_ocid, display_name=dhcp_name) dhcpResponse = client.create_dhcp_options( create_dhcp_details=dhcpDetails) dhcpRes = convert_response_to_dict(dhcpResponse) return dhcpRes except Exception as inst: exception = inst if inst.status and inst.message: error_handle("VCN", inst.status, inst.message) else: error_handle("VCN", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def get_drg_match_ocid(client, compartment_ocid, drg_name): try: listDRGs = client.list_drgs(compartment_id=compartment_ocid) drgs = convert_response_to_dict(listDRGs) for drg in drgs: if drg["display_name"] == drg_name: return drg["id"] else: return None except Exception as inst: exception = inst if inst.status and inst.message: error_handle("DRG", inst.status, inst.message) else: error_handle("DRG", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def check_drg_ocid_is_available(client, compartment_ocid, drg_ocid): try: drg = client.get_drg(drg_id=drg_ocid, retry_strategy=oci.retry.DEFAULT_RETRY_STRATEGY) drg_dict = convert_response_to_dict(drg) if drg_dict is not None and drg_dict["lifecycle_state"] == "AVAILABLE": return True else: return False except Exception as inst: exception = inst if inst.status and inst.message: error_handle("DRG", inst.status, inst.message) else: error_handle("DRG", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def filter_compartments_by_state(compartmentList=[], compartmentState="ACTIVE"): try: """Filter Compartments by their lifecycle state, ACTIVE| DELETNG | DELETED | CREATING""" filteredCompartments = [ compartment for compartment in compartmentList if compartment["lifecycle_state"] == compartmentState ] return filteredCompartments except Exception as inst: exception = inst if inst.status and inst.message: error_handle("COMPARTMENT", inst.status, inst.message) else: error_handle("COMPARTMENT", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def get_drg_attachment_status(client, drg_attachment_ocid): try: drg_attachment = client.get_drg_attachment( drg_attachment_id=drg_attachment_ocid) drg_attachment_dict = convert_response_to_dict(drg_attachment) if drg_attachment_dict is not None and drg_attachment_dict[ "lifecycle_state"] == "ATTACHED": return True else: return False except Exception as inst: exception = inst if inst.status and inst.message: error_handle("DRG ATTACHMENT", inst.status, inst.message) else: error_handle("DRG ATTACHMENT", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def get_compartment_ocid_from_name(client, tenancy_ocid, compartment_name): compartment_ocid = None try: compartments = fetch_all_compartments_in_tenancy(client, tenancy_ocid) activeCompartments = filter_compartments_by_state( compartmentList=compartments, compartmentState="ACTIVE") for compartment in activeCompartments: if compartment_name == compartment["name"]: compartment_ocid = compartment["id"] return compartment_ocid except Exception as inst: exception = inst if inst.status and inst.message: error_handle("COMPARTMENT", inst.status, inst.message) else: error_handle("COMPARTMENT", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def get_lpg_ocid_by_lpg_name(client, compartment_ocid, vcn_ocid, lpg_name): lpg_id = None try: listLpgs = client.list_local_peering_gateways( compartment_id=compartment_ocid, vcn_id=vcn_ocid) lpgs = convert_response_to_dict(listLpgs) for lpg in lpgs: if lpg["display_name"] == lpg_name: lpg_id = lpg["id"] return lpg_id except Exception as inst: exception = inst if inst.status and inst.message: error_handle("LPG", inst.status, inst.message) else: error_handle("LPG", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def check_peering_status(client, lpg_ocid): try: lpg = client.get_local_peering_gateway( local_peering_gateway_id=lpg_ocid, retry_strategy=oci.retry.DEFAULT_RETRY_STRATEGY, ) lpg_dict = convert_response_to_dict(lpg) if lpg_dict["peering_status"] == "PEERED": return True else: return False except Exception as inst: exception = inst if inst.status and inst.message: error_handle("LPG", inst.status, inst.message) else: error_handle("LPG", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def drg_attach(client, vcn_ocid, drg_ocid, drg_name): try: drg_attach_result = client.create_drg_attachment( oci.core.models.CreateDrgAttachmentDetails(display_name=drg_name, vcn_id=vcn_ocid, drg_id=drg_ocid)) drg_attachment = oci.wait_until( client, client.get_drg_attachment(drg_attach_result.data.id), 'lifecycle_state', 'ATTACHED') print_decorator('CREATED DRG ATTACHMENT') drg_attach = convert_response_to_dict(drg_attachment) return drg_attach["id"] except Exception as inst: exception = inst if inst.status and inst.message: error_handle("DRG", inst.status, inst.message) else: error_handle("DRG", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def create_drg(client, drg): try: compartment_ocid = get_compartment_ocid_from_name( identity_client, config["tenancy"], drg["compartment_name"]) drg_result = client.create_drg( oci.core.models.CreateDrgDetails(compartment_id=compartment_ocid, display_name=drg["name"])) drg = oci.wait_until(client, client.get_drg(drg_result.data.id), 'lifecycle_state', 'AVAILABLE') print_decorator("CREATING DRG") drg_new = convert_response_to_dict(drg) return drg_new["id"] except Exception as inst: exception = inst if inst.status and inst.message: error_handle("DRG", inst.status, inst.message) else: error_handle("DRG", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def check_if_lpg_exist_by_name(client, compartment_ocid, vcn_ocid, lpg_name): if_match_found = False try: listLpgs = client.list_local_peering_gateways( compartment_id=compartment_ocid, vcn_id=vcn_ocid) lpgs = convert_response_to_dict(listLpgs) lpg_names = extract_value_by_field(lpgs, "display_name") if lpg_name in lpg_names: if_match_found = True else: if_match_found = False return if_match_found except Exception as inst: exception = inst if inst.status and inst.message: error_handle("LPG", inst.status, inst.message) else: error_handle("LPG", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def fetch_all_compartments_in_tenancy(client, tenancy_ocid): try: """Fetch all Compartments in Tenancy , and look across all subtrees.""" compartmentResponse = oci.pagination.list_call_get_all_results( client.list_compartments, compartment_id=tenancy_ocid, limit=200, access_level="ACCESSIBLE", compartment_id_in_subtree=True, retry_strategy=oci.retry.DEFAULT_RETRY_STRATEGY, ) return convert_response_to_dict(compartmentResponse) except Exception as inst: exception = inst if inst.status and inst.message: error_handle("COMPARTMENT", inst.status, inst.message) else: error_handle("COMPARTMENT", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def check_vcn_name_match(client, compartment_id, vcn_name): try: listVCNReponse = client.list_vcns(compartment_id=compartment_id) vcns = convert_response_to_dict(listVCNReponse) vcn_names = extract_value_by_field(vcns, "display_name") if vcn_name in vcn_names: print_decorator("VCN NAME ALREADY EXIST. SKIPPING VCN CREATION") return True else: print_decorator( "NO VCN NAME MATCH FOUND. VCN CIDR CHECK IN PROGRESS...") return False except Exception as inst: exception = inst if inst.status and inst.message: error_handle("VCN", inst.status, inst.message) else: error_handle("VCN", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return False
def filter_drg_attachment_id(client, compartment_ocid, drg_id): try: drg_attachment_id = None drg_attachments = client.list_drg_attachments( compartment_id=compartment_ocid) drg_attachments_dict = convert_response_to_dict(drg_attachments) for drg_attachment in drg_attachments_dict: if drg_attachment["drg_id"] == drg_id: drg_attachment_id = drg_attachment["id"] else: drg_attachment_id = None return drg_attachment_id except Exception as inst: exception = inst if inst.status and inst.message: error_handle("DRG ATTACHMENT", inst.status, inst.message) else: error_handle("DRG ATTACHMENT", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def check_if_lpg_is_available(client, lpg_ocid, compartment_ocid): is_match_found = False try: lpg = client.get_local_peering_gateway( local_peering_gateway_id=lpg_ocid) lpg_dict = convert_response_to_dict(lpg) if lpg_dict["lifecycle_state"] == "AVAILABLE": is_match_found = True else: is_match_found = False return is_match_found except Exception as inst: print(inst) exception = inst if inst.status and inst.message: error_handle("LPG", inst.status, inst.message) else: error_handle("LPG", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def get_vcn_match_ocid(client, compartment_id, vcn_name=None, vcn_cidr=None): try: listVCNReponse = client.list_vcns(compartment_id=compartment_id) vcns = convert_response_to_dict(listVCNReponse) vcn_ocid = None for vcn in vcns: if vcn_name is not None: if vcn["display_name"] == vcn_name: vcn_ocid = vcn["id"] elif vcn_cidr is not None: if vcn["cidr_block"] == vcn_cidr: vcn_ocid = vcn["id"] return vcn_ocid except Exception as inst: exception = inst if inst.status and inst.message: error_handle("VCN", inst.status, inst.message) else: error_handle("VCN", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return False
def get_subnet_ocid_by_name(client, compartment_ocid, subnet_detail, vcn_ocid): matched_subnet_ocid = None try: subnetsList = client.list_subnets( compartment_id=compartment_ocid, vcn_id=vcn_ocid, retry_strategy=oci.retry.DEFAULT_RETRY_STRATEGY, ) subnets = convert_response_to_dict(subnetsList) for subnet in subnets: if subnet["display_name"] == subnet_detail["name"]: matched_subnet_ocid = subnet["id"] return matched_subnet_ocid except Exception as inst: if inst.status and inst.message: error_handle("SUBNET", inst.status, inst.message) else: error_handle("SUBNET", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def create_subnet(client, compartment_ocid, subnet_detail, vcn_ocid): assign_public_ip = True if subnet_detail["is_public"] == "True" else False try: create_subnet_details = oci.core.models.CreateSubnetDetails( cidr_block=subnet_detail["cidr"], display_name=subnet_detail["name"], compartment_id=compartment_ocid, prohibit_public_ip_on_vnic=assign_public_ip, vcn_id=vcn_ocid) subnet_response = client.create_subnet( create_subnet_details=create_subnet_details, ) subnet = convert_response_to_dict(subnet_response) print_decorator("SUBNET CREATED") return subnet["id"] except Exception as inst: if inst.status and inst.message: error_handle("SUBNET", inst.status, inst.message) else: error_handle("SUBNET", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def get_dhcp_ocid_by_name(client, compartment_id, vcn_ocid, dhcp_name): is_match_found_ocid = None try: listDhcpOptions = client.list_dhcp_options(compartment_id=compartment_id, vcn_id=vcn_ocid, retry_strategy=oci.retry.DEFAULT_RETRY_STRATEGY,) DhcpOptions = convert_response_to_dict(listDhcpOptions) for dhcp in DhcpOptions: if dhcp["display_name"] == dhcp_name: is_match_found_ocid = dhcp["id"] else: is_match_found_ocid = None return is_match_found_ocid except Exception as inst: exception = inst if inst.status and inst.message: error_handle("DHCP", inst.status, inst.message) else: error_handle("DHCP", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def check_if_dhcp_options_lifecycle_ocid(client, compartment_id, vcn_ocid, dhcp_ocid): is_match_found = False try: listDhcpOptions = client.list_dhcp_options(compartment_id=compartment_id, vcn_id=vcn_ocid, retry_strategy=oci.retry.DEFAULT_RETRY_STRATEGY,) DhcpOptions = convert_response_to_dict(listDhcpOptions) for dhcp in DhcpOptions: if dhcp["lifecycle_state"] == "AVAILABLE": is_match_found = True else: is_match_found = False return is_match_found except Exception as inst: exception = inst if inst.status and inst.message: error_handle("DHCP", inst.status, inst.message) else: error_handle("DHCP", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def check_subnet_availability(client, compartment_ocid, subnet_detail, vcn_ocid): is_match_found = False try: subnetsList = client.list_subnets( compartment_id=compartment_ocid, vcn_id=vcn_ocid, retry_strategy=oci.retry.DEFAULT_RETRY_STRATEGY, ) subnets = convert_response_to_dict(subnetsList) for subnet in subnets: if subnet["display_name"] == subnet_detail["name"]: if subnet["lifecycle_state"] == "AVAILABLE": is_match_found = True return is_match_found except Exception as inst: if inst.status and inst.message: error_handle("SUBNET", inst.status, inst.message) else: error_handle("SUBNET", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def create_vcn(client, composite_client, vcn): try: compartment_ocid = get_compartment_ocid_from_name( identity_client, config["tenancy"], vcn["compartment_name"]) vcn_details = oci.core.models.CreateVcnDetails( cidr_block=vcn["cidr_block"], display_name=vcn["name"], compartment_id=compartment_ocid, dns_label=vcn["dns_label"]) VCNResponse = composite_client.create_vcn_and_wait_for_state( vcn_details, wait_for_states=[oci.core.models.Vcn.LIFECYCLE_STATE_AVAILABLE]) vcn = convert_response_to_dict(VCNResponse) print_decorator("CREATING VCN") return vcn["id"] except Exception as inst: exception = inst if inst.status and inst.message: error_handle("VCN", inst.status, inst.message) else: error_handle("VCN", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def create_local_peering_gateway(composite_client, lpg_name, compartment_id, vcn_ocid): try: create_lpg_details = oci.core.models.CreateLocalPeeringGatewayDetails( compartment_id=compartment_id, display_name=lpg_name, vcn_id=vcn_ocid) create_lpg_response = composite_client.create_local_peering_gateway_and_wait_for_state( create_lpg_details, wait_for_states=[ oci.core.models.LocalPeeringGateway.LIFECYCLE_STATE_AVAILABLE ]) lpg = create_lpg_response lpg_dict = convert_response_to_dict(lpg) print_decorator("CREATED LPG") return lpg_dict["id"] except Exception as inst: if inst.status and inst.message: error_handle("LPG", inst.status, inst.message) else: error_handle("LPG", "UNKNOWN", "UNKNOWN ERROR MESSAGE") return None
def error(self, reqId, errorCode, errorString): if error_handle(errorCode): self.done = True