Skip to content

Commit

Permalink
Exporter for databricks_sql_global_config resource (#1806)
Browse files Browse the repository at this point in the history
This fixes #1640
  • Loading branch information
alexott authored Dec 2, 2022
1 parent b2208bb commit da583cd
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 8 deletions.
63 changes: 56 additions & 7 deletions exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,9 @@ var emptySqlQueries = qa.HTTPFixture{
}

var emptyWorkspaceConf = qa.HTTPFixture{
Method: "GET",
Resource: "/api/2.0/workspace-conf?",
Response: map[string]any{
"maxUserInactiveDays": '0',
},
Method: "GET",
Resource: "/api/2.0/workspace-conf?",
Response: map[string]any{},
ReuseRequest: true,
}

Expand All @@ -286,6 +284,13 @@ var allKnownWorkspaceConfs = qa.HTTPFixture{
ReuseRequest: true,
}

var emptyGlobalSQLConfig = qa.HTTPFixture{
Method: "GET",
Resource: "/api/2.0/sql/config/warehouses",
Response: sql.GlobalConfigForRead{},
ReuseRequest: true,
}

func TestImportingUsersGroupsSecretScopes(t *testing.T) {
qa.HTTPFixturesApply(t,
[]qa.HTTPFixture{
Expand All @@ -299,8 +304,9 @@ func TestImportingUsersGroupsSecretScopes(t *testing.T) {
emptySqlQueries,
emptyPipelines,
emptyWorkspaceConf,
dummyWorkspaceConf,
allKnownWorkspaceConfs,
dummyWorkspaceConf,
emptyGlobalSQLConfig,
{
Method: "GET",
Resource: "/api/2.0/preview/scim/v2/Groups?",
Expand Down Expand Up @@ -452,7 +458,14 @@ func TestImportingUsersGroupsSecretScopes(t *testing.T) {
func TestImportingNoResourcesError(t *testing.T) {
qa.HTTPFixturesApply(t,
[]qa.HTTPFixture{
meAdminFixture,
{
Method: "GET",
ReuseRequest: true,
Resource: "/api/2.0/preview/scim/v2/Me",
Response: scim.User{
Groups: []scim.ComplexValue{},
},
},
emptyRepos,
emptyWorkspaceConf,
dummyWorkspaceConf,
Expand Down Expand Up @@ -1400,6 +1413,7 @@ func TestImportingSqlObjects(t *testing.T) {
meAdminFixture,
emptyRepos,
emptyIpAccessLIst,
emptyGlobalSQLConfig,
{
Method: "GET",
Resource: "/api/2.0/global-init-scripts",
Expand Down Expand Up @@ -1599,3 +1613,38 @@ func TestImportingDLTPipelinesMatchingOnly(t *testing.T) {
assert.NoError(t, err)
})
}

func TestImportingGlobalSqlConfig(t *testing.T) {
qa.HTTPFixturesApply(t,
[]qa.HTTPFixture{
meAdminFixture,
{
Method: "GET",
Resource: "/api/2.0/sql/warehouses",
Response: sql.EndpointList{},
},
{
Method: "GET",
Resource: "/api/2.0/sql/config/warehouses",
Response: sql.GlobalConfigForRead{
EnableServerlessCompute: true,
InstanceProfileARN: "arn:...",
},
},
},
func(ctx context.Context, client *common.DatabricksClient) {
tmpDir := fmt.Sprintf("/tmp/tf-%s", qa.RandomName())
defer os.RemoveAll(tmpDir)

ic := newImportContext(client)
ic.Directory = tmpDir
ic.listing = "sql-endpoints"
ic.services = "sql-endpoints"

err := ic.Run()
assert.NoError(t, err)
})
}

// emptyRepos,
// emptyIpAccessLIst,
32 changes: 32 additions & 0 deletions exporter/importables.go
Original file line number Diff line number Diff line change
Expand Up @@ -1144,10 +1144,42 @@ var resourcesMap map[string]importable = map[string]importable{
ID: fmt.Sprintf("/sql/warehouses/%s", r.ID),
Name: "sql_endpoint_" + ic.Importables["databricks_sql_endpoint"].Name(r.Data),
})
ic.Emit(&resource{
Resource: "databricks_sql_global_config",
ID: sql.GlobalSqlConfigResourceID,
})
}
return nil
},
},
"databricks_sql_global_config": {
Service: "sql-endpoints",
Name: func(d *schema.ResourceData) string {
return "sql_global_config"
},
List: func(ic *importContext) error {
if ic.meAdmin {
ic.Emit(&resource{
Resource: "databricks_sql_global_config",
ID: sql.GlobalSqlConfigResourceID,
})
}
return nil
},
Import: func(ic *importContext, r *resource) error {
arn := r.Data.Get("instance_profile_arn").(string)
if arn != "" {
ic.Emit(&resource{
Resource: "databricks_instance_profile",
ID: arn,
})
}
return nil
},
Depends: []reference{
{Path: "instance_profile_arn", Resource: "databricks_instance_profile"},
},
},
"databricks_sql_dashboard": {
Service: "sql-dashboards",
Name: func(d *schema.ResourceData) string {
Expand Down
6 changes: 5 additions & 1 deletion sql/resource_sql_global_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var (
GlobalSqlConfigResourceID = "global"
)

type confPair struct {
Key string `json:"key"`
Value string `json:"value"`
Expand Down Expand Up @@ -108,7 +112,7 @@ func ResourceSqlGlobalConfig() *schema.Resource {
if err := NewSqlGlobalConfigAPI(ctx, c).Set(gc); err != nil {
return err
}
d.SetId("global")
d.SetId(GlobalSqlConfigResourceID)
return nil
}
return common.Resource{
Expand Down

0 comments on commit da583cd

Please sign in to comment.