-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handwritten id updates for several compute resources #2502
Changes from 7 commits
642d38f
ed51673
73fbeb3
5122763
47d5b65
e7a26d8
271f49a
fdbf488
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,7 +155,7 @@ func resourceComputeInstanceGroupCreate(d *schema.ResourceData, meta interface{} | |
} | ||
|
||
// It probably maybe worked, so store the ID now | ||
d.SetId(fmt.Sprintf("%s/%s", zone, name)) | ||
d.SetId(fmt.Sprintf("projects/%s/zones/%s/instanceGroups/%s", project, zone, name)) | ||
|
||
// Wait for the operation to complete | ||
err = computeOperationWait(config.clientCompute, op, project, "Creating InstanceGroup") | ||
|
@@ -378,18 +378,19 @@ func resourceComputeInstanceGroupDelete(d *schema.ResourceData, meta interface{} | |
} | ||
|
||
func resourceComputeInstanceGroupImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
parts := strings.Split(d.Id(), "/") | ||
if len(parts) == 2 { | ||
d.Set("zone", parts[0]) | ||
d.Set("name", parts[1]) | ||
} else if len(parts) == 3 { | ||
d.Set("project", parts[0]) | ||
d.Set("zone", parts[1]) | ||
d.Set("name", parts[2]) | ||
d.SetId(parts[1] + "/" + parts[2]) | ||
} else { | ||
return nil, fmt.Errorf("Invalid compute instance group specifier. Expecting {zone}/{name} or {project}/{zone}/{name}") | ||
config := meta.(*Config) | ||
if err := parseImportId([]string{ | ||
"projects/(?P<project>[^/]+)/zones/(?P<zone>[^/]+)/instanceGroups/(?P<name>[^/]+)", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add this form to the docs? |
||
"(?P<project>[^/]+)/(?P<zone>[^/]+)/(?P<name>[^/]+)", | ||
"(?P<zone>[^/]+)/(?P<name>[^/]+)", | ||
}, d, config); err != nil { | ||
return nil, err | ||
} | ||
id, err := replaceVars(d, config, "projects/{{project}}/zones/{{zone}}/instanceGroups/{{name}}") | ||
if err != nil { | ||
return nil, err | ||
} | ||
d.SetId(id) | ||
|
||
return []*schema.ResourceData{d}, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -392,7 +392,7 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte | |
} | ||
|
||
// It probably maybe worked, so store the ID now | ||
id, err := replaceVars(d, config, "{{project}}/{{zone}}/{{name}}") | ||
id, err := replaceVars(d, config, "projects/{{project}}/zones/{{zone}}/instanceGroupManagers/{{name}}") | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -451,7 +451,7 @@ func flattenFixedOrPercent(fixedOrPercent *computeBeta.FixedOrPercent) []map[str | |
|
||
func getManager(d *schema.ResourceData, meta interface{}) (*computeBeta.InstanceGroupManager, error) { | ||
config := meta.(*Config) | ||
if err := parseImportId([]string{"(?P<project>[^/]+)/(?P<zone>[^/]+)/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config); err != nil { | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What circumstances will this + others be needed under? Import should set all the appropriate values, will Create not? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can be removed now that we have an import method. |
||
return nil, err | ||
} | ||
|
||
|
@@ -595,7 +595,7 @@ 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{"(?P<project>[^/]+)/(?P<zone>[^/]+)/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config); err != nil { | ||
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 | ||
} | ||
|
||
|
@@ -818,7 +818,7 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte | |
func resourceComputeInstanceGroupManagerDelete(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
if err := parseImportId([]string{"(?P<project>[^/]+)/(?P<zone>[^/]+)/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config); err != nil { | ||
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) | ||
|
@@ -1020,12 +1020,12 @@ func flattenUpdatePolicy(updatePolicy *computeBeta.InstanceGroupManagerUpdatePol | |
func resourceInstanceGroupManagerStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
d.Set("wait_for_instances", false) | ||
config := meta.(*Config) | ||
if err := parseImportId([]string{"(?P<project>[^/]+)/(?P<zone>[^/]+)/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config); err != nil { | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above- I think we need docs for the full form |
||
return nil, err | ||
} | ||
|
||
// Replace import id for the resource id | ||
id, err := replaceVars(d, config, "{{project}}/{{zone}}/{{name}}") | ||
id, err := replaceVars(d, config, "projects/{{project}}/zones/{{zone}}/instanceGroupManagers/{{name}}") | ||
if err != nil { | ||
return nil, fmt.Errorf("Error constructing id: %s", err) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package google | |
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/hashicorp/errwrap" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/customdiff" | ||
|
@@ -19,7 +20,7 @@ func resourceComputeInstanceTemplate() *schema.Resource { | |
Read: resourceComputeInstanceTemplateRead, | ||
Delete: resourceComputeInstanceTemplateDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
State: resourceComputeInstanceTemplateImportState, | ||
}, | ||
SchemaVersion: 1, | ||
CustomizeDiff: customdiff.All( | ||
|
@@ -737,7 +738,7 @@ func resourceComputeInstanceTemplateCreate(d *schema.ResourceData, meta interfac | |
} | ||
|
||
// Store the ID now | ||
d.SetId(instanceTemplate.Name) | ||
d.SetId(fmt.Sprintf("projects/%s/global/instanceTemplates/%s", project, instanceTemplate.Name)) | ||
|
||
err = computeSharedOperationWait(config.clientCompute, op, project, "Creating Instance Template") | ||
if err != nil { | ||
|
@@ -963,7 +964,8 @@ func resourceComputeInstanceTemplateRead(d *schema.ResourceData, meta interface{ | |
return err | ||
} | ||
|
||
instanceTemplate, err := config.clientComputeBeta.InstanceTemplates.Get(project, d.Id()).Do() | ||
splits := strings.Split(d.Id(), "/") | ||
instanceTemplate, err := config.clientComputeBeta.InstanceTemplates.Get(project, splits[len(splits)-1]).Do() | ||
if err != nil { | ||
return handleNotFoundError(err, d, fmt.Sprintf("Instance Template %q", d.Get("name").(string))) | ||
} | ||
|
@@ -1091,8 +1093,9 @@ func resourceComputeInstanceTemplateDelete(d *schema.ResourceData, meta interfac | |
return err | ||
} | ||
|
||
splits := strings.Split(d.Id(), "/") | ||
op, err := config.clientCompute.InstanceTemplates.Delete( | ||
project, d.Id()).Do() | ||
project, splits[len(splits)-1]).Do() | ||
if err != nil { | ||
return fmt.Errorf("Error deleting instance template: %s", err) | ||
} | ||
|
@@ -1129,3 +1132,19 @@ func expandResourceComputeInstanceTemplateScheduling(d *schema.ResourceData, met | |
} | ||
return expanded, nil | ||
} | ||
|
||
func resourceComputeInstanceTemplateImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
config := meta.(*Config) | ||
if err := parseImportId([]string{"projects/(?P<project>[^/]+)/global/instanceTemplates/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will need new docs |
||
return nil, err | ||
} | ||
|
||
// Replace import id for the resource id | ||
id, err := replaceVars(d, config, "projects/{{project}}/global/instanceTemplates/{{name}}") | ||
if err != nil { | ||
return nil, fmt.Errorf("Error constructing id: %s", err) | ||
} | ||
d.SetId(id) | ||
|
||
return []*schema.ResourceData{d}, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least in Update, probably worth pulling
name
out to a variable.