更新时间:2024-04-19 GMT+08:00
分享

解析模板参数

功能介绍

解析模板参数(ParseTemplateVariables)

此API用于解析用户输入的模板中的参数(variable),将解析模板中的所有variable块并返回

  • 如果用户传入的模板中定义了variable参数,则返回200和所有variable

  • 如果用户传入的模板中没有定义variable参数,则返回200和空对象

  • 如果用户请求非法或传入的模板非法,则返回400

调试

您可以在API Explorer中调试该接口,支持自动认证鉴权。API Explorer可以自动生成SDK代码示例,并提供SDK代码示例调试功能。

URI

POST /v1/{project_id}/template-analyses/variables

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

项目ID,可以从调用API处获取,也可以从控制台获取。

项目ID获取方式

最小长度:3

最大长度:64

请求参数

表2 请求Header参数

参数

是否必选

参数类型

描述

Client-Request-Id

String

用户指定的,对于此请求的唯一ID,用于定位某个请求,推荐使用UUID

最小长度:36

最大长度:128

表3 请求Body参数

参数

是否必选

参数类型

描述

template_body

String

HCL模板,描述了资源的目标状态。资源编排服务将比较此模板与当前远程资源的状态之间的区别。

template_body和template_uri 必须有且只有一个存在

在CreateStack API中,template_body和template_uri可以都不给予

注意:

  • template_body中默认不应该含有任何敏感信息,资源编排服务会直接明文使用、log、展示、存储对应的template_body。如为敏感信息,建议将敏感信息通过vars_structure参数化,并设置encryption字段开启加密

最小长度:0

最大长度:51200

template_uri

String

HCL模板的OBS地址,该模板描述了资源的目标状态。资源编排服务将比较此模板与当前远程资源的状态之间的区别。

OBS地址支持同类型Region之间进行互相访问(Region分为通用Region和专属Region,通用Region指面向公共租户提供通用云服务的Region;专属Region指只承载同一类业务或只面向特定租户提供业务服务的专用Region)

对应的文件应该是纯tf文件或zip压缩包

纯tf文件需要以.tf或者.tf.json结尾,并遵守HCL语法

压缩包目前只支持zip格式,文件需要以.zip结尾。解压后的文件不得包含".tfvars"文件且必须是UTF8编码(其中.tf.json不能包含BOM头),zip压缩包当前支持的子文件数量最大为100

template_body和template_uri 必须有且只有一个存在

在CreateStack API中,template_body和template_uri可以都不给予

注意:

  • template_uri对应的模板文件中默认不应该含有任何敏感信息,资源编排服务会直接明文使用、log、展示、存储对应的模板文件内容。如为敏感信息,建议将敏感信息通过vars_structure参数化,并设置encryption字段开启加密

  • template_uri对应的模板文件如果为zip类型,则内部的文件或文件夹名称长度不得超过255个字节,最深路径长度不得超过2048字节,zip包大小不得超过1MB

最小长度:0

最大长度:2048

响应参数

状态码: 200

表4 响应Body参数

参数

参数类型

描述

variables

Array of VariableResponse objects

解析模板得到的参数列表

表5 VariableResponse

参数

参数类型

描述

name

String

参数的名字

以HCL格式的模板为例,name 为 my_hello_world_variable

variable "my_hello_world_variable" {
  type = string
  description = "this is a variable"
  default = "hello world"
  sensitive = false
  nullable = false
  validation {
    condition     = length(var.my_hello_world_variable) > 0 && substr(var.my_hello_world_variable, 0, 5) == "hello"
    error_message = "my_hello_world_variable should start with 'hello'."
  }
}

以json格式的模板为例,name 为 my_hello_world_variable

{
  "variable": {
    "my_hello_world_variable": [
      {
        "default": "hello world",
        "description": "this is a variable",
        "nullable": false,
        "sensitive": false,
        "type": "string",
        "validation": [
          {
            "condition": "${length(var.my_hello_world_variable) > 0 && substr(var.my_hello_world_variable, 0, 5) == \"hello\"}",
            "error_message": "my_hello_world_variable should start with 'hello'."
          }
        ]
      }
    ]
  }
}

type

String

参数的类型

