Help Center/ ServiceStage/ API Reference/ Examples/ Creating and Managing a Component
Updated on 2024-10-16 GMT+08:00

Creating and Managing a Component

Scenarios

A component is a service feature implementation of an application. It is carried by code or software packages and can be independently deployed and run in an environment.

The following describes how to call the APIs for Creating an Application Component, Obtaining Job Details, Modifying Component Information Based on the Component ID, and Delivering a Component Task Based on the Component ID.

For details about how to call APIs, see Calling APIs.

Prerequisites

  • You have obtained the IAM user token by calling the API for obtaining a user token, and set the token to an environment variable, that is, export Token={token}.
  • You have obtained the ServiceStage endpoint to replace {servicestage_endpoint} in the following procedure. For details, see Regions and Endpoints.

Procedure

  1. Call the API for Creating an Application Component to create a component for an application. The following is a request example:

    curl -k -H "Content-Type:application/json;charset=utf8" -H "X-Auth-Token:$token" -X POST "https://{servicestage_endpoint}/v3/{project_id}/cas/applications/{application_id}/components" -d ' 
    {
        "name": "test-component",
        "description": "",
        "labels": [],
        "version": "2023.0712.09204",
        "environment_id": "21a43958-f64f-4077-8ae3-3a03bf0111c7",
        "application_id": "2c458700-c8e0-4651-a73b-5219b70a6572",
        "enterprise_project_id": "0",
        "runtime_stack": {
            "name": "Docker",
            "version": "",
            "type": "Docker",
            "deploy_mode": "container"
        },
        "source": {
            "kind": "image",
            "storage": "swr",
            "url": "swr.ap-southeast-1.myhuaweicloud.com/servicestage-demo/test_02:v1",
            "version": "v1"
        },
        "tomcat_opts": {
            "server_xml": ""
        },
        "refer_resources": [
            {
                "id": "1d3091a2-1ecf-11ee-ae69-0255ac1001ba",
                "type": "cce",
                "parameters": {
                    "namespace": "default",
                    "type": "VirtualMachine"
                }
            }
        ],
        "replica": 2,
        "config_mode": "ui",
        "limit_cpu": 0.25,
        "limit_memory": 0.5,
        "request_cpu": 0.25,
        "request_memory": 0.5,
        "external_accesses": [],
        "storages": [],
        "logs": [],
        "affinity": {
            "az": [],
            "node": [],
            "component": []
        },
        "anti_affinity": {
            "az": [],
            "node": [],
            "component": []
        },
        "update_strategy": {
            "type": "RollingUpdate",
            "max_surge": 0,
            "max_unavailable": 1
        },
        "command": {
            "args": [],
            "command": []
        },
        "custom_metric": {}
    }'

    Information similar to the following is displayed.

    {
        "job_id": "JOB870e9ffe-227e-43a9-9c84-20a7ad02e77c",
        "component_id": "f3f6cb89-831d-4579-8bbc-664487889e38"
    }

    Record job_id and component_id in bold in the command output to replace {job_id} and {component_id} in subsequent examples.

  2. Call the API for Obtaining Job Details to check whether the component is created. The following is a request example:

    curl -k -H "Content-Type:application/json;charset=utf8" -H "X-Auth-Token:$Token" -X GET "https://{servicestage_endpoint}/v3/{project_id}/cas/jobs/{job_id}"

    Information similar to the following is displayed.

    {
        "job": {
            "JOB_ID": "JOB870e9ffe-227e-43a9-9c84-20a7ad02e77c", 
            "JOB_NAME": "provision/provMain", 
            "JOB_TYPE": "PROVISION", 
            "SERVICE_INSTANCE_ID": "429a35a0-dee8-4648-86f6-d1f8bc74ba52", 
            "PROJECT_ID": "063a3158b1a34710b36ad208b9497d00", 
            "ORDER_ID": "", 
            "JOB_DESC": "provision/provMain", 
            "EXECUTION_STATUS": "SUCCEEDED", 
            "CREATED_BY": "585cc5c16cda4954b28d4eb460f06261"
        }, 
        "tasks": [
            {
                "MESSAGES": "{}", 
                "TASK_ID": "Task-bc64b46c-1688-4787-b7ff-87cb53b346ea", 
                "TASK_TYPE": "echo", 
                "TASK_INDEX": 1, 
                "TASK_NAME": "SEPARATOR_PREPARE_RESOURCES", 
                "TASK_STATUS": "SUCCEEDED", 
                "LAST_HEALTH_CHECK": "2020-02-04T04:01:03.344Z", 
                "CREATED_AT": "2020-02-04T04:01:03.344Z", 
                "OWNER_ID": "cas-mgr-69bb87d7c-wpqwb:30114"
            }, 
            {
                "MESSAGES": "{}", 
                "TASK_ID": "Task-15ec252f-bca0-401e-ae5b-798a639df92e", 
                "TASK_TYPE": "echo", 
                "TASK_INDEX": 4, 
                "TASK_NAME": "SEPARATOR_BUILDING", 
                "TASK_STATUS": "SUCCEEDED", 
                "LAST_HEALTH_CHECK": "2020-02-04T04:01:03.346Z", 
                "CREATED_AT": "2020-02-04T04:01:03.345Z", 
                "OWNER_ID": "cas-mgr-69bb87d7c-wpqwb:30114"
            }, 
            {
                "MESSAGES": "{}", 
                "TASK_ID": "Task-52a5d1ae-2c54-46ec-8134-53e7b6a45fb7", 
                "TASK_TYPE": "restful", 
                "TASK_INDEX": 6, 
                "TASK_NAME": "TN00091", 
                "TASK_STATUS": "SUCCEEDED", 
                "LAST_HEALTH_CHECK": "2020-02-04T04:01:08.924Z", 
                "CREATED_AT": "2020-02-04T04:01:03.346Z", 
                "OWNER_ID": "cas-mgr-69bb87d7c-wpqwb:30114"
            }, 
            {
                "MESSAGES": "{}", 
                "TASK_ID": "Task-a149b585-d31b-4c72-a665-d820efcb0bf9", 
                "TASK_TYPE": "restful", 
                "TASK_INDEX": 7, 
                "TASK_NAME": "TN00094", 
                "TASK_STATUS": "SUCCEEDED", 
                "LAST_HEALTH_CHECK": "2020-02-04T04:01:08.961Z", 
                "CREATED_AT": "2020-02-04T04:01:08.925Z", 
                "OWNER_ID": "cas-mgr-69bb87d7c-wpqwb:30114"
            }, 
            {
                "MESSAGES": "", 
                "TASK_ID": "Task-a72fe8d8-8cc1-42b5-a097-0cf9e29692a7", 
                "TASK_TYPE": "restful", 
                "TASK_INDEX": 9, 
                "TASK_NAME": "TN00097", 
                "TASK_STATUS": "SUCCEEDED", 
                "LAST_HEALTH_CHECK": "2020-02-04T04:01:38.975Z", 
                "CREATED_AT": "2020-02-04T04:01:08.962Z", 
                "OWNER_ID": "cas-mgr-69bb87d7c-wpqwb:30114"
            }
        ], 
        "task_count": 5
    }

  3. Call the API for Modifying Component Information Based on the Component ID to upgrade the component. The following is a request example:

    curl -k -H "Content-Type:application/json;charset=utf8" -H "X-Auth-Token:$token" -X PUT "https://{servicestage_endpoint}/v3/{project_id}/cas/applications/{application_id}/components/{component_id}" -d ' 
    {
        "source": {
            "kind": "image",
            "storage": "swr",
            "url": "swr.ap-southeast-1.myhuaweicloud.com/demo/arm_tomcat8:v1"
        },
        "version": "2023.0717.17160",
        "refer_resources": [
            {
                "id": "1d3091a2-1ecf-11ee-ae69-0255ac1001ba",
                "type": "cce",
                "parameters": {
                    "namespace": "default",
                    "type": "ARM64"
                }
            }
        ],
        "limit_cpu": 0.25,
        "limit_memory": 0.5,
        "request_cpu": 0.25,
        "request_memory": 0.5,
        "mesher": {},
        "affinity": {},
        "anti_affinity": {},
        "liveness_probe": {},
        "readiness_probe": {},
        "command": {
            "command": [],
            "args": []
        },
        "envs": [
            {
                "name": "PAAS_PROJECT_ID",
                "inner": false,
                "value": "063a3158b1a34710b36ad208b9497d00"
            },
            {
                "name": "CAS_APP_ID",
                "inner": false,
                "value": "f3f6cb89-831d-4579-8bbc-664487889e38"
            },
            {
                "name": "PAAS_CLUSTER_ID",
                "inner": false,
                "value": "1d3091a2-1ecf-11ee-ae69-0255ac1001ba"
            },
            {
                "name": "CAS_APPLICATION_ID",
                "inner": false,
                "value": "2c458700-c8e0-4651-a73b-5219b70a6572"
            },
            {
                "name": "CAS_INSTANCE_VERSION",
                "inner": false,
                "value": "2023.0717.17062"
            },
            {
                "name": "CAS_COMPONENT_ID",
                "inner": false,
                "value": "e1c3021b-df40-48e9-bd41-ef033fc77f2b"
            },
            {
                "name": "CAS_INSTANCE_NAME",
                "inner": false,
                "value": "etst1-test-arm-5xdhrd"
            },
            {
                "name": "CAS_COMPONENT_NAME",
                "inner": false,
                "value": "etst1"
            },
            {
                "name": "PAAS_NAMESPACE",
                "inner": false,
                "value": "default"
            },
            {
                "name": "PAAS_APP_NAME",
                "inner": false,
                "value": "etst1"
            },
            {
                "name": "PAAS_PROJECT_NAME",
                "inner": false,
                "value": "cn-north-7"
            },
            {
                "name": "CAS_INSTANCE_ID",
                "inner": false,
                "value": "f3f6cb89-831d-4579-8bbc-664487889e38"
            },
            {
                "name": "CAS_ENVIRONMENT_NAME",
                "inner": false,
                "value": "test_arm"
            },
            {
                "name": "AOM_DISCOVERY_RULE_VERSION",
                "inner": false,
                "value": "V1"
            },
            {
                "name": "CAS_ENVIRONMENT_ID",
                "inner": false,
                "value": "21a43958-f64f-4077-8ae3-3a03bf0111c7"
            },
            {
                "name": "testName",
                "inner": false,
                "value": "testValue"
            },
            {
                "name": "PAAS_REGION_NAME",
                "inner": false,
                "value": "cn-north-7"
            },
            {
                "name": "CAS_APPLICATION_NAME",
                "inner": false,
                "value": "app-test-application"
            }
        ],
        "deploy_strategy": {
            "type": "RollingRelease",
            "rolling_release": {
                "batches": "1"
            }
        },
        "id": "f3f6cb89-831d-4579-8bbc-664487889e38",
        "name": "etst1"
    }'

    Information similar to the following is displayed.

    {
        "job_id": "JOB2209202b-617c-4591-b0c6-ef2521a943a2"
    }

    Record job_id in bold in the command output to replace {job_id} in subsequent examples.

  4. Call the API for 2, use the job_id returned in 3 to query the job status, and wait until the job is complete.
  5. Call the API for Delivering a Component Task Based on the Component ID to roll back the component. The following is a request example:

    curl -k -H "Content-Type:application/json;charset=utf8" -H "X-Auth-Token:$token" -X POST "https://{servicestage_endpoint}/v3/{project_id}/cas/applications/{application_id}/components/{component_id}/action" -d ' 
    {
        "action": "rollback_current"
    }'

    Information similar to the following is displayed.

    {
        "job_id": "JOB66eb56db-062e-4462-ba95-8d3c63d5d462"
    }

    Record job_id in bold in the command output to replace {job_id} in subsequent examples.

  6. Call the API for 2, use the job_id returned in 5 to query the job status, and wait until the job is complete.
  7. Call the API for Delivering a Component Task Based on the Component ID to stop the component instance. The following is a request example:

    curl -k -H "Content-Type:application/json;charset=utf8" -H "X-Auth-Token:$token" -X POST "https://{servicestage_endpoint}/v3/{project_id}/cas/applications/{application_id}/components/{component_id}/action" -d ' 
    {
        "action": "stop"
    }'

    Information similar to the following is displayed.

    {
        "job_id": "JOB2790bf9f-7d6e-473d-827f-1bb94e0cf10d"
    }

    Record job_id in bold in the command output to replace {job_id} in subsequent examples.

  8. Call the API for 2, use the job_id returned in 7 to query the job status, and wait until the job is complete.