Example #1
0
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
Example #2
0
def check_create_vcn(client, composite_client, vcn):
    matched_vcn_ocid = None
    compartment_ocid = get_compartment_ocid_from_name(identity_client,
                                                      config["tenancy"],
                                                      vcn["compartment_name"])
    compartment_exist_check = check_if_compartment_exist(
        identity_client, compartment_ocid)
    compartment_available_check = check_if_compartment_is_active(
        identity_client, compartment_ocid)
    if compartment_exist_check and compartment_available_check:
        print_decorator(
            "COMPARTMENT EXIST AND IS IN AVAILABLE LIFECYCLE STATE")
        vcn_name_check = check_vcn_name_match(client, compartment_ocid,
                                              vcn["name"])
        vcn_cidr_check = check_vcn_cidr_match(client, compartment_ocid,
                                              vcn["cidr_block"])
        if vcn_name_check:
            matched_vcn_ocid = get_vcn_match_ocid(client, compartment_ocid,
                                                  vcn["name"])
        if vcn_cidr_check:
            matched_vcn_ocid = get_vcn_match_ocid(client,
                                                  compartment_ocid,
                                                  vcn_cidr=vcn["cidr_block"])
    if matched_vcn_ocid is not None:
        vcn_available_check = check_vcn_ocid_is_available(
            client, compartment_ocid, matched_vcn_ocid)
        if vcn_available_check:
            return matched_vcn_ocid
    elif matched_vcn_ocid is None:
        return create_vcn(client, composite_client, vcn)
def check_create_lpg(client, composite_client, vcn, lpg, vcn_ocid):
    matched_lpg_ocid = None
    compartment_ocid = get_compartment_ocid_from_name(identity_client,
                                                      config["tenancy"],
                                                      lpg["compartment_name"])
    compartment_exist_check = check_if_compartment_exist(
        identity_client, compartment_ocid)
    compartment_available_check = check_if_compartment_is_active(
        identity_client, compartment_ocid)

    vcn_exist = check_vcn_exist_by_ocid(client, compartment_ocid, vcn_ocid)
    vcn_available_state = check_vcn_ocid_is_available(client, compartment_ocid,
                                                      vcn_ocid)

    lpg_name_check = check_if_lpg_exist_by_name(client, compartment_ocid,
                                                vcn_ocid, lpg["name"])

    if compartment_exist_check and compartment_available_check:
        print_decorator(
            "COMPARTMENT EXIST AND IS IN AVAILABLE LIFECYCLE STATE")
        if vcn_exist and vcn_available_state and lpg_name_check:
            matched_lpg_ocid = get_lpg_ocid_by_lpg_name(
                client, compartment_ocid, vcn_ocid, lpg["name"])
            lpg_available_state = check_if_lpg_is_available(
                client, matched_lpg_ocid, compartment_ocid)
            if matched_lpg_ocid is not None and lpg_available_state:
                print_decorator("LPG ALREADY EXIST")
                return matched_lpg_ocid
        elif matched_lpg_ocid is None:
            matched_lpg_ocid = create_local_peering_gateway(
                composite_client, lpg["name"], compartment_ocid, vcn_ocid)
    return matched_lpg_ocid
def check_connect_lpg(client, lpg, source_lpg_ocid, peer_lpg_ocid):
    if_source_lpg_peered = False
    if_target_lpg_peered = False

    peering_response = None
    compartment_ocid = get_compartment_ocid_from_name(identity_client,
                                                      config["tenancy"],
                                                      lpg["compartment_name"])
    compartment_exist_check = check_if_compartment_exist(
        identity_client, compartment_ocid)
    compartment_available_check = check_if_compartment_is_active(
        identity_client, compartment_ocid)

    #     vcn_exist = check_vcn_exist_by_ocid(client, lpg["compartment_id"], source_vcn_ocid)
    #     vcn_available_state = check_vcn_ocid_is_available(client, lpg["compartment_id"], source_vcn_ocid)

    if_source_lpg_peered = check_peering_status(client, source_lpg_ocid)
    if_target_lpg_peered = check_peering_status(client, peer_lpg_ocid)

    #     if vcn_exist and vcn_available_state:
    if if_source_lpg_peered:
        print_decorator(
            "SOURCE LPG {} IS ALREADY PEERED".format(source_lpg_ocid))
        peering_response = None
    elif if_target_lpg_peered:
        print_decorator(
            "TARGET LPG {} IS ALREADY PEERED".format(peer_lpg_ocid))
        peering_response = None
    elif not if_source_lpg_peered and not if_target_lpg_peered:
        peering_response = connect_local_peering_gateway(
            client, source_lpg_ocid, peer_lpg_ocid)
    else:
        peering_response = None
    return peering_response
