Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added AccountData, AccountClient and define generic databricks data for workspace and account #2429

Merged
merged 6 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type DatabricksClient struct {
// callback used to create API1.2 call wrapper, which simplifies unit tessting
commandFactory func(context.Context, *DatabricksClient) CommandExecutor
cachedWorkspaceClient *databricks.WorkspaceClient
cachedAccountClient *databricks.AccountClient
mu sync.Mutex
}

Expand All @@ -66,6 +67,20 @@ func (c *DatabricksClient) WorkspaceClient() (*databricks.WorkspaceClient, error
return w, nil
}

func (c *DatabricksClient) AccountClient() (*databricks.AccountClient, error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.cachedAccountClient != nil {
return c.cachedAccountClient, nil
}
acc, err := databricks.NewAccountClient((*databricks.Config)(c.DatabricksClient.Config))
if err != nil {
return nil, err
}
tanmay-db marked this conversation as resolved.
Show resolved Hide resolved
c.cachedAccountClient = acc
return acc, nil
}

// Get on path
func (c *DatabricksClient) Get(ctx context.Context, path string, request any, response any) error {
return c.Do(ctx, http.MethodGet, path, request, response, c.addApiPrefix)
Expand Down
45 changes: 45 additions & 0 deletions common/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,51 @@ func WorkspaceData[T any](read func(context.Context, *T, *databricks.WorkspaceCl
}
}

func AccountData[T any](read func(context.Context, *T, *databricks.AccountClient) error) *schema.Resource {
tanmay-db marked this conversation as resolved.
Show resolved Hide resolved
tanmay-db marked this conversation as resolved.
Show resolved Hide resolved
var dummy T
s := StructToSchema(dummy, func(m map[string]*schema.Schema) map[string]*schema.Schema {
// `id` attribute must be marked as computed, otherwise it's not set!
if v, ok := m["id"]; ok {
v.Computed = true
v.Required = false
}
return m
})
return &schema.Resource{
Schema: s,
ReadContext: func(ctx context.Context, d *schema.ResourceData, m any) (diags diag.Diagnostics) {
defer func() {
// using recoverable() would cause more complex rewrapping of DataToStructPointer & StructToData
if panic := recover(); panic != nil {
diags = diag.Errorf("panic: %v", panic)
}
}()
ptr := reflect.New(reflect.ValueOf(dummy).Type())
DataToReflectValue(d, &schema.Resource{Schema: s}, ptr.Elem())
client := m.(*DatabricksClient)
acc, err := client.AccountClient()
if err != nil {
err = nicerError(ctx, err, "read data")
return diag.FromErr(err)
}
err = read(ctx, ptr.Interface().(*T), acc)
if err != nil {
err = nicerError(ctx, err, "read data")
diags = diag.FromErr(err)
}
StructToData(ptr.Elem().Interface(), s, d)
// check if the resource schema has the `id` attribute (marked with `json:"id"` in the provided structure).
// and if yes, then use it as resource ID. If not, then use default value for resource ID (`_`)
if _, ok := s["id"]; ok {
d.SetId(d.Get("id").(string))
} else {
d.SetId("_")
}
return
},
}
}

func EqualFoldDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
if strings.EqualFold(old, new) {
log.Printf("[INFO] Suppressing diff on %s", k)
Expand Down