更新时间:2024-11-21 GMT+08:00
分享

通过模板新建应用

功能介绍

通过模板新建应用。该接口于2024年09月30日后不再维护,推荐使用新版新建应用(CreateApp)接口。

调用方法

请参见如何调用API

URI

POST /v2/tasks/template-task

请求参数

表1 请求Header参数

参数

是否必选

参数类型

描述

Content-Type

String

消息体的类型(格式)

X-Auth-Token

String

用户Token。通过调用IAM服务获取用户Token接口获取(响应消息头中X-Subject-Token的值)

表2 请求Body参数

参数

是否必选

参数类型

描述

project_id

String

项目id,获取方式请参见获取项目id

project_name

String

项目名称

template_id

String

部署模板id

task_name

String

应用名称

slave_cluster_id

String

自定义slave资源池id

configs

Array of ConfigInfoDO objects

部署参数类

表3 ConfigInfoDO

参数

是否必选

参数类型

描述

name

String

部署参数名称,用户可自定义

type

String

类型,如果填写name字段,则type必选,若type为空则默认为text

description

String

描述

value

String

部署参数值

task_id

String

部署任务id,创建应用后由系统自动生成

static_status

Integer

表示是否为静态参数,值为1时不支持部署时变更参数,值为0时支持,并且也会把该参数上报流水线

limits

Array of ParamTypeLimits objects

当参数类型为enum枚举类型时,必须填写可选值

表4 ParamTypeLimits

参数

是否必选

参数类型

描述

name

String

枚举值可选参数

响应参数

状态码: 200

表5 响应Body参数

参数

参数类型

描述

task_name

String

应用名称

task_id

String

部署任务id

请求示例

在指定项目下使用“SpringBoot应用部署”模板创建一个应用。

https://{endpoint}/v2/tasks/template-task

{
  "project_id" : "6039d4480efc4dddb178abff98719913",
  "project_name" : "Deploy",
  "template_id" : "6efb0b24e2e9489eb0e53ee12904a19e",
  "task_name" : "Deploytest",
  "configs" : [ {
    "name" : "serviceName",
    "type" : "text",
    "description" : "服务名称",
    "value" : "SpringBoot-Demo"
  }, {
    "name" : "releaseVersion",
    "type" : "text",
    "description" : "版本号",
    "value" : "1.1.1"
  }, {
    "name" : "jdk_path",
    "type" : "text",
    "description" : "",
    "value" : "/usr/local/jdk"
  }, {
    "name" : "package_url",
    "type" : "text",
    "description" : "",
    "value" : "/${serviceName}/${releaseVersion}/${serviceName}.jar"
  }, {
    "name" : "spring_path",
    "type" : "text",
    "description" : "",
    "value" : "/usr/local/${serviceName}.jar"
  }, {
    "name" : "download_path",
    "type" : "text",
    "description" : "",
    "value" : "/usr/local/"
  }, {
    "name" : "service_port",
    "type" : "text",
    "description" : "",
    "value" : "<%= service_port%>"
  }, {
    "name" : "host_group",
    "type" : "host_group",
    "description" : "",
    "value" : "<%= host_group%>"
  }, {
    "name" : "component_name",
    "type" : "text",
    "description" : "",
    "value" : "aom-${serviceName}"
  }, {
    "name" : "log_path",
    "type" : "text",
    "description" : "",
    "value" : "/usr/local/*.log"
  } ]
}

响应示例

状态码: 200

OK 请求响应成功

{
  "task_name" : "Deploytest",
  "task_id" : "140ca97e701d4c4c93c59ffd5bdb32ec"
}

SDK代码示例

SDK代码示例如下。

Java

在指定项目下使用“SpringBoot应用部署”模板创建一个应用。

  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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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.codeartsdeploy.v2.region.CodeArtsDeployRegion;
import com.huaweicloud.sdk.codeartsdeploy.v2.*;
import com.huaweicloud.sdk.codeartsdeploy.v2.model.*;

import java.util.List;
import java.util.ArrayList;

