Skip to content

Commit 3dc42c9

Browse files
authored
Merge pull request #210 from dbt-labs/release-0.2.13
Release 0.2.13
2 parents 4093264 + c491433 commit 3dc42c9

17 files changed

+489
-39
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ vendor/
33
terraform-provider*
44
.idea
55
.vscode
6-
autogen
6+
autogen
7+
TODO.md
8+
NOTES.md

CHANGELOG.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,19 @@
22

33
All notable changes to this project will be documented in this file.
44

5-
## [Unreleased](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.2.11...HEAD)
5+
## [Unreleased](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.2.13...HEAD)
6+
7+
## [0.2.13](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.2.11...v0.2.13)
8+
9+
## Changes
10+
11+
- Update connections to force new one when the project changes
12+
- Add support for the Datasource dbtcloud_group_users to get the list of users assigned to a given project
13+
14+
## Documentation
15+
16+
- Use d2 for showing the different resources
17+
- Update examples in docs
618

719
## [0.2.11](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.2.10...v0.2.11)
820

docs/data-sources/group_users.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "dbtcloud_group_users Data Source - dbtcloud"
4+
subcategory: ""
5+
description: |-
6+
Returns a list of users assigned to a specific dbt Cloud group
7+
---
8+
9+
# dbtcloud_group_users (Data Source)
10+
11+
Returns a list of users assigned to a specific dbt Cloud group
12+
13+
## Example Usage
14+
15+
```terraform
16+
data "dbtcloud_group_users" "my_group_users" {
17+
group_id = 1234
18+
}
19+
```
20+
21+
<!-- schema generated by tfplugindocs -->
22+
## Schema
23+
24+
### Required
25+
26+
- `group_id` (Number) ID of the group
27+
28+
### Read-Only
29+
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))
32+
33+
<a id="nestedatt--users"></a>
34+
### Nested Schema for `users`
35+
36+
Read-Only:
37+
38+
- `email` (String)
39+
- `id` (Number)
40+
41+

docs/guides/99_list_resources.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ The image below shows the different resources available in the provider and thei
99

1010
Click on it to be redirected to a larger version of the image.
1111

