forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add create_ignore_already_exists to google_sourcerepo_repository (Goo…
…gleCloudPlatform#11770) Co-authored-by: Zhenhua Li <[email protected]>
- Loading branch information
Showing
5 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
mmv1/templates/terraform/custom_create/source_repo_repository.go.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
obj := make(map[string]interface{}) | ||
nameProp, err := expandSourceRepoRepositoryName(d.Get("name"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("name"); !tpgresource.IsEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) { | ||
obj["name"] = nameProp | ||
} | ||
pubsubConfigsProp, err := expandSourceRepoRepositoryPubsubConfigs(d.Get("pubsub_configs"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("pubsub_configs"); !tpgresource.IsEmptyValue(reflect.ValueOf(pubsubConfigsProp)) && (ok || !reflect.DeepEqual(v, pubsubConfigsProp)) { | ||
obj["pubsubConfigs"] = pubsubConfigsProp | ||
} | ||
|
||
url, err := tpgresource.ReplaceVars(d, config, "{{"{{SourceRepoBasePath}}projects/{{project}}/repos"}}") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Creating new Repository: %#v", obj) | ||
billingProject := "" | ||
|
||
project, err := tpgresource.GetProject(d, config) | ||
if err != nil { | ||
return fmt.Errorf("Error fetching project for Repository: %s", err) | ||
} | ||
billingProject = project | ||
|
||
// err == nil indicates that the billing_project value was found | ||
if bp, err := tpgresource.GetBillingProject(d, config); err == nil { | ||
billingProject = bp | ||
} | ||
|
||
headers := make(http.Header) | ||
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "POST", | ||
Project: billingProject, | ||
RawURL: url, | ||
UserAgent: userAgent, | ||
Body: obj, | ||
Timeout: d.Timeout(schema.TimeoutCreate), | ||
Headers: headers, | ||
}) | ||
if err != nil { | ||
gerr, ok := err.(*googleapi.Error) | ||
alreadyExists := ok && gerr.Code == 409 && d.Get("create_ignore_already_exists").(bool) | ||
if alreadyExists { | ||
log.Printf("[DEBUG] Calling get Repository after already exists error") | ||
res, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "GET", | ||
Project: billingProject, | ||
RawURL: url, | ||
UserAgent: userAgent, | ||
Headers: headers, | ||
}) | ||
if err != nil { | ||
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("SourceRepoRepository %q", d.Id())) | ||
} | ||
} else { | ||
return fmt.Errorf("Error creating Repository: %s", err) | ||
} | ||
} | ||
|
||
|
||
// We poll until the resource is found due to eventual consistency issue | ||
// on part of the api https://cloud.google.com/iam/docs/overview#consistency | ||
err = transport_tpg.PollingWaitTime(resourceSourceRepoRepositoryPollRead(d, meta), transport_tpg.PollCheckForExistence, "Creating Source Repository", d.Timeout(schema.TimeoutCreate), 1) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
// We can't guarantee complete consistency even after polling, | ||
// so sleep for some additional time to reduce the likelihood of | ||
// eventual consistency failures. | ||
time.Sleep(10 * time.Second) | ||
|
||
// Store the ID now | ||
id, err := tpgresource.ReplaceVars(d, config, "{{"projects/{{project}}/repos/{{name}}"}}") | ||
if err != nil { | ||
return fmt.Errorf("Error constructing id: %s", err) | ||
} | ||
d.SetId(id) | ||
|
||
if v, ok := d.GetOkExists("pubsub_configs"); !tpgresource.IsEmptyValue(reflect.ValueOf(pubsubConfigsProp)) && (ok || !reflect.DeepEqual(v, pubsubConfigsProp)) { | ||
log.Printf("[DEBUG] Calling update after create to patch in pubsub_configs") | ||
// pubsub_configs cannot be added on create | ||
return resourceSourceRepoRepositoryUpdate(d, meta) | ||
} | ||
|
||
log.Printf("[DEBUG] Finished creating Repository %q: %#v", d.Id(), res) | ||
|
||
return resourceSourceRepoRepositoryRead(d, meta) |
6 changes: 6 additions & 0 deletions
6
mmv1/templates/terraform/extra_schema_entry/source_repo_repository.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
"create_ignore_already_exists": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Computed: false, | ||
Description: `If set to true, skip repository creation if a repository with the same name already exists.`, | ||
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters