Support import in compute instance (#873)

This commit is contained in:
Vincent Roseberry 2017-12-19 14:33:30 -08:00 committed by GitHub
parent ba44353e46
commit c1d9bb24c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 128 additions and 6 deletions

View File

@ -26,6 +26,9 @@ func resourceComputeInstance() *schema.Resource {
Read: resourceComputeInstanceRead,
Update: resourceComputeInstanceUpdate,
Delete: resourceComputeInstanceDelete,
Importer: &schema.ResourceImporter{
State: resourceComputeInstanceImportState,
},
SchemaVersion: 6,
MigrateState: resourceComputeInstanceMigrateState,
@ -738,12 +741,10 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
md := flattenMetadataBeta(instance.Metadata)
if _, scriptExists := d.GetOk("metadata_startup_script"); scriptExists {
d.Set("metadata_startup_script", md["startup-script"])
// Note that here we delete startup-script from our metadata list. This is to prevent storing the startup-script
// as a value in the metadata since the config specifically tracks it under 'metadata_startup_script'
delete(md, "startup-script")
}
d.Set("metadata_startup_script", md["startup-script"])
// Note that here we delete startup-script from our metadata list. This is to prevent storing the startup-script
// as a value in the metadata since the config specifically tracks it under 'metadata_startup_script'
delete(md, "startup-script")
existingMetadata := d.Get("metadata").(map[string]interface{})
@ -794,6 +795,7 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
// Set the tags fingerprint if there is one.
if instance.Tags != nil {
d.Set("tags_fingerprint", instance.Tags.Fingerprint)
d.Set("tags", convertStringArrToInterface(instance.Tags.Items))
}
if len(instance.Labels) > 0 {
@ -859,6 +861,7 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
d.Set("self_link", ConvertSelfLinkToV1(instance.SelfLink))
d.Set("instance_id", fmt.Sprintf("%d", instance.Id))
d.Set("project", project)
d.Set("name", instance.Name)
d.SetId(instance.Name)
return nil
@ -1239,6 +1242,20 @@ func resourceComputeInstanceDelete(d *schema.ResourceData, meta interface{}) err
return nil
}
func resourceComputeInstanceImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 3 {
return nil, fmt.Errorf("Invalid import id %q. Expecting {project}/{zone}/{instance_name}", d.Id())
}
d.Set("project", parts[0])
d.Set("zone", parts[1])
d.SetId(parts[2])
return []*schema.ResourceData{d}, nil
}
func expandBootDisk(d *schema.ResourceData, config *Config, zone *compute.Zone, project string) (*computeBeta.AttachedDisk, error) {
disk := &computeBeta.AttachedDisk{
AutoDelete: d.Get("boot_disk.0.auto_delete").(bool),

View File

@ -37,6 +37,12 @@ func TestAccComputeInstance_basic1(t *testing.T) {
testAccCheckComputeInstanceDisk(&instance, instanceName, true, true),
),
},
resource.TestStep{
ResourceName: "google_compute_instance.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("%s/%s/%s", getTestProjectFromEnv(), "us-central1-a", instanceName),
ImportStateVerifyIgnore: []string{"boot_disk.0.initialize_params", "create_timeout"},
},
},
})
}
@ -246,6 +252,12 @@ func TestAccComputeInstance_attachedDisk(t *testing.T) {
testAccCheckComputeInstanceDisk(&instance, diskName, false, false),
),
},
resource.TestStep{
ResourceName: "google_compute_instance.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("%s/%s/%s", getTestProjectFromEnv(), "us-central1-a", instanceName),
ImportStateVerifyIgnore: []string{"boot_disk.0.initialize_params"},
},
},
})
}
@ -270,6 +282,12 @@ func TestAccComputeInstance_attachedDisk_sourceUrl(t *testing.T) {
testAccCheckComputeInstanceDisk(&instance, diskName, false, false),
),
},
resource.TestStep{
ResourceName: "google_compute_instance.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("%s/%s/%s", getTestProjectFromEnv(), "us-central1-a", instanceName),
ImportStateVerifyIgnore: []string{"boot_disk.0.initialize_params"},
},
},
})
}
@ -347,6 +365,11 @@ func TestAccComputeInstance_bootDisk_source(t *testing.T) {
testAccCheckComputeInstanceBootDisk(&instance, diskName),
),
},
resource.TestStep{
ResourceName: "google_compute_instance.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("%s/%s/%s", getTestProjectFromEnv(), "us-central1-a", instanceName),
},
},
})
}
@ -371,6 +394,12 @@ func TestAccComputeInstance_bootDisk_sourceUrl(t *testing.T) {
testAccCheckComputeInstanceBootDisk(&instance, diskName),
),
},
resource.TestStep{
ResourceName: "google_compute_instance.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("%s/%s/%s", getTestProjectFromEnv(), "us-central1-a", instanceName),
ImportStateVerifyIgnore: []string{"boot_disk.0.initialize_params"},
},
},
})
}
@ -418,6 +447,12 @@ func TestAccComputeInstance_scratchDisk(t *testing.T) {
testAccCheckComputeInstanceScratchDisk(&instance, []string{"NVME", "SCSI"}),
),
},
resource.TestStep{
ResourceName: "google_compute_instance.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("%s/%s/%s", getTestProjectFromEnv(), "us-central1-a", instanceName),
ImportStateVerifyIgnore: []string{"boot_disk.0.initialize_params"},
},
},
})
}
@ -511,6 +546,12 @@ func TestAccComputeInstance_service_account(t *testing.T) {
"https://www.googleapis.com/auth/userinfo.email"),
),
},
resource.TestStep{
ResourceName: "google_compute_instance.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("%s/%s/%s", getTestProjectFromEnv(), "us-central1-a", instanceName),
ImportStateVerifyIgnore: []string{"boot_disk.0.initialize_params"},
},
},
})
}
@ -533,6 +574,12 @@ func TestAccComputeInstance_scheduling(t *testing.T) {
"google_compute_instance.foobar", &instance),
),
},
resource.TestStep{
ResourceName: "google_compute_instance.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("%s/%s/%s", getTestProjectFromEnv(), "us-central1-a", instanceName),
ImportStateVerifyIgnore: []string{"boot_disk.0.initialize_params"},
},
},
})
}
@ -556,6 +603,12 @@ func TestAccComputeInstance_subnet_auto(t *testing.T) {
testAccCheckComputeInstanceHasSubnet(&instance),
),
},
resource.TestStep{
ResourceName: "google_compute_instance.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("%s/%s/%s", getTestProjectFromEnv(), "us-central1-a", instanceName),
ImportStateVerifyIgnore: []string{"boot_disk.0.initialize_params"},
},
},
})
}
@ -579,6 +632,12 @@ func TestAccComputeInstance_subnet_custom(t *testing.T) {
testAccCheckComputeInstanceHasSubnet(&instance),
),
},
resource.TestStep{
ResourceName: "google_compute_instance.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("%s/%s/%s", getTestProjectFromEnv(), "us-central1-a", instanceName),
ImportStateVerifyIgnore: []string{"boot_disk.0.initialize_params"},
},
},
})
}
@ -700,6 +759,12 @@ func TestAccComputeInstance_forceChangeMachineTypeManually(t *testing.T) {
),
ExpectNonEmptyPlan: true,
},
resource.TestStep{
ResourceName: "google_compute_instance.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("%s/%s/%s", getTestProjectFromEnv(), "us-central1-a", instanceName),
ImportStateVerifyIgnore: []string{"boot_disk.0.initialize_params", "create_timeout"},
},
},
})
}
@ -724,6 +789,12 @@ func TestAccComputeInstance_multiNic(t *testing.T) {
testAccCheckComputeInstanceHasMultiNic(&instance),
),
},
resource.TestStep{
ResourceName: "google_compute_instance.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("%s/%s/%s", getTestProjectFromEnv(), "us-central1-a", instanceName),
ImportStateVerifyIgnore: []string{"boot_disk.0.initialize_params"},
},
},
})
}
@ -746,6 +817,12 @@ func TestAccComputeInstance_guestAccelerator(t *testing.T) {
testAccCheckComputeInstanceHasGuestAccelerator(&instance, "nvidia-tesla-k80", 1),
),
},
resource.TestStep{
ResourceName: "google_compute_instance.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("%s/%s/%s", getTestProjectFromEnv(), "us-east1-d", instanceName),
ImportStateVerifyIgnore: []string{"boot_disk.0.initialize_params"},
},
},
})
@ -769,6 +846,12 @@ func TestAccComputeInstance_minCpuPlatform(t *testing.T) {
testAccCheckComputeInstanceHasMinCpuPlatform(&instance, "Intel Haswell"),
),
},
resource.TestStep{
ResourceName: "google_compute_instance.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("%s/%s/%s", getTestProjectFromEnv(), "us-east1-d", instanceName),
ImportStateVerifyIgnore: []string{"boot_disk.0.initialize_params"},
},
},
})
}
@ -791,6 +874,12 @@ func TestAccComputeInstance_primaryAliasIpRange(t *testing.T) {
testAccCheckComputeInstanceHasAliasIpRange(&instance, "", "/24"),
),
},
resource.TestStep{
ResourceName: "google_compute_instance.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("%s/%s/%s", getTestProjectFromEnv(), "us-east1-d", instanceName),
ImportStateVerifyIgnore: []string{"boot_disk.0.initialize_params"},
},
},
})
}
@ -813,6 +902,12 @@ func TestAccComputeInstance_secondaryAliasIpRange(t *testing.T) {
testAccCheckComputeInstanceHasAliasIpRange(&instance, "inst-test-secondary", "172.16.0.0/24"),
),
},
resource.TestStep{
ResourceName: "google_compute_instance.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("%s/%s/%s", getTestProjectFromEnv(), "us-east1-d", instanceName),
ImportStateVerifyIgnore: []string{"boot_disk.0.initialize_params"},
},
},
})
}

View File

@ -272,3 +272,13 @@ exported:
* `disk.0.disk_encryption_key_sha256` - The [RFC 4648 base64](https://tools.ietf.org/html/rfc4648#section-4)
encoded SHA-256 hash of the [customer-supplied encryption key]
(https://cloud.google.com/compute/docs/disks/customer-supplied-encryption) that protects this resource.
## Import
~> **Note:** The fields `boot_disk.0.initialize_params`, `boot_disk.0.disk_entryption_raw` and `attached_disk.*.disk_encryption_key_raw` cannot be imported automatically. The API doesn't return this information. If you are setting one of these fields in your config, you will need to update your state manually after importing the resource.
Instances can be imported using the `project`, `zone` and `name`, e.g.
```
$ terraform import google_compute_instance.default gcp-project/us-central1-a/test
```