@@ -12,6 +12,11 @@ import (
1212 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1313)
1414
15+ var (
16+ defaultSettingId = "global"
17+ etagAttrName = "etag"
18+ )
19+
1520func retryOnEtagError [Req , Resp any ](f func (req Req ) (Resp , error ), firstReq Req , updateReq func (req * Req , newEtag string ), retriableErrors []error ) (Resp , error ) {
1621 req := firstReq
1722 // Retry once on etag error.
@@ -43,7 +48,7 @@ func getEtagFromError(err error) (string, error) {
4348 errorInfos := apierr .GetErrorInfo (err )
4449 if len (errorInfos ) > 0 {
4550 metadata := errorInfos [0 ].Metadata
46- if etag , ok := metadata ["etag" ]; ok {
51+ if etag , ok := metadata [etagAttrName ]; ok {
4752 return etag , nil
4853 }
4954 }
@@ -68,6 +73,9 @@ type genericSettingDefinition[T, U any] interface {
6873
6974 // Update the etag in the setting.
7075 SetETag (t * T , newEtag string )
76+
77+ // Generate resource ID from settings instance
78+ GetId (t * T ) string
7179}
7280
7381func getEtag [T any ](t T ) string {
@@ -104,6 +112,9 @@ type workspaceSetting[T any] struct {
104112
105113 // Delete the setting with the given etag, and return the new etag.
106114 deleteFunc func (ctx context.Context , w * databricks.WorkspaceClient , etag string ) (string , error )
115+
116+ // Optional function to generate resource ID from the settings. If not provided, will use predefined value `global`
117+ generateIdFunc func (setting * T ) string
107118}
108119
109120func (w workspaceSetting [T ]) SettingStruct () T {
@@ -121,6 +132,15 @@ func (w workspaceSetting[T]) Delete(ctx context.Context, c *databricks.Workspace
121132func (w workspaceSetting [T ]) GetETag (t * T ) string {
122133 return getEtag (t )
123134}
135+
136+ func (w workspaceSetting [T ]) GetId (t * T ) string {
137+ id := defaultSettingId
138+ if w .generateIdFunc != nil {
139+ id = w .generateIdFunc (t )
140+ }
141+ return id
142+ }
143+
124144func (w workspaceSetting [T ]) SetETag (t * T , newEtag string ) {
125145 setEtag (t , newEtag )
126146}
@@ -145,6 +165,9 @@ type accountSetting[T any] struct {
145165
146166 // Delete the setting with the given etag, and return the new etag.
147167 deleteFunc func (ctx context.Context , w * databricks.AccountClient , etag string ) (string , error )
168+
169+ // Optional function to generate resource ID from the settings. If not provided, will use predefined value `global`
170+ generateIdFunc func (setting * T ) string
148171}
149172
150173func (w accountSetting [T ]) SettingStruct () T {
@@ -166,12 +189,20 @@ func (w accountSetting[T]) SetETag(t *T, newEtag string) {
166189 setEtag (t , newEtag )
167190}
168191
192+ func (w accountSetting [T ]) GetId (t * T ) string {
193+ id := defaultSettingId
194+ if w .generateIdFunc != nil {
195+ id = w .generateIdFunc (t )
196+ }
197+ return id
198+ }
199+
169200var _ accountSettingDefinition [struct {}] = accountSetting [struct {}]{}
170201
171202func makeSettingResource [T , U any ](defn genericSettingDefinition [T , U ]) common.Resource {
172203 resourceSchema := common .StructToSchema (defn .SettingStruct (),
173204 func (s map [string ]* schema.Schema ) map [string ]* schema.Schema {
174- s ["etag" ].Computed = true
205+ s [etagAttrName ].Computed = true
175206 // Note: this may not always be computed, but it is for the default namespace setting. If other settings
176207 // are added for which setting_name is not computed, we'll need to expose this somehow as part of the setting
177208 // definition.
@@ -217,7 +248,8 @@ func makeSettingResource[T, U any](defn genericSettingDefinition[T, U]) common.R
217248 default :
218249 return fmt .Errorf ("unexpected setting type: %T" , defn )
219250 }
220- d .SetId (res )
251+ d .Set (etagAttrName , res )
252+ d .SetId (defn .GetId (& setting ))
221253 return nil
222254 }
223255
@@ -235,7 +267,7 @@ func makeSettingResource[T, U any](defn genericSettingDefinition[T, U]) common.R
235267 if err != nil {
236268 return err
237269 }
238- res , err = defn .Read (ctx , w , d .Id ( ))
270+ res , err = defn .Read (ctx , w , d .Get ( etagAttrName ).( string ))
239271 if err != nil {
240272 return err
241273 }
@@ -244,7 +276,7 @@ func makeSettingResource[T, U any](defn genericSettingDefinition[T, U]) common.R
244276 if err != nil {
245277 return err
246278 }
247- res , err = defn .Read (ctx , a , d .Id ( ))
279+ res , err = defn .Read (ctx , a , d .Get ( etagAttrName ).( string ))
248280 if err != nil {
249281 return err
250282 }
@@ -259,12 +291,12 @@ func makeSettingResource[T, U any](defn genericSettingDefinition[T, U]) common.R
259291 // with a response which is at least as recent as the etag.
260292 // Updating, while not always necessary, ensures that the
261293 // server responds with an updated response.
262- d .SetId ( defn .GetETag (res ))
294+ d .Set ( etagAttrName , defn .GetETag (res ))
263295 return nil
264296 },
265297 Update : func (ctx context.Context , d * schema.ResourceData , c * common.DatabricksClient ) error {
266298 var setting T
267- defn .SetETag (& setting , d .Id ( ))
299+ defn .SetETag (& setting , d .Get ( etagAttrName ).( string ))
268300 return createOrUpdate (ctx , d , c , setting )
269301 },
270302 Delete : func (ctx context.Context , d * schema.ResourceData , c * common.DatabricksClient ) error {
@@ -280,7 +312,7 @@ func makeSettingResource[T, U any](defn genericSettingDefinition[T, U]) common.R
280312 func (etag string ) (string , error ) {
281313 return defn .Delete (ctx , w , etag )
282314 },
283- d .Id ( ),
315+ d .Get ( etagAttrName ).( string ),
284316 updateETag ,
285317 deleteRetriableErrors )
286318 if err != nil {
@@ -295,7 +327,7 @@ func makeSettingResource[T, U any](defn genericSettingDefinition[T, U]) common.R
295327 func (etag string ) (string , error ) {
296328 return defn .Delete (ctx , a , etag )
297329 },
298- d .Id ( ),
330+ d .Get ( etagAttrName ).( string ),
299331 updateETag ,
300332 deleteRetriableErrors )
301333 if err != nil {
@@ -304,7 +336,7 @@ func makeSettingResource[T, U any](defn genericSettingDefinition[T, U]) common.R
304336 default :
305337 return fmt .Errorf ("unexpected setting type: %T" , defn )
306338 }
307- d .SetId ( etag )
339+ d .Set ( etagAttrName , etag )
308340 return nil
309341 },
310342 }
0 commit comments