예제 #1
0
def invoke_function(oci_cfg, compartment_id, name, content):
    """
    Invoke a Function!

    :param compartment_id: The compartment_id in of the Function.
    :type compartment_id: str

    :param name: The Function name.
    :type name: str

    :param content: The data to send when invoking the Function.
    :type content: str
    """
    fn_management_client = functions.FunctionsManagementClient(oci_cfg)

    print("invoke_function")

    app = get_unique_application_by_name(fn_management_client, compartment_id,
                                         application_name(name))
    fn = get_unique_function_by_name(fn_management_client, app.id,
                                     function_name(name))

    # 8. Create an invocation client and invoke the Function!
    invoke_client = functions.FunctionsInvokeClient(
        oci_cfg, service_endpoint=fn.invoke_endpoint)

    resp = invoke_client.invoke_function(fn.id, invoke_function_body=content)
    print(resp.data.text)
예제 #2
0
def setup_resources(oci_cfg, compartment_id, name, image: str):
    """
    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)

    print("setup_resources")

    #  1. A VCN is required to host subnets.
    vcn_display_name = vcn_name(name)
    vcn_cidr_block = "10.0.0.0/16"
    vcn = create_vcn(network_client, compartment_id, vcn_display_name,
                     vcn_cidr_block)

    # 2. An Internet Gateway is required to enable the VCN to talk to the wider world.
    ig_display_name = ig_name(name)
    ig = create_ig(network_client, compartment_id, vcn.id, ig_display_name)

    # 3. We must configure the VCN's traffic to be routed through the Internet Gateway.
    drt_display_name = drt_name(name)
    configure_ig(network_client, compartment_id, vcn.id, ig.id,
                 drt_display_name)

    # 4. To create subnets we need to place them in a valid 'Availability Domain' for
    # our 'Region'.
    ad = get_availability_domains(identity_client, compartment_id)[0]
    print("Using AD: ", ad.name)

    # 5. 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.
    subnet_display_name = subnet_name(name)
    subnet_cidr_block = "10.0.0.0/24"
    subnet = create_subnet(network_client, compartment_id, vcn.id,
                           subnet_display_name, ad.name, subnet_cidr_block)

    # 6. Create an Application to host and manage the function(s).
    app_display_name = application_name(name)
    subnet_ids = [subnet.id]
    app = create_application(fn_management_client, compartment_id,
                             app_display_name, subnet_ids)

    # 7. Create a single Function, set its execution image and limits.
    fn_display_name = function_name(name)
    memory_in_mbs = 128
    timeout_in_seconds = 30
    fn = create_function(fn_management_client, app.id, fn_display_name, image,
                         memory_in_mbs, timeout_in_seconds)
예제 #3
0
def teardown_resources(oci_cfg, compartment_id, name: str):
    """
    Clean up all Function resources for this example.

    NB: If nay errors occur, please tidy up resources manually using the OCI console.

    :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
    """
    network_client = core.VirtualNetworkClient(oci_cfg)
    identity_client = identity.IdentityClient(oci_cfg)
    fn_management_client = functions.FunctionsManagementClient(oci_cfg)

    print("teardown_resources")

    vcn = get_unique_vcn_by_name(network_client, compartment_id,
                                 vcn_name(name))
    ig = get_unique_ig_by_name(network_client, compartment_id, vcn.id,
                               ig_name(name))
    rt = get_unique_route_table_by_name(network_client, compartment_id, vcn.id,
                                        drt_name(name))
    sn = get_unique_subnet_by_name(network_client, compartment_id, vcn.id,
                                   subnet_name(name))
    app = get_unique_application_by_name(fn_management_client, compartment_id,
                                         application_name(name))
    fn = get_unique_function_by_name(fn_management_client, app.id,
                                     function_name(name))

    if fn is not None:
        delete_function(fn_management_client, fn.id)

    if app is not None:
        delete_application(fn_management_client, app.id)

    if sn is not None:
        delete_subnet(network_client, sn.id)

    if rt is not None:
        prepare_route_table_for_delete(network_client, rt.id)

    if ig is not None:
        delete_ig(network_client, ig.id)

    if vcn is not None:
        delete_vcn(network_client, vcn.id)

    return
예제 #4
0
                        "<function-name> <request payload>")

    compartment_name = sys.argv[1]
    app_name = sys.argv[2]
    fn_name = sys.argv[3]

    cfg = config.from_file(file_location=os.getenv("OCI_CONFIG_PATH",
                                                   config.DEFAULT_LOCATION),
                           profile_name=os.getenv("OCI_CONFIG_PROFILE",
                                                  config.DEFAULT_PROFILE))

    if int(os.getenv("DEBUG", "0")) > 0:
        requests_log = logging.getLogger("requests.packages.urllib3")
        requests_log.setLevel(logging.DEBUG)
        requests_log.propagate = True
        cfg.update({"log_requests": True})
    functions_client = functions.FunctionsManagementClient(cfg)
    config.validate_config(cfg)

    compartment = get_compartment(cfg, compartment_name)

    app = get_app(functions_client, app_name, compartment)

    fn = get_function(functions_client, app, fn_name)

    invoke_client = functions.FunctionsInvokeClient(
        cfg, service_endpoint=fn.invoke_endpoint)

    resp = invoke_client.invoke_function(fn.id, sys.argv[4])
    print(resp.data.text)
예제 #5
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
예제 #6
0
def teardown_resources(oci_cfg, compartment_id, name):
    """
    Clean up all Function resources for this example.

    NB: If nay errors occur, please tidy up resources manually using the OCI console.

    :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
    """
    network_client = core.VirtualNetworkClient(oci_cfg)
    fn_management_client = functions.FunctionsManagementClient(oci_cfg)

    vn_composite_ops = oci.core.VirtualNetworkClientCompositeOperations(
        network_client)
    fm_composite_ops = oci.functions.FunctionsManagementClientCompositeOperations(
        fn_management_client)

    print("teardown_resources")

    # Collect the OCI resources to delete.
    vcn = ig = rt = sn = app = fn = None

    vcn = get_unique_vcn_by_name(network_client, compartment_id,
                                 vcn_name(name))
    if vcn is not None:
        ig = get_unique_ig_by_name(network_client, compartment_id, vcn.id,
                                   ig_name(name))
        rt = get_unique_route_table_by_name(network_client, compartment_id,
                                            vcn.id, drt_name(name))
        sn = get_unique_subnet_by_name(network_client, compartment_id, vcn.id,
                                       subnet_name(name))

    app = get_unique_application_by_name(fn_management_client, compartment_id,
                                         application_name(name))
    if app is not None:
        fn = get_unique_function_by_name(fn_management_client, app.id,
                                         function_name(name))

    # Delete resources. Please note the ordering.
    if fn is not None:
        fm_composite_ops.delete_function_and_wait_for_state(
            fn.id,
            wait_for_states=[fn_models.Function.LIFECYCLE_STATE_DELETED])
        print('Delete Function: {}'.format(fn.id))

    if app is not None:
        fm_composite_ops.delete_application_and_wait_for_state(
            app.id,
            wait_for_states=[fn_models.Application.LIFECYCLE_STATE_DELETED])
        print('Delete Application: {}'.format(app.id))

    if sn is not None:
        vn_composite_ops.delete_subnet_and_wait_for_state(
            sn.id,
            wait_for_states=[
                oci.core.models.Subnet.LIFECYCLE_STATE_TERMINATED
            ])
        print('Delete Subnet: {}'.format(sn.id))

    if rt is not None:
        prepare_route_table_for_delete(network_client, rt.id)

    if ig is not None:
        vn_composite_ops.delete_internet_gateway_and_wait_for_state(
            ig.id,
            wait_for_states=[
                oci.core.models.InternetGateway.LIFECYCLE_STATE_TERMINATED
            ])
        print('Delete Internet Gateway: {}'.format(ig.id))

    if vcn is not None:
        vn_composite_ops.delete_vcn_and_wait_for_state(
            vcn.id,
            wait_for_states=[oci.core.models.Vcn.LIFECYCLE_STATE_TERMINATED])
        print('Delete VCN: {}'.format(vcn.id))