diff --git a/docs/data-sources/users.md b/docs/data-sources/users.md new file mode 100644 index 0000000..8f8fbca --- /dev/null +++ b/docs/data-sources/users.md @@ -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 + +### Read-Only + +- `id` (String) The unique identifier for the resource. +- `users` (List of Object) (see [below for nested schema](#nestedatt--users)) + + +### Nested Schema for `users` + +Read-Only: + +- `email` (String) +- `first_name` (String) +- `id` (String) +- `is_disabled` (Boolean) +- `last_name` (String) + + diff --git a/examples/data-sources/looker_users/data-source.tf b/examples/data-sources/looker_users/data-source.tf new file mode 100644 index 0000000..1f9118b --- /dev/null +++ b/examples/data-sources/looker_users/data-source.tf @@ -0,0 +1,2 @@ +data "looker_users" "looker_users" { +} diff --git a/pkg/looker/data_source_users.go b/pkg/looker/data_source_users.go new file mode 100644 index 0000000..7d48a18 --- /dev/null +++ b/pkg/looker/data_source_users.go @@ -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 +} diff --git a/pkg/looker/data_source_users_test.go b/pkg/looker/data_source_users_test.go new file mode 100644 index 0000000..09f846d --- /dev/null +++ b/pkg/looker/data_source_users_test.go @@ -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" { +} +` +} diff --git a/pkg/looker/provider.go b/pkg/looker/provider.go index 063944d..2b459c1 100644 --- a/pkg/looker/provider.go +++ b/pkg/looker/provider.go @@ -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, }