以HCL格式的模板为例,type 为 string

variable "my_hello_world_variable" {
  type = string
  description = "this is a variable"
  default = "hello world"
  sensitive = false
  nullable = false
  validation {
    condition     = length(var.my_hello_world_variable) > 0 && substr(var.my_hello_world_variable, 0, 5) == "hello"
    error_message = "my_hello_world_variable should start with 'hello'."
  }
}

以json格式的模板为例,type 为 string

{
  "variable": {
    "my_hello_world_variable": [
      {
        "default": "hello world",
        "description": "this is a variable",
        "nullable": false,
        "sensitive": false,
        "type": "string",
        "validation": [
          {
            "condition": "${length(var.my_hello_world_variable) > 0 && substr(var.my_hello_world_variable, 0, 5) == \"hello\"}",
            "error_message": "my_hello_world_variable should start with 'hello'."
          }
        ]
      }
    ]
  }
}

description

String

参数的描述

以HCL格式的模板为例,description 为 this is a variable

variable "my_hello_world_variable" {
  type = string
  description = "this is a variable"
  default = "hello world"
  sensitive = false
  nullable = false
  validation {
    condition     = length(var.my_hello_world_variable) > 0 && substr(var.my_hello_world_variable, 0, 5) == "hello"
    error_message = "my_hello_world_variable should start with 'hello'."
  }
}

以json格式的模板为例,description 为 this is a variable

{
  "variable": {
    "my_hello_world_variable": [
      {
        "default": "hello world",
        "description": "this is a variable",
        "nullable": false,
        "sensitive": false,
        "type": "string",
        "validation": [
          {
            "condition": "${length(var.my_hello_world_variable) > 0 && substr(var.my_hello_world_variable, 0, 5) == \"hello\"}",
            "error_message": "my_hello_world_variable should start with 'hello'."
          }
        ]
      }
    ]
  }
}

default

Object

参数默认值。此返回值的类型将与type保持一致

例如,对于type为string的变量,此值的返回类型为string;对于type为number的变量,此值的返回类型为number

以HCL格式的模板为例,default 为 hello world

variable "my_hello_world_variable" {
  type = string
  description = "this is a variable"
  default = "hello world"
  sensitive = false
  nullable = false
  validation {
    condition     = length(var.my_hello_world_variable) > 0 && substr(var.my_hello_world_variable, 0, 5) == "hello"
    error_message = "my_hello_world_variable should start with 'hello'."
  }
}

以json格式的模板为例,default 为 hello world

{
  "variable": {
    "my_hello_world_variable": [
      {
        "default": "hello world",
        "description": "this is a variable",
        "nullable": false,
        "sensitive": false,
        "type": "string",
        "validation": [
          {
            "condition": "${length(var.my_hello_world_variable) > 0 && substr(var.my_hello_world_variable, 0, 5) == \"hello\"}",
            "error_message": "my_hello_world_variable should start with 'hello'."
          }
        ]
      }
    ]
  }
}

sensitive

Boolean

参数是否为敏感字段

如果variable中没有定义sensitive,默认返回false。

以HCL格式的模板为例,sensitive 为 false

variable "my_hello_world_variable" {
  type = string
  description = "this is a variable"
  default = "hello world"
  sensitive = false
  nullable = false
  validation {
    condition     = length(var.my_hello_world_variable) > 0 && substr(var.my_hello_world_variable, 0, 5) == "hello"
    error_message = "my_hello_world_variable should start with 'hello'."
  }
}

以json格式的模板为例,sensitive 为 false

{
  "variable": {
    "my_hello_world_variable": [
      {
        "default": "hello world",
        "description": "this is a variable",
        "nullable": false,
        "sensitive": false,
        "type": "string",
        "validation": [
          {
            "condition": "${length(var.my_hello_world_variable) > 0 && substr(var.my_hello_world_variable, 0, 5) == \"hello\"}",
            "error_message": "my_hello_world_variable should start with 'hello'."
          }
        ]
      }
    ]
  }
}

nullable

Boolean

参数是否可设置为null。

如果variable中没有定义nullable,默认返回true。

以HCL格式的模板为例,nullable 为 false

variable "my_hello_world_variable" {
  type = string
  description = "this is a variable"
  default = "hello world"
  sensitive = false
  nullable = false
  validation {
    condition     = length(var.my_hello_world_variable) > 0 && substr(var.my_hello_world_variable, 0, 5) == "hello"
    error_message = "my_hello_world_variable should start with 'hello'."
  }
}

以json格式的模板为例,nullable 为 false

{
  "variable": {
    "my_hello_world_variable": [
      {
        "default": "hello world",
        "description": "this is a variable",
        "nullable": false,
        "sensitive": false,
        "type": "string",
        "validation": [
          {
            "condition": "${length(var.my_hello_world_variable) > 0 && substr(var.my_hello_world_variable, 0, 5) == \"hello\"}",
            "error_message": "my_hello_world_variable should start with 'hello'."
          }
        ]
      }
    ]
  }
}

validations

Array of VariableValidationResponse objects

参数的校验模块

表6 VariableValidationResponse

参数

参数类型

描述

condition

String

校验表达式

error_message

String

校验失败后的错误信息

状态码: 400

表7 响应Body参数

参数

参数类型

描述

error_code

String

响应码

最小长度:11

最大长度:11

error_msg

String

响应消息

encoded_authorization_message

String

包含有关未经授权请求的信息。

状态码: 401

表8 响应Body参数

参数

参数类型

描述

error_code

String

响应码

最小长度:11

最大长度:11

error_msg

String

响应消息

encoded_authorization_message

String

包含有关未经授权请求的信息。

状态码: 403

表9 响应Body参数

参数

参数类型

描述

error_code

String

响应码

最小长度:11

最大长度:11

error_msg

String

响应消息

encoded_authorization_message

String

包含有关未经授权请求的信息。

状态码: 429

表10 响应Body参数

参数

参数类型

描述

error_code

String

响应码

最小长度:11

最大长度:11

error_msg

String

响应消息

encoded_authorization_message

String

包含有关未经授权请求的信息。

状态码: 500

表11 响应Body参数

参数

参数类型

描述

error_code

String

响应码

最小长度:11

最大长度:11

error_msg

String

响应消息

encoded_authorization_message

String

包含有关未经授权请求的信息。

请求示例

  • 解析uri中的模板的参数

    POST https://{endpoint}/v1/ba2b9930c977f71edaeaa3a5e96a8ff1/template-analyses/variables
    
    {
      "template_uri" : "https://my_hello_world_bucket.{region}.myhuaweicloud.com/my_hello_world_template.zip"
    }
  • 解析请求中的模板的参数

    POST https://{endpoint}/v1/ba2b9930c977f71edaeaa3a5e96a8ff1/template-analyses/variables
    
    {
      "template_body" : "terraform {\n    required_providers {\n      huaweicloud = {\n        source = \"huawei.com/provider/huaweicloud\"\n        version = \"1.41.0\"\n        }\n    }\n}\nprovider \"huaweicloud\"{\n    insecure = true\n    cloud = \"{cloud_name}\"\n    region = \"{region}\"\n    endpoints = {\n        iam = \"{iam_endpoint}\",\n    }\n}\n\nvariable \"name\" {\n  type = string\n  default = \"my_default_vpc\"\n  sensitive = true\n  nullable = true\n  validation {\n  condition     = length(var.name) > 2 && substr(var.name, 0, 2) == \"my\"\n  error_message = \"The name value must be a valid name, starting with my.\"\n  }\n}\n\nresource \"huaweicloud_vpc\" \"vpc\" {\n  cidr = \"172.16.0.0/16\"\n  name = var.name\n}"
    }

响应示例

状态码: 200

解析成功

{
  "variables" : [ {
    "default" : "my_default_vpc",
    "name" : "name",
    "nullable" : true,
    "sensitive" : true,
    "type" : "string",
    "validations" : [ {
      "condition" : "${length(var.id) > 2 && substr(var.id, 0, 2) == \"my\"}",
      "error_message" : "The id value must be a valid id, starting with my."
    } ]
  } ]
}

状态码

状态码

描述

200

解析成功

400

用户请求非法

401

用户身份认证失败

403

用户无权限调用此API

429

请求数量过多

500

服务器内部错误

分享:

    相关文档

    相关产品