public class CreateDeployTaskByTemplateSolution {

    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");

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        CodeArtsDeployClient client = CodeArtsDeployClient.newBuilder()
                .withCredential(auth)
                .withRegion(CodeArtsDeployRegion.valueOf("<YOUR REGION>"))
                .build();
        CreateDeployTaskByTemplateRequest request = new CreateDeployTaskByTemplateRequest();
        TemplateTaskRequestBody body = new TemplateTaskRequestBody();
        List<ConfigInfoDO> listbodyConfigs = new ArrayList<>();
        listbodyConfigs.add(
            new ConfigInfoDO()
                .withName("serviceName")
                .withType(ConfigInfoDO.TypeEnum.fromValue("text"))
                .withDescription("服务名称")
                .withValue("SpringBoot-Demo")
        );
        listbodyConfigs.add(
            new ConfigInfoDO()
                .withName("releaseVersion")
                .withType(ConfigInfoDO.TypeEnum.fromValue("text"))
                .withDescription("版本号")
                .withValue("1.1.1")
        );
        listbodyConfigs.add(
            new ConfigInfoDO()
                .withName("jdk_path")
                .withType(ConfigInfoDO.TypeEnum.fromValue("text"))
                .withDescription("")
                .withValue("/usr/local/jdk")
        );
        listbodyConfigs.add(
            new ConfigInfoDO()
                .withName("package_url")
                .withType(ConfigInfoDO.TypeEnum.fromValue("text"))
                .withDescription("")
                .withValue("/${serviceName}/${releaseVersion}/${serviceName}.jar")
        );
        listbodyConfigs.add(
            new ConfigInfoDO()
                .withName("spring_path")
                .withType(ConfigInfoDO.TypeEnum.fromValue("text"))
                .withDescription("")
                .withValue("/usr/local/${serviceName}.jar")
        );
        listbodyConfigs.add(
            new ConfigInfoDO()
                .withName("download_path")
                .withType(ConfigInfoDO.TypeEnum.fromValue("text"))
                .withDescription("")
                .withValue("/usr/local/")
        );
        listbodyConfigs.add(
            new ConfigInfoDO()
                .withName("service_port")
                .withType(ConfigInfoDO.TypeEnum.fromValue("text"))
                .withDescription("")
                .withValue("<%= service_port%>")
        );
        listbodyConfigs.add(
            new ConfigInfoDO()
                .withName("host_group")
                .withType(ConfigInfoDO.TypeEnum.fromValue("host_group"))
                .withDescription("")
                .withValue("<%= host_group%>")
        );
        listbodyConfigs.add(
            new ConfigInfoDO()
                .withName("component_name")
                .withType(ConfigInfoDO.TypeEnum.fromValue("text"))
                .withDescription("")
                .withValue("aom-${serviceName}")
        );
        listbodyConfigs.add(
            new ConfigInfoDO()
                .withName("log_path")
                .withType(ConfigInfoDO.TypeEnum.fromValue("text"))
                .withDescription("")
                .withValue("/usr/local/*.log")
        );
        body.withConfigs(listbodyConfigs);
        body.withTaskName("Deploytest");
        body.withTemplateId("6efb0b24e2e9489eb0e53ee12904a19e");
        body.withProjectName("Deploy");
        body.withProjectId("6039d4480efc4dddb178abff98719913");
        request.withBody(body);
        try {
            CreateDeployTaskByTemplateResponse response = client.createDeployTaskByTemplate(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

在指定项目下使用“SpringBoot应用部署”模板创建一个应用。

 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
89
90
91
92
93
94
95
96
97
98
99
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcodeartsdeploy.v2.region.codeartsdeploy_region import CodeArtsDeployRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkcodeartsdeploy.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"]

    credentials = BasicCredentials(ak, sk)

    client = CodeArtsDeployClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(CodeArtsDeployRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = CreateDeployTaskByTemplateRequest()
        listConfigsbody = [
            ConfigInfoDO(
                name="serviceName",
                type="text",
                description="服务名称",
                value="SpringBoot-Demo"
            ),
            ConfigInfoDO(
                name="releaseVersion",
                type="text",
                description="版本号",
                value="1.1.1"
            ),
            ConfigInfoDO(
                name="jdk_path",
                type="text",
                description="",
                value="/usr/local/jdk"
            ),
            ConfigInfoDO(
                name="package_url",
                type="text",
                description="",
                value="/${serviceName}/${releaseVersion}/${serviceName}.jar"
            ),
            ConfigInfoDO(
                name="spring_path",
                type="text",
                description="",
                value="/usr/local/${serviceName}.jar"
            ),
            ConfigInfoDO(
                name="download_path",
                type="text",
                description="",
                value="/usr/local/"
            ),
            ConfigInfoDO(
                name="service_port",
                type="text",
                description="",
                value="<%= service_port%>"
            ),
            ConfigInfoDO(
                name="host_group",
                type="host_group",
                description="",
                value="<%= host_group%>"
            ),
            ConfigInfoDO(
                name="component_name",
                type="text",
                description="",
                value="aom-${serviceName}"
            ),
            ConfigInfoDO(
                name="log_path",
                type="text",
                description="",
                value="/usr/local/*.log"
            )
        ]
        request.body = TemplateTaskRequestBody(
            configs=listConfigsbody,
            task_name="Deploytest",
            template_id="6efb0b24e2e9489eb0e53ee12904a19e",
            project_name="Deploy",
            project_id="6039d4480efc4dddb178abff98719913"
        )
        response = client.create_deploy_task_by_template(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

在指定项目下使用“SpringBoot应用部署”模板创建一个应用。

  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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    codeartsdeploy "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/codeartsdeploy/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/codeartsdeploy/v2/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/codeartsdeploy/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")

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        Build()

    client := codeartsdeploy.NewCodeArtsDeployClient(
        codeartsdeploy.CodeArtsDeployClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.CreateDeployTaskByTemplateRequest{}
	nameConfigs:= "serviceName"
	typeConfigs:= model.GetConfigInfoDoTypeEnum().TEXT
	descriptionConfigs:= "服务名称"
	valueConfigs:= "SpringBoot-Demo"
	nameConfigs1:= "releaseVersion"
	typeConfigs1:= model.GetConfigInfoDoTypeEnum().TEXT
	descriptionConfigs1:= "版本号"
	valueConfigs1:= "1.1.1"
	nameConfigs2:= "jdk_path"
	typeConfigs2:= model.GetConfigInfoDoTypeEnum().TEXT
	descriptionConfigs2:= ""
	valueConfigs2:= "/usr/local/jdk"
	nameConfigs3:= "package_url"
	typeConfigs3:= model.GetConfigInfoDoTypeEnum().TEXT
	descriptionConfigs3:= ""
	valueConfigs3:= "/${serviceName}/${releaseVersion}/${serviceName}.jar"
	nameConfigs4:= "spring_path"
	typeConfigs4:= model.GetConfigInfoDoTypeEnum().TEXT
	descriptionConfigs4:= ""
	valueConfigs4:= "/usr/local/${serviceName}.jar"
	nameConfigs5:= "download_path"
	typeConfigs5:= model.GetConfigInfoDoTypeEnum().TEXT
	descriptionConfigs5:= ""
	valueConfigs5:= "/usr/local/"
	nameConfigs6:= "service_port"
	typeConfigs6:= model.GetConfigInfoDoTypeEnum().TEXT
	descriptionConfigs6:= ""
	valueConfigs6:= "<%= service_port%>"
	nameConfigs7:= "host_group"
	typeConfigs7:= model.GetConfigInfoDoTypeEnum().HOST_GROUP
	descriptionConfigs7:= ""
	valueConfigs7:= "<%= host_group%>"
	nameConfigs8:= "component_name"
	typeConfigs8:= model.GetConfigInfoDoTypeEnum().TEXT
	descriptionConfigs8:= ""
	valueConfigs8:= "aom-${serviceName}"
	nameConfigs9:= "log_path"
	typeConfigs9:= model.GetConfigInfoDoTypeEnum().TEXT
	descriptionConfigs9:= ""
	valueConfigs9:= "/usr/local/*.log"
	var listConfigsbody = []model.ConfigInfoDo{
        {
            Name: &nameConfigs,
            Type: &typeConfigs,
            Description: &descriptionConfigs,
            Value: &valueConfigs,
        },
        {
            Name: &nameConfigs1,
            Type: &typeConfigs1,
            Description: &descriptionConfigs1,
            Value: &valueConfigs1,
        },
        {
            Name: &nameConfigs2,
            Type: &typeConfigs2,
            Description: &descriptionConfigs2,
            Value: &valueConfigs2,
        },
        {
            Name: &nameConfigs3,
            Type: &typeConfigs3,
            Description: &descriptionConfigs3,
            Value: &valueConfigs3,
        },
        {
            Name: &nameConfigs4,
            Type: &typeConfigs4,
            Description: &descriptionConfigs4,
            Value: &valueConfigs4,
        },
        {
            Name: &nameConfigs5,
            Type: &typeConfigs5,
            Description: &descriptionConfigs5,
            Value: &valueConfigs5,
        },
        {
            Name: &nameConfigs6,
            Type: &typeConfigs6,
            Description: &descriptionConfigs6,
            Value: &valueConfigs6,
        },
        {
            Name: &nameConfigs7,
            Type: &typeConfigs7,
            Description: &descriptionConfigs7,
            Value: &valueConfigs7,
        },
        {
            Name: &nameConfigs8,
            Type: &typeConfigs8,
            Description: &descriptionConfigs8,
            Value: &valueConfigs8,
        },
        {
            Name: &nameConfigs9,
            Type: &typeConfigs9,
            Description: &descriptionConfigs9,
            Value: &valueConfigs9,
        },
    }
	request.Body = &model.TemplateTaskRequestBody{
		Configs: &listConfigsbody,
		TaskName: "Deploytest",
		TemplateId: "6efb0b24e2e9489eb0e53ee12904a19e",
		ProjectName: "Deploy",
		ProjectId: "6039d4480efc4dddb178abff98719913",
	}
	response, err := client.CreateDeployTaskByTemplate(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

200

OK 请求响应成功

错误码

请参见错误码

相关文档