Skip to content

Commit

Permalink
Add custom update to update metadata in a separate call (GoogleCloudP…
Browse files Browse the repository at this point in the history
  • Loading branch information
JoyceYingZhu authored Jul 2, 2024
1 parent b164fac commit f53dfd6
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mmv1/products/vertexai/Index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ examples:
- metadata.0.contents_delta_uri
- metadata.0.is_complete_overwrite
custom_code: !ruby/object:Provider::Terraform::CustomCode
pre_update: templates/terraform/pre_update/vertex_ai_index.go.erb
custom_update: templates/terraform/custom_update/vertex_ai_index.go.erb
parameters:
- !ruby/object:Api::Type::String
name: region
Expand Down
143 changes: 143 additions & 0 deletions mmv1/templates/terraform/custom_update/vertex_ai_index.go.erb
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)

0 comments on commit f53dfd6

Please sign in to comment.