当您编写模板时,可能需要配置一组关联参数。如果其中一个参数的取值不同,可能需要配置的关联参数也不同。本文以计费方式为例,为您介绍如何在计费方式为包年包月时动态呈现包年包月的单位和时长。

模板语法

您可以在参数中指定AssociationProperty和对应的AssociationPropertyMetadata,以便在指定计费方式下动态呈现对应参数。其中,AssociationPropertyMetadata需定义Visible字段,字段中使用函数定义参数是否呈现,具体如下:

Visible {
  Condition: Object; // 呈现当前参数时所依赖的条件。
}

Condition支持的函数和示例如下:

  • Fn::Equals"Fn::Equals": ['${targetInstanceChargeType}', "PostPaid"]
  • Fn::Not"Fn::Not": ${condition}
  • Fn::And"Fn::And": [${condition1}, ${condition2}, ...]
  • Fn::Or"Fn::Or": [${condition1}, ${condition2}, ...]

模板示例

在以下示例中,AssociationProperty表示计费方式,AssociationPropertyMetadata表示对不同参数添加筛选条件,以便在创建资源时进行参数筛选。

{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Description": {
    "zh-cn": "单实例自定义镜像模板示例,支持创建基础资源配置(VPC、VSwitch、SecurityGroup)或者选择已有基础资源配置,并支持配置ECS的计费方式为包年包月或者按量付费。",
    "en": "This template supports single-instance with custom image, including scenarios using existing or creating new VPC, VSwitch, SecurityGroup and creating ECS by prepaid or postpaid model."
  },
  "Parameters": {
    "targetInstanceChargeType": {
      "Description": {
        "name-en": "targetInstanceChargeType",
        "name-zh-cn": "计费方式"
      },
      "Type": "String",
      "AssociationProperty": "ChargeType"
    },
    "targetPeriodUnit": {
      "Description": {
        "name-en": "The unit of the subscription period",
        "name-zh-cn": "包年包月计费方式的时长单位"
      },
      "Type": "String",
      "AllowedValues": [
        "Week",
        "Month"
      ],
      "Default": "Month",
      "AssociationPropertyMetadata": {
        "Visible": {
          "Condition": {
            "Fn::Not": {
              "Fn::Equals": [
                "${targetInstanceChargeType}",
                "PostPaid"
              ]
            }
          }
        }
      }
    },
    "targetPeriod": {
      "Description": {
        "name-en": "The subscription period of the instance",
        "name-zh-cn": "购买包年包月资源的时长",
        "en": "Valid values: Valid values when PeriodUnit is set to Week: 1, 2, 3, and 4. Valid values when PeriodUnit is set to Month: 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 24, 36, 48, and 60.",
        "zh-cn": "取值:PeriodUnit取值为Week时:1、2、3、4。PeriodUnit取值为Month时:1、2、3、4、5、6、7、8、9、12、24、36、48、60。"
      },
      "Type": "Number",
      "AllowedValues": [
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        12,
        24,
        36,
        48,
        60
      ],
      "Default": 1,
      "AssociationPropertyMetadata": {
        "Visible": {
          "Condition": {
            "Fn::Not": {
              "Fn::Equals": [
                "${targetInstanceChargeType}",
                "PostPaid"
              ]
            }
          }
        }
      }
    }
  }
}

参数说明:

  • targetInstanceChargeType:计费方式。AssociationProperty取值为ChargeType,表示根据计费方式的取值筛选。
  • targetPeriodUnit:包年包月计费方式的时长单位。AssociationPropertyMetadata取值为{"Visible": {"Condition": {"Fn::Not": {"Fn::Equals": ["${targetInstanceChargeType}","PostPaid"]}}}},表示targetInstanceChargeType取值不为PostPaid时,呈现targetPeriodUnit。
    说明 targetInstanceChargeType取值为PrePaid或PostPaid。
  • targetPeriod:购买包年包月资源的时长。AssociationPropertyMetadata取值为{"Visible": {"Condition": {"Fn::Not": {"Fn::Equals": ["${targetInstanceChargeType}","PostPaid"]}}}},表示targetInstanceChargeType取值不为PostPaid时,呈现targetPeriod。

控制台配置项

当您使用模板示例创建资源栈时,如果计费方式为包年包月,ROS控制台将呈现如下关联参数。

包年包月