
# 解析模板参数
#### 功能介绍
解析模板参数（ParseTemplateVariables）
此API用于解析用户输入的模板中的参数（variable），将解析模板中的所有variable块并返回
- 如果用户传入的模板中定义了variable参数，则返回200和所有variable
  
- 如果用户传入的模板中没有定义variable参数，则返回200和空对象
  
- 如果用户请求非法或传入的模板非法，则返回400
  
 
#### 调试
您可以在[API Explorer](https://console.huaweicloud.com/apiexplorer/#/openapi/AOS/doc?api=ParseTemplateVariables)中调试该接口，支持自动认证鉴权。API Explorer可以自动生成SDK代码示例，并提供SDK代码示例调试功能。
#### URI
POST /v1/{project_id}/template-analyses/variables
表1路径参数 
| 参数         | 是否必选 | 参数类型   | 描述                                                                                                                                                                                                                                                                                                                                                                     |
|:---|:---|:---|:---|
| project_id | 是    | String | 项目ID，可以从调用API处获取，也可以从控制台获取。 [项目ID获取方式](https://support.huaweicloud.com/api-ticket/ticket_api_20002.html) 最小长度：**3** 最大长度：**64** |
   
#### 请求参数
表2请求Header参数 
| 参数                | 是否必选 | 参数类型   | 描述                                                                                                                                                                                                                    |
|:---|:---|:---|:---|
| Client-Request-Id | 是    | String | 用户指定的，对于此请求的唯一ID，用于定位某个请求，推荐使用UUID 最小长度：**36** 最大长度：**128** |
   
表3请求Body参数 
| 参数               | 是否必选 | 参数类型                                                                                                          | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
|:---|:---|:---|:---|
| template_body    | 否    | String                                                                                                        | HCL模板，描述了资源的目标状态。资源编排服务将比较此模板与当前远程资源的状态之间的区别。 template_body，template_uri和private_template必须有且只有一个存在 在CreateStack API中，template_body，template_uri和private_template可以都不给予 **注意：** - template_body中默认不应该含有任何敏感信息，资源编排服务会直接明文使用、log、展示和存储对应的template_body。如为敏感信息，建议在模板中抽取变量并声明为sensitive，将敏感信息通过vars_structure参数化，并设置encryption字段开启加密    （如果变量variable在模板中被标记为sensitive，则在 GetStackMetadata 和 GetExecutionPlanMetadata 接口返回的 vars_structure、vars_uri_content、vars_body 中，该变量的值将被脱敏） 最小长度：**0** 最大长度：**51200**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| template_uri     | 否    | String                                                                                                        | HCL模板的OBS地址，该模板描述了资源的目标状态。资源编排服务将比较此模板与当前远程资源的状态之间的区别。 请确保OBS地址所在局点与使用RFS服务局点一致。 对应的文件应该是纯tf文件或zip压缩包 纯tf文件需要以.tf或者.tf.json结尾，并遵守HCL语法 压缩包目前只支持zip格式，文件需要以.zip结尾。解压后的文件不得包含.tfvars文件且必须是UTF8编码（其中.tf.json不能包含BOM头），zip压缩包当前支持的子文件数量最大为100 template_body，template_uri和private_template必须有且只有一个存在 在CreateStack API中，template_body，template_uri和private_template可以都不给予 **注意：** - template_uri对应的模板文件中默认不应该含有任何敏感信息，资源编排服务会直接明文使用、log、展示和存储对应的模板文件内容。如为敏感信息，建议在模板中抽取变量并声明为sensitive，将敏感信息通过vars_structure参数化，并设置encryption字段开启加密   - template_uri对应的模板文件如果为zip类型，则内部的文件或文件夹名称长度不得超过255个字节，最深路径长度不得超过2048字节，zip包大小不得超过1MB    （如果变量variable在模板中被标记为sensitive，则在 GetStackMetadata 和 GetExecutionPlanMetadata 接口返回的 vars_structure、vars_uri_content、vars_body 中，该变量的值将被脱敏） 最小长度：**0** 最大长度：**2048** |
| private_template | 否    | [PrivateTemplate] object |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
   
 表4PrivateTemplate 
| 参数               | 是否必选 | 参数类型   | 描述                                                                                                                                                                                                          |
|:---|:---|:---|:---|
| template_id      | 是    | String | 私有模板（PrivateTemplate）的Id。 最小长度：**36** 最大长度：**36** |
| template_version | 是    | String | 模板版本Id，以大写V开头。 最小长度：**2** 最大长度：**11**             |
   
#### 响应参数
**状态码：200**
表5响应Body参数 
| 参数        | 参数类型                                                                                                                       | 描述          |
|:---|:---|:---|
| variables | Array of [VariableResponse] objects | 解析模板得到的参数列表 |
   
 表6VariableResponse 
| 参数          | 参数类型                                                                                                                                           | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
|:---|:---|:---|
| 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 | 参数的校验模块                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
   
 表7VariableValidationResponse 
| 参数            | 参数类型   | 描述         |
|:---|:---|:---|
| condition     | String | 校验表达式      |
| error_message | String | 校验失败后的错误信息 |
   
**状态码：400**
表8响应Body参数 
| 参数                            | 参数类型                                                                                                   | 描述                                                                                                                                                                                    |
|:---|:---|:---|
| error_code                    | String                                                                                                 | 响应码 最小长度：**11** 最大长度：**11** |
| error_msg                     | String                                                                                                 | 响应消息                                                                                                                                                                                  |
| encoded_authorization_message | String                                                                                                 | 包含有关未经授权请求的信息。                                                                                                                                                                        |
| details                       | Array of [Detail] objects | 权限拒绝时服务返回的详细错误信息。                                                                                                                                                                     |
   
 表9Detail 
| 参数         | 参数类型   | 描述   |
|:---|:---|:---|
| error_code | String | 响应码  |
| error_msg  | String | 响应消息 |
   
**状态码：401**
表10响应Body参数 
| 参数                            | 参数类型                                                                                                     | 描述                                                                                                                                                                                    |
|:---|:---|:---|
| error_code                    | String                                                                                                   | 响应码 最小长度：**11** 最大长度：**11** |
| error_msg                     | String                                                                                                   | 响应消息                                                                                                                                                                                  |
| encoded_authorization_message | String                                                                                                   | 包含有关未经授权请求的信息。                                                                                                                                                                        |
| details                       | Array of [Detail] objects | 权限拒绝时服务返回的详细错误信息。                                                                                                                                                                     |
   
 表11Detail 
| 参数         | 参数类型   | 描述   |
|:---|:---|:---|
| error_code | String | 响应码  |
| error_msg  | String | 响应消息 |
   
**状态码：403**
表12响应Body参数 
| 参数                            | 参数类型                                                                                                     | 描述                                                                                                                                                                                    |
|:---|:---|:---|
| error_code                    | String                                                                                                   | 响应码 最小长度：**11** 最大长度：**11** |
| error_msg                     | String                                                                                                   | 响应消息                                                                                                                                                                                  |
| encoded_authorization_message | String                                                                                                   | 包含有关未经授权请求的信息。                                                                                                                                                                        |
| details                       | Array of [Detail] objects | 权限拒绝时服务返回的详细错误信息。                                                                                                                                                                     |
   
 表13Detail 
| 参数         | 参数类型   | 描述   |
|:---|:---|:---|
| error_code | String | 响应码  |
| error_msg  | String | 响应消息 |
   
**状态码：429**
表14响应Body参数 
| 参数                            | 参数类型                                                                                                     | 描述                                                                                                                                                                                    |
|:---|:---|:---|
| error_code                    | String                                                                                                   | 响应码 最小长度：**11** 最大长度：**11** |
| error_msg                     | String                                                                                                   | 响应消息                                                                                                                                                                                  |
| encoded_authorization_message | String                                                                                                   | 包含有关未经授权请求的信息。                                                                                                                                                                        |
| details                       | Array of [Detail] objects | 权限拒绝时服务返回的详细错误信息。                                                                                                                                                                     |
   
 表15Detail 
| 参数         | 参数类型   | 描述   |
|:---|:---|:---|
| error_code | String | 响应码  |
| error_msg  | String | 响应消息 |
   
**状态码：500**
表16响应Body参数 
| 参数                            | 参数类型                                                                                                     | 描述                                                                                                                                                                                    |
|:---|:---|:---|
| error_code                    | String                                                                                                   | 响应码 最小长度：**11** 最大长度：**11** |
| error_msg                     | String                                                                                                   | 响应消息                                                                                                                                                                                  |
| encoded_authorization_message | String                                                                                                   | 包含有关未经授权请求的信息。                                                                                                                                                                        |
| details                       | Array of [Detail] objects | 权限拒绝时服务返回的详细错误信息。                                                                                                                                                                     |
   
 表17Detail 
| 参数         | 参数类型   | 描述   |
|:---|:---|:---|
| error_code | String | 响应码  |
| error_msg  | 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"
  }
  ```
  
- 解析请求中的模板的参数。RFS模板支持HCL语法，同时规定required_providers模块下用于声明provider路径的source字段，需按照huawei.com/provider/{provider_name}的形式拼接，例如huawei.com/provider/huaweicloud
  ```
  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}"
  }
  ```
  
- 解析私有模板中的模板的参数
  ```
  POST https://{endpoint}/v1/ba2b9930c977f71edaeaa3a5e96a8ff1/template-analyses/variables
  {
    "private_template" : {
      "template_id" : "69f8d5ea-eaa4-4a3b-a96d-bae9230e97c9",
      "template_version" : "V1"
    }
  }
  ```
  
 
#### 响应示例
**状态码：200**
解析成功
```
{
  "variables" : [ {
    "default" : "my_default_vpc",
    "name" : "name",
    "nullable" : true,
    "sensitive" : true,
    "type" : "string",
    "validations" : [ {
      "condition" : "${length(var.name) > 2 && substr(var.name, 0, 2) == \"my\"}",
      "error_message" : "The id value must be a valid id, starting with my."
    } ]
  } ]
}
```
#### 状态码
| 状态码 | 描述          |
|:---|:---|
| 200 | 解析成功        |
| 400 | 用户请求非法      |
| 401 | 用户身份认证失败    |
| 403 | 用户无权限调用此API |
| 429 | 请求数量过多      |
| 500 | 服务器内部错误     |
   
