|
| 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 | +} |
0 commit comments