|
| 1 | +/* |
| 2 | +Provides a resource to create a cvm export_images |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_cvm_export_images" "export_images" { |
| 8 | + bucket_name = "xxxxxx" |
| 9 | + image_id = "img-xxxxxx" |
| 10 | + file_name_prefix = "test-" |
| 11 | +} |
| 12 | +``` |
| 13 | +*/ |
| 14 | +package tencentcloud |
| 15 | + |
| 16 | +import ( |
| 17 | + "log" |
| 18 | + "time" |
| 19 | + |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 21 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 22 | + cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312" |
| 23 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 24 | +) |
| 25 | + |
| 26 | +func resourceTencentCloudCvmExportImages() *schema.Resource { |
| 27 | + return &schema.Resource{ |
| 28 | + Create: resourceTencentCloudCvmExportImagesCreate, |
| 29 | + Read: resourceTencentCloudCvmExportImagesRead, |
| 30 | + Delete: resourceTencentCloudCvmExportImagesDelete, |
| 31 | + Schema: map[string]*schema.Schema{ |
| 32 | + "bucket_name": { |
| 33 | + Required: true, |
| 34 | + ForceNew: true, |
| 35 | + Type: schema.TypeString, |
| 36 | + Description: "COS bucket name.", |
| 37 | + }, |
| 38 | + |
| 39 | + "image_id": { |
| 40 | + Required: true, |
| 41 | + ForceNew: true, |
| 42 | + Type: schema.TypeString, |
| 43 | + Description: "Image ID.", |
| 44 | + }, |
| 45 | + |
| 46 | + "file_name_prefix": { |
| 47 | + Required: true, |
| 48 | + ForceNew: true, |
| 49 | + Type: schema.TypeString, |
| 50 | + Description: "Prefix of exported file.", |
| 51 | + }, |
| 52 | + |
| 53 | + "export_format": { |
| 54 | + Optional: true, |
| 55 | + ForceNew: true, |
| 56 | + Type: schema.TypeString, |
| 57 | + Description: "Format of the exported image file. Valid values: RAW, QCOW2, VHD and VMDK. Default value: RAW.", |
| 58 | + }, |
| 59 | + |
| 60 | + "only_export_root_disk": { |
| 61 | + Optional: true, |
| 62 | + ForceNew: true, |
| 63 | + Type: schema.TypeBool, |
| 64 | + Description: "Whether to export only the system disk.", |
| 65 | + }, |
| 66 | + |
| 67 | + "dry_run": { |
| 68 | + Optional: true, |
| 69 | + ForceNew: true, |
| 70 | + Type: schema.TypeBool, |
| 71 | + Description: "Check whether the image can be exported.", |
| 72 | + }, |
| 73 | + |
| 74 | + "role_name": { |
| 75 | + Optional: true, |
| 76 | + ForceNew: true, |
| 77 | + Type: schema.TypeString, |
| 78 | + Description: "Role name (Default: CVM_QcsRole). Before exporting the images, make sure the role exists, and it has write permission to COS.", |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func resourceTencentCloudCvmExportImagesCreate(d *schema.ResourceData, meta interface{}) error { |
| 85 | + defer logElapsed("resource.tencentcloud_cvm_export_images.create")() |
| 86 | + defer inconsistentCheck(d, meta)() |
| 87 | + |
| 88 | + logId := getLogId(contextNil) |
| 89 | + |
| 90 | + var ( |
| 91 | + request = cvm.NewExportImagesRequest() |
| 92 | + imageId string |
| 93 | + bucketName string |
| 94 | + fileNamePrefix string |
| 95 | + ) |
| 96 | + imageId = d.Get("image_id").(string) |
| 97 | + bucketName = d.Get("bucket_name").(string) |
| 98 | + fileNamePrefix = d.Get("file_name_prefix").(string) |
| 99 | + request.ImageIds = []*string{&imageId} |
| 100 | + request.BucketName = helper.String(bucketName) |
| 101 | + request.FileNamePrefixList = []*string{&fileNamePrefix} |
| 102 | + |
| 103 | + if v, ok := d.GetOk("export_format"); ok { |
| 104 | + request.ExportFormat = helper.String(v.(string)) |
| 105 | + } |
| 106 | + |
| 107 | + if v, _ := d.GetOk("only_export_root_disk"); v != nil { |
| 108 | + request.OnlyExportRootDisk = helper.Bool(v.(bool)) |
| 109 | + } |
| 110 | + |
| 111 | + if v, _ := d.GetOk("dry_run"); v != nil { |
| 112 | + request.DryRun = helper.Bool(v.(bool)) |
| 113 | + } |
| 114 | + |
| 115 | + if v, ok := d.GetOk("role_name"); ok { |
| 116 | + request.RoleName = helper.String(v.(string)) |
| 117 | + } |
| 118 | + |
| 119 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 120 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseCvmClient().ExportImages(request) |
| 121 | + if e != nil { |
| 122 | + return retryError(e) |
| 123 | + } else { |
| 124 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 125 | + } |
| 126 | + return nil |
| 127 | + }) |
| 128 | + if err != nil { |
| 129 | + log.Printf("[CRITAL]%s operate cvm exportImages failed, reason:%+v", logId, err) |
| 130 | + return err |
| 131 | + } |
| 132 | + |
| 133 | + d.SetId(imageId) |
| 134 | + |
| 135 | + service := CvmService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 136 | + |
| 137 | + conf := BuildStateChangeConf([]string{}, []string{"NORMAL"}, 20*readRetryTimeout, time.Second, service.CvmSyncImagesStateRefreshFunc(d.Id(), []string{})) |
| 138 | + |
| 139 | + if _, e := conf.WaitForState(); e != nil { |
| 140 | + return e |
| 141 | + } |
| 142 | + |
| 143 | + return resourceTencentCloudCvmExportImagesRead(d, meta) |
| 144 | +} |
| 145 | + |
| 146 | +func resourceTencentCloudCvmExportImagesRead(d *schema.ResourceData, meta interface{}) error { |
| 147 | + defer logElapsed("resource.tencentcloud_cvm_export_images.read")() |
| 148 | + defer inconsistentCheck(d, meta)() |
| 149 | + |
| 150 | + return nil |
| 151 | +} |
| 152 | + |
| 153 | +func resourceTencentCloudCvmExportImagesDelete(d *schema.ResourceData, meta interface{}) error { |
| 154 | + defer logElapsed("resource.tencentcloud_cvm_export_images.delete")() |
| 155 | + defer inconsistentCheck(d, meta)() |
| 156 | + |
| 157 | + return nil |
| 158 | +} |
0 commit comments