Skip to content

Commit e461489

Browse files
Migrate group users data source (#391)
1 parent 9aa8baa commit e461489

8 files changed

Lines changed: 187 additions & 98 deletions

File tree

docs/data-sources/group_users.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
page_title: "dbtcloud_group_users Data Source - dbtcloud"
44
subcategory: ""
55
description: |-
6-
Returns a list of users assigned to a specific dbt Cloud group
6+
Databricks credential data source
77
---
88

99
# dbtcloud_group_users (Data Source)
1010

11-
Returns a list of users assigned to a specific dbt Cloud group
11+
Databricks credential data source
1212

1313
## Example Usage
1414

@@ -27,13 +27,13 @@ data "dbtcloud_group_users" "my_group_users" {
2727

2828
### Read-Only
2929

30-
- `id` (String) The ID of this resource.
31-
- `users` (Set of Object) List of users (map of ID and email) in the group (see [below for nested schema](#nestedatt--users))
30+
- `id` (String) The ID of this resource. Contains the project ID and the credential ID.
31+
- `users` (Attributes Set) List of users (map of ID and email) in the group (see [below for nested schema](#nestedatt--users))
3232

3333
<a id="nestedatt--users"></a>
3434
### Nested Schema for `users`
3535

36-
Read-Only:
36+
Required:
3737

38-
- `email` (String)
39-
- `id` (Number)
38+
- `email` (String) Email of the user
39+
- `id` (Number) ID of the user
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package group_users
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
8+
"github.com/hashicorp/terraform-plugin-framework/datasource"
9+
"github.com/hashicorp/terraform-plugin-framework/types"
10+
)
11+
12+
// Ensure the implementation satisfies the expected interfaces.
13+
var (
14+
_ datasource.DataSource = &groupUsersDataSource{}
15+
_ datasource.DataSourceWithConfigure = &groupUsersDataSource{}
16+
)
17+
18+
// GroupUsersDataSource is a helper function to simplify the provider implementation.
19+
func GroupUsersDataSource() datasource.DataSource {
20+
return &groupUsersDataSource{}
21+
}
22+
23+
// groupUsersDataSource is the data source implementation.
24+
type groupUsersDataSource struct {
25+
client *dbt_cloud.Client
26+
}
27+
28+
// Configure adds the provider configured client to the data source.
29+
func (d *groupUsersDataSource) Configure(
30+
ctx context.Context,
31+
req datasource.ConfigureRequest,
32+
resp *datasource.ConfigureResponse,
33+
) {
34+
if req.ProviderData == nil {
35+
return
36+
}
37+
38+
client, ok := req.ProviderData.(*dbt_cloud.Client)
39+
if !ok {
40+
resp.Diagnostics.AddError(
41+
"Unexpected Data Source Configure Type",
42+
fmt.Sprintf(
43+
"Expected *dbt_cloud.Client, got: %T. Please report this issue to the provider developers.",
44+
req.ProviderData,
45+
),
46+
)
47+
return
48+
}
49+
50+
d.client = client
51+
}
52+
53+
// Metadata returns the data source type name.
54+
func (d *groupUsersDataSource) Metadata(
55+
ctx context.Context,
56+
req datasource.MetadataRequest,
57+
resp *datasource.MetadataResponse,
58+
) {
59+
resp.TypeName = req.ProviderTypeName + "_group_users"
60+
}
61+
62+
// Schema defines the schema for the data source.
63+
func (d *groupUsersDataSource) Schema(
64+
ctx context.Context,
65+
req datasource.SchemaRequest,
66+
resp *datasource.SchemaResponse,
67+
) {
68+
resp.Schema = dataSourceSchema
69+
}
70+
71+
// Read refreshes the Terraform state with the latest data.
72+
func (d *groupUsersDataSource) Read(
73+
ctx context.Context,
74+
req datasource.ReadRequest,
75+
resp *datasource.ReadResponse,
76+
) {
77+
var state GroupUsersDataSourceModel
78+
diags := req.Config.Get(ctx, &state)
79+
resp.Diagnostics.Append(diags...)
80+
if resp.Diagnostics.HasError() {
81+
return
82+
}
83+
84+
groupID := int(state.GroupID.ValueInt64())
85+
86+
users, err := d.client.GetUsers()
87+
if err != nil {
88+
resp.Diagnostics.AddError(
89+
"Error reading users",
90+
"Could not read users "+state.ID.ValueString()+": "+err.Error(),
91+
)
92+
return
93+
}
94+
95+
usersModels := []userDataSourceModel{}
96+
for _, user := range users {
97+
userGroups := user.Permissions[0].Groups
98+
99+
userInGroup := false
100+
for _, userGroup := range userGroups {
101+
if userGroup.ID == groupID {
102+
userInGroup = true
103+
// we can stop looping
104+
break
105+
}
106+
}
107+
108+
if userInGroup {
109+
userModel := userDataSourceModel{
110+
ID: types.Int64Value(int64(user.ID)),
111+
Email: types.StringValue(user.Email),
112+
}
113+
usersModels = append(usersModels, userModel)
114+
}
115+
}
116+
117+
state.Users = usersModels
118+
119+
// Set state
120+
diags = resp.State.Set(ctx, &state)
121+
resp.Diagnostics.Append(diags...)
122+
if resp.Diagnostics.HasError() {
123+
return
124+
}
125+
}

pkg/sdkv2/data_sources/group_users_acceptance_test.go renamed to pkg/framework/objects/group_users/data_source_acceptance_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package data_sources_test
1+
package group_users_test
22

33
import (
44
"fmt"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package group_users
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework/types"
5+
)
6+
7+
// GroupUsersDataSourceModel is the model for the resource
8+
type GroupUsersDataSourceModel struct {
9+
ID types.String `tfsdk:"id"`
10+
GroupID types.Int64 `tfsdk:"group_id"`
11+
Users []userDataSourceModel `tfsdk:"users"`
12+
}
13+
14+
type userDataSourceModel struct {
15+
ID types.Int64 `tfsdk:"id"`
16+
Email types.String `tfsdk:"email"`
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package group_users
2+
3+
import (
4+
datasource_schema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
5+
)
6+
7+
var dataSourceSchema = datasource_schema.Schema{
8+
Description: "Databricks credential data source",
9+
Attributes: map[string]datasource_schema.Attribute{
10+
"id": datasource_schema.StringAttribute{
11+
Description: "The ID of this resource. Contains the project ID and the credential ID.",
12+
Computed: true,
13+
},
14+
"group_id": datasource_schema.Int64Attribute{
15+
Description: "ID of the group",
16+
Required: true,
17+
},
18+
"users": datasource_schema.SetNestedAttribute{
19+
Computed: true,
20+
Description: "List of users (map of ID and email) in the group",
21+
NestedObject: datasource_schema.NestedAttributeObject{
22+
Attributes: map[string]datasource_schema.Attribute{
23+
"id": datasource_schema.Int64Attribute{
24+
Description: "ID of the user",
25+
Required: true,
26+
},
27+
"email": datasource_schema.StringAttribute{
28+
Description: "Email of the user",
29+
Required: true,
30+
},
31+
},
32+
},
33+
},
34+
},
35+
}

pkg/provider/framework_provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/objects/environment_variable"
99
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/objects/environment_variable_job_override"
1010
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/objects/extended_attributes"
11+
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/objects/group_users"
1112
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/objects/privatelink_endpoint"
1213
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/objects/snowflake_credential"
1314
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/objects/teradata_credential"
@@ -232,6 +233,7 @@ func (p *dbtCloudProvider) DataSources(_ context.Context) []func() datasource.Da
232233
environment_variable.EnvironmentVariableDataSource,
233234
project.ProjectDataSource,
234235
privatelink_endpoint.PrivatelinkEndpointDataSource,
236+
group_users.GroupUsersDataSource,
235237
}
236238
}
237239

pkg/provider/sdk_provider.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ func SDKProvider(version string) func() *schema.Provider {
3737
},
3838
DataSourcesMap: map[string]*schema.Resource{
3939
"dbtcloud_connection": data_sources.DatasourceConnection(),
40-
"dbtcloud_group_users": data_sources.DatasourceGroupUsers(),
4140
},
4241
ResourcesMap: map[string]*schema.Resource{
4342
"dbtcloud_connection": resources.ResourceConnection(),

pkg/sdkv2/data_sources/group_users.go

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)