资源(resources) V3
描述堆栈中每一个资源的属性和依赖关系。一个资源可以被其他资源所引用。
语法
资源部分由资源名称和资源描述组成。所有资源描述都被括在括号里。如果您声明多个资源,则可用逗号将它们分隔开。以下代码段描述了 resources 的语法结构:
1"resources" : {
2 "resource1": {
3 "type": "资源类型",
4 "count": 资源数量,
5 "properties": {
6 资源属性
7 },
8 "dependsOn": [资源依赖列表]
9 },
10 "resource2": {
11 "type": "资源类型",
12 "count": 资源数量,
13 "properties": {
14 资源属性
15 },
16 "dependsOn": [资源依赖列表]
17 }
18}
资源名称
资源名称 在模板中具有唯一性。可使用资源名称在模板的其他部分中引用资源。
资源类型
资源类型标识您正在声明的资源的类型。有关所有资源的列表,请参阅 云编排资源。
资源数量
此为可选字段,利用此字段可以创建多份相同规格的资源实例,需要注意的是,指定count后COS会对模板进行预处理,把该资源展开成多个资源。 操作资源栈时使用处理后的模板。 例如:资源名为A,其count值为3。在处理后的模板中,没有A资源,取而代之的是A[0]、A[1]和A[2]这3个资源。
1// 处理前的模板
2"resources" : {
3 "A": {
4 count: 3
5 }
6}
7
8// 处理后的模板
9"resources" : {
10 "A[0]": {
11 ...
12 },
13 "A[1]": {
14 ...
15 }
16 "A[2]": {
17 ...
18 }
19}
指定了count的资源的属性(properties)中可以使用伪参数BCE::Index,在预处理的时候会被替换为相应的数值。例如,A[0]使用的BCE::Index会被替换为0,A[1]使用的BCE::Index会被替换为1。模板其他地方不能使用BCE::Index。 如果指定了count的资源出现在dependsOn、Ref中,会被展开。 下面描述一个场景:创建了2个EIP和2台BCC,并把EIP与BCC逐一绑定。
1"bcc": {
2 "type": "BCE::BCC::Instance",
3 "count": 2,
4 "properties": {
5 "imageId": "m-xxx",
6 "billing": {
7 "paymentTiming": "Postpaid"
8 },
9 "spec": "bcc.g3.c1m1",
10 "rootDiskSizeInGb": 40,
11 "rootDiskStorageType": "ssd",
12 "name": "bcc_instance_name"
13 }
14},
15"eip": {
16 "type": "BCE::EIP::Instance",
17 "count": 2,
18 "properties": {
19 "bandwidthInMbps": 5
20 }
21},
22"bcc_eip": {
23 "type": "BCE::BCC::EipAttachment",
24 "count": 2,
25 "properties": {
26 "bccInstanceId": {
27 "Ref": "bcc[BCE::Index].bccInstanceId"
28 },
29 "eip": {
30 "Ref": "eip[BCE::Index].eip"
31 }
32 },
33 // 此处显式声明依赖可以不写
34 "dependsOn": ["bcc[BCE::Index]", "eip[BCE::Index]"]
35}
根据上面描述的展开规则处理之后的模板如下:
1"bcc[0]": {
2 "type": "BCE::BCC::Instance",
3 "properties": {
4 "imageId": "m-8S2nu8s3",
5 "billing": {
6 "paymentTiming": "Postpaid"
7 },
8 "spec": "bcc.g3.c1m1",
9 "rootDiskSizeInGb": 40,
10 "rootDiskStorageType": "ssd",
11 "name": "bcc_instance_name"
12 }
13},
14"bcc[1]": {
15 "type": "BCE::BCC::Instance",
16 "properties": {
17 // 省略
18 }
19},
20"eip[0]": {
21 "type": "BCE::EIP::Instance",
22 "properties": {
23 "bandwidthInMbps": 5
24 }
25},
26"eip[1]": {
27 "type": "BCE::EIP::Instance",
28 "properties": {
29 // 省略
30 }
31},
32"bcc_eip[0]": {
33 "type": "BCE::BCC::EipAttachment",
34 "properties": {
35 "bccInstanceId": {
36 "Ref": "bcc[0].bccInstanceId"
37 }
38 "eip": {
39 "Ref": "eip[0].eip"
40 }
41 },
42 "dependsOn": ["bcc[0]", "eip[0]"]
43},
44"bcc_eip[1]": {
45 "type": "BCE::BCC::EipAttachment",
46 "properties": {
47 "bccInstanceId": {
48 "Ref": "bcc[1].bccInstanceId"
49 }
50 "eip": {
51 "Ref": "eip[1].eip"
52 }
53 },
54 "dependsOn": ["bcc[1]", "eip[1]"]
55}
资源属性
资源属性是可以为资源指定的附加选项。例如,对于每个百度云BCC实例,必须为该实例指定一个 Image ID和spec。如以下代码段所示:
1"resources" : {
2 "bcc": {
3 "type": "BCE::BCC::Instance",
4 "properties": {
5 "imageId": {
6 "Ref": "imageId"
7 },
8 "spec": {
9 "Ref": "spec"
10 }
11 }
12 }
13}
如果资源不需要声明任何属性,那么您可以忽略该资源的属性部分。
资源依赖列表
此字段是可选字段,用来定义此资源依赖的其他资源名称;依赖其他资源,代表本资源会在其他资源创建完成后再创建。 例如:
1// 代表只有当B和C都创建完成后,A才会创建。
2"resources" : {
3 "A": {
4 ...
5 "dependsOn": ["B","C"]
6 },
7 "B":{...},
8 "C":{...},
9}
除了用户显式的定义依赖关系,COS也会通过Ref等函数自动推断依赖关系。 下面描述一个场景:创建了EIP和BCC,并把EIP与BCC绑定;本场景中虽然并未显式的定义bcc_eip 依赖 bcc和eip,但是由于Ref函数引用了bcc和eip,COS服务预处理模版会自动添加依赖。
1// 代表只有当B和C都创建完成后,A才会创建。
2"resources" : {
3 "bcc": {
4 "type": "BCE::BCC::Instance",
5 "properties": {
6 "imageId": "m-xxx",
7 "spec": "bcc.g3.c1m1",
8 "billing": {
9 "paymentTiming": "Postpaid"
10 }
11 }
12 },
13 "eip": {
14 "type": "BCE::EIP::Instance",
15 "properties": {
16 "bandwidthInMbps": 5
17 }
18 },
19 "bcc_eip":{
20 "type": "BCE::BCC::EipAttachment",
21 "properties":{
22 "bccInstanceId":{
23 "Ref": "bcc.id"
24 },
25 "eip": {
26 "Ref": "eip.eip"
27 }
28 }
29 }
30}
示例
以下示例显示的是典型的资源声明。
1{
2 "resources": {
3 "bcc": {
4 "type": "BCE::BCC::Instance",
5 "properties": {
6 "imageId": {
7 "Ref": "imageId"
8 },
9 "spec": {
10 "Ref": "spec"
11 },
12 "name": {
13 "Ref": "bccName"
14 },
15 "hostname": {
16 "Ref": "hostname"
17 },
18 "rootDiskStorageType": {
19 "Ref": "rootDiskStorageType"
20 },
21 "rootDiskSizeInGb": {
22 "Ref": "rootDiskSizeInGb"
23 },
24 "zoneName": {
25 "Ref": "zoneName"
26 },
27 "billing": {
28 "Ref": "bccBilling"
29 },
30 "subnetId": {
31 "Ref": "subnetId"
32 },
33 "securityGroupIds": {
34 "Ref": "securityGroupIds"
35 },
36 "keypairId": {
37 "Ref": "keypairId"
38 }
39 }
40 }
41 }
42}