Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add import support for google_compute_image. #194

Merged
merged 4 commits into from
Jul 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
```