Skip to content

Commit

Permalink
Add import support for google_compute_image. (#194)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rosbo authored and danawillow committed Jul 14, 2017
1 parent 2367357 commit a5281c6
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
44 changes: 44 additions & 0 deletions google/import_compute_image_test.go
Original file line number Diff line number Diff line change
@@ -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,
},
},
})
}
38 changes: 36 additions & 2 deletions google/resource_compute_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -81,8 +87,7 @@ func resourceComputeImage() *schema.Resource {
"create_timeout": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 4,
ForceNew: true,
Default: computeImageCreateTimeoutDefault,
},
},
}
Expand Down Expand Up @@ -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)

Expand All @@ -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
}
8 changes: 8 additions & 0 deletions website/docs/r/compute_image.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

0 comments on commit a5281c6

Please sign in to comment.