mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-07-01 07:42:40 +00:00
Add import support for google_compute_image. (#194)
* Add import support for google_compute_image. * Added comment explaning why we set the create_timeout in the import state method * Don't ForceNew for create_timeout field * Update image name in import documentation
This commit is contained in:
parent
2367357869
commit
a5281c6e7c
44
google/import_compute_image_test.go
Normal file
44
google/import_compute_image_test.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package google
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccComputeImage_importFromRawDisk(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckComputeImageDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccComputeImage_basic,
|
||||
},
|
||||
resource.TestStep{
|
||||
ResourceName: "google_compute_image.foobar",
|
||||
ImportState: true,
|
||||
ImportStateVerify: true,
|
||||
ImportStateVerifyIgnore: []string{"raw_disk", "create_timeout"},
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccComputeImage_importFromSourceDisk(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckComputeImageDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccComputeImage_basedondisk,
|
||||
},
|
||||
resource.TestStep{
|
||||
ResourceName: "google_compute_image.foobar",
|
||||
ImportState: true,
|
||||
ImportStateVerify: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
|
@ -8,11 +8,17 @@ import (
|
|||
"google.golang.org/api/compute/v1"
|
||||
)
|
||||
|
||||
const computeImageCreateTimeoutDefault = 4
|
||||
|
||||
func resourceComputeImage() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Create: resourceComputeImageCreate,
|
||||
Read: resourceComputeImageRead,
|
||||
Update: resourceComputeImageUpdate,
|
||||
Delete: resourceComputeImageDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: resourceComputeImageImportState,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
// TODO(cblecker): one of source_disk or raw_disk is required
|
||||
|
@ -81,8 +87,7 @@ func resourceComputeImage() *schema.Resource {
|
|||
"create_timeout": &schema.Schema{
|
||||
Type: schema.TypeInt,
|
||||
Optional: true,
|
||||
Default: 4,
|
||||
ForceNew: true,
|
||||
Default: computeImageCreateTimeoutDefault,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
@ -166,11 +171,30 @@ func resourceComputeImageRead(d *schema.ResourceData, meta interface{}) error {
|
|||
return handleNotFoundError(err, d, fmt.Sprintf("Image %q", d.Get("name").(string)))
|
||||
}
|
||||
|
||||
if image.SourceDisk != "" {
|
||||
d.Set("source_disk", image.SourceDisk)
|
||||
} else if image.RawDisk != nil {
|
||||
// `raw_disk.*.source` is only used at image creation but is not returned when calling Get.
|
||||
// `raw_disk.*.sha1` is not supported, the value is simply discarded by the server.
|
||||
// Leaving `raw_disk` to current state value.
|
||||
} else {
|
||||
return fmt.Errorf("Either raw_disk or source_disk configuration is required.")
|
||||
}
|
||||
|
||||
d.Set("name", image.Name)
|
||||
d.Set("description", image.Description)
|
||||
d.Set("family", image.Family)
|
||||
d.Set("self_link", image.SelfLink)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceComputeImageUpdate(d *schema.ResourceData, meta interface{}) error {
|
||||
// Pass-through for updates to Terraform-specific `create_timeout` field.
|
||||
// The Google Cloud Image resource doesn't support update.
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceComputeImageDelete(d *schema.ResourceData, meta interface{}) error {
|
||||
config := meta.(*Config)
|
||||
|
||||
|
@ -195,3 +219,13 @@ func resourceComputeImageDelete(d *schema.ResourceData, meta interface{}) error
|
|||
d.SetId("")
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceComputeImageImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
||||
// `create_timeout` field is specific to this Terraform resource implementation. Thus, this value cannot be
|
||||
// imported from the Google Cloud REST API.
|
||||
// Setting to default value otherwise Terraform requires a ForceNew to change the resource to match the
|
||||
// default `create_timeout`.
|
||||
d.Set("create_timeout", computeImageCreateTimeoutDefault)
|
||||
|
||||
return []*schema.ResourceData{d}, nil
|
||||
}
|
||||
|
|
|
@ -83,3 +83,11 @@ In addition to the arguments listed above, the following computed attributes are
|
|||
exported:
|
||||
|
||||
* `self_link` - The URI of the created resource.
|
||||
|
||||
## Import
|
||||
|
||||
VM image can be imported using the `name`, e.g.
|
||||
|
||||
```
|
||||
$ terraform import google_compute_image.web-image my-custom-image
|
||||
```
|
||||
|
|
Loading…
Reference in New Issue
Block a user