文档首页 > > API参考> 应用示例> 组件管理

组件管理

分享
更新时间:2020/06/09 GMT+08:00

场景描述

本章节指导用户通过API实现组件管理。API的调用方法请参见如何调用API

流程介绍

  1. 在某个应用下创建组件
  2. 创建组件实例
  3. 查询创建组件实例任务状态
  4. 升级组件实例
  5. 获取组件实例快照
  6. 回滚组件实例
  7. 对组件实例进行其他生命周期的操作,如停止,启动,重启等

前提条件

  • 获取IAM的token,调用Token认证,并将token值设置为环境变量:export Token={token}。
  • 获取ServiceStage的endpoint,参考地区和终端节点,以下示例中以{servicestage_endpoint}代替。

操作步骤

  1. 在某个应用下创建组件。调用应用中创建组件接口,请求示例如下。

    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": "Webapp", 
        "category": "Common", 
        "description": "component_desc", 
        "source": {
            "kind": "artifact", 
            "spec": {
                "storage": "swr", 
                "type": "package", 
                "url": "https://{IP}:20202/xxx/xxx.jar", 
                "auth": "iam"
            }
        }
    }'

    返回结果:

    {
      "id": "string",
      "name": "string",
      "runtime": "Tomcat8",
      "category": "Webapp",
      "sub_category": "Web",
      "description": "string",
      "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,后续示例中以{component_id}代替。

  2. 创建组件实例。调用创建组件实例接口。请求示例如下。

    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, 
        "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"
            }
        ]
    }'
    返回结果:
    {
        "instance_id": "89f5baf5-efe4-4f12-9c0d-734d2af5a184", 
        "job_id": "JOB66761060-f209-407c-a093-4df6f531b9dc"
    }

    得到的job_id,后续示例中以{job_id}代替。得到的instance_id,后续示例中以{instance_id}代替。

  3. 查询创建组件实例任务状态。调用获取部署任务详细信息接口,去查询任务状态是否成功。请求示例如下。

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

    返回结果:

    {
        "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. 升级组件实例。调用修改应用组件实例接口。请求示例如下。然后还是调用步骤3的接口,用返回的job_id查询任务状态,等待升级完成。

    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
    }'

    返回结果:

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

  5. 获取组件实例快照。调用获取组件实例快照接口。请求示例如下。该步骤是回退的前置步骤,用于查询可以回退的版本。

    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"

    返回结果:

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

  6. 回滚组件实例。调用对组件实例的操作接口。请求示例如下。然后用返回的job_id调用步骤3等待任务完成。

    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"
      }
    }'

    返回结果:

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

  7. 对组件实例进行其他生命周期的操作,如停止,启动,重启等。调用对组件实例的操作接口。这里以停止组件实例为例子,请求示例如下。然后用返回的job_id调用步骤3等待任务完成。

    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"
    }'

    返回结果:

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

分享:

    相关文档

    相关产品

文档是否有解决您的问题?

提交成功!

非常感谢您的反馈,我们会继续努力做到更好!

反馈提交失败,请稍后再试!

*必选

请至少选择或填写一项反馈信息

字符长度不能超过200

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

智能客服提问云社区提问