Skip to content

Commit 4c49f40

Browse files
tongyimingmikatong
andauthored
cvm resource (#1854)
* cvm resource * update --------- Co-authored-by: mikatong <mikatong@tencent.com>
1 parent d173967 commit 4c49f40

9 files changed

+993
-0
lines changed

tencentcloud/provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,6 +2461,10 @@ func Provider() *schema.Provider {
24612461
"tencentcloud_cvm_renew_instance": resourceTencentCloudCvmRenewInstance(),
24622462
"tencentcloud_cvm_export_images": resourceTencentCloudCvmExportImages(),
24632463
"tencentcloud_cvm_image_share_permission": resourceTencentCloudCvmImageSharePermission(),
2464+
"tencentcloud_cvm_import_image": resourceTencentCloudCvmImportImage(),
2465+
"tencentcloud_cvm_renew_host": resourceTencentCloudCvmRenewHost(),
2466+
"tencentcloud_cvm_program_fpga_image": resourceTencentCloudCvmProgramFpgaImage(),
2467+
"tencentcloud_cvm_modify_instance_disk_type": resourceTencentCloudCvmModifyInstanceDiskType(),
24642468
"tencentcloud_lighthouse_disk_backup": resourceTencentCloudLighthouseDiskBackup(),
24652469
"tencentcloud_lighthouse_apply_disk_backup": resourceTencentCloudLighthouseApplyDiskBackup(),
24662470
"tencentcloud_lighthouse_disk_attachment": resourceTencentCloudLighthouseDiskAttachment(),
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
/*
2+
Provides a resource to create a cvm import_image
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_cvm_import_image" "import_image" {
8+
architecture = "x86_64"
9+
os_type = "CentOS"
10+
os_version = "7"
11+
image_url = ""
12+
image_name = "sample"
13+
image_description = "sampleimage"
14+
dry_run = false
15+
force = false
16+
tag_specification {
17+
resource_type = "image"
18+
tags {
19+
key = "tagKey"
20+
value = "tagValue"
21+
}
22+
23+
}
24+
license_type = "TencentCloud"
25+
boot_mode = "Legacy BIOS"
26+
tags = {
27+
"createdBy" = "terraform"
28+
}
29+
}
30+
```
31+
32+
Import
33+
34+
cvm import_image can be imported using the id, e.g.
35+
36+
```
37+
terraform import tencentcloud_cvm_import_image.import_image import_image_id
38+
```
39+
*/
40+
package tencentcloud
41+
42+
import (
43+
"log"
44+
45+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
46+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
47+
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
48+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
49+
)
50+
51+
func resourceTencentCloudCvmImportImage() *schema.Resource {
52+
return &schema.Resource{
53+
Create: resourceTencentCloudCvmImportImageCreate,
54+
Read: resourceTencentCloudCvmImportImageRead,
55+
Delete: resourceTencentCloudCvmImportImageDelete,
56+
Importer: &schema.ResourceImporter{
57+
State: schema.ImportStatePassthrough,
58+
},
59+
Schema: map[string]*schema.Schema{
60+
"architecture": {
61+
Required: true,
62+
ForceNew: true,
63+
Type: schema.TypeString,
64+
Description: "OS architecture of the image to be imported, `x86_64` or `i386`.",
65+
},
66+
67+
"os_type": {
68+
Required: true,
69+
ForceNew: true,
70+
Type: schema.TypeString,
71+
Description: "OS type of the image to be imported. You can call `DescribeImportImageOs` to obtain the list of supported operating systems.",
72+
},
73+
74+
"os_version": {
75+
Required: true,
76+
ForceNew: true,
77+
Type: schema.TypeString,
78+
Description: "OS version of the image to be imported. You can call `DescribeImportImageOs` to obtain the list of supported operating systems.",
79+
},
80+
81+
"image_url": {
82+
Required: true,
83+
ForceNew: true,
84+
Type: schema.TypeString,
85+
Description: "Address on COS where the image to be imported is stored.",
86+
},
87+
88+
"image_name": {
89+
Required: true,
90+
ForceNew: true,
91+
Type: schema.TypeString,
92+
Description: "Image name.",
93+
},
94+
95+
"image_description": {
96+
Optional: true,
97+
ForceNew: true,
98+
Type: schema.TypeString,
99+
Description: "Image description.",
100+
},
101+
102+
"dry_run": {
103+
Optional: true,
104+
ForceNew: true,
105+
Type: schema.TypeBool,
106+
Description: "Dry run to check the parameters without performing the operation.",
107+
},
108+
109+
"force": {
110+
Optional: true,
111+
ForceNew: true,
112+
Type: schema.TypeBool,
113+
Description: "Whether to force import the image.",
114+
},
115+
116+
"tag_specification": {
117+
Optional: true,
118+
ForceNew: true,
119+
Type: schema.TypeList,
120+
Description: "Tag description list. This parameter is used to bind a tag to a custom image.",
121+
Elem: &schema.Resource{
122+
Schema: map[string]*schema.Schema{
123+
"resource_type": {
124+
Type: schema.TypeString,
125+
Required: true,
126+
Description: "Resource type. Valid values: instance (CVM), host (CDH), image (for image), and keypair (for key). Note: This field may return null, indicating that no valid values can be obtained.",
127+
},
128+
"tags": {
129+
Type: schema.TypeList,
130+
Required: true,
131+
Description: "Tag pairs Note: This field may return null, indicating that no valid values can be obtained.",
132+
Elem: &schema.Resource{
133+
Schema: map[string]*schema.Schema{
134+
"key": {
135+
Type: schema.TypeString,
136+
Required: true,
137+
Description: "Tag key.",
138+
},
139+
"value": {
140+
Type: schema.TypeString,
141+
Required: true,
142+
Description: "Tag value.",
143+
},
144+
},
145+
},
146+
},
147+
},
148+
},
149+
},
150+
151+
"license_type": {
152+
Optional: true,
153+
ForceNew: true,
154+
Type: schema.TypeString,
155+
Description: "The license type used to activate the OS after importing an image. Valid values: TencentCloud: Tencent Cloud official license BYOL: Bring Your Own License.",
156+
},
157+
158+
"boot_mode": {
159+
Optional: true,
160+
ForceNew: true,
161+
Type: schema.TypeString,
162+
Description: "Boot mode.",
163+
},
164+
},
165+
}
166+
}
167+
168+
func resourceTencentCloudCvmImportImageCreate(d *schema.ResourceData, meta interface{}) error {
169+
defer logElapsed("resource.tencentcloud_cvm_import_image.create")()
170+
defer inconsistentCheck(d, meta)()
171+
172+
logId := getLogId(contextNil)
173+
174+
var (
175+
request = cvm.NewImportImageRequest()
176+
imageUrl string
177+
)
178+
if v, ok := d.GetOk("architecture"); ok {
179+
request.Architecture = helper.String(v.(string))
180+
}
181+
182+
if v, ok := d.GetOk("os_type"); ok {
183+
request.OsType = helper.String(v.(string))
184+
}
185+
186+
if v, ok := d.GetOk("os_version"); ok {
187+
request.OsVersion = helper.String(v.(string))
188+
}
189+
190+
if v, ok := d.GetOk("image_url"); ok {
191+
imageUrl = v.(string)
192+
request.ImageUrl = helper.String(imageUrl)
193+
}
194+
195+
if v, ok := d.GetOk("image_name"); ok {
196+
request.ImageName = helper.String(v.(string))
197+
}
198+
199+
if v, ok := d.GetOk("image_description"); ok {
200+
request.ImageDescription = helper.String(v.(string))
201+
}
202+
203+
if v, _ := d.GetOk("dry_run"); v != nil {
204+
request.DryRun = helper.Bool(v.(bool))
205+
}
206+
207+
if v, _ := d.GetOk("force"); v != nil {
208+
request.Force = helper.Bool(v.(bool))
209+
}
210+
211+
if v, ok := d.GetOk("tag_specification"); ok {
212+
for _, item := range v.([]interface{}) {
213+
dMap := item.(map[string]interface{})
214+
tagSpecification := cvm.TagSpecification{}
215+
if v, ok := dMap["resource_type"]; ok {
216+
tagSpecification.ResourceType = helper.String(v.(string))
217+
}
218+
if v, ok := dMap["tags"]; ok {
219+
for _, item := range v.([]interface{}) {
220+
tagsMap := item.(map[string]interface{})
221+
tag := cvm.Tag{}
222+
if v, ok := tagsMap["key"]; ok {
223+
tag.Key = helper.String(v.(string))
224+
}
225+
if v, ok := tagsMap["value"]; ok {
226+
tag.Value = helper.String(v.(string))
227+
}
228+
tagSpecification.Tags = append(tagSpecification.Tags, &tag)
229+
}
230+
}
231+
request.TagSpecification = append(request.TagSpecification, &tagSpecification)
232+
}
233+
}
234+
235+
if v, ok := d.GetOk("license_type"); ok {
236+
request.LicenseType = helper.String(v.(string))
237+
}
238+
239+
if v, ok := d.GetOk("boot_mode"); ok {
240+
request.BootMode = helper.String(v.(string))
241+
}
242+
243+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
244+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseCvmClient().ImportImage(request)
245+
if e != nil {
246+
return retryError(e)
247+
} else {
248+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
249+
}
250+
return nil
251+
})
252+
if err != nil {
253+
log.Printf("[CRITAL]%s operate cvm importImage failed, reason:%+v", logId, err)
254+
return err
255+
}
256+
257+
d.SetId(imageUrl)
258+
259+
return resourceTencentCloudCvmImportImageRead(d, meta)
260+
}
261+
262+
func resourceTencentCloudCvmImportImageRead(d *schema.ResourceData, meta interface{}) error {
263+
defer logElapsed("resource.tencentcloud_cvm_import_image.read")()
264+
defer inconsistentCheck(d, meta)()
265+
266+
return nil
267+
}
268+
269+
func resourceTencentCloudCvmImportImageDelete(d *schema.ResourceData, meta interface{}) error {
270+
defer logElapsed("resource.tencentcloud_cvm_import_image.delete")()
271+
defer inconsistentCheck(d, meta)()
272+
273+
return nil
274+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudNeedFixCvmImportImageResource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccCvmImportImage,
19+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_cvm_import_image.import_image", "id")),
20+
},
21+
{
22+
ResourceName: "tencentcloud_cvm_import_image.import_image",
23+
ImportState: true,
24+
ImportStateVerify: true,
25+
},
26+
},
27+
})
28+
}
29+
30+
const testAccCvmImportImage = `
31+
32+
resource "tencentcloud_cvm_import_image" "import_image" {
33+
architecture = "x86_64"
34+
os_type = "CentOS"
35+
os_version = "7"
36+
image_url = ""
37+
image_name = "sample"
38+
image_description = "sampleimage"
39+
dry_run = false
40+
force = false
41+
tag_specification {
42+
resource_type = "image"
43+
tags {
44+
key = "tagKey"
45+
value = "tagValue"
46+
}
47+
48+
}
49+
license_type = "TencentCloud"
50+
boot_mode = "Legacy BIOS"
51+
tags = {
52+
"createdBy" = "terraform"
53+
}
54+
}
55+
56+
`

0 commit comments

Comments
 (0)