forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom update to update metadata in a separate call (GoogleCloudP…
- Loading branch information
1 parent
b164fac
commit f53dfd6
Showing
2 changed files
with
144 additions
and
1 deletion.
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
143 changes: 143 additions & 0 deletions
143
mmv1/templates/terraform/custom_update/vertex_ai_index.go.erb
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,143 @@ | ||
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
billingProject := "" | ||
|
||
project, err := tpgresource.GetProject(d, config) | ||
if err != nil { | ||
return fmt.Errorf("Error fetching project for Index: %s", err) | ||
} | ||
billingProject = project | ||
|
||
obj := make(map[string]interface{}) | ||
displayNameProp, err := expandVertexAIIndexDisplayName(d.Get("display_name"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("display_name"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, displayNameProp)) { | ||
obj["displayName"] = displayNameProp | ||
} | ||
descriptionProp, err := expandVertexAIIndexDescription(d.Get("description"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("description"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, descriptionProp)) { | ||
obj["description"] = descriptionProp | ||
} | ||
metadataProp, err := expandVertexAIIndexMetadata(d.Get("metadata"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("metadata"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, metadataProp)) { | ||
obj["metadata"] = metadataProp | ||
} | ||
labelsProp, err := expandVertexAIIndexEffectiveLabels(d.Get("effective_labels"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("effective_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) { | ||
obj["labels"] = labelsProp | ||
} | ||
|
||
url, err := tpgresource.ReplaceVars(d, config, "{{VertexAIBasePath}}projects/{{project}}/locations/{{region}}/indexes/{{name}}") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Updating Index %q: %#v", d.Id(), obj) | ||
headers := make(http.Header) | ||
updateMask := []string{} | ||
|
||
if d.HasChange("display_name") { | ||
updateMask = append(updateMask, "displayName") | ||
} | ||
|
||
if d.HasChange("description") { | ||
updateMask = append(updateMask, "description") | ||
} | ||
|
||
if d.HasChange("effective_labels") { | ||
updateMask = append(updateMask, "labels") | ||
} | ||
|
||
// err == nil indicates that the billing_project value was found | ||
if bp, err := tpgresource.GetBillingProject(d, config); err == nil { | ||
billingProject = bp | ||
} | ||
|
||
// if updateMask is empty we are not updating anything so skip the post | ||
if len(updateMask) > 0 { | ||
log.Printf("[DEBUG] Updating first Index with updateMask: %#v", updateMask) | ||
// updateMask is a URL parameter but not present in the schema, so ReplaceVars | ||
// won't set it | ||
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")}) | ||
if err != nil { | ||
return err | ||
} | ||
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "PATCH", | ||
Project: billingProject, | ||
RawURL: url, | ||
UserAgent: userAgent, | ||
Body: obj, | ||
Timeout: d.Timeout(schema.TimeoutUpdate), | ||
Headers: headers, | ||
}) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error updating first Index %q: %s", d.Id(), err) | ||
} else { | ||
log.Printf("[DEBUG] Finished updating first Index %q: %#v", d.Id(), res) | ||
} | ||
|
||
err = VertexAIOperationWaitTime( | ||
config, res, project, "Updating Index", userAgent, | ||
d.Timeout(schema.TimeoutUpdate)) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
secondUpdateMask := []string{} | ||
// 'If `contents_delta_gcs_uri` is set as part of `index.metadata`, | ||
// then no other Index fields can be also updated as part of the same update call.' | ||
// Metadata update need to be done in a separate update call. | ||
if d.HasChange("metadata") { | ||
secondUpdateMask = append(secondUpdateMask, "metadata") | ||
} | ||
|
||
// if secondUpdateMask is empty we are not updating anything so skip the post | ||
if len(secondUpdateMask) > 0 { | ||
log.Printf("[DEBUG] Updating second Index with updateMask: %#v", secondUpdateMask) | ||
// Override updateMask with secondUpdateMask | ||
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(secondUpdateMask, ",")}) | ||
if err != nil { | ||
return err | ||
} | ||
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "PATCH", | ||
Project: billingProject, | ||
RawURL: url, | ||
UserAgent: userAgent, | ||
Body: obj, | ||
Timeout: d.Timeout(schema.TimeoutUpdate), | ||
Headers: headers, | ||
}) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error Updating second Index %q: %s", d.Id(), err) | ||
} else { | ||
log.Printf("[DEBUG] Finished Updating second Index %q: %#v", d.Id(), res) | ||
} | ||
|
||
err = VertexAIOperationWaitTime( | ||
config, res, project, "Updating Index", userAgent, | ||
d.Timeout(schema.TimeoutUpdate)) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return resourceVertexAIIndexRead(d, meta) |