12-
[![Terraform resources](https://github.com/dbt-labs/terraform-provider-dbtcloud/blob/main/terraform_resources.excalidraw.png?raw=true)](https://github.com/dbt-labs/terraform-provider-dbtcloud/blob/main/terraform_resources.excalidraw.png)
12+
[![Terraform resources](https://github.com/dbt-labs/terraform-provider-dbtcloud/blob/main/terraform_resources.png?raw=true)](https://github.com/dbt-labs/terraform-provider-dbtcloud/blob/main/terraform_resources.png)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data "dbtcloud_group_users" "my_group_users" {
2+
group_id = 1234
3+
}

guides/99_list_resources.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ The image below shows the different resources available in the provider and thei
99

1010
Click on it to be redirected to a larger version of the image.
1111

12-
[![Terraform resources](https://github.com/dbt-labs/terraform-provider-dbtcloud/blob/main/terraform_resources.excalidraw.png?raw=true)](https://github.com/dbt-labs/terraform-provider-dbtcloud/blob/main/terraform_resources.excalidraw.png)
12+
[![Terraform resources](https://github.com/dbt-labs/terraform-provider-dbtcloud/blob/main/terraform_resources.png?raw=true)](https://github.com/dbt-labs/terraform-provider-dbtcloud/blob/main/terraform_resources.png)

pkg/data_sources/group_users.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package data_sources
2+
3+
import (
4+
"context"
5+
"strconv"
6+
7+
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
var groupUsersSchema = map[string]*schema.Schema{
13+
"group_id": &schema.Schema{
14+
Type: schema.TypeInt,
15+
Required: true,
16+
Description: "ID of the group",
17+
},
18+
"users": &schema.Schema{
19+
Type: schema.TypeSet,
20+
Computed: true,
21+
Description: "List of users (map of ID and email) in the group",
22+
Elem: &schema.Resource{
23+
Schema: map[string]*schema.Schema{
24+
"id": {
25+
Type: schema.TypeInt,
26+
Required: true,
27+
},
28+
"email": {
29+
Type: schema.TypeString,
30+
Required: true,
31+
},
32+
},
33+
},
34+
},
35+
}
36+
37+
func DatasourceGroupUsers() *schema.Resource {
38+
return &schema.Resource{
39+
ReadContext: datasourceGroupUsersRead,
40+
Schema: groupUsersSchema,
41+
Description: "Returns a list of users assigned to a specific dbt Cloud group",
42+
}
43+
}
44+
45+
func datasourceGroupUsersRead(
46+
ctx context.Context,
47+
d *schema.ResourceData,
48+
m interface{},
49+
) diag.Diagnostics {
50+
c := m.(*dbt_cloud.Client)
51+
52+
var diags diag.Diagnostics
53+
54+
groupID := d.Get("group_id").(int)
55+
56+
users, err := c.GetUsers()
57+
if err != nil {
58+
return diag.FromErr(err)
59+
}
60+
61+
usersParams := []map[string]interface{}{}
62+
for _, user := range users {
63+
userGroups := user.Permissions[0].Groups
64+
65+
userInGroup := false
66+
for _, userGroup := range userGroups {
67+
if userGroup.ID == groupID {
68+
userInGroup = true
69+
// we can stop looping
70+
break
71+
}
72+
}
73+
74+
if userInGroup {
75+
userToAdd := map[string]interface{}{}
76+
77+
userToAdd["id"] = user.ID
78+
userToAdd["email"] = user.Email
79+
usersParams = append(usersParams, userToAdd)
80+
}
81+
}
82+
if err := d.Set("users", usersParams); err != nil {
83+
return diag.FromErr(err)
84+
}
85+
86+
d.SetId(strconv.Itoa(groupID))
87+
88+
return diags
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package data_sources_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
func TestAccDbtCloudGroupUsersDataSource(t *testing.T) {
12+
13+
groupName := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
14+
15+
config := group_users(groupName)
16+
17+
check := resource.ComposeAggregateTestCheckFunc(
18+
resource.TestCheckResourceAttrSet(
19+
"data.dbtcloud_group_users.test_group_users_read",
20+
"group_id",
21+
),
22+
// we check that there is no user in the group as we just created it
23+
resource.TestCheckResourceAttr(
24+
"data.dbtcloud_group_users.test_group_users_read",
25+
"users.#",
26+
"0",
27+
),
28+
)
29+
30+
resource.ParallelTest(t, resource.TestCase{
31+
Providers: providers(),
32+
Steps: []resource.TestStep{
33+
{
34+
Config: config,
35+
Check: check,
36+
},
37+
},
38+
})
39+
}
40+
41+
func group_users(groupName string) string {
42+
return fmt.Sprintf(`
43+
resource "dbtcloud_group" "test_group" {
44+
name = "%s"
45+
}
46+
47+
data "dbtcloud_group_users" "test_group_users_read" {
48+
group_id = dbtcloud_group.test_group.id
49+
}
50+
`, groupName)
51+
}

pkg/dbt_cloud/user.go

+32-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import (
1111
type User struct {
1212
ID int `json:"id"`
1313
Email string `json:"email"`
14+
// only the first permission is filled id, it is a list with 1 element
15+
Permissions []struct {
16+
Groups []struct {
17+
ID int `json:"id"`
18+
} `json:"groups"`
19+
} `json:"permissions"`
1420
}
1521

1622
type UserListResponse struct {
@@ -28,8 +34,12 @@ type CurrentUserResponse struct {
2834
Status ResponseStatus `json:"status"`
2935
}
3036

31-
func (c *Client) GetUser(email string) (*User, error) {
32-
req, err := http.NewRequest("GET", fmt.Sprintf("%s/v2/accounts/%s/users/", c.HostURL, strconv.Itoa(c.AccountID)), nil)
37+
func (c *Client) GetUsers() ([]User, error) {
38+
req, err := http.NewRequest(
39+
"GET",
40+
fmt.Sprintf("%s/v2/accounts/%s/users/", c.HostURL, strconv.Itoa(c.AccountID)),
41+
nil,
42+
)
3343
if err != nil {
3444
return nil, err
3545
}
@@ -52,7 +62,16 @@ func (c *Client) GetUser(email string) (*User, error) {
5262
numUsers := userListResponse.Extra.Pagination.Count
5363
for numUsers < userListResponse.Extra.Pagination.TotalCount {
5464

55-
req, err := http.NewRequest("GET", fmt.Sprintf("%s/v2/accounts/%s/users/?offset=%d", c.HostURL, strconv.Itoa(c.AccountID), numUsers), nil)
65+
req, err := http.NewRequest(
66+
"GET",
67+
fmt.Sprintf(
68+
"%s/v2/accounts/%s/users/?offset=%d",
69+
c.HostURL,
70+
strconv.Itoa(c.AccountID),
71+
numUsers,
72+
),
73+
nil,
74+
)
5675
if err != nil {
5776
return nil, err
5877
}
@@ -81,6 +100,16 @@ func (c *Client) GetUser(email string) (*User, error) {
81100
}
82101
}
83102

103+
return listAllUsers, nil
104+
}
105+
106+
func (c *Client) GetUser(email string) (*User, error) {
107+
108+
listAllUsers, err := c.GetUsers()
109+
if err != nil {
110+
return nil, err
111+
}
112+
84113
for i, user := range listAllUsers {
85114
if strings.EqualFold(user.Email, email) {
86115
return &listAllUsers[i], nil

pkg/provider/provider.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ func Provider() *schema.Provider {
2727
Description: "Account identifier for your dbt Cloud implementation",
2828
},
2929
"host_url": &schema.Schema{
30-
Type: schema.TypeString,
31-
Optional: true,
32-
DefaultFunc: schema.EnvDefaultFunc("DBT_CLOUD_HOST_URL", "https://cloud.getdbt.com/api"),
30+
Type: schema.TypeString,
31+
Optional: true,
32+
DefaultFunc: schema.EnvDefaultFunc(
33+
"DBT_CLOUD_HOST_URL",
34+
"https://cloud.getdbt.com/api",
35+
),
3336
Description: "URL for your dbt Cloud deployment - Defaults to https://cloud.getdbt.com/api",
3437
},
3538
},
@@ -53,6 +56,7 @@ func Provider() *schema.Provider {
5356
"dbtcloud_notification": data_sources.DatasourceNotification(),
5457
"dbtcloud_user_groups": data_sources.DatasourceUserGroups(),
5558
"dbtcloud_extended_attributes": data_sources.DatasourceExtendedAttributes(),
59+
"dbtcloud_group_users": data_sources.DatasourceGroupUsers(),
5660
// legacy data sources to remove from 0.3
5761
"dbt_cloud_group": data_sources.DatasourceGroup(),
5862
"dbt_cloud_job": data_sources.DatasourceJob(),
@@ -116,7 +120,10 @@ func Provider() *schema.Provider {
116120
}
117121
}
118122

119-
func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
123+
func providerConfigure(
124+
ctx context.Context,
125+
d *schema.ResourceData,
126+
) (interface{}, diag.Diagnostics) {
120127

121128
token := d.Get("token").(string)
122129
account_id := d.Get("account_id").(int)

0 commit comments

Comments
 (0)