Skip to content

Commit

Permalink
feature - Add data source for all Tableau groups
Browse files Browse the repository at this point in the history
  • Loading branch information
Gary James authored and Gary James committed Nov 25, 2024
1 parent ef45925 commit 187fe3a
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/data-sources/datasources.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Retrieve datasource details as a list of datasources available to read
### Read-Only

- `datasources` (Attributes List) List of datasources and their attributes (see [below for nested schema](#nestedatt--datasources))
- `id` (String) ID of the project
- `id` (String) ID of the list of datasources

<a id="nestedatt--datasources"></a>
### Nested Schema for `datasources`
Expand Down
30 changes: 30 additions & 0 deletions docs/data-sources/groups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "tableau_groups Data Source - terraform-provider-tableau"
subcategory: ""
description: |-
Retrieve groups details
---

# tableau_groups (Data Source)

Retrieve groups details



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

### Read-Only

- `groups` (Attributes List) List of groups and their attributes (see [below for nested schema](#nestedatt--groups))
- `id` (String) ID of the list of groups

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

Read-Only:

- `id` (String) ID of the group
- `minimum_site_role` (String) Minimum site role for the groups
- `name` (String) Name for the groups
2 changes: 1 addition & 1 deletion docs/data-sources/projects.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Retrieve project details as a list of projects available to read

### Read-Only

- `id` (String) ID of the project
- `id` (String) ID of the list of projects
- `projects` (Attributes List) List of projects and their attributes (see [below for nested schema](#nestedatt--projects))

<a id="nestedatt--projects"></a>
Expand Down
4 changes: 2 additions & 2 deletions tableau/datasources_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (d *datasourcesDataSource) Schema(_ context.Context, _ datasource.SchemaReq
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "ID of the project",
Description: "ID of the list of datasources",
},
"datasources": schema.ListNestedAttribute{
Description: "List of datasources and their attributes",
Expand Down Expand Up @@ -86,7 +86,7 @@ func (d *datasourcesDataSource) Read(ctx context.Context, req datasource.ReadReq
datasources, err := d.client.GetDatasources()
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read Tableau Datasource",
"Unable to Read Tableau Datasources",
err.Error(),
)
return
Expand Down
51 changes: 51 additions & 0 deletions tableau/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,57 @@ type GroupListResponse struct {
Pagination PaginationDetails `json:"pagination"`
}

func (c *Client) GetGroups() ([]Group, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/groups", c.ApiUrl), nil)
if err != nil {
return nil, err
}

body, err := c.doRequest(req)
if err != nil {
return nil, err
}

groupListResponse := GroupListResponse{}
err = json.Unmarshal(body, &groupListResponse)
if err != nil {
return nil, err
}

// TODO: Generalise pagination handling and use elsewhere
pageNumber, totalPageCount, totalAvailable, err := GetPaginationNumbers(groupListResponse.Pagination)
if err != nil {
return nil, err
}

allGroups := make([]Group, 0, totalAvailable)
for _, group := range groupListResponse.GroupsResponse.Groups {
allGroups = append(allGroups, group)
}

for page := pageNumber + 1; page <= totalPageCount; page++ {
fmt.Printf("Searching page %d", page)
req, err = http.NewRequest("GET", fmt.Sprintf("%s/groups?pageNumber=%s", c.ApiUrl, strconv.Itoa(page)), nil)
if err != nil {
return nil, err
}
body, err = c.doRequest(req)
if err != nil {
return nil, err
}
groupListResponse = GroupListResponse{}
err = json.Unmarshal(body, &groupListResponse)
if err != nil {
return nil, err
}
for _, group := range groupListResponse.GroupsResponse.Groups {
allGroups = append(allGroups, group)
}
}

return allGroups, nil
}

func (c *Client) GetGroup(groupID string) (*Group, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/groups", c.ApiUrl), nil)
if err != nil {
Expand Down
109 changes: 109 additions & 0 deletions tableau/groups_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
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 = &groupsDataSource{}
_ datasource.DataSourceWithConfigure = &groupsDataSource{}
)

func GroupsDataSource() datasource.DataSource {
return &groupsDataSource{}
}

type groupsDataSource struct {
client *Client
}

type groupsNestedDataModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
MinimumSiteRole types.String `tfsdk:"minimum_site_role"`
}

type groupsDataSourceModel struct {
ID types.String `tfsdk:"id"`
Groups []groupsNestedDataModel `tfsdk:"groups"`
}

func (d *groupsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_groups"
}

func (d *groupsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Retrieve groups details",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "ID of the list of groups",
},
"groups": schema.ListNestedAttribute{
Description: "List of groups and their attributes",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "ID of the group",
},
"name": schema.StringAttribute{
Computed: true,
Description: "Name for the groups",
},
"minimum_site_role": schema.StringAttribute{
Computed: true,
Description: "Minimum site role for the groups",
},
},
},
},
},
}
}

func (d *groupsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state groupsDataSourceModel

resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)

groups, err := d.client.GetGroups()
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read Tableau Groups",
err.Error(),
)
return
}

for _, group := range groups {
groupDataSourceModel := groupsNestedDataModel{
ID: types.StringValue(group.ID),
Name: types.StringValue(group.Name),
MinimumSiteRole: types.StringValue(group.MinimumSiteRole),
}
state.Groups = append(state.Groups, groupDataSourceModel)
}

state.ID = types.StringValue("allGroups")

diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

func (d *groupsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

d.client = req.ProviderData.(*Client)
}
24 changes: 24 additions & 0 deletions tableau/groups_data_source_test.go
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 TestAccGroupsDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: providerConfig + `
data "tableau_groups" "test" {
}`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.tableau_groups.test", "id"),
resource.TestCheckResourceAttrSet("data.tableau_groups.test", "groups.#"),
),
},
},
})
}
2 changes: 1 addition & 1 deletion tableau/projects_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (d *projectsDataSource) Schema(_ context.Context, _ datasource.SchemaReques
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "ID of the project",
Description: "ID of the list of projects",
},
"projects": schema.ListNestedAttribute{
Description: "List of projects and their attributes",
Expand Down
1 change: 1 addition & 0 deletions tableau/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ func (p *tableauProvider) Configure(ctx context.Context, req provider.ConfigureR
func (p *tableauProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
GroupDataSource,
GroupsDataSource,
UserDataSource,
ProjectDataSource,
ProjectsDataSource,
Expand Down

0 comments on commit 187fe3a

Please sign in to comment.