Help Center> ServiceStage> API Reference> Examples> Component Management

Component Management

Scenario

This section describes how to manage components by calling APIs. For details about how to call APIs, see Making an API Request.

Process

  1. Create a component under an application.
  2. Create a component instance.
  3. Query the status of the component instance creation task.
  4. Upgrade the component instance.
  5. Obtain the component instance snapshot.
  6. Roll back the component instance.
  7. Perform stop, start, restart, or other lifecycle management operations on the component instance.

Prerequisites

  • You have called the API to obtain the Identity and Access Management (IAM) token for authentication. You have also set the token to an environment variable. That is, export Token={token}.
  • You have obtained the endpoint of ServiceStage. For details, see Regions and Endpoints. The following uses {servicestage_endpoint} as an example.

Procedure

  1. Create a component under an application. Call the API for creating an application component. The following shows an example request.

    curl -k -H "Content-Type:application/json" -H "X-Auth-Token:$Token" -X POST "https://{servicestage_endpoint}/v2/{project_id}/cas/applications/{application_id}/components" -d '{
        "name": "component-a", 
        "runtime": "Tomcat8", 
        "category": "Common", 
        "description": "component_desc", 
        "source": {
            "kind": "artifact", 
            "spec": {
                "storage": "swr", 
                "type": "package", 
                "url": "https://{IP}:20202/xxx/xxx.jar", 
                "auth": "iam"
            }
        }
    }'

    Information similar to the following is displayed.

    {
      "id": "string",
      "name": "component-a",
      "runtime": "Tomcat8",
      "category": "Common",
    
      "description": "component_desc",
      "project_id": "string",
      "application_id": "string",
      "source": {
        "kind": "artifact",
        "spec": {
          "storage": "swr",
          "type": "package",
          "url": "https://{IP}:20202/xxx/xxx.jar",
          "auth": "iam"
        }
      },
      "build": null,
      "pipeline_ids": [],
      "create_time": 0,
      "update_time": 0
    }

    id will be replaced with {component_id} in the following example.

    The value of runtime can be Tomcat8, Java8, Php7, Nodejs8, Docker, or Python3.

  2. Create a component instance. Call the API for creating a component instance. The following shows an example request.

    curl -k -H "Content-Type:application/json" -H "X-Auth-Token:$Token" -X POST  "https://{servicestage_endpoint}/v2/{project_id}/cas/applications/{application_id}/components/{component_id}/instances" -d '{
        "name": "component-instance-name", 
        "environment_id": "6e763000-9128-4a9d-adea-34c42cc5344d", 
        "flavor_id": "MICRO-5G:0.5C:1G", 
        "replica": 1, 
        "artifacts": {
            "container-name": {
                "storage": "swr", 
                "type": "image", 
                "url": "{IP}:20202/xxxxxxxx/junit-platformstest:1.0", 
                "auth": "iam"
            }
        }, 
        "version": "1.0.0", 
        "description": "instance desc", 
        "configuration": {
            "env": [
                {
                    "name": "log-level", 
                    "value": "warn"
                }
            ]
        }, 
        "refer_resources": [
            {
                "id": "b6862a62-d916-11e9-bdf1-0255ac101fd9", 
                "type": "cce", 
                "parameters": {
                    "namespace": "default"
                }
            }, 
            {
                "id": "8c0a45cc-626f-4d65-8257-507ee059aa9a", 
                "type": "elb"
            }
        ]
    }'
    Information similar to the following is displayed.
    {
        "instance_id": "89f5baf5-efe4-4f12-9c0d-734d2af5a184", 
        "job_id": "JOB66761060-f209-407c-a093-4df6f531b9dc"
    }

    job_id will be replaced with {job_id} in the following example. instance_id will be replaced with {instance_id} in the following example.

  3. Query the status of the component instance creation task. Call the API for obtaining job details to query the task status. The following shows an example request.

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

    Information similar to the following is displayed.

    {
        "job": {
            "JOB_ID": "JOB474362ef-091d-429f-9b3e-c9246b37dee6", 
            "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
    }

  4. Upgrade the component instance. Call the API for modifying an application component instance. The following shows an example request. Then, call the API in step 3 to query the task status using the returned job_id and wait until the task is complete.

    curl -k -H "Content-Type:application/json" -H "X-Auth-Token:$Token" -X PUT "https://{servicestage_endpoint}/v2/{project_id}/cas/applications/{application_id}/components/{component_id}/instances/{instance_id}" 
    -d '{
        "name": "component-instance-name", 
        "environment_id": "6e763000-9128-4a9d-adea-34c42cc5344d", 
        "flavor_id": "MICRO-5G:0.5C:1G", 
        "replica": 2, 
        "version": 2
    }'

    Information similar to the following is displayed.

    {
        "instance_id": "89f5baf5-efe4-4f12-9c0d-734d2af5a184", 
        "job_id": "JOB66761060-f209-407c-a093-4df6f531b9dd"
    }

  5. Obtain the component instance snapshot. Call the API for obtaining component instance snapshots. The following shows an example request. This step is the prerequisite for the rollback and is used to query the version that can be rolled back.

    curl -k -H "Content-Type:application/json" -H "X-Auth-Token:$Token" -X GET "https://{servicestage_endpoint}/v2/{project_id}/cas/applications/{application_id}/components/{component_id}/instances/{instance_id}/snapshots"

    Information similar to the following is displayed.

    {
        "snapshots": [{
            "create_time": 1571647078435,
            "description": "xxx",
            "instance_id": "89f5baf5-efe4-4f12-9c0d-734d2af5a184",
            "version": "1"
        },
        "count": 1]
    }

  6. Roll back the component instance. Call the API for performing operations on a component instance. The following shows an example request. Then, call the API in step 3 to query the task status using the returned job_id and wait until the task is complete.

    curl -k -H "Content-Type:application/json" -H "X-Auth-Token:$Token" -X POST "https://{servicestage_endpoint}/v2/{project_id}/cas/applications/{application_id}/components/{component_id}/instances/{instance_id}/action" -d '{
      "action": "rollback",
      "parameters": {
        "version": "1"
      }
    }'

    Information similar to the following is displayed.

    {
        "job_id": "JOB66761060-f209-407c-a093-4df6f531b9dc"
    }

  7. Perform stop, start, restart, or other lifecycle management operations on the component instance. Call the API for performing operations on a component instance. The following shows an example request for stopping the component instance. Then, call the API in step 3 to query the task status using the returned job_id and wait until the task is complete.

    curl -k -H "Content-Type:application/json" -H "X-Auth-Token:$Token" -X POST "https://{servicestage_endpoint}/v2/{project_id}/cas/applications/{application_id}/components/{component_id}/instances/{instance_id}/action" -d '{
      "action": "stop"
    }'

    Information similar to the following is displayed.

    {
        "job_id": "JOB66761060-f209-407c-a093-4df6f531b9dc"
    }