Skip to content

Commit

Permalink
Add custom update to update metadata in a separate call (#11081) (#18632
Browse files Browse the repository at this point in the history
)

[upstream:f53dfd68dbce629a86360a7785bc0aa20a78647d]

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed Jul 2, 2024
1 parent fd50d61 commit 1708979
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 33 deletions.
3 changes: 3 additions & 0 deletions .changelog/11081.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
index_update: fixed pre-update use unsupported nested updatemask "metadata.contentsDeltaUri". Use first layer update mask and update "metadata" field in a separate call.
```
83 changes: 50 additions & 33 deletions google/services/vertexai/resource_vertex_ai_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,49 +554,66 @@ func resourceVertexAIIndexUpdate(d *schema.ResourceData, meta interface{}) error
updateMask = append(updateMask, "description")
}

if d.HasChange("metadata") {
updateMask = append(updateMask, "metadata")
}

if d.HasChange("effective_labels") {
updateMask = append(updateMask, "labels")
}
// 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
}
newUpdateMask := []string{}

if d.HasChange("metadata.0.contents_delta_uri") {
// Use the current value of isCompleteOverwrite when updating contentsDeltaUri
newUpdateMask = append(newUpdateMask, "metadata.contentsDeltaUri")
newUpdateMask = append(newUpdateMask, "metadata.isCompleteOverwrite")
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}

for _, mask := range updateMask {
// Use granular update masks instead of 'metadata' to avoid the following error:
// '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.'
if mask == "metadata" {
continue
// 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
}
newUpdateMask = append(newUpdateMask, mask)
}
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,
})

// Refreshing updateMask after adding extra schema entries
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(newUpdateMask, ",")})
if err != nil {
return err
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
}
}

// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
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 updateMask is empty we are not updating anything so skip the post
if len(updateMask) > 0 {
// 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",
Expand All @@ -609,9 +626,9 @@ func resourceVertexAIIndexUpdate(d *schema.ResourceData, meta interface{}) error
})

if err != nil {
return fmt.Errorf("Error updating Index %q: %s", d.Id(), err)
return fmt.Errorf("Error Updating second Index %q: %s", d.Id(), err)
} else {
log.Printf("[DEBUG] Finished updating Index %q: %#v", d.Id(), res)
log.Printf("[DEBUG] Finished Updating second Index %q: %#v", d.Id(), res)
}

err = VertexAIOperationWaitTime(
Expand Down

0 comments on commit 1708979

Please sign in to comment.