|
| 1 | +package dresources |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/databricks/cli/bundle/config/resources" |
| 7 | + "github.com/databricks/cli/bundle/deployplan" |
| 8 | + "github.com/databricks/databricks-sdk-go" |
| 9 | + "github.com/databricks/databricks-sdk-go/service/ml" |
| 10 | +) |
| 11 | + |
| 12 | +type ResourceMlflowModel struct { |
| 13 | + client *databricks.WorkspaceClient |
| 14 | +} |
| 15 | + |
| 16 | +func (*ResourceMlflowModel) New(client *databricks.WorkspaceClient) *ResourceMlflowModel { |
| 17 | + return &ResourceMlflowModel{client: client} |
| 18 | +} |
| 19 | + |
| 20 | +func (*ResourceMlflowModel) PrepareState(input *resources.MlflowModel) *ml.CreateModelRequest { |
| 21 | + return &input.CreateModelRequest |
| 22 | +} |
| 23 | + |
| 24 | +func (*ResourceMlflowModel) RemapState(model *ml.ModelDatabricks) *ml.CreateModelRequest { |
| 25 | + return &ml.CreateModelRequest{ |
| 26 | + Name: model.Name, |
| 27 | + Tags: model.Tags, |
| 28 | + Description: model.Description, |
| 29 | + ForceSendFields: filterFields[ml.CreateModelRequest](model.ForceSendFields), |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func (r *ResourceMlflowModel) DoRefresh(ctx context.Context, id string) (*ml.ModelDatabricks, error) { |
| 34 | + response, err := r.client.ModelRegistry.GetModel(ctx, ml.GetModelRequest{ |
| 35 | + Name: id, |
| 36 | + }) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + return response.RegisteredModelDatabricks, nil |
| 41 | +} |
| 42 | + |
| 43 | +func (r *ResourceMlflowModel) DoCreate(ctx context.Context, config *ml.CreateModelRequest) (string, *ml.ModelDatabricks, error) { |
| 44 | + response, err := r.client.ModelRegistry.CreateModel(ctx, *config) |
| 45 | + if err != nil { |
| 46 | + return "", nil, err |
| 47 | + } |
| 48 | + // Create API call returns [ml.Model] while DoRefresh returns [ml.ModelDatabricks]. |
| 49 | + // Thus we need to convert the response to the expected type. |
| 50 | + modelDatabricks := &ml.ModelDatabricks{ |
| 51 | + Name: response.RegisteredModel.Name, |
| 52 | + Description: response.RegisteredModel.Description, |
| 53 | + Tags: response.RegisteredModel.Tags, |
| 54 | + ForceSendFields: filterFields[ml.ModelDatabricks](response.RegisteredModel.ForceSendFields, "CreationTimestamp", "Id", "LastUpdatedTimestamp", "LatestVersions", "PermissionLevel", "UserId"), |
| 55 | + |
| 56 | + // Coping the fields only to satisfy the linter. These fields are not |
| 57 | + // part of the configuration tree so they don't need to be copied. |
| 58 | + // The linter works as a safeguard to ensure we add new fields to the bundle config tree |
| 59 | + // to the mapping logic here as well. |
| 60 | + CreationTimestamp: 0, |
| 61 | + Id: "", |
| 62 | + LastUpdatedTimestamp: 0, |
| 63 | + LatestVersions: nil, |
| 64 | + PermissionLevel: "", |
| 65 | + UserId: "", |
| 66 | + } |
| 67 | + return response.RegisteredModel.Name, modelDatabricks, nil |
| 68 | +} |
| 69 | + |
| 70 | +func (r *ResourceMlflowModel) DoUpdate(ctx context.Context, id string, config *ml.CreateModelRequest) (*ml.ModelDatabricks, error) { |
| 71 | + updateRequest := ml.UpdateModelRequest{ |
| 72 | + Name: id, |
| 73 | + Description: config.Description, |
| 74 | + ForceSendFields: filterFields[ml.UpdateModelRequest](config.ForceSendFields), |
| 75 | + } |
| 76 | + |
| 77 | + response, err := r.client.ModelRegistry.UpdateModel(ctx, updateRequest) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + |
| 82 | + // Update API call returns [ml.Model] while DoRefresh returns [ml.ModelDatabricks]. |
| 83 | + // Thus we need to convert the response to the expected type. |
| 84 | + modelDatabricks := &ml.ModelDatabricks{ |
| 85 | + Name: response.RegisteredModel.Name, |
| 86 | + Description: response.RegisteredModel.Description, |
| 87 | + Tags: response.RegisteredModel.Tags, |
| 88 | + ForceSendFields: filterFields[ml.ModelDatabricks](response.RegisteredModel.ForceSendFields, "CreationTimestamp", "Id", "LastUpdatedTimestamp", "LatestVersions", "PermissionLevel", "UserId"), |
| 89 | + |
| 90 | + // Coping the fields only to satisfy the linter. These fields are not |
| 91 | + // part of the configuration tree so they don't need to be copied. |
| 92 | + // The linter works as a safeguard to ensure we add new fields to the bundle config tree |
| 93 | + // to the mapping logic here as well. |
| 94 | + CreationTimestamp: 0, |
| 95 | + Id: "", |
| 96 | + LastUpdatedTimestamp: 0, |
| 97 | + LatestVersions: nil, |
| 98 | + PermissionLevel: "", |
| 99 | + UserId: "", |
| 100 | + } |
| 101 | + return modelDatabricks, nil |
| 102 | +} |
| 103 | + |
| 104 | +func (r *ResourceMlflowModel) DoDelete(ctx context.Context, id string) error { |
| 105 | + return r.client.ModelRegistry.DeleteModel(ctx, ml.DeleteModelRequest{ |
| 106 | + Name: id, |
| 107 | + }) |
| 108 | +} |
| 109 | + |
| 110 | +func (*ResourceMlflowModel) FieldTriggers() map[string]deployplan.ActionType { |
| 111 | + return map[string]deployplan.ActionType{ |
| 112 | + // Recreate matches current behavior of Terraform. It is possible to rename without recreate |
| 113 | + // but that would require dynamic select of the method during update since |
| 114 | + // the [ml.RenameModel] needs to be called instead of [ml.UpdateModel]. |
| 115 | + // |
| 116 | + // We might reasonably choose to never fix this because this is a legacy resource. |
| 117 | + "name": deployplan.ActionTypeRecreate, |
| 118 | + |
| 119 | + // Allowing updates for tags requires dynamic selection of the method since |
| 120 | + // tags can only be updated by calling [ml.SetModelTag] or [ml.DeleteModelTag] methods. |
| 121 | + // |
| 122 | + // Skip annotation matches the current behavior of Terraform where tags changes are showed |
| 123 | + // in plan but are just ignored / not applied. Since this is a legacy resource we might |
| 124 | + // reasonably choose to not fix it here as well. |
| 125 | + "tags": deployplan.ActionTypeSkip, |
| 126 | + } |
| 127 | +} |
0 commit comments