-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature - Add data source for all Tableau users
- Loading branch information
Gary James
authored and
Gary James
committed
Nov 25, 2024
1 parent
187fe3a
commit 7811577
Showing
10 changed files
with
253 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
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
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 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "tableau_users Data Source - terraform-provider-tableau" | ||
subcategory: "" | ||
description: |- | ||
Retrieve user details as a list of users available to read | ||
--- | ||
|
||
# tableau_users (Data Source) | ||
|
||
Retrieve user details as a list of users available to read | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "tableau_users" "example" { | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Read-Only | ||
|
||
- `id` (String) ID of the users | ||
- `users` (Attributes List) List of users and their attributes (see [below for nested schema](#nestedatt--users)) | ||
|
||
<a id="nestedatt--users"></a> | ||
### Nested Schema for `users` | ||
|
||
Read-Only: | ||
|
||
- `email` (String) User email | ||
- `id` (String) ID of the user | ||
- `name` (String) Name for the user | ||
- `site_role` (String) Site role for the user |
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 "tableau_groups" "example" { | ||
} |
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 "tableau_datasources" "example" { | ||
} |
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 "tableau_users" "example" { | ||
} |
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
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
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,115 @@ | ||
package tableau | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
var ( | ||
_ datasource.DataSource = &usersDataSource{} | ||
_ datasource.DataSourceWithConfigure = &usersDataSource{} | ||
) | ||
|
||
func UsersDataSource() datasource.DataSource { | ||
return &usersDataSource{} | ||
} | ||
|
||
type usersDataSource struct { | ||
client *Client | ||
} | ||
|
||
type usersNestedDataModel struct { | ||
ID types.String `tfsdk:"id"` | ||
Name types.String `tfsdk:"name"` | ||
Email types.String `tfsdk:"email"` | ||
SiteRole types.String `tfsdk:"site_role"` | ||
} | ||
|
||
type usersDataSourceModel struct { | ||
ID types.String `tfsdk:"id"` | ||
Users []usersNestedDataModel `tfsdk:"users"` | ||
} | ||
|
||
func (d *usersDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_users" | ||
} | ||
|
||
func (d *usersDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Description: "Retrieve user details as a list of users available to read", | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "ID of the users", | ||
}, | ||
"users": schema.ListNestedAttribute{ | ||
Description: "List of users and their attributes", | ||
Computed: true, | ||
NestedObject: schema.NestedAttributeObject{ | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "ID of the user", | ||
}, | ||
"name": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "Name for the user", | ||
}, | ||
"email": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "User email", | ||
}, | ||
"site_role": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "Site role for the user", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *usersDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var state usersDataSourceModel | ||
|
||
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...) | ||
|
||
users, err := d.client.GetUsers() | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Unable to Read Tableau Users", | ||
err.Error(), | ||
) | ||
return | ||
} | ||
|
||
for _, user := range users { | ||
userDataSourceModel := usersNestedDataModel{ | ||
ID: types.StringValue(user.ID), | ||
Name: types.StringValue(user.Name), | ||
Email: types.StringValue(user.Email), | ||
SiteRole: types.StringValue(user.SiteRole), | ||
} | ||
state.Users = append(state.Users, userDataSourceModel) | ||
} | ||
|
||
state.ID = types.StringValue("allUsers") | ||
|
||
diags := resp.State.Set(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} | ||
|
||
func (d *usersDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
d.client = req.ProviderData.(*Client) | ||
} |
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,24 @@ | ||
package tableau | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccUsersDataSource(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: providerConfig + ` | ||
data "tableau_users" "test" { | ||
}`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.tableau_users.test", "id"), | ||
resource.TestCheckResourceAttrSet("data.tableau_users.test", "users.#"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |