예제 #1
0
def create_function(fn_management_client, application_id, display_name, image,
                    memory_in_m_bs, timeout_in_seconds) -> fn_models.Function:
    """
    Creates a Function and waits for it to become available to use.

    :param fn_management_client: OCI FunctionsManagementClient client.
    :type fn_management_client: functions.FunctionsManagementClient

    :param application_id: The OCID of the Application which owns the Function.
    :type compartment_id: str

    :param display_name: The display name of the Function.
    :type display_name: str

    :param image: An accessible OCIR image implementing the function to be executed.
    :type image: str

    :param memory_in_m_bs: The maximum ammount of memory available (128, 256, 512, 1024) to the function in MB.
    :type image: int

    :param timeout_in_seconds: The maximum ammout of time a function can execute (30 - 120) in seconds.
    :type image: int

    :return: The Function.
    :rtype: fn_models.Function
    """
    create_function_details = fn_models.CreateFunctionDetails(
        application_id=application_id,
        display_name=display_name,
        image=image,
        memory_in_m_bs=memory_in_m_bs,
        timeout_in_seconds=timeout_in_seconds)
    result = fn_management_client.create_function(create_function_details)
    get_fn_response = oci.wait_until(
        fn_management_client,
        fn_management_client.get_function(result.data.id), 'lifecycle_state',
        'ACTIVE')
    print('Created Function: {}'.format(result.data.display_name))
    return result.data
예제 #2
0
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