diff --git a/third_party/terraform/data_sources/data_source_google_cloudfunctions_function.go b/third_party/terraform/data_sources/data_source_google_cloudfunctions_function.go index 6722215b9cc9..303e1b0a3368 100644 --- a/third_party/terraform/data_sources/data_source_google_cloudfunctions_function.go +++ b/third_party/terraform/data_sources/data_source_google_cloudfunctions_function.go @@ -39,7 +39,7 @@ func dataSourceGoogleCloudFunctionsFunctionRead(d *schema.ResourceData, meta int Name: d.Get("name").(string), } - d.SetId(cloudFuncId.terraformId()) + d.SetId(cloudFuncId.cloudFunctionId()) err = resourceCloudFunctionsRead(d, meta) if err != nil { diff --git a/third_party/terraform/resources/resource_cloudfunctions_function.go b/third_party/terraform/resources/resource_cloudfunctions_function.go index 641c50c2f778..236a4068c78b 100644 --- a/third_party/terraform/resources/resource_cloudfunctions_function.go +++ b/third_party/terraform/resources/resource_cloudfunctions_function.go @@ -43,21 +43,19 @@ func (s *cloudFunctionId) locationId() string { return fmt.Sprintf("projects/%s/locations/%s", s.Project, s.Region) } -func (s *cloudFunctionId) terraformId() string { - return fmt.Sprintf("%s/%s/%s", s.Project, s.Region, s.Name) -} - -func parseCloudFunctionId(id string, config *Config) (*cloudFunctionId, error) { - if parts := strings.Split(id, "/"); len(parts) == 3 { - return &cloudFunctionId{ - Project: parts[0], - Region: parts[1], - Name: parts[2], - }, nil - } - - return nil, fmt.Errorf("Invalid CloudFunction id format, expecting " + - "`{projectId}/{regionId}/{cloudFunctionName}`") +func parseCloudFunctionId(d *schema.ResourceData, config *Config) (*cloudFunctionId, error) { + if err := parseImportId([]string{ + "projects/(?P[^/]+)/locations/(?P[^/]+)/functions/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", + "(?P[^/]+)", + }, d, config); err != nil { + return nil, err + } + return &cloudFunctionId{ + Project: d.Get("project").(string), + Region: d.Get("region").(string), + Name: d.Get("name").(string), + }, nil } func joinMapKeys(mapToJoin *map[int]bool) string { @@ -372,7 +370,7 @@ func resourceCloudFunctionsCreate(d *schema.ResourceData, meta interface{}) erro } // Name of function should be unique - d.SetId(cloudFuncId.terraformId()) + d.SetId(cloudFuncId.cloudFunctionId()) err = cloudFunctionsOperationWait(config.clientCloudFunctions, op, "Creating CloudFunctions Function", int(d.Timeout(schema.TimeoutCreate).Minutes())) @@ -386,7 +384,7 @@ func resourceCloudFunctionsCreate(d *schema.ResourceData, meta interface{}) erro func resourceCloudFunctionsRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - cloudFuncId, err := parseCloudFunctionId(d.Id(), config) + cloudFuncId, err := parseCloudFunctionId(d, config) if err != nil { return err } @@ -447,7 +445,7 @@ func resourceCloudFunctionsUpdate(d *schema.ResourceData, meta interface{}) erro return err } - cloudFuncId, err := parseCloudFunctionId(d.Id(), config) + cloudFuncId, err := parseCloudFunctionId(d, config) if err != nil { return err } @@ -536,7 +534,7 @@ func resourceCloudFunctionsUpdate(d *schema.ResourceData, meta interface{}) erro func resourceCloudFunctionsDestroy(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - cloudFuncId, err := parseCloudFunctionId(d.Id(), config) + cloudFuncId, err := parseCloudFunctionId(d, config) if err != nil { return err }