Skip to content

Commit

Permalink
Added looker_users data source (#51)
Browse files Browse the repository at this point in the history
* add looker_users data source

* set unique id from user Emails

* add test code
  • Loading branch information
nakamura1878 authored Jul 23, 2024
1 parent 5c82409 commit 8076168
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 0 deletions.
39 changes: 39 additions & 0 deletions docs/data-sources/users.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "looker_users Data Source - terraform-provider-looker"
subcategory: ""
description: |-
---

# looker_users (Data Source)



## Example Usage

```terraform
data "looker_users" "looker_users" {
}
```

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

### Read-Only

- `id` (String) The unique identifier for the resource.
- `users` (List of Object) (see [below for nested schema](#nestedatt--users))

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

Read-Only:

- `email` (String)
- `first_name` (String)
- `id` (String)
- `is_disabled` (Boolean)
- `last_name` (String)


2 changes: 2 additions & 0 deletions examples/data-sources/looker_users/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data "looker_users" "looker_users" {
}
85 changes: 85 additions & 0 deletions pkg/looker/data_source_users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package looker

import (
"context"
"sort"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
apiclient "github.com/looker-open-source/sdk-codegen/go/sdk/v4"
)

func dataSourceUsers() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceUsersRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for the resource.",
},
"users": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"email": {
Type: schema.TypeString,
Computed: true,
},
"first_name": {
Type: schema.TypeString,
Computed: true,
},
"last_name": {
Type: schema.TypeString,
Computed: true,
},
"is_disabled": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
},
}
}

func dataSourceUsersRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*apiclient.LookerSDK)
request := apiclient.RequestAllUsers{}
users, err := client.AllUsers(request, nil)

if err != nil {
return diag.FromErr(err)
}

userList := make([]map[string]interface{}, len(users))
userEmails := make([]string, len(users))
for i, user := range users {
userList[i] = map[string]interface{}{
"id": *user.Id,
"email": *user.Email,
"first_name": *user.FirstName,
"last_name": *user.LastName,
"is_disabled": *user.IsDisabled,
}
userEmails[i] = *user.Email
}

if err := d.Set("users", userList); err != nil {
return diag.FromErr(err)
}

// Generate a hash of the user emails to use as the resource ID
sort.Strings(userEmails)
d.SetId(hash(strings.Join(userEmails, ",")))

return nil
}
36 changes: 36 additions & 0 deletions pkg/looker/data_source_users_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package looker

import (
"testing"

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

func TestAccDataSourceUsers(t *testing.T) {
dataSourceName := "data.looker_users.test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceUsersConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "id"),
resource.TestCheckResourceAttrSet(dataSourceName, "users.#"),
resource.TestCheckResourceAttrSet(dataSourceName, "users.0.id"),
resource.TestCheckResourceAttrSet(dataSourceName, "users.0.email"),
resource.TestCheckResourceAttrSet(dataSourceName, "users.0.first_name"),
resource.TestCheckResourceAttrSet(dataSourceName, "users.0.last_name"),
resource.TestCheckResourceAttrSet(dataSourceName, "users.0.is_disabled"),
),
},
},
})
}

func testAccDataSourceUsersConfig() string {
return `
data "looker_users" "test" {
}
`
}
3 changes: 3 additions & 0 deletions pkg/looker/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func Provider() *schema.Provider {
"looker_connection": resourceConnection(),
"looker_lookml_model": resourceLookMLModel(),
},
DataSourcesMap: map[string]*schema.Resource{
"looker_users": dataSourceUsers(),
},

ConfigureContextFunc: providerConfigure,
}
Expand Down

0 comments on commit 8076168

Please sign in to comment.