def check_create_subnet(client, subnet_details, vcn_ocid):
    matched_subnet_ocid = None
    compartment_ocid = get_compartment_ocid_from_name(
        identity_client, config["tenancy"], subnet_details["compartment_name"])
    compartment_exist_check = check_if_compartment_exist(
        identity_client, compartment_ocid)
    compartment_available_check = check_if_compartment_is_active(
        identity_client, compartment_ocid)

    if compartment_exist_check and compartment_available_check:
        vcn_exist = check_vcn_exist_by_ocid(client, compartment_ocid, vcn_ocid)
        vcn_available_state = check_vcn_ocid_is_available(
            client, compartment_ocid, vcn_ocid)

        if vcn_exist and vcn_available_state:
            subnet_name_match = check_subnet_name_match(
                client, compartment_ocid, subnet_details, vcn_ocid)
            if subnet_name_match:
                subnet_availability = check_subnet_availability(
                    client, compartment_ocid, subnet_details, vcn_ocid)
                if subnet_availability:
                    subnet_ocid = get_subnet_ocid_by_name(
                        client, compartment_ocid, subnet_details, vcn_ocid)
                    matched_subnet_ocid = subnet_ocid
                    print_decorator(
                        "SUBNET ALREADY EXISTS. SKIPPING SUBNET CREATION")
            if matched_subnet_ocid is None:
                matched_subnet_ocid = create_subnet(client, compartment_ocid,
                                                    subnet_details, vcn_ocid)
    return matched_subnet_ocid
Example #6
0
def check_create_dhcp(client, compartment_name, vcn, vcn_ocid, dhcp):
    matched_dhcp_ocid = None
    compartment_ocid = get_compartment_ocid_from_name(
        identity_client, config["tenancy"], compartment_name)
    compartment_exist_check = check_if_compartment_exist(
        identity_client, compartment_ocid)
    compartment_available_check = check_if_compartment_is_active(
        identity_client, compartment_ocid)

    vcn_exist = check_vcn_exist_by_ocid(client, compartment_ocid, vcn_ocid)
    vcn_available_state = check_vcn_ocid_is_available(
        client, compartment_ocid, vcn_ocid)

    dhcp_name_check = check_if_dhcp_options_exists_by_name(
        client, compartment_ocid, vcn_ocid, dhcp["name"])

    if compartment_exist_check and compartment_available_check:
        print_decorator(
            "COMPARTMENT EXIST AND IS IN AVAILABLE LIFECYCLE STATE")
        if vcn_exist and vcn_available_state and dhcp_name_check:
            matched_dhcp_ocid = get_dhcp_ocid_by_name(
                client, compartment_ocid, vcn_ocid, dhcp["name"])
            dhcp_available_state = check_if_dhcp_options_lifecycle_ocid(
                client, compartment_ocid, vcn_ocid, matched_dhcp_ocid)
            if matched_dhcp_ocid is not None and dhcp_available_state:
                print_decorator("DHCP ALREADY EXIST")
                return matched_dhcp_ocid
        elif matched_dhcp_ocid is None:
            matched_dhcp_ocid = create_dhcp(
                virtual_network_client, compartment_ocid, vcn_ocid, dhcp["options"], dhcp["name"])
    return matched_dhcp_ocid
