Skip to content

Commit ca8cd4b

Browse files
cbleckerstack72
authored andcommitted
Add support for using source_disk to google_compute_image (hashicorp#9614)
1 parent 7153615 commit ca8cd4b

3 files changed

Lines changed: 63 additions & 12 deletions

File tree

‎builtin/providers/google/resource_compute_image.go‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ func resourceComputeImage() *schema.Resource {
1616
Delete: resourceComputeImageDelete,
1717

1818
Schema: map[string]*schema.Schema{
19+
// TODO(cblecker): one of source_disk or raw_disk is required
20+
1921
"name": &schema.Schema{
2022
Type: schema.TypeString,
2123
Required: true,
@@ -39,9 +41,15 @@ func resourceComputeImage() *schema.Resource {
3941
ForceNew: true,
4042
},
4143

44+
"source_disk": &schema.Schema{
45+
Type: schema.TypeString,
46+
Optional: true,
47+
ForceNew: true,
48+
},
49+
4250
"raw_disk": &schema.Schema{
4351
Type: schema.TypeList,
44-
Required: true,
52+
Optional: true,
4553
ForceNew: true,
4654
MaxItems: 1,
4755
Elem: &schema.Resource{
@@ -95,15 +103,24 @@ func resourceComputeImageCreate(d *schema.ResourceData, meta interface{}) error
95103
image.Family = v.(string)
96104
}
97105

98-
rawDiskEle := d.Get("raw_disk").([]interface{})[0].(map[string]interface{})
99-
imageRawDisk := &compute.ImageRawDisk{
100-
Source: rawDiskEle["source"].(string),
101-
ContainerType: rawDiskEle["container_type"].(string),
106+
// Load up the source_disk for this image if specified
107+
if v, ok := d.GetOk("source_disk"); ok {
108+
image.SourceDisk = v.(string)
102109
}
103-
if val, ok := rawDiskEle["sha1"]; ok {
104-
imageRawDisk.Sha1Checksum = val.(string)
110+
111+
// Load up the raw_disk for this image if specified
112+
if v, ok := d.GetOk("raw_disk"); ok {
113+
rawDiskEle := v.([]interface{})[0].(map[string]interface{})
114+
imageRawDisk := &compute.ImageRawDisk{
115+
Source: rawDiskEle["source"].(string),
116+
ContainerType: rawDiskEle["container_type"].(string),
117+
}
118+
if val, ok := rawDiskEle["sha1"]; ok {
119+
imageRawDisk.Sha1Checksum = val.(string)
120+
}
121+
122+
image.RawDisk = imageRawDisk
105123
}
106-
image.RawDisk = imageRawDisk
107124

108125
// Insert the image
109126
op, err := config.clientCompute.Images.Insert(

‎builtin/providers/google/resource_compute_image_test.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ func TestAccComputeImage_basic(t *testing.T) {
2929
})
3030
}
3131

32+
func TestAccComputeImage_basedondisk(t *testing.T) {
33+
var image compute.Image
34+
35+
resource.Test(t, resource.TestCase{
36+
PreCheck: func() { testAccPreCheck(t) },
37+
Providers: testAccProviders,
38+
CheckDestroy: testAccCheckComputeImageDestroy,
39+
Steps: []resource.TestStep{
40+
resource.TestStep{
41+
Config: testAccComputeImage_basedondisk,
42+
Check: resource.ComposeTestCheckFunc(
43+
testAccCheckComputeImageExists(
44+
"google_compute_image.foobar", &image),
45+
),
46+
},
47+
},
48+
})
49+
}
50+
3251
func testAccCheckComputeImageDestroy(s *terraform.State) error {
3352
config := testAccProvider.Meta().(*Config)
3453

@@ -83,3 +102,14 @@ resource "google_compute_image" "foobar" {
83102
source = "https://storage.googleapis.com/bosh-cpi-artifacts/bosh-stemcell-3262.4-google-kvm-ubuntu-trusty-go_agent-raw.tar.gz"
84103
}
85104
}`, acctest.RandString(10))
105+
106+
var testAccComputeImage_basedondisk = fmt.Sprintf(`
107+
resource "google_compute_disk" "foobar" {
108+
name = "disk-test-%s"
109+
zone = "us-central1-a"
110+
image = "debian-8-jessie-v20160803"
111+
}
112+
resource "google_compute_image" "foobar" {
113+
name = "image-test-%s"
114+
source_disk = "${google_compute_disk.foobar.self_link}"
115+
}`, acctest.RandString(10), acctest.RandString(10))

‎website/source/docs/providers/google/r/compute_image.html.markdown‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,18 @@ resource "google_compute_instance" "vm" {
4040

4141
## Argument Reference
4242

43-
The following arguments are supported:
43+
The following arguments are supported: (Note that one of either source_disk or
44+
raw_disk is required)
4445

4546
* `name` - (Required) A unique name for the resource, required by GCE.
4647
Changing this forces a new resource to be created.
4748

48-
* `raw_disk` - (Required) The raw disk that will be used as the source of
49-
the image. Changing this forces a new resource to be created.
50-
Structure is documented below.
49+
* `source_disk` - The URL of a disk that will be used as the source of the
50+
image. Changing this forces a new resource to be created.
51+
52+
* `raw_disk` - The raw disk that will be used as the source of the image.
53+
Changing this forces a new resource to be created. Structure is documented
54+
below.
5155

5256
The `raw_disk` block supports:
5357

0 commit comments

Comments
 (0)