Skip to content

Commit 319826a

Browse files
committed
Fix cluster policy configuration exporter bug
1 parent ea58f99 commit 319826a

File tree

5 files changed

+88
-5
lines changed

5 files changed

+88
-5
lines changed

exporter/exporter_test.go

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,23 @@ func TestImportingJobs_JobList(t *testing.T) {
679679
Resource: "/api/2.0/permissions/cluster-policies/123",
680680
Response: getJSONObject("test-data/get-cluster-policy-permissions.json"),
681681
},
682-
682+
{
683+
Method: "GET",
684+
Resource: "/api/2.0/instance-profiles/list",
685+
Response: getJSONObject("test-data/list-instance-profiles.json"),
686+
},
687+
{
688+
Method: "GET",
689+
Resource: "/api/2.0/instance-pools/get?instance_pool_id=pool1",
690+
ReuseRequest: true,
691+
Response: getJSONObject("test-data/get-instance-pool1.json"),
692+
},
693+
{
694+
Method: "GET",
695+
Resource: "/api/2.0/permissions/instance-pools/pool1",
696+
ReuseRequest: true,
697+
Response: getJSONObject("test-data/get-job-14-permissions.json"),
698+
},
683699
{
684700
Method: "GET",
685701
Resource: "/api/2.0/jobs/runs/list?completed_only=true&job_id=14&limit=1",
@@ -865,3 +881,49 @@ func TestImportingGlobalInitScripts(t *testing.T) {
865881
assert.NoError(t, err)
866882
})
867883
}
884+
885+
func TestImportingUser(t *testing.T) {
886+
qa.HTTPFixturesApply(t,
887+
[]qa.HTTPFixture{
888+
{
889+
Method: "GET",
890+
ReuseRequest: true,
891+
Resource: "/api/2.0/preview/scim/v2/Users?filter=userName%20eq%20%27me%27",
892+
Response: identity.UserList{
893+
Resources: []identity.ScimUser{
894+
{
895+
ID: "123",
896+
UserName: "me",
897+
Groups: []identity.GroupsListItem{
898+
{
899+
Value: "abc",
900+
Type: "direct",
901+
},
902+
},
903+
},
904+
},
905+
},
906+
},
907+
}, func(ctx context.Context, client *common.DatabricksClient) {
908+
ic := newImportContext(client)
909+
err := resourcesMap["databricks_user"].Search(ic, &resource{
910+
Resource: "databricks_user",
911+
Value: "me",
912+
})
913+
assert.NoError(t, err)
914+
915+
d := ic.Resources["databricks_user"].TestResourceData()
916+
d.Set("user_name", "me")
917+
err = resourcesMap["databricks_user"].Import(ic, &resource{
918+
Resource: "databricks_user",
919+
Data: d,
920+
})
921+
assert.NoError(t, err)
922+
})
923+
}
924+
925+
func TestEitherString(t *testing.T) {
926+
assert.Equal(t, "a", eitherString("a", nil))
927+
assert.Equal(t, "a", eitherString(nil, "a"))
928+
assert.Equal(t, "", eitherString(nil, nil))
929+
}

exporter/importables.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,19 +309,19 @@ var resourcesMap map[string]importable = map[string]importable{
309309
for k, policy := range definition {
310310
value, vok := policy["value"]
311311
defaultValue, dok := policy["defaultValue"]
312-
if !vok || !dok {
312+
if !vok && !dok {
313313
continue
314314
}
315315
if k == "aws_attributes.instance_profile_arn" {
316316
ic.Emit(&resource{
317317
Resource: "databricks_instance_profile",
318-
ID: fmt.Sprintf("%s%s", value, defaultValue),
318+
ID: eitherString(value, defaultValue),
319319
})
320320
}
321321
if k == "instance_pool_id" {
322322
ic.Emit(&resource{
323323
Resource: "databricks_instance_pool",
324-
ID: fmt.Sprintf("%s%s", value, defaultValue),
324+
ID: eitherString(value, defaultValue),
325325
})
326326
}
327327
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"created_at_timestamp": 1606308550000,
3-
"definition": "{\"autoscale.max_workers\":{\"defaultValue\":2,\"maxValue\":5,\"type\":\"range\"},\"autoscale.min_workers\":{\"hidden\":true,\"type\":\"fixed\",\"value\":1},\"autotermination_minutes\":{\"defaultValue\":20,\"maxValue\":30,\"type\":\"range\"},\"cluster_log_conf.path\":{\"hidden\":true,\"type\":\"fixed\",\"value\":\"dbfs:/cluster-logs\"},\"cluster_log_conf.type\":{\"hidden\":true,\"type\":\"fixed\",\"value\":\"DBFS\"},\"custom_tags.team\":{\"type\":\"fixed\",\"value\":\"users\"},\"dbus_per_hour\":{\"maxValue\":10,\"type\":\"range\"},\"driver_node_type_id\":{\"defaultValue\":\"Standard_F4s\",\"type\":\"whitelist\",\"values\":[\"Standard_F4s\",\"Standard_F8s\"]},\"node_type_id\":{\"defaultValue\":\"Standard_F4s\",\"type\":\"whitelist\",\"values\":[\"Standard_F4s\",\"Standard_F8s\"]},\"spark_version\":{\"defaultValue\":\"7.3.x-scala2.12\",\"type\":\"whitelist\",\"values\":[\"7.2.x-scala2.12\",\"7.3.x-scala2.12\"]}}",
3+
"definition": "{\"aws_attributes.instance_profile_arn\":{\"hidden\":true,\"type\":\"fixed\",\"value\":\"arn:aws:iam::12345:instance-profile/shard-s3-access\"},\"instance_pool_id\":{\"hidden\":true,\"type\":\"fixed\",\"value\":\"pool1\"},\"autoscale.max_workers\":{\"defaultValue\":2,\"maxValue\":5,\"type\":\"range\"}}",
44
"name": "users cluster policy",
55
"policy_id": "123"
66
}

exporter/util.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,13 @@ func (ic *importContext) getMountsThroughCluster(
245245
err = json.Unmarshal([]byte(lines[0]), &mm)
246246
return
247247
}
248+
249+
func eitherString(a interface{}, b interface{}) string {
250+
if a != nil {
251+
return a.(string)
252+
}
253+
if b != nil {
254+
return b.(string)
255+
}
256+
return ""
257+
}

identity/resource_service_principal_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,17 @@ func TestResourceServicePrincipalCreate_Error(t *testing.T) {
193193
require.Error(t, err, err)
194194
}
195195

196+
func TestResourceServicePrincipalCreate_Error_AzureNoApplicationID(t *testing.T) {
197+
qa.ResourceFixture{
198+
Resource: ResourceServicePrincipal(),
199+
Create: true,
200+
Azure: true,
201+
HCL: `
202+
display_name = "abc"
203+
`,
204+
}.ExpectError(t, "application_id is required for service principals in Azure Databricks")
205+
}
206+
196207
func TestResourceServicePrincipalUpdate(t *testing.T) {
197208
newServicePrincipal := ScimUser{
198209
Schemas: []URN{ServicePrincipalSchema},

0 commit comments

Comments
 (0)