def check_create_drg(client, drg):
    matched_drg_ocid = None
    compartment_ocid = get_compartment_ocid_from_name(identity_client,
                                                      config["tenancy"],
                                                      drg["compartment_name"])
    drg_name_check = check_if_drg_exist_by_name(client, compartment_ocid,
                                                drg["name"])
    compartment_exist_check = check_if_compartment_exist(
        identity_client, compartment_ocid)
    compartment_available_check = check_if_compartment_is_active(
        identity_client, compartment_ocid)
    if compartment_exist_check and compartment_available_check:
        print_decorator(
            "COMPARTMENT EXIST AND IS IN AVAILABLE LIFECYCLE STATE")
        if drg_name_check:
            matched_drg_ocid = get_drg_match_ocid(client, compartment_ocid,
                                                  drg["name"])
            drg_available_state = check_drg_ocid_is_available(
                client, compartment_ocid, matched_drg_ocid)
            if matched_drg_ocid is not None and drg_available_state:
                print_decorator("DRG ALREADY EXIST")
                return matched_drg_ocid
        elif matched_drg_ocid is None:
            matched_drg_ocid = create_drg(client, drg)
    return matched_drg_ocid
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 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
Example #10
0
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 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
Example #12
0
def class_dist(df, dep_var, prob_type):
    if not prob_type:
        # For classification problems
        print_decorator('Class Distribution')
        if not prob_type:
            classes = df[dep_var].unique()
            balance = df[dep_var].value_counts()

        fig = go.Figure(
            go.Bar(x=df[dep_var].value_counts().keys(),
                   y=df[dep_var].value_counts().values))
        fig.update_layout(
            xaxis_title="Classes",
            yaxis_title="Counts",
        )
        st.plotly_chart(fig)
    else:
        # For regresssion problems
        sns.distplot(df[dep_var])
        st.pyplot()
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
Example #14
0
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 check_create_drg_attachment(client, drg, vcn, drg_ocid, vcn_ocid):
    matched_drg_attachment_ocid = None
    compartment_ocid = get_compartment_ocid_from_name(identity_client,
                                                      config["tenancy"],
                                                      drg["compartment_name"])
    compartment_exist_check = check_if_compartment_exist(
        identity_client, compartment_ocid)
    compartment_available_check = check_if_compartment_is_active(
        identity_client, compartment_ocid)

    vcn_exist = check_vcn_exist_by_ocid(client, compartment_ocid, vcn_ocid)
    vcn_available_state = check_vcn_ocid_is_available(client, compartment_ocid,
                                                      vcn_ocid)

    drg_name_check = check_if_drg_exist_by_name(client, compartment_ocid,
                                                drg["name"])
    if compartment_exist_check and compartment_available_check:
        print_decorator(
            "COMPARTMENT EXIST AND IS IN AVAILABLE LIFECYCLE STATE")
        if vcn_exist and vcn_available_state and drg_name_check:
            drg_ocid = get_drg_match_ocid(client, compartment_ocid,
                                          drg["name"])
            drg_available_state = check_drg_ocid_is_available(
                client, compartment_ocid, drg_ocid)
            if drg_ocid is not None and drg_available_state:
                drg_attachment_ocid = filter_drg_attachment_id(
                    client, compartment_ocid, drg_ocid)
                if drg_attachment_ocid is not None:
                    drg_attached_state = get_drg_attachment_status(
                        client, drg_attachment_ocid)
                    if drg_attachment_ocid is not None and drg_attached_state:
                        matched_drg_attachment_ocid = drg_attachment_ocid
                        print_decorator("DRG ALREADY ATTACHED TO EXISTING VCN")
                        return matched_drg_attachment_ocid
                elif matched_drg_attachment_ocid is None:
                    matched_drg_attachment_ocid = drg_attach(
                        client, vcn_ocid, drg_ocid, drg["name"])
    return matched_drg_attachment_ocid
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