Help Center/ CodeArts Deploy/ API Reference/ APIs/ Managing Applications/ Creating an Application Using a Template
Updated on 2024-11-25 GMT+08:00

Creating an Application Using a Template

Function

This API is used to create an application using a template. This API will not be maintained after September 30, 2024. You can use the CreateApp API instead.

Calling Method

For details, see Calling APIs.

URI

POST /v2/tasks/template-task

Request Parameters

Table 1 Request header parameters

Parameter

Mandatory

Type

Description

Content-Type

Yes

String

Message body type (format).

X-Auth-Token

Yes

String

User token. It can be obtained by calling an IAM API. The value of X-Subject-Token in the response header is the user token.

Table 2 Request body parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Project ID. For details, see Obtaining a Project ID.

project_name

Yes

String

Project name

template_id

Yes

String

Deployment template ID

task_name

Yes

String

Application name

slave_cluster_id

No

String

Custom slave resource pool ID

configs

No

Array of ConfigInfoDO objects

Deployment parameter type

Table 3 ConfigInfoDO

Parameter

Mandatory

Type

Description

name

No

String

Deployment parameter name which can be customized.

type

No

String

Type. If name is set, type is mandatory. If type is left empty, the default value text is used.

description

No

String

Description

value

No

String

Deployment parameter value

task_id

No

String

Deployment task ID, which is automatically generated when the application is created.

static_status

No

Integer

Whether the static parameter is used. 1: The parameter cannot be changed during deployment. 0: the parameter can be changed and is reported to the pipeline.

limits

No

Array of ParamTypeLimits objects

When the parameter type is enum, the optional value must be entered.

Table 4 ParamTypeLimits

Parameter

Mandatory

Type

Description

name

No

String

Enumerated value optional parameter

Response Parameters

Status code: 200

Table 5 Response body parameters

Parameter

Type

Description

task_name

String

Application name

task_id

String

Deployment task ID

Example Requests

This API is used to create an application using the Deploy a Spring Boot Application template in a specified project.

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

{
  "project_id" : "6039d4480efc4dddb178abff98719913",
  "project_name" : "Deploy",
  "template_id" : "6efb0b24e2e9489eb0e53ee12904a19e",
  "task_name" : "Deploytest",
  "configs" : [ {
    "name" : "serviceName",
    "type" : "text",
    "description" : "Service name",
    "value" : "SpringBoot-Demo"
  }, {
    "name" : "releaseVersion",
    "type" : "text",
    "description" : "Version number",
    "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"
  } ]
}

Example Responses

Status code: 200

OK: The request is successful.

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

SDK Sample Code

The SDK sample code is as follows.

Java

This API is used to create an application using the Deploy a Spring Boot Application template in a specified project.

  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("Service name")
                .withValue("SpringBoot-Demo")
        );
        listbodyConfigs.add(
            new ConfigInfoDO()
                .withName("releaseVersion")
                .withType(ConfigInfoDO.TypeEnum.fromValue("text"))
                .withDescription("Version number")
                .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

This API is used to create an application using the Deploy a Spring Boot Application template in a specified project.

 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="Service name",
                value="SpringBoot-Demo"
            ),
            ConfigInfoDO(
                name="releaseVersion",
                type="text",
                description="Version number",
                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

This API is used to create an application using the Deploy a Spring Boot Application template in a specified project.

  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:= "Service name"
	valueConfigs:= "SpringBoot-Demo"
	nameConfigs1:= "releaseVersion"
	typeConfigs1:= model.GetConfigInfoDoTypeEnum().TEXT
	descriptionConfigs1:= "Version number"
	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)
    }
}

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

200

OK: The request is successful.

Error Codes

See Error Codes.