Skip to content

Commit 40d08fa

Browse files
authored
Added databricks_instance_profiles data source (#2891)
* Added `databricks_instance_profiles` data source * Fix typo in provider map * Add depends_on to integration test * Add 10s wait to integration test * Remove wait & resource creation in acceptance test
1 parent e021b55 commit 40d08fa

File tree

5 files changed

+221
-0
lines changed

5 files changed

+221
-0
lines changed

aws/data_instance_profiles.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package aws
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/databricks/databricks-sdk-go"
8+
"github.com/databricks/terraform-provider-databricks/common"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
func DataSourceInstanceProfiles() *schema.Resource {
13+
type instanceProfileData struct {
14+
Name string `json:"name,omitempty" tf:"computed"`
15+
Arn string `json:"arn,omitempty" tf:"computed"`
16+
RoleArn string `json:"role_arn,omitempty" tf:"computed"`
17+
IsMeta bool `json:"is_meta,omitempty" tf:"computed"`
18+
}
19+
return common.WorkspaceData(func(ctx context.Context, data *struct {
20+
InstanceProfiles []instanceProfileData `json:"instance_profiles,omitempty" tf:"computed"`
21+
}, w *databricks.WorkspaceClient) error {
22+
instanceProfiles, err := w.InstanceProfiles.ListAll(ctx)
23+
if err != nil {
24+
return err
25+
}
26+
for _, v := range instanceProfiles {
27+
arnSlices := strings.Split(v.InstanceProfileArn, "/")
28+
name := arnSlices[len(arnSlices)-1]
29+
data.InstanceProfiles = append(data.InstanceProfiles, instanceProfileData{
30+
Name: name,
31+
Arn: v.InstanceProfileArn,
32+
RoleArn: v.IamRoleArn,
33+
IsMeta: v.IsMetaInstanceProfile,
34+
})
35+
}
36+
return nil
37+
})
38+
}

aws/data_instance_profiles_test.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package aws
2+
3+
import (
4+
"testing"
5+
6+
"github.com/databricks/databricks-sdk-go/service/compute"
7+
"github.com/databricks/terraform-provider-databricks/qa"
8+
)
9+
10+
func TestInstanceProfilesData(t *testing.T) {
11+
qa.ResourceFixture{
12+
Fixtures: []qa.HTTPFixture{
13+
{
14+
Method: "GET",
15+
Resource: "/api/2.0/instance-profiles/list",
16+
Response: compute.ListInstanceProfilesResponse{
17+
InstanceProfiles: []compute.InstanceProfile{
18+
{
19+
IamRoleArn: "arn:aws:iam::123456789012:role/S3Access",
20+
InstanceProfileArn: "arn:aws:iam::123456789012:instance-profile/S3Access",
21+
IsMetaInstanceProfile: true,
22+
},
23+
{
24+
IamRoleArn: "arn:aws:iam::123456789098:role/KMSAccess",
25+
InstanceProfileArn: "arn:aws:iam::123456789098:instance-profile/KMSAccess",
26+
IsMetaInstanceProfile: false,
27+
},
28+
{
29+
InstanceProfileArn: "arn:aws:iam::123456789098:instance-profile/different",
30+
IamRoleArn: "arn:aws:iam::123456789098:role/value",
31+
IsMetaInstanceProfile: false,
32+
},
33+
},
34+
},
35+
},
36+
},
37+
Resource: DataSourceInstanceProfiles(),
38+
Read: true,
39+
NonWritable: true,
40+
ID: "_",
41+
}.ApplyAndExpectData(t, map[string]any{
42+
"instance_profiles": []interface{}{
43+
map[string]interface{}{
44+
"name": "S3Access",
45+
"arn": "arn:aws:iam::123456789012:instance-profile/S3Access",
46+
"role_arn": "arn:aws:iam::123456789012:role/S3Access",
47+
"is_meta": true,
48+
},
49+
map[string]interface{}{
50+
"name": "KMSAccess",
51+
"arn": "arn:aws:iam::123456789098:instance-profile/KMSAccess",
52+
"role_arn": "arn:aws:iam::123456789098:role/KMSAccess",
53+
"is_meta": false,
54+
},
55+
map[string]interface{}{
56+
"name": "different",
57+
"arn": "arn:aws:iam::123456789098:instance-profile/different",
58+
"role_arn": "arn:aws:iam::123456789098:role/value",
59+
"is_meta": false,
60+
},
61+
},
62+
})
63+
}
64+
65+
func TestInstanceProfilesDataEmpty(t *testing.T) {
66+
qa.ResourceFixture{
67+
Fixtures: []qa.HTTPFixture{
68+
{
69+
Method: "GET",
70+
Resource: "/api/2.0/instance-profiles/list",
71+
Response: compute.ListInstanceProfilesResponse{
72+
InstanceProfiles: []compute.InstanceProfile{},
73+
},
74+
},
75+
},
76+
Resource: DataSourceInstanceProfiles(),
77+
Read: true,
78+
NonWritable: true,
79+
ID: "_",
80+
}.ApplyAndExpectData(t, map[string]any{
81+
"instance_profiles": []interface{}{},
82+
})
83+
}
84+
85+
func TestInstanceProfilesDataDuplicate(t *testing.T) {
86+
qa.ResourceFixture{
87+
Fixtures: []qa.HTTPFixture{
88+
{
89+
Method: "GET",
90+
Resource: "/api/2.0/instance-profiles/list",
91+
Response: compute.ListInstanceProfilesResponse{
92+
InstanceProfiles: []compute.InstanceProfile{
93+
{
94+
IamRoleArn: "arn:aws:iam::123456789012:role/S3Access",
95+
InstanceProfileArn: "arn:aws:iam::123456789012:instance-profile/S3Access",
96+
IsMetaInstanceProfile: true,
97+
},
98+
{
99+
IamRoleArn: "arn:aws:iam::123456789012:role/S3Access",
100+
InstanceProfileArn: "arn:aws:iam::123456789012:instance-profile/S3Access",
101+
IsMetaInstanceProfile: true,
102+
},
103+
},
104+
},
105+
},
106+
},
107+
Resource: DataSourceInstanceProfiles(),
108+
Read: true,
109+
NonWritable: true,
110+
ID: "_",
111+
}.ApplyAndExpectData(t, map[string]any{
112+
"instance_profiles": []interface{}{
113+
map[string]interface{}{
114+
"name": "S3Access",
115+
"arn": "arn:aws:iam::123456789012:instance-profile/S3Access",
116+
"role_arn": "arn:aws:iam::123456789012:role/S3Access",
117+
"is_meta": true,
118+
},
119+
map[string]interface{}{
120+
"name": "S3Access",
121+
"arn": "arn:aws:iam::123456789012:instance-profile/S3Access",
122+
"role_arn": "arn:aws:iam::123456789012:role/S3Access",
123+
"is_meta": true,
124+
},
125+
},
126+
})
127+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
subcategory: "Deployment"
3+
---
4+
5+
# databricks_instance_profiles Data Source
6+
7+
Lists all available [databricks_instance_profiles](../resources/instance_profile.md).
8+
9+
## Example Usage
10+
11+
Get all instance profiles:
12+
13+
```hcl
14+
data "databricks_instance_profiles" "all" {
15+
}
16+
17+
output "all_instance_profiles" {
18+
value = data.databricks_instance_profiles.all.instance_profiles
19+
}
20+
```
21+
22+
## Argument Reference
23+
24+
There are no arguments available for this data source.
25+
26+
## Attribute Reference
27+
28+
This data source exports the following attributes:
29+
* `instance_profiles` - Set of objects for a [databricks_instance_profile](../resources/instance_profile.md). This contains the following attributes:
30+
* `name` - Name of the instance profile.
31+
* `arn` - ARN of the instance profile.
32+
* `role_arn` - ARN of the role attached to the instance profile.
33+
* `is_meta` - Whether the instance profile is a meta instance profile or not.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package acceptance
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestAccDataSourceInstanceProfiles(t *testing.T) {
8+
GetEnvOrSkipTest(t, "TEST_EC2_INSTANCE_PROFILE")
9+
workspaceLevel(t, step{
10+
Template: `
11+
data "databricks_instance_profiles" "this" {
12+
}
13+
14+
output "instance_profiles" {
15+
value = data.databricks_instance_profiles.this.instance_profiles
16+
}
17+
18+
output "instance_profile" {
19+
value = data.databricks_instance_profiles.this.instance_profiles[0]
20+
}`,
21+
})
22+
}

provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func DatabricksProvider() *schema.Provider {
6868
"databricks_directory": workspace.DataSourceDirectory(),
6969
"databricks_group": scim.DataSourceGroup(),
7070
"databricks_instance_pool": pools.DataSourceInstancePool(),
71+
"databricks_instance_profiles": aws.DataSourceInstanceProfiles(),
7172
"databricks_jobs": jobs.DataSourceJobs(),
7273
"databricks_job": jobs.DataSourceJob(),
7374
"databricks_metastore": catalog.DataSourceMetastore(),

0 commit comments

Comments
 (0)