@@ -12,6 +12,10 @@ import (
1212 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1313)
1414
15+ var (
16+ defaultSettingId = "global"
17+ )
18+
1519func retryOnEtagError [Req , Resp any ](f func (req Req ) (Resp , error ), firstReq Req , updateReq func (req * Req , newEtag string ), retriableErrors []error ) (Resp , error ) {
1620 req := firstReq
1721 // Retry once on etag error.
@@ -68,6 +72,9 @@ type genericSettingDefinition[T, U any] interface {
6872
6973 // Update the etag in the setting.
7074 SetETag (t * T , newEtag string )
75+
76+ // Generate resource ID from settings instance
77+ GetId (t * T ) string
7178}
7279
7380func getEtag [T any ](t T ) string {
@@ -104,6 +111,9 @@ type workspaceSetting[T any] struct {
104111
105112 // Delete the setting with the given etag, and return the new etag.
106113 deleteFunc func (ctx context.Context , w * databricks.WorkspaceClient , etag string ) (string , error )
114+
115+ // Optional function to generate resource ID from the settings
116+ generateIdFunc func (setting * T ) string
107117}
108118
109119func (w workspaceSetting [T ]) SettingStruct () T {
@@ -121,6 +131,15 @@ func (w workspaceSetting[T]) Delete(ctx context.Context, c *databricks.Workspace
121131func (w workspaceSetting [T ]) GetETag (t * T ) string {
122132 return getEtag (t )
123133}
134+
135+ func (w workspaceSetting [T ]) GetId (t * T ) string {
136+ id := defaultSettingId
137+ if w .generateIdFunc != nil {
138+ id = w .generateIdFunc (t )
139+ }
140+ return id
141+ }
142+
124143func (w workspaceSetting [T ]) SetETag (t * T , newEtag string ) {
125144 setEtag (t , newEtag )
126145}
@@ -145,6 +164,9 @@ type accountSetting[T any] struct {
145164
146165 // Delete the setting with the given etag, and return the new etag.
147166 deleteFunc func (ctx context.Context , w * databricks.AccountClient , etag string ) (string , error )
167+
168+ // Optional function to generate resource ID from the settings
169+ generateIdFunc func (setting * T ) string
148170}
149171
150172func (w accountSetting [T ]) SettingStruct () T {
@@ -166,6 +188,14 @@ func (w accountSetting[T]) SetETag(t *T, newEtag string) {
166188 setEtag (t , newEtag )
167189}
168190
191+ func (w accountSetting [T ]) GetId (t * T ) string {
192+ id := defaultSettingId
193+ if w .generateIdFunc != nil {
194+ id = w .generateIdFunc (t )
195+ }
196+ return id
197+ }
198+
169199var _ accountSettingDefinition [struct {}] = accountSetting [struct {}]{}
170200
171201func makeSettingResource [T , U any ](defn genericSettingDefinition [T , U ]) common.Resource {
@@ -217,7 +247,8 @@ func makeSettingResource[T, U any](defn genericSettingDefinition[T, U]) common.R
217247 default :
218248 return fmt .Errorf ("unexpected setting type: %T" , defn )
219249 }
220- d .SetId (res )
250+ d .Set ("etag" , res )
251+ d .SetId (defn .GetId (& setting ))
221252 return nil
222253 }
223254
@@ -235,7 +266,7 @@ func makeSettingResource[T, U any](defn genericSettingDefinition[T, U]) common.R
235266 if err != nil {
236267 return err
237268 }
238- res , err = defn .Read (ctx , w , d .Id ( ))
269+ res , err = defn .Read (ctx , w , d .Get ( "etag" ).( string ))
239270 if err != nil {
240271 return err
241272 }
@@ -244,7 +275,7 @@ func makeSettingResource[T, U any](defn genericSettingDefinition[T, U]) common.R
244275 if err != nil {
245276 return err
246277 }
247- res , err = defn .Read (ctx , a , d .Id ( ))
278+ res , err = defn .Read (ctx , a , d .Get ( "etag" ).( string ))
248279 if err != nil {
249280 return err
250281 }
@@ -259,12 +290,12 @@ func makeSettingResource[T, U any](defn genericSettingDefinition[T, U]) common.R
259290 // with a response which is at least as recent as the etag.
260291 // Updating, while not always necessary, ensures that the
261292 // server responds with an updated response.
262- d .SetId ( defn .GetETag (res ))
293+ d .Set ( "etag" , defn .GetETag (res ))
263294 return nil
264295 },
265296 Update : func (ctx context.Context , d * schema.ResourceData , c * common.DatabricksClient ) error {
266297 var setting T
267- defn .SetETag (& setting , d .Id ( ))
298+ defn .SetETag (& setting , d .Get ( "etag" ).( string ))
268299 return createOrUpdate (ctx , d , c , setting )
269300 },
270301 Delete : func (ctx context.Context , d * schema.ResourceData , c * common.DatabricksClient ) error {
@@ -280,7 +311,7 @@ func makeSettingResource[T, U any](defn genericSettingDefinition[T, U]) common.R
280311 func (etag string ) (string , error ) {
281312 return defn .Delete (ctx , w , etag )
282313 },
283- d .Id ( ),
314+ d .Get ( "etag" ).( string ),
284315 updateETag ,
285316 deleteRetriableErrors )
286317 if err != nil {
@@ -295,7 +326,7 @@ func makeSettingResource[T, U any](defn genericSettingDefinition[T, U]) common.R
295326 func (etag string ) (string , error ) {
296327 return defn .Delete (ctx , a , etag )
297328 },
298- d .Id ( ),
329+ d .Get ( "etag" ).( string ),
299330 updateETag ,
300331 deleteRetriableErrors )
301332 if err != nil {
@@ -304,7 +335,7 @@ func makeSettingResource[T, U any](defn genericSettingDefinition[T, U]) common.R
304335 default :
305336 return fmt .Errorf ("unexpected setting type: %T" , defn )
306337 }
307- d .SetId ( etag )
338+ d .Set ( "etag" , etag )
308339 return nil
309340 },
310341 }
0 commit comments