Skip to content

Commit 041207d

Browse files
direct: Add DoUpdateWithChanges (#3901)
## Why Useful for model serving endpoint where we need to call multiple API endpoints based on the fields that were modified or had a remote drift. relevant TF code: https://github.com/databricks/terraform-provider-databricks/blob/5501f662eb0a8a31bd5f94ba9e9130288b9314a6/serving/resource_model_serving.go#L370
1 parent 1a8df6c commit 041207d

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

bundle/direct/apply.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (d *DeploymentUnit) Destroy(ctx context.Context, db *dstate.DeploymentState
3232
return nil
3333
}
3434

35-
func (d *DeploymentUnit) Deploy(ctx context.Context, db *dstate.DeploymentState, newState any, actionType deployplan.ActionType) error {
35+
func (d *DeploymentUnit) Deploy(ctx context.Context, db *dstate.DeploymentState, newState any, actionType deployplan.ActionType, changes *deployplan.Changes) error {
3636
if actionType == deployplan.ActionTypeCreate {
3737
return d.Create(ctx, db, newState)
3838
}
@@ -51,7 +51,7 @@ func (d *DeploymentUnit) Deploy(ctx context.Context, db *dstate.DeploymentState,
5151
case deployplan.ActionTypeRecreate:
5252
return d.Recreate(ctx, db, oldID, newState)
5353
case deployplan.ActionTypeUpdate:
54-
return d.Update(ctx, db, oldID, newState)
54+
return d.Update(ctx, db, oldID, newState, changes)
5555
case deployplan.ActionTypeUpdateWithID:
5656
return d.UpdateWithID(ctx, db, oldID, newState)
5757
case deployplan.ActionTypeResize:
@@ -107,8 +107,16 @@ func (d *DeploymentUnit) Recreate(ctx context.Context, db *dstate.DeploymentStat
107107
return d.Create(ctx, db, newState)
108108
}
109109

110-
func (d *DeploymentUnit) Update(ctx context.Context, db *dstate.DeploymentState, id string, newState any) error {
111-
remoteState, err := d.Adapter.DoUpdate(ctx, id, newState)
110+
func (d *DeploymentUnit) Update(ctx context.Context, db *dstate.DeploymentState, id string, newState any, changes *deployplan.Changes) error {
111+
var remoteState any
112+
var err error
113+
114+
if d.Adapter.HasDoUpdateWithChanges() && changes != nil {
115+
remoteState, err = d.Adapter.DoUpdateWithChanges(ctx, id, newState, changes)
116+
} else {
117+
remoteState, err = d.Adapter.DoUpdate(ctx, id, newState)
118+
}
119+
112120
if err != nil {
113121
return fmt.Errorf("updating id=%s: %w", id, err)
114122
}

bundle/direct/bundle_apply.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (b *DeploymentBundle) Apply(ctx context.Context, client *databricks.Workspa
9797

9898
// TODO: redo calcDiff to downgrade planned action if possible (?)
9999

100-
err = d.Deploy(ctx, &b.StateDB, entry.NewState.Config, at)
100+
err = d.Deploy(ctx, &b.StateDB, entry.NewState.Config, at, entry.Changes)
101101
if err != nil {
102102
logdiag.LogError(ctx, fmt.Errorf("%s: %w", errorPrefix, err))
103103
return false

bundle/direct/dresources/adapter.go

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ type IResourceNoRefresh interface {
7272
// Example: func (r *ResourceJob) DoUpdate(ctx context.Context, id string, newState *jobs.JobSettings) error
7373
DoUpdate(ctx context.Context, id string, newState any) error
7474

75+
// [Optional] DoUpdateWithChanges updates the resource with information about changes computed during plan.
76+
DoUpdateWithChanges(ctx context.Context, id string, newState any, changes *deployplan.Changes) error
77+
7578
// [Optional] DoUpdateWithID performs an update that may result in resource having a new ID
7679
// Example: func (r *ResourceVolume) DoUpdateWithID(ctx, id string, newState *catalog.CreateVolumeRequestContent) (string, error)
7780
DoUpdateWithID(ctx context.Context, id string, newState any) (string, error)
@@ -104,6 +107,10 @@ type IResourceWithRefresh interface {
104107
// Example: func (r *ResourceSchema) DoUpdate(ctx context.Context, id string, newState *catalog.CreateSchema) (*catalog.SchemaInfo, error)
105108
DoUpdate(ctx context.Context, id string, newState any) (remoteState any, e error)
106109

110+
// [Optional] DoUpdateWithChanges updates the resource with information about changes computed during plan. Returns remote state.
111+
// Example: func (r *ResourceModelServingEndpoint) DoUpdateWithChanges(ctx context.Context, id string, newState *serving.CreateServingEndpoint, changes *deployplan.Changes) (*serving.ServingEndpointInfo, error)
112+
DoUpdateWithChanges(ctx context.Context, id string, newState any, changes *deployplan.Changes) (remoteState any, e error)
113+
107114
// Optional: updates that may change ID. Returns new id and remote state when available.
108115
DoUpdateWithID(ctx context.Context, id string, newState any) (newID string, remoteState any, e error)
109116

@@ -126,11 +133,12 @@ type Adapter struct {
126133
doUpdate *calladapt.BoundCaller
127134

128135
// Optional:
129-
doUpdateWithID *calladapt.BoundCaller
130-
waitAfterCreate *calladapt.BoundCaller
131-
waitAfterUpdate *calladapt.BoundCaller
132-
classifyChange *calladapt.BoundCaller
133-
doResize *calladapt.BoundCaller
136+
doUpdateWithChanges *calladapt.BoundCaller
137+
doUpdateWithID *calladapt.BoundCaller
138+
waitAfterCreate *calladapt.BoundCaller
139+
waitAfterUpdate *calladapt.BoundCaller
140+
classifyChange *calladapt.BoundCaller
141+
doResize *calladapt.BoundCaller
134142

135143
fieldTriggersLocal map[string]deployplan.ActionType
136144
fieldTriggersRemote map[string]deployplan.ActionType
@@ -156,6 +164,7 @@ func NewAdapter(typedNil any, client *databricks.WorkspaceClient) (*Adapter, err
156164
doDelete: nil,
157165
doCreate: nil,
158166
doUpdate: nil,
167+
doUpdateWithChanges: nil,
159168
doUpdateWithID: nil,
160169
doResize: nil,
161170
waitAfterCreate: nil,
@@ -260,6 +269,11 @@ func (a *Adapter) initMethods(resource any) error {
260269

261270
// Optional methods:
262271

272+
a.doUpdateWithChanges, err = prepareCallFromTwoVariants(resource, "DoUpdateWithChanges")
273+
if err != nil {
274+
return err
275+
}
276+
263277
a.doUpdateWithID, err = prepareCallFromTwoVariants(resource, "DoUpdateWithID")
264278
if err != nil {
265279
return err
@@ -345,6 +359,13 @@ func (a *Adapter) validate() error {
345359
validations = append(validations, "DoUpdate remoteState return", a.doUpdate.OutTypes[0], remoteType)
346360
}
347361

362+
if a.doUpdateWithChanges != nil {
363+
validations = append(validations, "DoUpdateWithChanges newState", a.doUpdateWithChanges.InTypes[2], stateType)
364+
if len(a.doUpdateWithChanges.OutTypes) == 2 {
365+
validations = append(validations, "DoUpdateWithChanges remoteState return", a.doUpdateWithChanges.OutTypes[0], remoteType)
366+
}
367+
}
368+
348369
if a.doResize != nil {
349370
validations = append(validations, "DoResize newState", a.doResize.InTypes[2], stateType)
350371
}
@@ -497,6 +518,32 @@ func (a *Adapter) DoUpdate(ctx context.Context, id string, newState any) (any, e
497518
}
498519
}
499520

521+
// HasDoUpdateWithChanges returns true if the resource implements DoUpdateWithChanges method.
522+
func (a *Adapter) HasDoUpdateWithChanges() bool {
523+
return a.doUpdateWithChanges != nil
524+
}
525+
526+
// DoUpdateWithChanges updates the resource with information about changes computed during plan.
527+
// If the implementation returns remote state, it will be returned as the first value; otherwise it will be nil.
528+
func (a *Adapter) DoUpdateWithChanges(ctx context.Context, id string, newState any, changes *deployplan.Changes) (any, error) {
529+
if a.doUpdateWithChanges == nil {
530+
return nil, errors.New("internal error: DoUpdateWithChanges not found")
531+
}
532+
533+
outs, err := a.doUpdateWithChanges.Call(ctx, id, newState, changes)
534+
if err != nil {
535+
return nil, err
536+
}
537+
538+
if len(outs) == 1 {
539+
// WithRefresh version
540+
return outs[0], nil
541+
} else {
542+
// NoRefresh version
543+
return nil, nil
544+
}
545+
}
546+
500547
// HasDoUpdateWithID returns true if the resource implements DoUpdateWithID method.
501548
func (a *Adapter) HasDoUpdateWithID() bool {
502549
return a.doUpdateWithID != nil

0 commit comments

Comments
 (0)