Updated on 2024-08-07 GMT+08:00

Using a CTS Trigger

Prerequisites

You have created an agency on IAM. For details, see Configuring Agency Permissions.

Creating a CTS Trigger

  1. Log in to the FunctionGraph console. In the navigation pane, choose Functions > Function List.
  2. On the Function List page, click Create Function in the upper right corner.
  3. Set the following parameters:

    • Function Name: Enter a function name, for example, HelloWorld.
    • Agency: Select Use no agency.
    • Enterprise Project: Select default.
    • Runtime: Select Python 2.7.

  4. Click Create Function.
  5. On the Code tab page, copy the following code to the code window and click Deploy.

    # -*- coding:utf-8 -*-
    '''
    CTS trigger event:
    {
      "cts":  {
            "time": "",
            "user": {
                "name": "userName",
                "id": "",
                "domain": {
                    "name": "domainName",
                    "id": ""
                }
            },
            "request": {},
            "response": {},
            "code": 204,
            "service_type": "FunctionGraph",
            "resource_type": "",
            "resource_name": "",
            "resource_id": {},
            "trace_name": "",
            "trace_type": "ConsoleAction",
            "record_time": "",
            "trace_id": "",
            "trace_status": "normal"
        }
    }
    '''
    def handler (event, context):
        trace_name = event["cts"]["resource_name"]
        timeinfo = event["cts"]["time"]
        print(timeinfo+' '+trace_name)

  6. Choose Configuration > Triggers and click Create Trigger.

    Figure 1 Creating a trigger

  7. Configure the trigger information.

    Table 1 Trigger information

    Parameter

    Description

    Trigger Type

    Select Cloud Trace Service (CTS).

    Event Notification Name

    Enter a notification name, for example, Test.

    Service Type

    Select FunctionGraph.

    Resource Type

    Resource types supported by the selected service, such as triggers, instances, and functions.

    Trace Name

    Operations that can be performed on the selected resource type, such as creating or deleting a trigger.

  8. Click OK.

Configuring a CTS Event to Trigger the Function

  1. Return to the FunctionGraph console. In the navigation pane, choose Functions > Function List.
  2. Click the function to be configured to go to the function details page.
  3. On the function details page, select a version, and click Test. The Configure Test Event dialog box is displayed.
  4. Set the parameters described in Table 2 and click Save.

    Table 2 Test event information

    Parameter

    Description

    Configure Test Event

    You can choose to create a test event or edit an existing one.

    Use the default option Create new test event.

    Event Template

    Select cts-event-template.

    Event Name

    Enter an event name, for example, cts-test.

    Event data

    The system automatically loads the event data in the CTS event template. You can modify the event data as required.

  5. Click Test. The function test result is displayed.