Skip to content

Commit 14c07d7

Browse files
authored
add account workspace setting type (#3415)
1 parent 0e9f804 commit 14c07d7

File tree

1 file changed

+125
-1
lines changed

1 file changed

+125
-1
lines changed

settings/generic_setting.go

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ var _ workspaceSettingDefinition[struct{}] = workspaceSetting[struct{}]{}
149149

150150
type accountSettingDefinition[T any] genericSettingDefinition[T, *databricks.AccountClient]
151151

152-
// An account setting is a setting that is scoped to a workspace.
152+
// An account setting is a setting that is scoped to an account.
153153
type accountSetting[T any] struct {
154154
// The struct corresponding to the setting. The schema of the Terraform resource will be generated from this struct.
155155
// This struct must have an Etag field of type string.
@@ -199,6 +199,100 @@ func (w accountSetting[T]) GetId(t *T) string {
199199

200200
var _ accountSettingDefinition[struct{}] = accountSetting[struct{}]{}
201201

202+
type accountWorkspaceSettingDefinition[T any] genericSettingDefinition[T, *common.DatabricksClient]
203+
204+
// An account workspace setting is a setting that can be scoped to either an account or a workspace.
205+
type accountWorkspaceSetting[T any] struct {
206+
// The struct corresponding to the setting. The schema of the Terraform resource will be generated from this struct.
207+
// This struct must have an Etag field of type string.
208+
settingStruct T
209+
210+
// Read the setting from the server. The etag is provided as the third argument.
211+
readAccFunc func(ctx context.Context, acc *databricks.AccountClient, etag string) (*T, error)
212+
213+
readWsFunc func(ctx context.Context, w *databricks.WorkspaceClient, etag string) (*T, error)
214+
215+
// Update the setting to the value specified by t, and return the new etag. If the setting name is user-settable,
216+
// it will be provided in the third argument. If not, you must set the SettingName field appropriately. You must
217+
// also set AllowMissing: true and the field mask to the field to update.
218+
updateAccFunc func(ctx context.Context, w *databricks.AccountClient, setting T) (string, error)
219+
220+
updateWsFunc func(ctx context.Context, w *databricks.WorkspaceClient, setting T) (string, error)
221+
222+
// Delete the setting with the given etag, and return the new etag.
223+
deleteAccFunc func(ctx context.Context, acc *databricks.AccountClient, etag string) (string, error)
224+
225+
deleteWsFunc func(ctx context.Context, w *databricks.WorkspaceClient, etag string) (string, error)
226+
227+
// Optional function to generate resource ID from the settings. If not provided, will use predefined value `global`
228+
generateIdFunc func(setting *T) string
229+
}
230+
231+
func (aw accountWorkspaceSetting[T]) SettingStruct() T {
232+
return aw.settingStruct
233+
}
234+
func (aw accountWorkspaceSetting[T]) Read(ctx context.Context, c *common.DatabricksClient, etag string) (*T, error) {
235+
if c.Config.IsAccountClient() {
236+
a, err := c.AccountClient()
237+
if err != nil {
238+
return nil, err
239+
}
240+
return aw.readAccFunc(ctx, a, etag)
241+
} else {
242+
ws, err := c.WorkspaceClient()
243+
if err != nil {
244+
return nil, err
245+
}
246+
return aw.readWsFunc(ctx, ws, etag)
247+
}
248+
}
249+
func (aw accountWorkspaceSetting[T]) Update(ctx context.Context, c *common.DatabricksClient, t T) (string, error) {
250+
if c.Config.IsAccountClient() {
251+
a, err := c.AccountClient()
252+
if err != nil {
253+
return "", err
254+
}
255+
return aw.updateAccFunc(ctx, a, t)
256+
} else {
257+
ws, err := c.WorkspaceClient()
258+
if err != nil {
259+
return "", err
260+
}
261+
return aw.updateWsFunc(ctx, ws, t)
262+
}
263+
}
264+
func (aw accountWorkspaceSetting[T]) Delete(ctx context.Context, c *common.DatabricksClient, etag string) (string, error) {
265+
if c.Config.IsAccountClient() {
266+
a, err := c.AccountClient()
267+
if err != nil {
268+
return "", err
269+
}
270+
return aw.deleteAccFunc(ctx, a, etag)
271+
} else {
272+
ws, err := c.WorkspaceClient()
273+
if err != nil {
274+
return "", err
275+
}
276+
return aw.deleteWsFunc(ctx, ws, etag)
277+
}
278+
}
279+
func (aw accountWorkspaceSetting[T]) GetETag(t *T) string {
280+
return getEtag(t)
281+
}
282+
func (aw accountWorkspaceSetting[T]) SetETag(t *T, newEtag string) {
283+
setEtag(t, newEtag)
284+
}
285+
286+
func (aw accountWorkspaceSetting[T]) GetId(t *T) string {
287+
id := defaultSettingId
288+
if aw.generateIdFunc != nil {
289+
id = aw.generateIdFunc(t)
290+
}
291+
return id
292+
}
293+
294+
var _ accountWorkspaceSettingDefinition[struct{}] = accountWorkspaceSetting[struct{}]{}
295+
202296
func makeSettingResource[T, U any](defn genericSettingDefinition[T, U]) common.Resource {
203297
resourceSchema := common.StructToSchema(defn.SettingStruct(),
204298
func(s map[string]*schema.Schema) map[string]*schema.Schema {
@@ -245,6 +339,18 @@ func makeSettingResource[T, U any](defn genericSettingDefinition[T, U]) common.R
245339
if err != nil {
246340
return err
247341
}
342+
case accountWorkspaceSettingDefinition[T]:
343+
var err error
344+
res, err = retryOnEtagError(
345+
func(setting T) (string, error) {
346+
return defn.Update(ctx, c, setting)
347+
},
348+
setting,
349+
defn.SetETag,
350+
createOrUpdateRetriableErrors)
351+
if err != nil {
352+
return err
353+
}
248354
default:
249355
return fmt.Errorf("unexpected setting type: %T", defn)
250356
}
@@ -280,6 +386,12 @@ func makeSettingResource[T, U any](defn genericSettingDefinition[T, U]) common.R
280386
if err != nil {
281387
return err
282388
}
389+
case accountWorkspaceSettingDefinition[T]:
390+
var err error
391+
res, err = defn.Read(ctx, c, d.Get(etagAttrName).(string))
392+
if err != nil {
393+
return err
394+
}
283395
default:
284396
return fmt.Errorf("unexpected setting type: %T", defn)
285397
}
@@ -333,6 +445,18 @@ func makeSettingResource[T, U any](defn genericSettingDefinition[T, U]) common.R
333445
if err != nil {
334446
return err
335447
}
448+
case accountWorkspaceSettingDefinition[T]:
449+
var err error
450+
etag, err = retryOnEtagError(
451+
func(etag string) (string, error) {
452+
return defn.Delete(ctx, c, etag)
453+
},
454+
d.Get(etagAttrName).(string),
455+
updateETag,
456+
deleteRetriableErrors)
457+
if err != nil {
458+
return err
459+
}
336460
default:
337461
return fmt.Errorf("unexpected setting type: %T", defn)
338462
}

0 commit comments

Comments
 (0)