Add support for using source_disk to google_compute_image (#9614)

This commit is contained in:
Christoph Blecker 2016-11-01 03:32:47 -07:00 committed by Paul Stack
parent 71536155e0
commit ca8cd4b1f0
3 changed files with 63 additions and 12 deletions

View File

@ -16,6 +16,8 @@ func resourceComputeImage() *schema.Resource {
Delete: resourceComputeImageDelete,
Schema: map[string]*schema.Schema{
// TODO(cblecker): one of source_disk or raw_disk is required
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
@ -39,9 +41,15 @@ func resourceComputeImage() *schema.Resource {
ForceNew: true,
},
"source_disk": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"raw_disk": &schema.Schema{
Type: schema.TypeList,
Required: true,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
@ -95,15 +103,24 @@ func resourceComputeImageCreate(d *schema.ResourceData, meta interface{}) error
image.Family = v.(string)
}
rawDiskEle := d.Get("raw_disk").([]interface{})[0].(map[string]interface{})
imageRawDisk := &compute.ImageRawDisk{
Source: rawDiskEle["source"].(string),
ContainerType: rawDiskEle["container_type"].(string),
// Load up the source_disk for this image if specified
if v, ok := d.GetOk("source_disk"); ok {
image.SourceDisk = v.(string)
}
if val, ok := rawDiskEle["sha1"]; ok {
imageRawDisk.Sha1Checksum = val.(string)
// Load up the raw_disk for this image if specified
if v, ok := d.GetOk("raw_disk"); ok {
rawDiskEle := v.([]interface{})[0].(map[string]interface{})
imageRawDisk := &compute.ImageRawDisk{
Source: rawDiskEle["source"].(string),
ContainerType: rawDiskEle["container_type"].(string),
}
if val, ok := rawDiskEle["sha1"]; ok {
imageRawDisk.Sha1Checksum = val.(string)
}
image.RawDisk = imageRawDisk
}
image.RawDisk = imageRawDisk
// Insert the image
op, err := config.clientCompute.Images.Insert(

View File

@ -29,6 +29,25 @@ func TestAccComputeImage_basic(t *testing.T) {
})
}
func TestAccComputeImage_basedondisk(t *testing.T) {
var image compute.Image
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeImageDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeImage_basedondisk,
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeImageExists(
"google_compute_image.foobar", &image),
),
},
},
})
}
func testAccCheckComputeImageDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
@ -83,3 +102,14 @@ resource "google_compute_image" "foobar" {
source = "https://storage.googleapis.com/bosh-cpi-artifacts/bosh-stemcell-3262.4-google-kvm-ubuntu-trusty-go_agent-raw.tar.gz"
}
}`, acctest.RandString(10))
var testAccComputeImage_basedondisk = fmt.Sprintf(`
resource "google_compute_disk" "foobar" {
name = "disk-test-%s"
zone = "us-central1-a"
image = "debian-8-jessie-v20160803"
}
resource "google_compute_image" "foobar" {
name = "image-test-%s"
source_disk = "${google_compute_disk.foobar.self_link}"
}`, acctest.RandString(10), acctest.RandString(10))

View File

@ -40,14 +40,18 @@ resource "google_compute_instance" "vm" {
## Argument Reference
The following arguments are supported:
The following arguments are supported: (Note that one of either source_disk or
raw_disk is required)
* `name` - (Required) A unique name for the resource, required by GCE.
Changing this forces a new resource to be created.
* `raw_disk` - (Required) The raw disk that will be used as the source of
the image. Changing this forces a new resource to be created.
Structure is documented below.
* `source_disk` - The URL of a disk that will be used as the source of the
image. Changing this forces a new resource to be created.
* `raw_disk` - The raw disk that will be used as the source of the image.
Changing this forces a new resource to be created. Structure is documented
below.
The `raw_disk` block supports: