forked from DevotedHealth/terraform-provider-looker
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added looker_users data source (#51)
* add looker_users data source * set unique id from user Emails * add test code
- Loading branch information
1 parent
5c82409
commit 8076168
Showing
5 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
data "looker_users" "looker_users" { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" { | ||
} | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters