Skip to content

Commit

Permalink
Refactor, remove parseImportId in IGM
Browse files Browse the repository at this point in the history
  • Loading branch information
slevenick committed Oct 23, 2019
1 parent 271f49a commit fdbf488
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 23 deletions.
22 changes: 11 additions & 11 deletions third_party/terraform/resources/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<project>[^/]+)/zones/(?P<zone>[^/]+)/instanceGroupManagers/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<zone>[^/]+)/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config); err != nil {
return nil, err
}

project, err := getProject(d, config)
if err != nil {
Expand Down Expand Up @@ -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<project>[^/]+)/zones/(?P<zone>[^/]+)/instanceGroupManagers/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<zone>[^/]+)/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config); err != nil {
return err
}

project, err := getProject(d, config)
if err != nil {
return err
Expand Down Expand Up @@ -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<project>[^/]+)/zones/(?P<zone>[^/]+)/instanceGroupManagers/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<zone>[^/]+)/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config); err != nil {
return err
}
project, err := getProject(d, config)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fdbf488

Please sign in to comment.