From fdbf4881bb5d7e104f54edc9a9275e8280c55ffa Mon Sep 17 00:00:00 2001 From: Sam Levenick Date: Wed, 23 Oct 2019 10:19:13 -0700 Subject: [PATCH] Refactor, remove parseImportId in IGM --- .../resources/resource_compute_instance.go | 22 +++++++++---------- ...urce_compute_instance_group_manager.go.erb | 10 --------- .../r/compute_instance_group.html.markdown | 1 + ...mpute_instance_group_manager.html.markdown | 1 + .../r/compute_instance_template.html.markdown | 6 +++-- 5 files changed, 17 insertions(+), 23 deletions(-) diff --git a/third_party/terraform/resources/resource_compute_instance.go b/third_party/terraform/resources/resource_compute_instance.go index 83f14cda5b9d..6e90ca789ae4 100644 --- a/third_party/terraform/resources/resource_compute_instance.go +++ b/third_party/terraform/resources/resource_compute_instance.go @@ -906,7 +906,7 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err // Change back to getInstance(config, d) once updating alias ips is GA. instance, err := config.clientComputeBeta.Instances.Get(project, zone, d.Get("name").(string)).Do() if err != nil { - return handleNotFoundError(err, d, fmt.Sprintf("Instance %s", d.Get("name").(string))) + return handleNotFoundError(err, d, fmt.Sprintf("Instance %s", instance.Name)) } // Enable partial mode for the resource since it is possible @@ -928,14 +928,14 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err func() error { // retrieve up-to-date metadata from the API in case several updates hit simultaneously. instances // sometimes but not always share metadata fingerprints. - instance, err := config.clientComputeBeta.Instances.Get(project, zone, d.Get("name").(string)).Do() + instance, err := config.clientComputeBeta.Instances.Get(project, zone, instance.Name).Do() if err != nil { return fmt.Errorf("Error retrieving metadata: %s", err) } metadataV1.Fingerprint = instance.Metadata.Fingerprint - op, err := config.clientCompute.Instances.SetMetadata(project, zone, d.Get("name").(string), metadataV1).Do() + op, err := config.clientCompute.Instances.SetMetadata(project, zone, instance.Name, metadataV1).Do() if err != nil { return fmt.Errorf("Error updating metadata: %s", err) } @@ -981,7 +981,7 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err labelFingerprint := d.Get("label_fingerprint").(string) req := compute.InstancesSetLabelsRequest{Labels: labels, LabelFingerprint: labelFingerprint} - op, err := config.clientCompute.Instances.SetLabels(project, zone, d.Get("name").(string), &req).Do() + op, err := config.clientCompute.Instances.SetLabels(project, zone, instance.Name, &req).Do() if err != nil { return fmt.Errorf("Error updating labels: %s", err) } @@ -1001,7 +1001,7 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err } op, err := config.clientComputeBeta.Instances.SetScheduling( - project, zone, d.Get("name").(string), scheduling).Do() + project, zone, instance.Name, scheduling).Do() if err != nil { return fmt.Errorf("Error updating scheduling policy: %s", err) } @@ -1042,7 +1042,7 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err // Delete any accessConfig that currently exists in instNetworkInterface for _, ac := range instNetworkInterface.AccessConfigs { op, err := config.clientCompute.Instances.DeleteAccessConfig( - project, zone, d.Get("name").(string), ac.Name, networkName).Do() + project, zone, instance.Name, ac.Name, networkName).Do() if err != nil { return fmt.Errorf("Error deleting old access_config: %s", err) } @@ -1067,7 +1067,7 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err } op, err := config.clientComputeBeta.Instances.AddAccessConfig( - project, zone, d.Get("name").(string), networkName, ac).Do() + project, zone, instance.Name, networkName, ac).Do() if err != nil { return fmt.Errorf("Error adding new access_config: %s", err) } @@ -1087,7 +1087,7 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err Fingerprint: instNetworkInterface.Fingerprint, ForceSendFields: []string{"AliasIpRanges"}, } - op, err := config.clientComputeBeta.Instances.UpdateNetworkInterface(project, zone, d.Get("name").(string), networkName, ni).Do() + op, err := config.clientComputeBeta.Instances.UpdateNetworkInterface(project, zone, instance.Name, networkName, ni).Do() if err != nil { return errwrap.Wrapf("Error removing alias_ip_range: {{err}}", err) } @@ -1101,7 +1101,7 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err ranges := d.Get(prefix + ".alias_ip_range").([]interface{}) if len(ranges) > 0 { if rereadFingerprint { - instance, err = config.clientComputeBeta.Instances.Get(project, zone, d.Get("name").(string)).Do() + instance, err = config.clientComputeBeta.Instances.Get(project, zone, instance.Name).Do() if err != nil { return err } @@ -1111,7 +1111,7 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err AliasIpRanges: expandAliasIpRanges(ranges), Fingerprint: instNetworkInterface.Fingerprint, } - op, err := config.clientComputeBeta.Instances.UpdateNetworkInterface(project, zone, d.Get("name").(string), networkName, ni).Do() + op, err := config.clientComputeBeta.Instances.UpdateNetworkInterface(project, zone, instance.Name, networkName, ni).Do() if err != nil { return errwrap.Wrapf("Error adding alias_ip_range: {{err}}", err) } @@ -1340,7 +1340,7 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err if d.HasChange("shielded_instance_config") { shieldedVmConfig := expandShieldedVmConfigs(d) - op, err := config.clientComputeBeta.Instances.UpdateShieldedVmConfig(project, zone, d.Get("name").(string), shieldedVmConfig).Do() + op, err := config.clientComputeBeta.Instances.UpdateShieldedVmConfig(project, zone, instance.Name, shieldedVmConfig).Do() if err != nil { return fmt.Errorf("Error updating shielded vm config: %s", err) } diff --git a/third_party/terraform/resources/resource_compute_instance_group_manager.go.erb b/third_party/terraform/resources/resource_compute_instance_group_manager.go.erb index fd49c5462294..75743b5af66a 100644 --- a/third_party/terraform/resources/resource_compute_instance_group_manager.go.erb +++ b/third_party/terraform/resources/resource_compute_instance_group_manager.go.erb @@ -451,9 +451,6 @@ func flattenFixedOrPercent(fixedOrPercent *computeBeta.FixedOrPercent) []map[str func getManager(d *schema.ResourceData, meta interface{}) (*computeBeta.InstanceGroupManager, error) { config := meta.(*Config) - if err := parseImportId([]string{"projects/(?P[^/]+)/zones/(?P[^/]+)/instanceGroupManagers/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)"}, d, config); err != nil { - return nil, err - } project, err := getProject(d, config) if err != nil { @@ -595,10 +592,6 @@ func performZoneUpdate(d *schema.ResourceData, config *Config, id string, update func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - if err := parseImportId([]string{"projects/(?P[^/]+)/zones/(?P[^/]+)/instanceGroupManagers/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)"}, d, config); err != nil { - return err - } - project, err := getProject(d, config) if err != nil { return err @@ -818,9 +811,6 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte func resourceComputeInstanceGroupManagerDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - if err := parseImportId([]string{"projects/(?P[^/]+)/zones/(?P[^/]+)/instanceGroupManagers/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)"}, d, config); err != nil { - return err - } project, err := getProject(d, config) if err != nil { return err diff --git a/third_party/terraform/website/docs/r/compute_instance_group.html.markdown b/third_party/terraform/website/docs/r/compute_instance_group.html.markdown index 96627ba56df6..401ac57a255c 100644 --- a/third_party/terraform/website/docs/r/compute_instance_group.html.markdown +++ b/third_party/terraform/website/docs/r/compute_instance_group.html.markdown @@ -180,4 +180,5 @@ Instance group can be imported using the `zone` and `name` with an optional `pro ``` $ terraform import google_compute_instance_group.webservers us-central1-a/terraform-webservers $ terraform import google_compute_instance_group.webservers big-project/us-central1-a/terraform-webservers +$ terraform import google_compute_instance_group.webservers projects/big-project/zones/us-central1-a/instanceGroups/terraform-webservers ``` diff --git a/third_party/terraform/website/docs/r/compute_instance_group_manager.html.markdown b/third_party/terraform/website/docs/r/compute_instance_group_manager.html.markdown index 21b2771b528c..dee65b6d8d1d 100644 --- a/third_party/terraform/website/docs/r/compute_instance_group_manager.html.markdown +++ b/third_party/terraform/website/docs/r/compute_instance_group_manager.html.markdown @@ -252,6 +252,7 @@ This resource provides the following Instance group managers can be imported using any of these accepted formats: ``` +$ terraform import google_compute_instance_group_manager.appserver projects/{{project}}/zones/{{zone}}/instanceGroupManagers/{{name}} $ terraform import google_compute_instance_group_manager.appserver {{project}}/{{zone}}/{{name}} $ terraform import google_compute_instance_group_manager.appserver {{project}}/{{name}} $ terraform import google_compute_instance_group_manager.appserver {{name}} diff --git a/third_party/terraform/website/docs/r/compute_instance_template.html.markdown b/third_party/terraform/website/docs/r/compute_instance_template.html.markdown index d04cfa16ee05..5e036491ddd5 100644 --- a/third_party/terraform/website/docs/r/compute_instance_template.html.markdown +++ b/third_party/terraform/website/docs/r/compute_instance_template.html.markdown @@ -418,10 +418,12 @@ exported: ## Import -Instance templates can be imported using the `name`, e.g. +Instance templates can be imported using any of these accepted formats: ``` -$ terraform import google_compute_instance_template.default appserver-template +$ terraform import google_compute_instance_template.default projects/{{project}}/global/instanceTemplates/{{name}} +$ terraform import google_compute_instance_template.default {{project}}/{{name}} +$ terraform import google_compute_instance_template.default {{name}} ``` [custom-vm-types]: https://cloud.google.com/dataproc/docs/concepts/compute/custom-machine-types