Creating a Trigger
Function
This API is used to create a trigger.
Calling Method
For details, see Calling APIs.
Authorization Information
Each account has all the permissions required to call all APIs, but IAM users must be assigned the required permissions. For details about the required permissions, see Permissions Policies and Supported Actions.
URI
POST /v2/{project_id}/fgs/triggers/{function_urn}
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| project_id | Yes | String | Parameter description: Project ID. For details, see Obtaining a Project ID. Constraints N/A Value range: 32 characters, including letters and digits. Default value: N/A |
| function_urn | Yes | String | Parameter description: URN of the function. For details, see Function Model. Constraints N/A Value range: Only letters, digits, underscores (_), hyphens (-), and colons (:) are allowed. Default value: N/A |
Request Parameters
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| X-Auth-Token | Yes | String | Parameter description: User token. The token can be obtained by calling the IAM API. The value of X-Subject-Token in the response header is the user token. Constraints N/A Value range: N/A Default value: N/A |
| Content-Type | Yes | String | Message body type (format). |
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| trigger_type_code | Yes | String | Parameter description: Trigger type. Constraints None Range Default Value N/A |
| trigger_status | No | String | Parameter description: Trigger status. Constraints None Range Default Value DISABLED |
| event_type_code | No | String | Parameter description: Message code. Constraints None Value range: None Default value: None |
| event_data | Yes | TriggerEventDataRequestBody object | Parameter description: Create a trigger event attribute. Constraints None Value range: None Default value: None |
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| name | No | String | Timer trigger: trigger name APIG trigger: API name CTS trigger: notification name OBS trigger: event notification name. The default value is the trigger ID. |
| schedule_type | No | String | Timer trigger type (timer trigger parameter). This parameter is mandatory for timer triggers.
|
| schedule | No | String | Triggering rule. (timer trigger parameter). This parameter is mandatory for timer triggers. When the trigger type is Cron, the Cron expression is used. |
| user_event | No | String | Additional information (timer trigger parameter). When the timer trigger triggers the function, the execution event (the event parameter of the function) is {"version": "v1.0", "time": "2018-06-01T08:30:00+08:00", "trigger_type": "TIMER", "trigger_name": "Timer_001", "user_event": "Additional information you entered"}. |
| type | No | Integer | API visibility(APIG trigger parameter). This parameter is mandatory for APIG triggers. |
| path | No | String | Path of the API (APIG trigger parameter). This parameter is mandatory for APIG triggers. |
| protocol | No | String | Request protocol of the API (APIG trigger parameter). This parameter is mandatory for APIG triggers. |
| req_method | No | String | Request method of the API (APIG trigger parameter). This parameter is mandatory for APIG triggers. |
| group_id | No | String | ID of the group to which the API belongs (APIG trigger parameter). This parameter is mandatory for APIG triggers. |
| group_name | No | String | Name of the API group to which the API belongs. |
| match_mode | No | String | Matching mode of the API (APIG trigger parameter). This parameter is mandatory for APIG triggers. |
| env_name | No | String | Environment in which the API is published (APIG trigger parameter). This parameter is mandatory for APIG triggers. |
| env_id | No | String | ID of the environment in which the API has been published (APIG trigger parameter) This parameter is mandatory for APIG triggers. |
| auth | No | String | Security authentication (APIG trigger parameter). This parameter is mandatory for APIG triggers.
|
| func_info | No | ApigTriggerFuncInfo object | FunctionGraph backend details (APIG trigger parameter). This parameter is mandatory for APIG triggers. |
| sl_domain | No | String | Subdomain name allocated by the APIG system by default (APIG trigger parameter). |
| backend_type | No | String | Backend type of the API (APIG trigger parameter). |
| operations | No | Array of strings | Custom operations (CTS trigger parameter). This parameter is mandatory for CTS triggers. CTS collects operation records of subscribed cloud resources. If you create a function with a CTS trigger, collected operation records of specified cloud services will be passed as a parameter to invoke the function. |
| instance_id | No | String | Instance ID. This parameter is mandatory for DDS, RabbitMQ, and RocketMQ and Kafka triggers. APIG trigger: APIG gateway ID. DDS trigger: DDS instance ID. Kafka trigger: Kafka instance ID. RabbitMQ trigger: RabbitMQ instance ID. RocketMQ trigger: RocketMQ instance ID. |
| instance_ip | No | String | Database IP address (DynamoDB trigger parameter). |
| table_name | No | String | Database table name (DynamoDB trigger parameter). |
| collection_name | No | String | Collection name (DDS trigger parameter). This parameter is mandatory for DDS triggers. |
| db_name | No | String | Database name (DDS trigger parameter). This parameter is mandatory for DDS triggers. |
| db_password | No | String | DDS database password (DDS trigger parameter). This parameter is mandatory for DDS triggers. |
| batch_size | No | Integer | Batch size: Maximum number of data records that can be processed by the function at a time. This parameter is mandatory for DIS, DDS, Kafka, RocketMQ, and RabbitMQ triggers. DDS trigger: Enter a value from 1 to 10,000. DIS trigger: Enter a value from 1 to 10,000. Kafka trigger: Enter a value from 1 to 1,000. RabbitMQ trigger: Enter a value from 1 to 1,000. |
| queue_id | No | String | Queue ID (DMS trigger parameter). This parameter is mandatory for DMS triggers. |
| consumer_group_id | No | String | Consumer group ID (DMS trigger parameter). This parameter is mandatory for DMS triggers. |
| polling_interval | No | Integer | Pull period. This parameter is mandatory for DIS, DMS, and DYNAMODB triggers. |
| stream_name | No | String | Stream name (DIS trigger parameter). This parameter is mandatory for DIS triggers. |
| sharditerator_type | No | String | Starting position (DIS/DYNAMODB trigger parameter). This parameter is mandatory for DIS triggers. |
| polling_unit | No | String | Pull period unit (DIS trigger parameter). This parameter is mandatory for DIS triggers. |
| max_fetch_bytes | No | Integer | Max. fetch bytes (DIS trigger parameter). |
| is_serial | No | String | Serial data processing (DIS trigger parameter). If enabled, FunctionGraph pulls data from the stream only after previous data is processed. If disabled, FunctionGraph pulls data from the stream as long as the pull period ends. This parameter is mandatory for DIS triggers. |
| log_group_id | No | String | Log group ID (LTS trigger parameter). This parameter is mandatory for LTS triggers. |
| log_topic_id | No | String | Log stream ID (LTS trigger parameter). This parameter is mandatory for LTS triggers. |
| bucket | No | String | Bucket name (OBS trigger parameter). The name of the OBS bucket used as the event source cannot be the same as that of an existing bucket of the current user or another user. After being created, the bucket name cannot be modified. This parameter is mandatory for OBS triggers. |
| prefix | No | String | Prefix (OBS trigger parameter). Enter a prefix to limit notifications to objects whose names start with the matching characters. |
| suffix | No | String | Suffix (OBS trigger parameter). Enter a suffix to limit notifications to objects whose names end with the matching characters. |
| events | No | Array of strings | Trigger event (OBS trigger parameter). This parameter is mandatory for OBS triggers.
DeleteMarkerCreated: Deletes an object without specifying its version ID. |
| topic_urn | No | String | Topic URN (SMN trigger parameter) This parameter is mandatory for SMN triggers. |
| topic_ids | No | Array of strings | The Kafka topic ID list (Kafka trigger parameter). This parameter is mandatory for Kafka triggers. |
| kafka_user | No | String | Kafka account name (Kafka trigger parameter). |
| kafka_password | No | String | Kafka password (Kafka trigger parameter). |
| kafka_connect_address | No | String | Kafka instance connection address (Kafka trigger parameter). |
| kafka_ssl_enable | No | Boolean | Whether to enable SASL authentication(Kafka trigger parameter). |
| access_password | No | String | RabbitMQ account password (RabbitMQ trigger parameter). This parameter is mandatory for RabbitMQ triggers. |
| access_user | No | String | RabbitMQ username (RabbitMQ trigger parameter). |
| connect_address | No | String | Instance IP address (RabbitMQ trigger parameter). |
| exchange_name | No | String | Switch name (RabbitMQ trigger parameter). This parameter is mandatory for RabbitMQ triggers. |
| vhost | No | String | Virtual host (RabbitMQ trigger parameter). |
| ssl_enable | No | Boolean | Whether to enable SSL (RabbitMQ trigger parameter). |
| enable_acl | No | Boolean | Whether to enable username and password authentication (RocketMQ trigger parameter). |
| access_key | No | String | Connection username (RocketMQ trigger parameter). This parameter is mandatory when enable_acl is set to true. |
| secret_key | No | String | Connection password (RocketMQ trigger parameter). This parameter is mandatory when enable_acl is set to true. |
| dynamodb_user | No | String | Database username (DynamoDB trigger parameter). This parameter is mandatory. |
| dynamodb_password | No | String | Database password (DynamoDB trigger parameter). This parameter is mandatory. |
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| function_urn | No | String | Parameter description: Function URN. For details, see the function model descriptions. Range: None |
| invocation_type | No | String | Parameter description: Execution mode of a function. Range sync: synchronous execution async: asynchronous execution |
| timeout | Yes | Integer | Parameter description: Timeout allowed for APIG to request the FunctionGraph service. The unit is in millisecond. This parameter is mandatory for APIG triggers. Range 1 to 60000 seconds |
| version | No | String | Parameter description: Function version information. Range: None |
Response Parameters
Status code: 201
| Parameter | Type | Description |
|---|---|---|
| trigger_id | String | Parameter description: Trigger ID. Range: None |
| trigger_type_code | String | Parameter description: Trigger type. Range |
| trigger_status | String | Parameter description: Trigger status. Range |
| event_data | TriggerEventDataResponseBody object | Parameter description: Trigger attribute return body. Range: None |
| last_updated_time | String | Parameter description: Last update time. Range Time |
| created_time | String | Parameter description: Time when the trigger was created. Range Time |
| Parameter | Type | Description |
|---|---|---|
| name | String | Parameter description: Specifies the trigger name. Range: None |
| schedule_type | String | Parameter description: Timer trigger type (timer trigger parameter). Range
|
| schedule | String | Parameter description: Triggering rule. (timer trigger parameter). Range When the trigger type is Rate, the scheduled rule is used. When the trigger type is Cron, the Cron expression is used. |
| user_event | String | Parameter description: Additional information (timer trigger parameter). When the Timer trigger triggers function execution, the execution event (event parameter of the function) is as follows: {"version": "v1.0", "time": "2018-06-01T08:30:00+08:00", "trigger_type": "TIMER", "trigger_name": "Timer_001", "user_event": "Additional information you entered." Value range: N/A |
| triggerid | String | Parameter description: APIG trigger ID (APIG trigger parameter). Range: None |
| type | Integer | Parameter description: API type (APIG trigger parameter). Range |
| path | String | Parameter description: Path of the API (APIG trigger parameter). Range The value must comply with the URI naming rules. |
| protocol | String | Parameter description: Request protocol of the API (APIG trigger parameter). Range HTTP: HTTP protocol HTTPS: HTTPS protocol |
| req_method | String | Parameter description: Request mode of the API (APIG trigger parameter). Range GET: triggered by an HTTP GET request POST: triggered by an HTTP POST request PUT: triggered by an HTTP PUT request DELETE: triggered by an HTTP DELETE request HEAD: triggered by an HTTP HEAD request PATCH: triggered by an HTTP PATCH request OPTIONS: triggered by an HTTP OPTIONS request ANY: triggered by any HTTP request |
| group_id | String | Parameter description: ID of the group to which the API belongs (APIG trigger parameter). Range: None |
| group_name | String | Parameter description: Name of the group to which the API belongs (APIG trigger parameter). Range: None |
| match_mode | String | Parameter description: Matching mode of the API (APIG trigger parameter). Range |
| env_name | String | Parameter description: Environment in which the API is published (APIG trigger parameter). Range: None |
| env_id | String | Parameter description: ID of the environment in which the API has been published (APIG trigger parameter). Range: None |
| api_id | String | Parameter description: API ID (APIG trigger parameter). Range: None |
| api_name | String | Parameter description: API name (APIG trigger parameter). Range: None |
| auth | String | Parameter description: Security authentication (APIG trigger parameter). Range IAM: IAM authentication. This mode grants access permissions to IAM users only and is of medium security. APP: AppKey and AppSecret authentication. This mode is of high security and is recommended. NONE: Authentication is not required. All users can access this API. This mode is not recommended. |
| invoke_url | String | Parameter description: API calling address (APIG trigger parameter). Range: None |
| func_info | ApigTriggerFuncInfo object | Parameter description: Function backend information corresponding to the APIG trigger. Range: None |
| sl_domain | String | Parameter description: Subdomain name allocated by the APIG system by default (APIG trigger parameter). Range: None |
| backend_type | String | Parameter description: Backend type of the API (APIG trigger parameter). Range FUNCTION |
| instance_id | String | Parameter description: Instance ID. This parameter is mandatory for DDS, RabbitMQ, and RocketMQ and Kafka triggers. Range APIG trigger: APIG gateway ID. DDS trigger: DDS instance ID. Kafka trigger: Kafka instance ID. RabbitMQ trigger: RabbitMQ instance ID. RocketMQ trigger: RocketMQ instance ID. |
| instance_ip | String | Parameter description: Database IP address (DynamoDB trigger parameter). Range: None |
| table_name | String | Parameter description: Database table name (DynamoDB trigger parameter). Range: None |
| roma_app_id | String | Parameter description: ID of the integration application to which the API belongs (APIG trigger parameter). Range: None |
| operations | Array of strings | Parameter description: Custom operations (CTS trigger parameter). CTS collects operation records of subscribed cloud resources. If you create a function with a CTS trigger, collected operation records of specified cloud services will be passed as a parameter to invoke the function. Range: None |
| collection_name | String | Parameter description: Collection name (DDS trigger parameter). Range: None |
| db_name | String | Parameter description: Database name (DDS trigger parameter). Range: None |
| db_password | String | Parameter description: DDS database password (DDS trigger parameter). Range: None |
| db_user | String | Parameter description: DDS database username (DDS trigger parameter). Range: None |
| instance_addrs | Array of strings | Parameter description: DDS database instance address (DDS trigger parameter). Range: None |
| mode | String | Parameter description: DDS database instance type (DDS trigger parameter). Range Sharding: cluster instance ReplicaSet: replica set instance Single: single node instance |
| batch_size | Integer | Parameter description: Batch size: Maximum number of data records that can be processed by the function at a time. This parameter is mandatory for DIS, DDS, Kafka, RocketMQ, and RabbitMQ triggers. Range DDS trigger: Enter a value from 1 to 10,000. DIS trigger: Enter a value from 1 to 10,000. Kafka trigger: Enter a value from 1 to 1,000. RabbitMQ trigger: Enter a value from 1 to 1,000. RocketMQ trigger: Enter a value from 1 to 1,000. |
| queue_id | String | Parameter description: Queue ID (DMS trigger parameter). Range: None |
| consumer_group_id | String | Parameter description: Consumer group ID (DMS trigger parameter). Range: None |
| polling_interval | Integer | Parameter description: Pull period. Range: None |
| stream_name | String | Parameter description: Stream name (DIS trigger parameter). Range: None |
| sharditerator_type | String | Parameter description: Starting position (DIS/DynamoDB trigger parameter). Range |
| polling_unit | String | Parameter description: Pull period unit (DIS trigger parameter). Range ms: millisecond |
| max_fetch_bytes | Integer | Parameter description: Max. fetch bytes (DIS trigger parameter). Range: None |
| is_serial | String | Parameter description: Whether to process data in serial mode (DIS trigger parameter). Range |
| log_group_id | String | Parameter description: Log group ID (LTS trigger parameter). Range: None |
| log_topic_id | String | Parameter description: Log stream ID (LTS trigger parameter). Range: None |
| bucket | String | Parameter description: Bucket name (OBS trigger parameter). The name of the OBS bucket used as the event source cannot be the same as that of an existing bucket of the current user or another user. After being created, the bucket name cannot be modified. Range: None |
| prefix | String | Parameter description: Prefix (OBS trigger parameter). Enter a prefix to limit notifications to objects whose names start with the matching characters. Range: None |
| suffix | String | Parameter description: Suffix (OBS trigger parameter). Enter a suffix to limit notifications to objects whose names end with the matching characters. Range: None |
| events | Array of strings | Parameter description: Trigger event (OBS trigger parameter). Range ObjectCreated: all kinds of object creation operations, including PUT, POST, COPY, and part assembling Put: object upload using PUT Post: object upload using POST Copy: object copying using COPY CompleteMultipartUpload: Assemble parts in a multipart upload. ObjectRemoved: deleting objects Delete: object deletion with a version ID specified DeleteMarkerCreated: object deletion with no version ID specified |
| topic_urn | String | Parameter description: Topic URN (SMN trigger parameter) Range: None |
| topic_ids | Array of strings | Parameter description: The Kafka topic ID list (Kafka trigger parameter). Range: None |
| kafka_user | String | Parameter description: Kafka account name (Kafka trigger parameter). Range: None |
| kafka_password | String | Parameter description: Kafka password (Kafka trigger parameter). Range: None |
| kafka_connect_address | String | Parameter description: Kafka instance connection address (Kafka trigger parameter). Range: None |
| kafka_ssl_enable | Boolean | Parameter description: Whether to enable security authentication (Kafka trigger parameter). Range: None |
| access_password | String | Parameter description: RabbitMQ account password (RabbitMQ trigger parameter). Range: None |
| access_user | String | Parameter description: RabbitMQ username (RabbitMQ trigger parameter). Range: None |
| connect_address | String | Parameter description: Instance IP address (RabbitMQ trigger parameter). Range: None |
| exchange_name | String | Parameter description: Switch name (RabbitMQ trigger parameter). Range: None |
| vhost | String | Parameter description: Virtual host (RabbitMQ trigger parameter). Range: None |
| ssl_enable | Boolean | Parameter description: Whether to enable security authentication (RabbitMQ trigger parameter). Range |
| enable_acl | Boolean | Parameter description: Whether to enable username and password authentication (RocketMQ trigger parameter). Range false: no |
| access_key | String | Parameter description: Connection username (RocketMQ trigger parameter). This parameter is mandatory when enable_acl is set to true. Range: None |
| secret_key | String | Parameter description: Connection password (RocketMQ trigger parameter). This parameter is mandatory when enable_acl is set to true. Range: None |
| Parameter | Type | Description |
|---|---|---|
| function_urn | String | Parameter description: Function URN. For details, see the function model descriptions. Range: None |
| invocation_type | String | Parameter description: Execution mode of a function. Range sync: synchronous execution async: asynchronous execution |
| timeout | Integer | Parameter description: Timeout allowed for APIG to request the FunctionGraph service. The unit is in millisecond. This parameter is mandatory for APIG triggers. Range 1 to 60000 seconds |
| version | String | Parameter description: Function version information. Range: None |
Status code: 400
| Parameter | Type | Description |
|---|---|---|
| error_code | String | Error code. |
| error_msg | String | Error message. |
Status code: 401
| Parameter | Type | Description |
|---|---|---|
| error_code | String | Error code. |
| error_msg | String | Error message. |
Status code: 403
| Parameter | Type | Description |
|---|---|---|
| error_code | String | Error code. |
| error_msg | String | Error message. |
Status code: 404
| Parameter | Type | Description |
|---|---|---|
| error_code | String | Error code. |
| error_msg | String | Error message. |
Status code: 500
| Parameter | Type | Description |
|---|---|---|
| error_code | String | Error code. |
| error_msg | String | Error message. |
Example Requests
-
Create an OBS trigger named obs-event-1ec7 with events of uploading files to the input-picture2 bucket.
POST https://{Endpoint}/v2/{project_id}/fgs/triggers/{function_urn} { "trigger_type_code" : "OBS", "trigger_status" : "ACTIVE", "event_data" : { "bucket" : "input-picture2", "events" : [ "ObjectCreated" ], "name" : "obs-event-1ec7" } } -
Create a dedicated APIG trigger named API_test_function, with None authentication (temporary domain name) and 5000s timeout.
POST https://{Endpoint}/v2/{project_id}/fgs/triggers/{function_urn} { "trigger_type_code" : "DEDICATEDGATEWAY", "trigger_status" : "ACTIVE", "event_data" : { "func_info" : { "timeout" : 5000 }, "name" : "API_test_function", "instance_id" : "86ad3efc-c019-460e-91e7-xxx", "env_id" : "DEFAULT_ENVIRONMENT_RELEASE_ID", "env_name" : "RELEASE", "protocol" : "HTTPS", "auth" : "IAM", "group_id" : "5106210cf79e4xxxx", "sl_domain" : "5106210cf79e4xxxx.apic.{region_id}.xxx.com", "match_mode" : "SWA", "req_method" : "ANY", "backend_type" : "FUNCTION", "type" : 1, "path" : "/test_gxz_v1_billing" } } -
Create a Kafka trigger named 86ad3efcxx to pull messages from the PUSH_TOKEN_RECORD consumer group.
POST https://{Endpoint}/v2/{project_id}/fgs/triggers/{function_urn} { "trigger_type_code" : "KAFKA", "trigger_status" : "DISABLED", "event_data" : { "instance_id" : "86ad3efc-c019-460e-91e7-317b1a89f95x", "topic_ids" : [ "PUSH_TOKEN_RECORD" ], "batch_size" : 100 } } -
Create a timer trigger to trigger a function every 3 minutes.
POST https://{Endpoint}/v2/{project_id}/fgs/triggers/{function_urn} { "trigger_type_code" : "TIMER", "trigger_status" : "ACTIVE", "event_data" : { "name" : "Timer-l8v2", "schedule" : "3m", "schedule_type" : "Rate" } } -
Create an LTS trigger. (An agency with LTS permissions is required.)
POST https://{Endpoint}/v2/{project_id}/fgs/triggers/{function_urn} { "trigger_type_code" : "LTS", "trigger_status" : "ACTIVE", "event_data" : { "log_group_id" : "ccd3fa30-7c86-4590-8a9d-XXX", "log_topic_id" : "5b4944dd-9603-481d-9b91-XXX" } } -
POST https://{Endpoint}/v2/{project_id}/fgs/triggers/{function_urn} { "trigger_type_code" : "SMN", "trigger_status" : "ACTIVE", "event_data" : { "topic_urn" : "urn:smn:{region}:{project_id}:Failed_times" } }
Example Responses
Status code: 201
{
"trigger_id" : "9a14fae1-78cf-4185-ac7a-429eb6dc41fb",
"trigger_type_code" : "TIMER",
"trigger_status" : "ACTIVE",
"event_data" : {
"name" : "Timer-cpg3",
"schedule" : "3m",
"schedule_type" : "Rate"
},
"last_updated_time" : "2022-11-09 16:37:24",
"created_time" : "2022-11-09 16:37:24"
} SDK Sample Code
The SDK sample code is as follows.
Java
-
Create an OBS trigger named obs-event-1ec7 with events of uploading files to the input-picture2 bucket.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
package com.huaweicloud.sdk.test; import com.huaweicloud.sdk.core.auth.ICredential; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ConnectionException; import com.huaweicloud.sdk.core.exception.RequestTimeoutException; import com.huaweicloud.sdk.core.exception.ServiceResponseException; import com.huaweicloud.sdk.functiongraph.v2.region.FunctionGraphRegion; import com.huaweicloud.sdk.functiongraph.v2.*; import com.huaweicloud.sdk.functiongraph.v2.model.*; import java.util.List; import java.util.ArrayList; public class CreateFunctionTriggerSolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); String projectId = "{project_id}"; ICredential auth = new BasicCredentials() .withProjectId(projectId) .withAk(ak) .withSk(sk); FunctionGraphClient client = FunctionGraphClient.newBuilder() .withCredential(auth) .withRegion(FunctionGraphRegion.valueOf("<YOUR REGION>")) .build(); CreateFunctionTriggerRequest request = new CreateFunctionTriggerRequest(); request.withFunctionUrn("{function_urn}"); CreateFunctionTriggerRequestBody body = new CreateFunctionTriggerRequestBody(); List<String> listEventDataEvents = new ArrayList<>(); listEventDataEvents.add("ObjectCreated"); TriggerEventDataRequestBody eventDatabody = new TriggerEventDataRequestBody(); eventDatabody.withName("obs-event-1ec7") .withBucket("input-picture2") .withEvents(listEventDataEvents); body.withEventData(eventDatabody); body.withTriggerStatus(CreateFunctionTriggerRequestBody.CreateFunctionTriggerRequestBody.fromValue("ACTIVE")); body.withTriggerTypeCode(CreateFunctionTriggerRequestBody.CreateFunctionTriggerRequestBody.fromValue("OBS")); request.withBody(body); try { CreateFunctionTriggerResponse response = client.createFunctionTrigger(request); System.out.println(response.toString()); } catch (ConnectionException e) { e.printStackTrace(); } catch (RequestTimeoutException e) { e.printStackTrace(); } catch (ServiceResponseException e) { e.printStackTrace(); System.out.println(e.getHttpStatusCode()); System.out.println(e.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } }
-
Create a dedicated APIG trigger named API_test_function, with None authentication (temporary domain name) and 5000s timeout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
package com.huaweicloud.sdk.test; import com.huaweicloud.sdk.core.auth.ICredential; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ConnectionException; import com.huaweicloud.sdk.core.exception.RequestTimeoutException; import com.huaweicloud.sdk.core.exception.ServiceResponseException; import com.huaweicloud.sdk.functiongraph.v2.region.FunctionGraphRegion; import com.huaweicloud.sdk.functiongraph.v2.*; import com.huaweicloud.sdk.functiongraph.v2.model.*; public class CreateFunctionTriggerSolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); String projectId = "{project_id}"; ICredential auth = new BasicCredentials() .withProjectId(projectId) .withAk(ak) .withSk(sk); FunctionGraphClient client = FunctionGraphClient.newBuilder() .withCredential(auth) .withRegion(FunctionGraphRegion.valueOf("<YOUR REGION>")) .build(); CreateFunctionTriggerRequest request = new CreateFunctionTriggerRequest(); request.withFunctionUrn("{function_urn}"); CreateFunctionTriggerRequestBody body = new CreateFunctionTriggerRequestBody(); ApigTriggerFuncInfo funcInfoEventData = new ApigTriggerFuncInfo(); funcInfoEventData.withTimeout(5000); TriggerEventDataRequestBody eventDatabody = new TriggerEventDataRequestBody(); eventDatabody.withName("API_test_function") .withType(1) .withPath("/test_gxz_v1_billing") .withProtocol(TriggerEventDataRequestBody.ProtocolEnum.fromValue("HTTPS")) .withReqMethod(TriggerEventDataRequestBody.ReqMethodEnum.fromValue("ANY")) .withGroupId("5106210cf79e4xxxx") .withMatchMode(TriggerEventDataRequestBody.MatchModeEnum.fromValue("SWA")) .withEnvName("RELEASE") .withEnvId("DEFAULT_ENVIRONMENT_RELEASE_ID") .withAuth(TriggerEventDataRequestBody.AuthEnum.fromValue("IAM")) .withFuncInfo(funcInfoEventData) .withSlDomain("5106210cf79e4xxxx.apic.{region_id}.xxx.com") .withBackendType(TriggerEventDataRequestBody.BackendTypeEnum.fromValue("FUNCTION")) .withInstanceId("86ad3efc-c019-460e-91e7-xxx"); body.withEventData(eventDatabody); body.withTriggerStatus(CreateFunctionTriggerRequestBody.CreateFunctionTriggerRequestBody.fromValue("ACTIVE")); body.withTriggerTypeCode(CreateFunctionTriggerRequestBody.CreateFunctionTriggerRequestBody.fromValue("DEDICATEDGATEWAY")); request.withBody(body); try { CreateFunctionTriggerResponse response = client.createFunctionTrigger(request); System.out.println(response.toString()); } catch (ConnectionException e) { e.printStackTrace(); } catch (RequestTimeoutException e) { e.printStackTrace(); } catch (ServiceResponseException e) { e.printStackTrace(); System.out.println(e.getHttpStatusCode()); System.out.println(e.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } }
-
Create a Kafka trigger named 86ad3efcxx to pull messages from the PUSH_TOKEN_RECORD consumer group.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
package com.huaweicloud.sdk.test; import com.huaweicloud.sdk.core.auth.ICredential; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ConnectionException; import com.huaweicloud.sdk.core.exception.RequestTimeoutException; import com.huaweicloud.sdk.core.exception.ServiceResponseException; import com.huaweicloud.sdk.functiongraph.v2.region.FunctionGraphRegion; import com.huaweicloud.sdk.functiongraph.v2.*; import com.huaweicloud.sdk.functiongraph.v2.model.*; import java.util.List; import java.util.ArrayList; public class CreateFunctionTriggerSolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); String projectId = "{project_id}"; ICredential auth = new BasicCredentials() .withProjectId(projectId) .withAk(ak) .withSk(sk); FunctionGraphClient client = FunctionGraphClient.newBuilder() .withCredential(auth) .withRegion(FunctionGraphRegion.valueOf("<YOUR REGION>")) .build(); CreateFunctionTriggerRequest request = new CreateFunctionTriggerRequest(); request.withFunctionUrn("{function_urn}"); CreateFunctionTriggerRequestBody body = new CreateFunctionTriggerRequestBody(); List<String> listEventDataTopicIds = new ArrayList<>(); listEventDataTopicIds.add("PUSH_TOKEN_RECORD"); TriggerEventDataRequestBody eventDatabody = new TriggerEventDataRequestBody(); eventDatabody.withInstanceId("86ad3efc-c019-460e-91e7-317b1a89f95x") .withBatchSize(100) .withTopicIds(listEventDataTopicIds); body.withEventData(eventDatabody); body.withTriggerStatus(CreateFunctionTriggerRequestBody.CreateFunctionTriggerRequestBody.fromValue("DISABLED")); body.withTriggerTypeCode(CreateFunctionTriggerRequestBody.CreateFunctionTriggerRequestBody.fromValue("KAFKA")); request.withBody(body); try { CreateFunctionTriggerResponse response = client.createFunctionTrigger(request); System.out.println(response.toString()); } catch (ConnectionException e) { e.printStackTrace(); } catch (RequestTimeoutException e) { e.printStackTrace(); } catch (ServiceResponseException e) { e.printStackTrace(); System.out.println(e.getHttpStatusCode()); System.out.println(e.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } }
-
Create a timer trigger to trigger a function every 3 minutes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
package com.huaweicloud.sdk.test; import com.huaweicloud.sdk.core.auth.ICredential; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ConnectionException; import com.huaweicloud.sdk.core.exception.RequestTimeoutException; import com.huaweicloud.sdk.core.exception.ServiceResponseException; import com.huaweicloud.sdk.functiongraph.v2.region.FunctionGraphRegion; import com.huaweicloud.sdk.functiongraph.v2.*; import com.huaweicloud.sdk.functiongraph.v2.model.*; public class CreateFunctionTriggerSolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); String projectId = "{project_id}"; ICredential auth = new BasicCredentials() .withProjectId(projectId) .withAk(ak) .withSk(sk); FunctionGraphClient client = FunctionGraphClient.newBuilder() .withCredential(auth) .withRegion(FunctionGraphRegion.valueOf("<YOUR REGION>")) .build(); CreateFunctionTriggerRequest request = new CreateFunctionTriggerRequest(); request.withFunctionUrn("{function_urn}"); CreateFunctionTriggerRequestBody body = new CreateFunctionTriggerRequestBody(); TriggerEventDataRequestBody eventDatabody = new TriggerEventDataRequestBody(); eventDatabody.withName("Timer-l8v2") .withScheduleType(TriggerEventDataRequestBody.ScheduleTypeEnum.fromValue("Rate")) .withSchedule("3m"); body.withEventData(eventDatabody); body.withTriggerStatus(CreateFunctionTriggerRequestBody.CreateFunctionTriggerRequestBody.fromValue("ACTIVE")); body.withTriggerTypeCode(CreateFunctionTriggerRequestBody.CreateFunctionTriggerRequestBody.fromValue("TIMER")); request.withBody(body); try { CreateFunctionTriggerResponse response = client.createFunctionTrigger(request); System.out.println(response.toString()); } catch (ConnectionException e) { e.printStackTrace(); } catch (RequestTimeoutException e) { e.printStackTrace(); } catch (ServiceResponseException e) { e.printStackTrace(); System.out.println(e.getHttpStatusCode()); System.out.println(e.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } }
-
Create an LTS trigger. (An agency with LTS permissions is required.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
package com.huaweicloud.sdk.test; import com.huaweicloud.sdk.core.auth.ICredential; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ConnectionException; import com.huaweicloud.sdk.core.exception.RequestTimeoutException; import com.huaweicloud.sdk.core.exception.ServiceResponseException; import com.huaweicloud.sdk.functiongraph.v2.region.FunctionGraphRegion; import com.huaweicloud.sdk.functiongraph.v2.*; import com.huaweicloud.sdk.functiongraph.v2.model.*; public class CreateFunctionTriggerSolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); String projectId = "{project_id}"; ICredential auth = new BasicCredentials() .withProjectId(projectId) .withAk(ak) .withSk(sk); FunctionGraphClient client = FunctionGraphClient.newBuilder() .withCredential(auth) .withRegion(FunctionGraphRegion.valueOf("<YOUR REGION>")) .build(); CreateFunctionTriggerRequest request = new CreateFunctionTriggerRequest(); request.withFunctionUrn("{function_urn}"); CreateFunctionTriggerRequestBody body = new CreateFunctionTriggerRequestBody(); TriggerEventDataRequestBody eventDatabody = new TriggerEventDataRequestBody(); eventDatabody.withLogGroupId("ccd3fa30-7c86-4590-8a9d-XXX") .withLogTopicId("5b4944dd-9603-481d-9b91-XXX"); body.withEventData(eventDatabody); body.withTriggerStatus(CreateFunctionTriggerRequestBody.CreateFunctionTriggerRequestBody.fromValue("ACTIVE")); body.withTriggerTypeCode(CreateFunctionTriggerRequestBody.CreateFunctionTriggerRequestBody.fromValue("LTS")); request.withBody(body); try { CreateFunctionTriggerResponse response = client.createFunctionTrigger(request); System.out.println(response.toString()); } catch (ConnectionException e) { e.printStackTrace(); } catch (RequestTimeoutException e) { e.printStackTrace(); } catch (ServiceResponseException e) { e.printStackTrace(); System.out.println(e.getHttpStatusCode()); System.out.println(e.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } }
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
package com.huaweicloud.sdk.test; import com.huaweicloud.sdk.core.auth.ICredential; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ConnectionException; import com.huaweicloud.sdk.core.exception.RequestTimeoutException; import com.huaweicloud.sdk.core.exception.ServiceResponseException; import com.huaweicloud.sdk.functiongraph.v2.region.FunctionGraphRegion; import com.huaweicloud.sdk.functiongraph.v2.*; import com.huaweicloud.sdk.functiongraph.v2.model.*; public class CreateFunctionTriggerSolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); String projectId = "{project_id}"; ICredential auth = new BasicCredentials() .withProjectId(projectId) .withAk(ak) .withSk(sk); FunctionGraphClient client = FunctionGraphClient.newBuilder() .withCredential(auth) .withRegion(FunctionGraphRegion.valueOf("<YOUR REGION>")) .build(); CreateFunctionTriggerRequest request = new CreateFunctionTriggerRequest(); request.withFunctionUrn("{function_urn}"); CreateFunctionTriggerRequestBody body = new CreateFunctionTriggerRequestBody(); TriggerEventDataRequestBody eventDatabody = new TriggerEventDataRequestBody(); eventDatabody.withTopicUrn("urn:smn:{region}:{project_id}:Failed_times"); body.withEventData(eventDatabody); body.withTriggerStatus(CreateFunctionTriggerRequestBody.CreateFunctionTriggerRequestBody.fromValue("ACTIVE")); body.withTriggerTypeCode(CreateFunctionTriggerRequestBody.CreateFunctionTriggerRequestBody.fromValue("SMN")); request.withBody(body); try { CreateFunctionTriggerResponse response = client.createFunctionTrigger(request); System.out.println(response.toString()); } catch (ConnectionException e) { e.printStackTrace(); } catch (RequestTimeoutException e) { e.printStackTrace(); } catch (ServiceResponseException e) { e.printStackTrace(); System.out.println(e.getHttpStatusCode()); System.out.println(e.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } }
Python
-
Create an OBS trigger named obs-event-1ec7 with events of uploading files to the input-picture2 bucket.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
# coding: utf-8 import os from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkfunctiongraph.v2.region.functiongraph_region import FunctionGraphRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkfunctiongraph.v2 import * if __name__ == "__main__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = os.environ["CLOUD_SDK_AK"] sk = os.environ["CLOUD_SDK_SK"] projectId = "{project_id}" credentials = BasicCredentials(ak, sk, projectId) client = FunctionGraphClient.new_builder() \ .with_credentials(credentials) \ .with_region(FunctionGraphRegion.value_of("<YOUR REGION>")) \ .build() try: request = CreateFunctionTriggerRequest() request.function_urn = "{function_urn}" listEventsEventData = [ "ObjectCreated" ] eventDatabody = TriggerEventDataRequestBody( name="obs-event-1ec7", bucket="input-picture2", events=listEventsEventData ) request.body = CreateFunctionTriggerRequestBody( event_data=eventDatabody, trigger_status="ACTIVE", trigger_type_code="OBS" ) response = client.create_function_trigger(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
-
Create a dedicated APIG trigger named API_test_function, with None authentication (temporary domain name) and 5000s timeout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
# coding: utf-8 import os from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkfunctiongraph.v2.region.functiongraph_region import FunctionGraphRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkfunctiongraph.v2 import * if __name__ == "__main__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = os.environ["CLOUD_SDK_AK"] sk = os.environ["CLOUD_SDK_SK"] projectId = "{project_id}" credentials = BasicCredentials(ak, sk, projectId) client = FunctionGraphClient.new_builder() \ .with_credentials(credentials) \ .with_region(FunctionGraphRegion.value_of("<YOUR REGION>")) \ .build() try: request = CreateFunctionTriggerRequest() request.function_urn = "{function_urn}" funcInfoEventData = ApigTriggerFuncInfo( timeout=5000 ) eventDatabody = TriggerEventDataRequestBody( name="API_test_function", type=1, path="/test_gxz_v1_billing", protocol="HTTPS", req_method="ANY", group_id="5106210cf79e4xxxx", match_mode="SWA", env_name="RELEASE", env_id="DEFAULT_ENVIRONMENT_RELEASE_ID", auth="IAM", func_info=funcInfoEventData, sl_domain="5106210cf79e4xxxx.apic.{region_id}.xxx.com", backend_type="FUNCTION", instance_id="86ad3efc-c019-460e-91e7-xxx" ) request.body = CreateFunctionTriggerRequestBody( event_data=eventDatabody, trigger_status="ACTIVE", trigger_type_code="DEDICATEDGATEWAY" ) response = client.create_function_trigger(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
-
Create a Kafka trigger named 86ad3efcxx to pull messages from the PUSH_TOKEN_RECORD consumer group.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
# coding: utf-8 import os from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkfunctiongraph.v2.region.functiongraph_region import FunctionGraphRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkfunctiongraph.v2 import * if __name__ == "__main__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = os.environ["CLOUD_SDK_AK"] sk = os.environ["CLOUD_SDK_SK"] projectId = "{project_id}" credentials = BasicCredentials(ak, sk, projectId) client = FunctionGraphClient.new_builder() \ .with_credentials(credentials) \ .with_region(FunctionGraphRegion.value_of("<YOUR REGION>")) \ .build() try: request = CreateFunctionTriggerRequest() request.function_urn = "{function_urn}" listTopicIdsEventData = [ "PUSH_TOKEN_RECORD" ] eventDatabody = TriggerEventDataRequestBody( instance_id="86ad3efc-c019-460e-91e7-317b1a89f95x", batch_size=100, topic_ids=listTopicIdsEventData ) request.body = CreateFunctionTriggerRequestBody( event_data=eventDatabody, trigger_status="DISABLED", trigger_type_code="KAFKA" ) response = client.create_function_trigger(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
-
Create a timer trigger to trigger a function every 3 minutes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
# coding: utf-8 import os from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkfunctiongraph.v2.region.functiongraph_region import FunctionGraphRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkfunctiongraph.v2 import * if __name__ == "__main__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = os.environ["CLOUD_SDK_AK"] sk = os.environ["CLOUD_SDK_SK"] projectId = "{project_id}" credentials = BasicCredentials(ak, sk, projectId) client = FunctionGraphClient.new_builder() \ .with_credentials(credentials) \ .with_region(FunctionGraphRegion.value_of("<YOUR REGION>")) \ .build() try: request = CreateFunctionTriggerRequest() request.function_urn = "{function_urn}" eventDatabody = TriggerEventDataRequestBody( name="Timer-l8v2", schedule_type="Rate", schedule="3m" ) request.body = CreateFunctionTriggerRequestBody( event_data=eventDatabody, trigger_status="ACTIVE", trigger_type_code="TIMER" ) response = client.create_function_trigger(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
-
Create an LTS trigger. (An agency with LTS permissions is required.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
# coding: utf-8 import os from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkfunctiongraph.v2.region.functiongraph_region import FunctionGraphRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkfunctiongraph.v2 import * if __name__ == "__main__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = os.environ["CLOUD_SDK_AK"] sk = os.environ["CLOUD_SDK_SK"] projectId = "{project_id}" credentials = BasicCredentials(ak, sk, projectId) client = FunctionGraphClient.new_builder() \ .with_credentials(credentials) \ .with_region(FunctionGraphRegion.value_of("<YOUR REGION>")) \ .build() try: request = CreateFunctionTriggerRequest() request.function_urn = "{function_urn}" eventDatabody = TriggerEventDataRequestBody( log_group_id="ccd3fa30-7c86-4590-8a9d-XXX", log_topic_id="5b4944dd-9603-481d-9b91-XXX" ) request.body = CreateFunctionTriggerRequestBody( event_data=eventDatabody, trigger_status="ACTIVE", trigger_type_code="LTS" ) response = client.create_function_trigger(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
# coding: utf-8 import os from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkfunctiongraph.v2.region.functiongraph_region import FunctionGraphRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkfunctiongraph.v2 import * if __name__ == "__main__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = os.environ["CLOUD_SDK_AK"] sk = os.environ["CLOUD_SDK_SK"] projectId = "{project_id}" credentials = BasicCredentials(ak, sk, projectId) client = FunctionGraphClient.new_builder() \ .with_credentials(credentials) \ .with_region(FunctionGraphRegion.value_of("<YOUR REGION>")) \ .build() try: request = CreateFunctionTriggerRequest() request.function_urn = "{function_urn}" eventDatabody = TriggerEventDataRequestBody( topic_urn="urn:smn:{region}:{project_id}:Failed_times" ) request.body = CreateFunctionTriggerRequestBody( event_data=eventDatabody, trigger_status="ACTIVE", trigger_type_code="SMN" ) response = client.create_function_trigger(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
Go
-
Create an OBS trigger named obs-event-1ec7 with events of uploading files to the input-picture2 bucket.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" functiongraph "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") projectId := "{project_id}" auth, err := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). WithProjectId(projectId). SafeBuild() if err != nil { fmt.Println(err) return } hcClient, err := functiongraph.FunctionGraphClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). SafeBuild() if err != nil { fmt.Println(err) return } client := functiongraph.NewFunctionGraphClient(hcClient) request := &model.CreateFunctionTriggerRequest{} request.FunctionUrn = "{function_urn}" var listEventsEventData = []string{ "ObjectCreated", } nameEventData:= "obs-event-1ec7" bucketEventData:= "input-picture2" eventDatabody := &model.TriggerEventDataRequestBody{ Name: &nameEventData, Bucket: &bucketEventData, Events: &listEventsEventData, } triggerStatusCreateFunctionTriggerRequestBody:= model.GetCreateFunctionTriggerRequestBodyTriggerStatusEnum().ACTIVE request.Body = &model.CreateFunctionTriggerRequestBody{ EventData: eventDatabody, TriggerStatus: &triggerStatusCreateFunctionTriggerRequestBody, TriggerTypeCode: model.GetCreateFunctionTriggerRequestBodyTriggerTypeCodeEnum().OBS, } response, err := client.CreateFunctionTrigger(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
-
Create a dedicated APIG trigger named API_test_function, with None authentication (temporary domain name) and 5000s timeout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" functiongraph "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") projectId := "{project_id}" auth, err := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). WithProjectId(projectId). SafeBuild() if err != nil { fmt.Println(err) return } hcClient, err := functiongraph.FunctionGraphClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). SafeBuild() if err != nil { fmt.Println(err) return } client := functiongraph.NewFunctionGraphClient(hcClient) request := &model.CreateFunctionTriggerRequest{} request.FunctionUrn = "{function_urn}" funcInfoEventData := &model.ApigTriggerFuncInfo{ Timeout: int32(5000), } nameEventData:= "API_test_function" typeEventData:= int32(1) pathEventData:= "/test_gxz_v1_billing" protocolEventData:= model.GetTriggerEventDataRequestBodyProtocolEnum().HTTPS reqMethodEventData:= model.GetTriggerEventDataRequestBodyReqMethodEnum().ANY groupIdEventData:= "5106210cf79e4xxxx" matchModeEventData:= model.GetTriggerEventDataRequestBodyMatchModeEnum().SWA envNameEventData:= "RELEASE" envIdEventData:= "DEFAULT_ENVIRONMENT_RELEASE_ID" authEventData:= model.GetTriggerEventDataRequestBodyAuthEnum().IAM slDomainEventData:= "5106210cf79e4xxxx.apic.{region_id}.xxx.com" backendTypeEventData:= model.GetTriggerEventDataRequestBodyBackendTypeEnum().FUNCTION instanceIdEventData:= "86ad3efc-c019-460e-91e7-xxx" eventDatabody := &model.TriggerEventDataRequestBody{ Name: &nameEventData, Type: &typeEventData, Path: &pathEventData, Protocol: &protocolEventData, ReqMethod: &reqMethodEventData, GroupId: &groupIdEventData, MatchMode: &matchModeEventData, EnvName: &envNameEventData, EnvId: &envIdEventData, Auth: &authEventData, FuncInfo: funcInfoEventData, SlDomain: &slDomainEventData, BackendType: &backendTypeEventData, InstanceId: &instanceIdEventData, } triggerStatusCreateFunctionTriggerRequestBody:= model.GetCreateFunctionTriggerRequestBodyTriggerStatusEnum().ACTIVE request.Body = &model.CreateFunctionTriggerRequestBody{ EventData: eventDatabody, TriggerStatus: &triggerStatusCreateFunctionTriggerRequestBody, TriggerTypeCode: model.GetCreateFunctionTriggerRequestBodyTriggerTypeCodeEnum().DEDICATEDGATEWAY, } response, err := client.CreateFunctionTrigger(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
-
Create a Kafka trigger named 86ad3efcxx to pull messages from the PUSH_TOKEN_RECORD consumer group.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" functiongraph "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") projectId := "{project_id}" auth, err := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). WithProjectId(projectId). SafeBuild() if err != nil { fmt.Println(err) return } hcClient, err := functiongraph.FunctionGraphClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). SafeBuild() if err != nil { fmt.Println(err) return } client := functiongraph.NewFunctionGraphClient(hcClient) request := &model.CreateFunctionTriggerRequest{} request.FunctionUrn = "{function_urn}" var listTopicIdsEventData = []string{ "PUSH_TOKEN_RECORD", } instanceIdEventData:= "86ad3efc-c019-460e-91e7-317b1a89f95x" batchSizeEventData:= int32(100) eventDatabody := &model.TriggerEventDataRequestBody{ InstanceId: &instanceIdEventData, BatchSize: &batchSizeEventData, TopicIds: &listTopicIdsEventData, } triggerStatusCreateFunctionTriggerRequestBody:= model.GetCreateFunctionTriggerRequestBodyTriggerStatusEnum().DISABLED request.Body = &model.CreateFunctionTriggerRequestBody{ EventData: eventDatabody, TriggerStatus: &triggerStatusCreateFunctionTriggerRequestBody, TriggerTypeCode: model.GetCreateFunctionTriggerRequestBodyTriggerTypeCodeEnum().KAFKA, } response, err := client.CreateFunctionTrigger(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
-
Create a timer trigger to trigger a function every 3 minutes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" functiongraph "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") projectId := "{project_id}" auth, err := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). WithProjectId(projectId). SafeBuild() if err != nil { fmt.Println(err) return } hcClient, err := functiongraph.FunctionGraphClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). SafeBuild() if err != nil { fmt.Println(err) return } client := functiongraph.NewFunctionGraphClient(hcClient) request := &model.CreateFunctionTriggerRequest{} request.FunctionUrn = "{function_urn}" nameEventData:= "Timer-l8v2" scheduleTypeEventData:= model.GetTriggerEventDataRequestBodyScheduleTypeEnum().RATE scheduleEventData:= "3m" eventDatabody := &model.TriggerEventDataRequestBody{ Name: &nameEventData, ScheduleType: &scheduleTypeEventData, Schedule: &scheduleEventData, } triggerStatusCreateFunctionTriggerRequestBody:= model.GetCreateFunctionTriggerRequestBodyTriggerStatusEnum().ACTIVE request.Body = &model.CreateFunctionTriggerRequestBody{ EventData: eventDatabody, TriggerStatus: &triggerStatusCreateFunctionTriggerRequestBody, TriggerTypeCode: model.GetCreateFunctionTriggerRequestBodyTriggerTypeCodeEnum().TIMER, } response, err := client.CreateFunctionTrigger(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
-
Create an LTS trigger. (An agency with LTS permissions is required.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" functiongraph "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") projectId := "{project_id}" auth, err := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). WithProjectId(projectId). SafeBuild() if err != nil { fmt.Println(err) return } hcClient, err := functiongraph.FunctionGraphClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). SafeBuild() if err != nil { fmt.Println(err) return } client := functiongraph.NewFunctionGraphClient(hcClient) request := &model.CreateFunctionTriggerRequest{} request.FunctionUrn = "{function_urn}" logGroupIdEventData:= "ccd3fa30-7c86-4590-8a9d-XXX" logTopicIdEventData:= "5b4944dd-9603-481d-9b91-XXX" eventDatabody := &model.TriggerEventDataRequestBody{ LogGroupId: &logGroupIdEventData, LogTopicId: &logTopicIdEventData, } triggerStatusCreateFunctionTriggerRequestBody:= model.GetCreateFunctionTriggerRequestBodyTriggerStatusEnum().ACTIVE request.Body = &model.CreateFunctionTriggerRequestBody{ EventData: eventDatabody, TriggerStatus: &triggerStatusCreateFunctionTriggerRequestBody, TriggerTypeCode: model.GetCreateFunctionTriggerRequestBodyTriggerTypeCodeEnum().LTS, } response, err := client.CreateFunctionTrigger(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" functiongraph "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") projectId := "{project_id}" auth, err := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). WithProjectId(projectId). SafeBuild() if err != nil { fmt.Println(err) return } hcClient, err := functiongraph.FunctionGraphClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). SafeBuild() if err != nil { fmt.Println(err) return } client := functiongraph.NewFunctionGraphClient(hcClient) request := &model.CreateFunctionTriggerRequest{} request.FunctionUrn = "{function_urn}" topicUrnEventData:= "urn:smn:{region}:{project_id}:Failed_times" eventDatabody := &model.TriggerEventDataRequestBody{ TopicUrn: &topicUrnEventData, } triggerStatusCreateFunctionTriggerRequestBody:= model.GetCreateFunctionTriggerRequestBodyTriggerStatusEnum().ACTIVE request.Body = &model.CreateFunctionTriggerRequestBody{ EventData: eventDatabody, TriggerStatus: &triggerStatusCreateFunctionTriggerRequestBody, TriggerTypeCode: model.GetCreateFunctionTriggerRequestBodyTriggerTypeCodeEnum().SMN, } response, err := client.CreateFunctionTrigger(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
More
For SDK sample code of more programming languages, see the Sample Code tab in API Explorer. SDK sample code can be automatically generated.
Status Codes
| Status Code | Description |
|---|---|
| 201 | Created |
| 400 | Bad request. |
| 401 | Unauthorized. |
| 403 | Forbidden. |
| 404 | Not found. |
| 500 | Internal server error. |
Error Codes
See Error Codes.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot