Updated on 2024-12-12 GMT+08:00

Parsing Template Variables

Function

ParseTemplateVariables

This API parses variables in your input template and returns all variable blocks in the template.

  • If variables are defined in the input template, 200 and all variables are returned.
  • If variables are not defined in the input template, 200 and an empty object are returned.
  • If your request or the input template is invalid, 400 is returned.

URI

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

Table 1 Path Parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

A project ID is obtained by calling an API or from the console.

Obtaining a Project ID

Minimum: 3

Maximum: 64

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

Client-Request-Id

Yes

String

A unique request ID is specified by a user to locate a request. UUID is recommended.

Minimum: 36

Maximum: 128

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

template_body

No

String

HCL template. It describes the target status of a resource. RFS compares the differences between the statuses of this template and the current remote resources.

Either template_body or template_uri must be specified but they both cannot be specified together.

In the CreateStack API, template_body and template_uri are optional.

Note:

  • template_body cannot contain any sensitive information. RFS directly uses, logs, displays, and stores the corresponding template_body in plaintext. If the information is sensitive, you are advised to use vars_structure to change the information to a variable and set the encryption field to enable encrypted transmission.

Minimum: 0

Maximum: 51200

template_uri

No

String

OBS address of an HCL template. The template describes the target status of a resource. RFS compares the differences between the statuses of this template and the current remote resources.

The OBS address allows mutual access to regions of the same type. Regions are classified into universal regions and dedicated regions. A universal region provides universal cloud services for common tenants. A dedicated region provides specific services for specific tenants.

The corresponding file must be a tf file or a zip package.

A pure .tf file must end with .tf or .tf.json and comply with the HCL syntax.

Currently, only the .zip package is supported. The file name extension must be .zip. The decompressed file cannot contain the .tfvars file and must be encoded in UTF8 format (the .tf.json file cannot contain the BOM header). The .zip package supports a maximum of 100 subfiles.

Either template_body or template_uri must be specified.

In the CreateStack API, template_body and template_uri are optional.

Note:

  • The template file corresponds to template_uri cannot contain any sensitive information. RFS directly uses, logs, displays, and stores the content of the template file in plaintext. If the information is sensitive, you are advised to use vars_structure to change the information to a variable and set the encryption field to enable encrypted transmission.
  • If the template file corresponding to template_uri is of zip type, the length of the internal file or folder name must not exceed 255 bytes, the length of the deepest path must not exceed 2048 bytes, and the size of the zip package must not exceed 1MB.

Minimum: 0

Maximum: 2048

Response Parameters

Status code: 200

Table 4 Response body parameters

Parameter

Type

Description

variables

Array of VariableResponse objects

Variables parsed from a template.

Table 5 VariableResponse

Parameter

Type

Description

name

String

Variable name.

For example, in the following HCL template, the value of name is 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'."
  }
}

In a JSON template, the value of name is 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

Variable type.

For example, in the following HCL template, the value of type is 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'."
  }
}

In a JSON template, the value of type is 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

Variable description.

For example, in the following HCL template, the value of description is 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'."
  }
}

In a JSON template, the value of description is 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

Default variable value. The type of the return value is the same as that defined in the type field.

For example, for a variable whose type is string, the type of the return value is string; for a variable whose type is number, the type of the return value is number.

For example, in the following HCL template, the value of default is 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'."
  }
}

In a JSON template, the value of default is 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

Whether the variable is sensitive.

If sensitive is not defined in the variable, false is returned by default.

For example, in the following HCL template, the value of sensitive is 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'."
  }
}

In a JSON template, the value of sensitive is 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

Whether the variable can be set to null.

If nullable is not defined in the variable, true is returned by default.

For example, in the following HCL template, the value of nullable is 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'."
  }
}

In a JSON template, the value of nullable is 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

Variable verification module.

Table 6 VariableValidationResponse

Parameter

Type

Description

condition

String

Variable expression.

error_message

String

Error message generated upon verification failure.

Status code: 400

Table 7 Response body parameters

Parameter

Type

Description

error_code

String

Response code.

Minimum: 11

Maximum: 11

error_msg

String

Response message.

encoded_authorization_message

String

The message contains information about unauthorized requests.

Status code: 401

Table 8 Response body parameters

Parameter

Type

Description

error_code

String

Response code.

Minimum: 11

Maximum: 11

error_msg

String

Response message.

encoded_authorization_message

String

The message contains information about unauthorized requests.

Status code: 403

Table 9 Response body parameters

Parameter

Type

Description

error_code

String

Response code.

Minimum: 11

Maximum: 11

error_msg

String

Response message.

encoded_authorization_message

String

The message contains information about unauthorized requests.

Status code: 429

Table 10 Response body parameters

Parameter

Type

Description

error_code

String

Response code.

Minimum: 11

Maximum: 11

error_msg

String

Response message.

encoded_authorization_message

String

The message contains information about unauthorized requests.

Status code: 500

Table 11 Response body parameters

Parameter

Type

Description

error_code

String

Response code.

Minimum: 11

Maximum: 11

error_msg

String

Response message.

encoded_authorization_message

String

The message contains information about unauthorized requests.

Example Requests

  • Parse template variables in the URI.
    POST https://{endpoint}/v1/ba2b9930c977f71edaeaa3a5e96a8ff1/template-analyses/variables
    
    {
      "template_uri" : "https://my_hello_world_bucket.{region}.myhuaweicloud.com/my_hello_world_template.zip"
    }
  • Parse template variables in the request.
    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}"
    }

Example Responses

Status code: 200

Variables parsed.

{
  "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."
    } ]
  } ]
}

Status Codes

Status Code

Description

200

Variables parsed.

400

Invalid request.

401

Authentication failed.

403

The user does not have the permission to call this API.

429

Too frequent requests.

500

Internal server error.