Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 0.2.13 #210

Merged
merged 6 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ vendor/
terraform-provider*
.idea
.vscode
autogen
autogen
TODO.md
NOTES.md
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@

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

## [Unreleased](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.2.11...HEAD)
## [Unreleased](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.2.13...HEAD)

## [0.2.13](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.2.11...v0.2.13)

## Changes

- Update connections to force new one when the project changes
- Add support for the Datasource dbtcloud_group_users to get the list of users assigned to a given project

## Documentation

- Use d2 for showing the different resources
- Update examples in docs

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

Expand Down
41 changes: 41 additions & 0 deletions docs/data-sources/group_users.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "dbtcloud_group_users Data Source - dbtcloud"
subcategory: ""
description: |-
Returns a list of users assigned to a specific dbt Cloud group
---

# dbtcloud_group_users (Data Source)

Returns a list of users assigned to a specific dbt Cloud group

## Example Usage

```terraform
data "dbtcloud_group_users" "my_group_users" {
group_id = 1234
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `group_id` (Number) ID of the group

### Read-Only

- `id` (String) The ID of this resource.
- `users` (Set of Object) List of users (map of ID and email) in the group (see [below for nested schema](#nestedatt--users))

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

Read-Only:

- `email` (String)
- `id` (Number)


2 changes: 1 addition & 1 deletion docs/guides/99_list_resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ The image below shows the different resources available in the provider and thei

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

[![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)
[![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)
3 changes: 3 additions & 0 deletions examples/data-sources/dbtcloud_group_users/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "dbtcloud_group_users" "my_group_users" {
group_id = 1234
}
2 changes: 1 addition & 1 deletion guides/99_list_resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ The image below shows the different resources available in the provider and thei

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

[![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)
[![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)
89 changes: 89 additions & 0 deletions pkg/data_sources/group_users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package data_sources

import (
"context"
"strconv"

"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var groupUsersSchema = map[string]*schema.Schema{
"group_id": &schema.Schema{
Type: schema.TypeInt,
Required: true,
Description: "ID of the group",
},
"users": &schema.Schema{
Type: schema.TypeSet,
Computed: true,
Description: "List of users (map of ID and email) in the group",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Required: true,
},
"email": {
Type: schema.TypeString,
Required: true,
},
},
},
},
}

func DatasourceGroupUsers() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceGroupUsersRead,
Schema: groupUsersSchema,
Description: "Returns a list of users assigned to a specific dbt Cloud group",
}
}

func datasourceGroupUsersRead(
ctx context.Context,
d *schema.ResourceData,
m interface{},
) diag.Diagnostics {
c := m.(*dbt_cloud.Client)

var diags diag.Diagnostics

groupID := d.Get("group_id").(int)

users, err := c.GetUsers()
if err != nil {
return diag.FromErr(err)
}

usersParams := []map[string]interface{}{}
for _, user := range users {
userGroups := user.Permissions[0].Groups

userInGroup := false
for _, userGroup := range userGroups {
if userGroup.ID == groupID {
userInGroup = true
// we can stop looping
break
}
}

if userInGroup {
userToAdd := map[string]interface{}{}

userToAdd["id"] = user.ID
userToAdd["email"] = user.Email
usersParams = append(usersParams, userToAdd)
}
}
if err := d.Set("users", usersParams); err != nil {
return diag.FromErr(err)
}

d.SetId(strconv.Itoa(groupID))

return diags
}
51 changes: 51 additions & 0 deletions pkg/data_sources/group_users_acceptance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package data_sources_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDbtCloudGroupUsersDataSource(t *testing.T) {

groupName := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

config := group_users(groupName)

check := resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(
"data.dbtcloud_group_users.test_group_users_read",
"group_id",
),
// we check that there is no user in the group as we just created it
resource.TestCheckResourceAttr(
"data.dbtcloud_group_users.test_group_users_read",
"users.#",
"0",
),
)

resource.ParallelTest(t, resource.TestCase{
Providers: providers(),
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
}

func group_users(groupName string) string {
return fmt.Sprintf(`
resource "dbtcloud_group" "test_group" {
name = "%s"
}

data "dbtcloud_group_users" "test_group_users_read" {
group_id = dbtcloud_group.test_group.id
}
`, groupName)
}
35 changes: 32 additions & 3 deletions pkg/dbt_cloud/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import (
type User struct {
ID int `json:"id"`
Email string `json:"email"`
// only the first permission is filled id, it is a list with 1 element
Permissions []struct {
Groups []struct {
ID int `json:"id"`
} `json:"groups"`
} `json:"permissions"`
}

type UserListResponse struct {
Expand All @@ -28,8 +34,12 @@ type CurrentUserResponse struct {
Status ResponseStatus `json:"status"`
}

func (c *Client) GetUser(email string) (*User, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/v2/accounts/%s/users/", c.HostURL, strconv.Itoa(c.AccountID)), nil)
func (c *Client) GetUsers() ([]User, error) {
req, err := http.NewRequest(
"GET",
fmt.Sprintf("%s/v2/accounts/%s/users/", c.HostURL, strconv.Itoa(c.AccountID)),
nil,
)
if err != nil {
return nil, err
}
Expand All @@ -52,7 +62,16 @@ func (c *Client) GetUser(email string) (*User, error) {
numUsers := userListResponse.Extra.Pagination.Count
for numUsers < userListResponse.Extra.Pagination.TotalCount {

req, err := http.NewRequest("GET", fmt.Sprintf("%s/v2/accounts/%s/users/?offset=%d", c.HostURL, strconv.Itoa(c.AccountID), numUsers), nil)
req, err := http.NewRequest(
"GET",
fmt.Sprintf(
"%s/v2/accounts/%s/users/?offset=%d",
c.HostURL,
strconv.Itoa(c.AccountID),
numUsers,
),
nil,
)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -81,6 +100,16 @@ func (c *Client) GetUser(email string) (*User, error) {
}
}

return listAllUsers, nil
}

func (c *Client) GetUser(email string) (*User, error) {

listAllUsers, err := c.GetUsers()
if err != nil {
return nil, err
}

for i, user := range listAllUsers {
if strings.EqualFold(user.Email, email) {
return &listAllUsers[i], nil
Expand Down
15 changes: 11 additions & 4 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ func Provider() *schema.Provider {
Description: "Account identifier for your dbt Cloud implementation",
},
"host_url": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("DBT_CLOUD_HOST_URL", "https://cloud.getdbt.com/api"),
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc(
"DBT_CLOUD_HOST_URL",
"https://cloud.getdbt.com/api",
),
Description: "URL for your dbt Cloud deployment - Defaults to https://cloud.getdbt.com/api",
},
},
Expand All @@ -53,6 +56,7 @@ func Provider() *schema.Provider {
"dbtcloud_notification": data_sources.DatasourceNotification(),
"dbtcloud_user_groups": data_sources.DatasourceUserGroups(),
"dbtcloud_extended_attributes": data_sources.DatasourceExtendedAttributes(),
"dbtcloud_group_users": data_sources.DatasourceGroupUsers(),
// legacy data sources to remove from 0.3
"dbt_cloud_group": data_sources.DatasourceGroup(),
"dbt_cloud_job": data_sources.DatasourceJob(),
Expand Down Expand Up @@ -116,7 +120,10 @@ func Provider() *schema.Provider {
}
}

func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
func providerConfigure(
ctx context.Context,
d *schema.ResourceData,
) (interface{}, diag.Diagnostics) {

token := d.Get("token").(string)
account_id := d.Get("account_id").(int)
Expand Down
Loading