1
1
package cvm_test
2
2
3
3
import (
4
- tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"
5
- tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
6
- svccvm "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/cvm"
7
-
8
4
"context"
9
5
"fmt"
10
6
"testing"
11
7
12
8
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
13
9
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
10
+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"
11
+ tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"
12
+ tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
13
+ svccvm "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/cvm"
14
14
)
15
15
16
- const (
17
- ImageSnap = "tencentcloud_image.image_snap"
18
- ImageInstance = "tencentcloud_image.image_instance"
19
- )
20
-
21
- func TestAccTencentCloudImageResource (t * testing.T ) {
16
+ func TestAccTencentCloudCvmImageResource_UseSnapshotId (t * testing.T ) {
22
17
t .Parallel ()
23
18
resource .Test (t , resource.TestCase {
24
- PreCheck : func () { tcacctest .AccPreCheck (t ) },
25
- Providers : tcacctest .AccProviders ,
26
- CheckDestroy : testAccCheckImageDestroy ,
19
+ PreCheck : func () {
20
+ acctest .AccPreCheck (t )
21
+ },
22
+ Providers : acctest .AccProviders ,
23
+ CheckDestroy : testAccCheckCvmImageDestroy ,
27
24
Steps : []resource.TestStep {
28
- // use snapshot id
29
25
{
30
- Config : testAccImageWithSnapShot ,
31
- Check : resource .ComposeTestCheckFunc (
32
- testAccCheckImageExists (ImageSnap ),
33
- resource .TestCheckResourceAttr (ImageSnap , "image_name" , "image-snapshot-keep" ),
34
- resource .TestCheckResourceAttr (ImageSnap , "snapshot_ids.#" , "1" ),
35
- resource .TestCheckResourceAttr (ImageSnap , "force_poweroff" , "true" ),
36
- resource .TestCheckResourceAttr (ImageSnap , "image_description" , "create image with snapshot" ),
37
- ),
26
+ Config : testAccCvmImageResource_UseSnapshotIdCreate ,
27
+ Check : resource .ComposeTestCheckFunc (testAccCheckCvmImageExists ("tencentcloud_image.image_snap" ), resource .TestCheckResourceAttr ("tencentcloud_image.image_snap" , "image_name" , "image-snapshot-keep" ), resource .TestCheckResourceAttr ("tencentcloud_image.image_snap" , "snapshot_ids.#" , "1" ), resource .TestCheckResourceAttr ("tencentcloud_image.image_snap" , "force_poweroff" , "true" ), resource .TestCheckResourceAttr ("tencentcloud_image.image_snap" , "image_description" , "create image with instance" )),
38
28
},
39
29
{
40
- Config : testAccImageWithSnapShotUpdate ,
41
- Check : resource .ComposeAggregateTestCheckFunc (
42
- resource .TestCheckResourceAttr (ImageSnap , "image_name" , "image-snapshot-update-keep" ),
43
- resource .TestCheckResourceAttr (ImageSnap , "snapshot_ids.#" , "1" ),
44
- resource .TestCheckResourceAttr (ImageSnap , "force_poweroff" , "false" ),
45
- resource .TestCheckResourceAttr (ImageSnap , "image_description" , "update image with snapshot" ),
46
- ),
30
+ Config : testAccCvmImageResource_UseSnapshotIdChange1 ,
31
+ Check : resource .ComposeTestCheckFunc (testAccCheckCvmImageExists ("tencentcloud_image.image_snap" ), resource .TestCheckResourceAttr ("tencentcloud_image.image_snap" , "image_description" , "update image with instance" ), resource .TestCheckResourceAttr ("tencentcloud_image.image_snap" , "image_name" , "image-snapshot-update-keep" ), resource .TestCheckResourceAttr ("tencentcloud_image.image_snap" , "force_poweroff" , "false" )),
47
32
},
48
33
{
49
- ResourceName : ImageSnap ,
34
+ ResourceName : "tencentcloud_image.image_snap" ,
50
35
ImportState : true ,
51
36
ImportStateVerify : true ,
52
37
ImportStateVerifyIgnore : []string {"force_poweroff" },
53
38
},
54
- // use instance id
39
+ },
40
+ })
41
+ }
42
+
43
+ const testAccCvmImageResource_UseSnapshotIdCreate = `
44
+
45
+ resource "tencentcloud_image" "image_snap" {
46
+ image_name = "image-snapshot-keep"
47
+ snapshot_ids = ["snap-gem0ivcj"]
48
+ force_poweroff = true
49
+ image_description = "create image with instance"
50
+ }
51
+
52
+ `
53
+ const testAccCvmImageResource_UseSnapshotIdChange1 = `
54
+
55
+ resource "tencentcloud_image" "image_snap" {
56
+ image_name = "image-snapshot-update-keep"
57
+ snapshot_ids = ["snap-gem0ivcj"]
58
+ force_poweroff = false
59
+ image_description = "update image with instance"
60
+ }
61
+
62
+ `
63
+
64
+ func TestAccTencentCloudCvmImageResource_UseInstanceId (t * testing.T ) {
65
+ t .Parallel ()
66
+ resource .Test (t , resource.TestCase {
67
+ PreCheck : func () {
68
+ acctest .AccPreCheck (t )
69
+ },
70
+ Providers : acctest .AccProviders ,
71
+ CheckDestroy : testAccCheckCvmImageDestroy ,
72
+ Steps : []resource.TestStep {
55
73
{
56
- Config : testAccImageWithInstance ,
57
- Check : resource .ComposeTestCheckFunc (
58
- testAccCheckImageExists (ImageInstance ),
59
- resource .TestCheckResourceAttr (ImageInstance , "image_name" , "image-instance-keep" ),
60
- resource .TestCheckResourceAttrSet (ImageInstance , "instance_id" ),
61
- resource .TestCheckResourceAttr (ImageInstance , "data_disk_ids.#" , "1" ),
62
- resource .TestCheckResourceAttr (ImageInstance , "image_description" , "create image with instance" ),
63
- ),
74
+ Config : testAccCvmImageResource_UseInstanceIdCreate ,
75
+ Check : resource .ComposeTestCheckFunc (testAccCheckCvmImageExists ("tencentcloud_image.image_instance" ), resource .TestCheckResourceAttr ("tencentcloud_image.image_instance" , "image_name" , "image-instance-keep" ), resource .TestCheckResourceAttrSet ("tencentcloud_image.image_instance" , "instance_id" ), resource .TestCheckResourceAttr ("tencentcloud_image.image_instance" , "data_disk_ids.#" , "1" ), resource .TestCheckResourceAttr ("tencentcloud_image.image_instance" , "image_description" , "create image with instance" )),
64
76
},
65
77
{
66
- Config : testAccImageWithInstanceUpdate ,
67
- Check : resource .ComposeAggregateTestCheckFunc (
68
- resource .TestCheckResourceAttr (ImageInstance , "image_name" , "image-instance-update-keep" ),
69
- resource .TestCheckResourceAttr (ImageInstance , "image_description" , "update image with instance" ),
70
- ),
78
+ Config : testAccCvmImageResource_UseInstanceIdChange1 ,
79
+ Check : resource .ComposeTestCheckFunc (testAccCheckCvmImageExists ("tencentcloud_image.image_instance" ), resource .TestCheckResourceAttr ("tencentcloud_image.image_instance" , "image_name" , "image-instance-update-keep" ), resource .TestCheckResourceAttr ("tencentcloud_image.image_instance" , "image_description" , "update image with instance" )),
71
80
},
72
81
},
73
82
})
74
83
}
75
84
76
- func testAccCheckImageDestroy (s * terraform.State ) error {
85
+ const testAccCvmImageResource_UseInstanceIdCreate = `
86
+
87
+ data "tencentcloud_availability_zones" "default" {
88
+ }
89
+ data "tencentcloud_images" "default" {
90
+ image_type = ["PUBLIC_IMAGE"]
91
+ image_name_regex = "Final"
92
+ }
93
+ data "tencentcloud_images" "testing" {
94
+ image_type = ["PUBLIC_IMAGE"]
95
+ }
96
+ data "tencentcloud_instance_types" "default" {
97
+
98
+ filter {
99
+ values = ["ap-guangzhou-7"]
100
+ name = "zone"
101
+ }
102
+ filter {
103
+ name = "instance-family"
104
+ values = ["S4","SA2"]
105
+ }
106
+ cpu_core_count = 2
107
+ memory_size = 2
108
+ exclude_sold_out = true
109
+ }
110
+ resource "tencentcloud_vpc" "vpc" {
111
+ name = "image-vpc"
112
+ cidr_block = "10.0.0.0/16"
113
+ }
114
+ resource "tencentcloud_subnet" "subnet" {
115
+ vpc_id = tencentcloud_vpc.vpc.id
116
+ name = "image-subnet"
117
+ cidr_block = "10.0.0.0/16"
118
+ availability_zone = "ap-guangzhou-7"
119
+ }
120
+ resource "tencentcloud_instance" "cvm_image" {
121
+ availability_zone = "ap-guangzhou-7"
122
+ instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type
123
+
124
+ data_disks {
125
+ delete_with_instance = true
126
+ data_disk_type = "CLOUD_PREMIUM"
127
+ data_disk_size = 100
128
+ }
129
+ subnet_id = tencentcloud_subnet.subnet.id
130
+ system_disk_type = "CLOUD_PREMIUM"
131
+ image_id = data.tencentcloud_images.default.images.0.image_id
132
+ vpc_id = tencentcloud_vpc.vpc.id
133
+ project_id = 0
134
+ instance_name = "cvm-image"
135
+ }
136
+ resource "tencentcloud_image" "image_instance" {
137
+ instance_id = tencentcloud_instance.cvm_image.id
138
+ data_disk_ids = [tencentcloud_instance.cvm_image.data_disks.0.data_disk_id]
139
+ image_description = "create image with instance"
140
+ image_name = "image-instance-keep"
141
+ }
142
+
143
+ `
144
+ const testAccCvmImageResource_UseInstanceIdChange1 = `
145
+
146
+ data "tencentcloud_availability_zones" "default" {
147
+ }
148
+ data "tencentcloud_images" "default" {
149
+ image_type = ["PUBLIC_IMAGE"]
150
+ image_name_regex = "Final"
151
+ }
152
+ data "tencentcloud_images" "testing" {
153
+ image_type = ["PUBLIC_IMAGE"]
154
+ }
155
+ data "tencentcloud_instance_types" "default" {
156
+
157
+ filter {
158
+ values = ["ap-guangzhou-7"]
159
+ name = "zone"
160
+ }
161
+ filter {
162
+ values = ["S4","SA2"]
163
+ name = "instance-family"
164
+ }
165
+ cpu_core_count = 2
166
+ memory_size = 2
167
+ exclude_sold_out = true
168
+ }
169
+ resource "tencentcloud_vpc" "vpc" {
170
+ name = "image-vpc"
171
+ cidr_block = "10.0.0.0/16"
172
+ }
173
+ resource "tencentcloud_subnet" "subnet" {
174
+ vpc_id = tencentcloud_vpc.vpc.id
175
+ name = "image-subnet"
176
+ cidr_block = "10.0.0.0/16"
177
+ availability_zone = "ap-guangzhou-7"
178
+ }
179
+ resource "tencentcloud_instance" "cvm_image" {
180
+ availability_zone = "ap-guangzhou-7"
181
+ instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type
182
+
183
+ data_disks {
184
+ delete_with_instance = true
185
+ data_disk_type = "CLOUD_PREMIUM"
186
+ data_disk_size = 100
187
+ }
188
+ subnet_id = tencentcloud_subnet.subnet.id
189
+ system_disk_type = "CLOUD_PREMIUM"
190
+ image_id = data.tencentcloud_images.default.images.0.image_id
191
+ vpc_id = tencentcloud_vpc.vpc.id
192
+ project_id = 0
193
+ instance_name = "cvm-image"
194
+ }
195
+ resource "tencentcloud_image" "image_instance" {
196
+ instance_id = tencentcloud_instance.cvm_image.id
197
+ data_disk_ids = [tencentcloud_instance.cvm_image.data_disks.0.data_disk_id]
198
+ image_description = "update image with instance"
199
+ image_name = "image-instance-update-keep"
200
+ }
201
+
202
+ `
203
+
204
+ func testAccCheckCvmImageDestroy (s * terraform.State ) error {
77
205
logId := tccommon .GetLogId (tccommon .ContextNil )
78
206
ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
79
207
@@ -107,7 +235,7 @@ func testAccCheckImageDestroy(s *terraform.State) error {
107
235
return nil
108
236
}
109
237
110
- func testAccCheckImageExists (n string ) resource.TestCheckFunc {
238
+ func testAccCheckCvmImageExists (n string ) resource.TestCheckFunc {
111
239
return func (s * terraform.State ) error {
112
240
logId := tccommon .GetLogId (tccommon .ContextNil )
113
241
ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
@@ -130,136 +258,3 @@ func testAccCheckImageExists(n string) resource.TestCheckFunc {
130
258
return nil
131
259
}
132
260
}
133
-
134
- const (
135
- testAccImageWithSnapShot = tcacctest .DefaultCvmImageVariable + `
136
- resource "tencentcloud_image" "image_snap" {
137
- image_name = "image-snapshot-keep"
138
- snapshot_ids = [var.snap_id]
139
- force_poweroff = true
140
- image_description = "create image with snapshot"
141
- }`
142
-
143
- testAccImageWithSnapShotUpdate = tcacctest .DefaultCvmImageVariable + `
144
- resource "tencentcloud_image" "image_snap" {
145
- image_name = "image-snapshot-update-keep"
146
- snapshot_ids = [var.snap_id]
147
- force_poweroff = false
148
- image_description = "update image with snapshot"
149
- }`
150
-
151
- testAccImageWithInstance = tcacctest .DefaultCvmImageVariable + `
152
-
153
- data "tencentcloud_availability_zones" "default" {
154
- }
155
- data "tencentcloud_images" "default" {
156
- image_type = ["PUBLIC_IMAGE"]
157
- image_name_regex = "Final"
158
- }
159
- data "tencentcloud_images" "testing" {
160
- image_type = ["PUBLIC_IMAGE"]
161
- }
162
- data "tencentcloud_instance_types" "default" {
163
-
164
- filter {
165
- name = "instance-family"
166
- values = ["S4","SA2"]
167
- }
168
- filter {
169
- values = ["ap-guangzhou-7"]
170
- name = "zone"
171
- }
172
- cpu_core_count = 2
173
- memory_size = 2
174
- exclude_sold_out = true
175
- }
176
- resource "tencentcloud_vpc" "vpc" {
177
- name = "image-vpc"
178
- cidr_block = "10.0.0.0/16"
179
- }
180
- resource "tencentcloud_subnet" "subnet" {
181
- vpc_id = tencentcloud_vpc.vpc.id
182
- name = "image-subnet"
183
- cidr_block = "10.0.0.0/16"
184
- availability_zone = "ap-guangzhou-7"
185
- }
186
- resource "tencentcloud_instance" "cvm_image" {
187
- subnet_id = tencentcloud_subnet.subnet.id
188
- system_disk_type = "CLOUD_PREMIUM"
189
- project_id = 0
190
- instance_name = "cvm-image"
191
- availability_zone = "ap-guangzhou-7"
192
- image_id = data.tencentcloud_images.default.images.0.image_id
193
- instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type
194
- vpc_id = tencentcloud_vpc.vpc.id
195
- data_disks {
196
- delete_with_instance = true
197
- data_disk_type = "CLOUD_PREMIUM"
198
- data_disk_size = 100
199
- }
200
- }
201
-
202
- resource "tencentcloud_image" "image_instance" {
203
- image_name = "image-instance-keep"
204
- instance_id = tencentcloud_instance.cvm_image.id
205
- data_disk_ids = [tencentcloud_instance.cvm_image.data_disks.0.data_disk_id]
206
- image_description = "create image with instance"
207
- }`
208
-
209
- testAccImageWithInstanceUpdate = tcacctest .DefaultCvmImageVariable + `
210
- data "tencentcloud_availability_zones" "default" {
211
- }
212
- data "tencentcloud_images" "default" {
213
- image_type = ["PUBLIC_IMAGE"]
214
- image_name_regex = "Final"
215
- }
216
- data "tencentcloud_images" "testing" {
217
- image_type = ["PUBLIC_IMAGE"]
218
- }
219
- data "tencentcloud_instance_types" "default" {
220
-
221
- filter {
222
- name = "instance-family"
223
- values = ["S4","SA2"]
224
- }
225
- filter {
226
- values = ["ap-guangzhou-7"]
227
- name = "zone"
228
- }
229
- cpu_core_count = 2
230
- memory_size = 2
231
- exclude_sold_out = true
232
- }
233
- resource "tencentcloud_vpc" "vpc" {
234
- name = "image-vpc"
235
- cidr_block = "10.0.0.0/16"
236
- }
237
- resource "tencentcloud_subnet" "subnet" {
238
- vpc_id = tencentcloud_vpc.vpc.id
239
- name = "image-subnet"
240
- cidr_block = "10.0.0.0/16"
241
- availability_zone = "ap-guangzhou-7"
242
- }
243
- resource "tencentcloud_instance" "cvm_image" {
244
- subnet_id = tencentcloud_subnet.subnet.id
245
- system_disk_type = "CLOUD_PREMIUM"
246
- project_id = 0
247
- instance_name = "cvm-image"
248
- availability_zone = "ap-guangzhou-7"
249
- image_id = data.tencentcloud_images.default.images.0.image_id
250
- instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type
251
- vpc_id = tencentcloud_vpc.vpc.id
252
- data_disks {
253
- delete_with_instance = true
254
- data_disk_type = "CLOUD_PREMIUM"
255
- data_disk_size = 100
256
- }
257
- }
258
-
259
- resource "tencentcloud_image" "image_instance" {
260
- image_name = "image-instance-update-keep"
261
- instance_id = tencentcloud_instance.cvm_image.id
262
- data_disk_ids = [tencentcloud_instance.cvm_image.data_disks.0.data_disk_id]
263
- image_description = "update image with instance"
264
- }`
265
- )
0 commit comments