-
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.
feature - Add tableau_workbook_revisions as data source (#42)
* Adds tableau_workbook_revisions as data source * Update tableau/workbook_revisions_data_source.go Co-authored-by: Gary James <[email protected]> * Update tableau/workbook_revisions_data_source.go Co-authored-by: Gary James <[email protected]> * Update tableau/workbook_revisions_data_source.go Co-authored-by: Gary James <[email protected]> --------- Co-authored-by: Gary James <[email protected]>
- Loading branch information
Showing
5 changed files
with
241 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,41 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "tableau_workbook_revisions Data Source - terraform-provider-tableau" | ||
subcategory: "" | ||
description: |- | ||
Retrieve virtual connection revisions details | ||
--- | ||
|
||
# tableau_workbook_revisions (Data Source) | ||
|
||
Retrieve virtual connection revisions details | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "tableau_workbook_revisions" "example" { | ||
id = data.tableau_workbooks.wb[0].id | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `id` (String) ID of the virtual connections | ||
|
||
### Read-Only | ||
|
||
- `revisions` (Attributes List) List database connections of virtual connection and their attributes (see [below for nested schema](#nestedatt--revisions)) | ||
|
||
<a id="nestedatt--revisions"></a> | ||
### Nested Schema for `revisions` | ||
|
||
Read-Only: | ||
|
||
- `current` (Boolean) Current revision | ||
- `deleted` (Boolean) Deleted revision | ||
- `published_at` (String) Published at given date | ||
- `publisher_id` (String) ID of the user | ||
- `revision_number` (String) Revision number |
3 changes: 3 additions & 0 deletions
3
examples/data-sources/tableau_workbook_revisions/data-source.tf
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,3 @@ | ||
data "tableau_workbook_revisions" "example" { | ||
id = data.tableau_workbooks.wb[0].id | ||
} |
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,78 @@ | ||
package tableau | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
type WorkbookRevision struct { | ||
WorkbookID string | ||
Current bool `json:"current,omitempty"` | ||
Deleted bool `json:"deleted,omitempty"` | ||
PublishedAt string `json:"publishedAt,omitempty"` | ||
RevisionNumber string `json:"revisionNumber,omitempty"` | ||
Publisher struct { | ||
ID string `json:"id,omitempty"` | ||
// Name string `json:"name,omitempty"` | ||
} `json:"publisher,omitempty"` | ||
} | ||
|
||
type WorkbookRevisionRequest struct { | ||
WorkbookRevision WorkbookRevision `json:"workbookRevisions"` | ||
} | ||
|
||
type WorkbookRevisionsResponse struct { | ||
WorkbookRevisions []WorkbookRevision `json:"revision"` | ||
} | ||
|
||
type WorkbookRevisionListResponse struct { | ||
WorkbookRevisionsResponse WorkbookRevisionsResponse `json:"revisions"` | ||
Pagination PaginationDetails `json:"pagination"` | ||
} | ||
|
||
func (c *Client) GetWorkbookRevisions(workbookID string) ([]WorkbookRevision, error) { | ||
req, err := http.NewRequest("GET", fmt.Sprintf("%s/workbooks/%s/revisions", c.ApiUrl, workbookID), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := c.doRequest(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
workbookRevisionsListResponse := WorkbookRevisionListResponse{} | ||
err = json.Unmarshal(body, &workbookRevisionsListResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
pageNumber, totalPageCount, totalAvailable, err := GetPaginationNumbers(workbookRevisionsListResponse.Pagination) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
allWorkbookRevisions := make([]WorkbookRevision, 0, totalAvailable) | ||
allWorkbookRevisions = append(allWorkbookRevisions, workbookRevisionsListResponse.WorkbookRevisionsResponse.WorkbookRevisions...) | ||
for page := pageNumber + 1; page <= totalPageCount; page++ { | ||
fmt.Printf("Searching page %d", page) | ||
req, err = http.NewRequest("GET", fmt.Sprintf("%s/workbooks/%s/revisions?pageNumber=%s", c.ApiUrl, workbookID, strconv.Itoa(page)), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
body, err = c.doRequest(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
workbookRevisionsListResponse = WorkbookRevisionListResponse{} | ||
err = json.Unmarshal(body, &workbookRevisionsListResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
allWorkbookRevisions = append(allWorkbookRevisions, workbookRevisionsListResponse.WorkbookRevisionsResponse.WorkbookRevisions...) | ||
} | ||
for idx := range allWorkbookRevisions { | ||
allWorkbookRevisions[idx].WorkbookID = workbookID | ||
} | ||
return allWorkbookRevisions, 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,118 @@ | ||
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 = &workbookRevisionsDataSource{} | ||
_ datasource.DataSourceWithConfigure = &workbookRevisionsDataSource{} | ||
) | ||
|
||
func WorkbookRevisionsDataSource() datasource.DataSource { | ||
return &workbookRevisionsDataSource{} | ||
} | ||
|
||
type workbookRevisionsDataSource struct { | ||
client *Client | ||
} | ||
|
||
type workbookRevisionNestedDataModel struct { | ||
PublisherID types.String `tfsdk:"publisher_id"` | ||
Current types.Bool `tfsdk:"current"` | ||
Deleted types.Bool `tfsdk:"deleted"` | ||
PublishedAt types.String `tfsdk:"published_at"` | ||
RevisionNumber types.String `tfsdk:"revision_number"` | ||
} | ||
|
||
type workbookRevisionsDataSourceModel struct { | ||
ID types.String `tfsdk:"id"` | ||
Revisions []workbookRevisionNestedDataModel `tfsdk:"revisions"` | ||
} | ||
|
||
func (d *workbookRevisionsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_workbook_revisions" | ||
} | ||
|
||
func (d *workbookRevisionsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Description: "Retrieve workbook revisions details", | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Required: true, | ||
Description: "ID of the workbook", | ||
}, | ||
"revisions": schema.ListNestedAttribute{ | ||
Description: "List workbook revisions and their attributes", | ||
Computed: true, | ||
NestedObject: schema.NestedAttributeObject{ | ||
Attributes: map[string]schema.Attribute{ | ||
"publisher_id": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "ID of the user", | ||
}, | ||
"current": schema.BoolAttribute{ | ||
Computed: true, | ||
Description: "Current revision", | ||
}, | ||
"deleted": schema.BoolAttribute{ | ||
Computed: true, | ||
Description: "Deleted revision", | ||
}, | ||
"published_at": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "Published at given date", | ||
}, | ||
"revision_number": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "Revision number", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *workbookRevisionsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var state workbookRevisionsDataSourceModel | ||
|
||
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...) | ||
|
||
revisions, err := d.client.GetWorkbookRevisions(state.ID.ValueString()) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Unable to Read Tableau Workbook Revisions", | ||
err.Error(), | ||
) | ||
return | ||
} | ||
for _, revision := range revisions { | ||
workbookRevision := workbookRevisionNestedDataModel{ | ||
PublisherID: types.StringValue(revision.Publisher.ID), | ||
Current: types.BoolValue(revision.Current), | ||
Deleted: types.BoolValue(revision.Deleted), | ||
PublishedAt: types.StringValue(revision.PublishedAt), | ||
RevisionNumber: types.StringValue(revision.RevisionNumber), | ||
} | ||
state.Revisions = append(state.Revisions, workbookRevision) | ||
} | ||
|
||
diags := resp.State.Set(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} | ||
|
||
func (d *workbookRevisionsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
d.client = req.ProviderData.(*Client) | ||
} |