更新时间:2022-02-22 GMT+08:00

get_attribute

get_attribute用于获取模板中定义的其他元素初始化后的结果

语法

get_attribute: [resourceName, attributeName ]

若attributeName对应的内容是一个结构体,包含多个key-vaule字段时,可以延伸定义。格式如下:

get_attribute: [resourceName, attributeName1, attributeName2, […]]

参数说明

表1 参数说明

参数

是否必填

参数说明

resourceName

用户在模板中自定义的资源名称

attributeName

您想要获取的资源特定属性名称。属性名称详见元素对象的输出章节outputs。当模板中定义的属性名称不存在时,将返回空。

目前大部分元素仅支持获取refID和refName。

  • refID:资源创建后所生成的唯一标识。
  • refName:资源的名称。

返回值

所获取的属性值。

  • 当创建单个资源时,refID、refName返回值为String类型
  • 当创建多个资源时(如一次性创建多台ECS),refID、refName返回值为String数组。

示例

  • 获取参数并赋值给output的参数。

    示例:获取my-first-vpc创建后的id,并赋值给堆栈的输出参数vpc_id。

    tosca_definitions_version: cloud_tosca_version_1_0
    inputs:
      name:
        default: test-vpc
      cidr:
        default: 10.0.0.0/8  
    node_templates:
      my-first-vpc:
        type: Cloud.VPC.VPC
        properties:
          name: {get_input: name}
          ciddr: {get_input: cidr}
    outputs:
      vpc_id:
        value: {get_attribute: [my-first-vpc,refID]}
  • 获取参数并作为其他资源创建的输入参数

    示例:获取my-second-vpc创建后的id,并赋值给subnet资源,作为创建subnet资源的输入,实现一个blueprint中联动创建多个资源。

    node_templates:
      my-subnet:
        type: Cloud.VPC.Subnet
        properties:
          name: {get_input: subnet-name}
          cidr: {get_input: vpc-cidr}
          gateway: {get_input: subnet-gateway}
          dnsList: {get_input: dnsList}
          vpc: {get_attribute: [my-second-vpc,refID]}
          availabilityZone: {get_input: az}
        requirements:
          - vpcId:
              node: my-vpc    
      my-second-vpc:
        type: Cloud.VPC.VPC
        properties:
          name: {get_input: vpc-name}
          cidr: {get_input: vpc-cidr}