def create_vcn(network_client, compartment_id, display_name, cidr_block) -> core_models.Vcn: """ Creates a Vcn and waits for it to become available to use. :param network_client: OCI VirtualNetworkClient client. :type network_client: oci.core.VirtualNetworkClient :param compartment_id: The OCID of the compartment which owns the Vcn. :type compartment_id: str :param display_name: The display name of the Vcn. :type display_name: str :param cidr_block: The CIDR range of the Vcn. :type cidr_block: str :return: The Vcn. :rtype: core_models.Vcn """ create_vcn_details = core_models.CreateVcnDetails( compartment_id=compartment_id, display_name=display_name, cidr_block=cidr_block) result = network_client.create_vcn(create_vcn_details) get_vcn_response = oci.wait_until(network_client, network_client.get_vcn(result.data.id), 'lifecycle_state', 'AVAILABLE') print('Created Vcn: {}'.format(result.data.display_name)) return result.data
def setup_resources(oci_cfg, compartment_id, name, image): """ Setup the minimal OCI resources required to establish an invocable function. :param compartment_id: The compartment_id in which to create the resources. :type compartment_id: str :param name: The name to identify all created resources with. :type name: str :param image: An accessible OCIR Function image that can be invoked. :type image: str """ identity_client = identity.IdentityClient(oci_cfg) network_client = core.VirtualNetworkClient(oci_cfg) fn_management_client = functions.FunctionsManagementClient(oci_cfg) vn_composite_ops = oci.core.VirtualNetworkClientCompositeOperations( network_client) print("setup_resources") # 1. A VCN is required to host subnets. vcn = get_unique_vcn_by_name(network_client, compartment_id, vcn_name(name)) if vcn is None: vcn_details = core_models.CreateVcnDetails( compartment_id=compartment_id, display_name=vcn_name(name), cidr_block="10.0.0.0/16") result = vn_composite_ops.create_vcn_and_wait_for_state( vcn_details, wait_for_states=[oci.core.models.Vcn.LIFECYCLE_STATE_AVAILABLE]) print('Created Vcn: {}'.format(result.data.display_name)) vcn = result.data # 2. An Internet Gateway is required to enable the VCN to talk to the wider world. ig = get_unique_ig_by_name(network_client, compartment_id, vcn.id, ig_name(name)) if ig is None: gateway_details = core_models.CreateInternetGatewayDetails( compartment_id=compartment_id, vcn_id=vcn.id, display_name=ig_name(name), is_enabled=True) result = vn_composite_ops.create_internet_gateway_and_wait_for_state( gateway_details, wait_for_states=[ oci.core.models.InternetGateway.LIFECYCLE_STATE_AVAILABLE ]) print('Created Internet Gateway: {}'.format(result.data.display_name)) ig = result.data # 3. We must configure the VCN's traffic to be routed through the Internet Gateway. rt = get_unique_route_table_by_name(network_client, compartment_id, vcn.id, drt_name(name)) if rt is not None: configure_ig(network_client, compartment_id, vcn.id, ig.id, drt_name(name)) # 4. A subnet is required to expose and be able invoke the function. # In multiple AD regions, subnets can be created in multiple ADs to provide redundency. sn = get_unique_subnet_by_name(network_client, compartment_id, vcn.id, subnet_name(name)) if sn is None: ad = get_availability_domains(identity_client, compartment_id)[0] print("Using AD: " + str(ad.name)) subnet_details = core_models.CreateSubnetDetails( compartment_id=compartment_id, vcn_id=vcn.id, availability_domain=ad.name, display_name=subnet_name(name), cidr_block="10.0.0.0/24") result = vn_composite_ops.create_subnet_and_wait_for_state( subnet_details, wait_for_states=[oci.core.models.Subnet.LIFECYCLE_STATE_AVAILABLE]) print('Created Subnet: {}'.format(result.data.display_name)) sn = result.data # 5. Create an Application to host and manage the function(s). app = get_unique_application_by_name(fn_management_client, compartment_id, application_name(name)) if app is None: fm_composite_ops = oci.functions.FunctionsManagementClientCompositeOperations( fn_management_client) app_details = fn_models.CreateApplicationDetails( compartment_id=compartment_id, display_name=application_name(name), subnet_ids=[sn.id]) result = fm_composite_ops.create_application_and_wait_for_state( app_details, wait_for_states=[fn_models.Application.LIFECYCLE_STATE_ACTIVE]) print('Created Application: {}'.format(result.data.display_name)) app = result.data # 6. Create a single Function, set its execution image and limits. fn = get_unique_function_by_name(fn_management_client, app.id, function_name(name)) if fn is None: fm_composite_ops = oci.functions.FunctionsManagementClientCompositeOperations( fn_management_client) fn_details = fn_models.CreateFunctionDetails( application_id=app.id, display_name=function_name(name), image=image, memory_in_mbs=128, timeout_in_seconds=30) result = fm_composite_ops.create_function_and_wait_for_state( fn_details, wait_for_states=[fn_models.Function.LIFECYCLE_STATE_ACTIVE]) print('Created Function: {}'.format(result.data.display_name)) fn = result.data