-
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_connections as data source (#43)
* Adds tableau_workbook_connections as data source * Update tableau/workbook_connections_data_source.go Co-authored-by: Gary James <[email protected]> * Update tableau/workbook_connections_data_source.go Co-authored-by: Gary James <[email protected]> * Update tableau/workbook_connections_data_source.go Co-authored-by: Gary James <[email protected]> * Update tableau/workbook_connections_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
257 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,46 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "tableau_workbook_connections Data Source - terraform-provider-tableau" | ||
subcategory: "" | ||
description: |- | ||
Retrieve virtual connection connections details | ||
--- | ||
|
||
# tableau_workbook_connections (Data Source) | ||
|
||
Retrieve virtual connection connections details | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "tableau_workbook_connections" "example" { | ||
id = data.tableau_workbooks.wb[0].id | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `id` (String) ID of the virtual connections | ||
|
||
### Read-Only | ||
|
||
- `connections` (Attributes List) List database connections of virtual connection and their attributes (see [below for nested schema](#nestedatt--connections)) | ||
|
||
<a id="nestedatt--connections"></a> | ||
### Nested Schema for `connections` | ||
|
||
Read-Only: | ||
|
||
- `authentication_type` (String) Authentication type | ||
- `datasource_id` (String) ID of datasource | ||
- `embed_password` (Boolean) Embed database password into connection | ||
- `id` (String) ID of the connection in Virtual Connection | ||
- `query_tagging_enabled` (Boolean) Query tagging enabled | ||
- `server_address` (String) Server address | ||
- `server_port` (String) Server port | ||
- `type` (String) Database connection type | ||
- `use_oauth_managed_keychain` (Boolean) Use OAuth managed keychain | ||
- `username` (String) Username |
3 changes: 3 additions & 0 deletions
3
examples/data-sources/tableau_workbook_connections/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_connections" "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,59 @@ | ||
package tableau | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type WorkbookConnection struct { | ||
WorkbookID string | ||
ID string `json:"id,omitempty"` | ||
Type string `json:"type,omitempty"` | ||
ServerAddress string `json:"serverAddress,omitempty"` | ||
ServerPort string `json:"serverPort,omitempty"` | ||
UserName string `json:"userName,omitempty"` | ||
QueryTaggingEnabled bool `json:"query_tagging_enabled,omitempty"` | ||
AuthenticationType string `json:"authenticationType,omitempty"` | ||
EmbedPassword bool `json:"embedPassword,omitempty"` | ||
UseOAuthManagedKeychain bool `json:"useOauthManagedKeychain,omitempty"` | ||
DataSourceID struct { | ||
ID string `json:"id,omitempty"` | ||
// Name string `json:"name,omitempty"` | ||
} `json:"datasource,omitempty"` | ||
} | ||
|
||
type WorkbookConnectionRequest struct { | ||
WorkbookConnection WorkbookConnection `json:"workbookConnections"` | ||
} | ||
|
||
type WorkbookConnectionsResponse struct { | ||
WorkbookConnections []WorkbookConnection `json:"connection"` | ||
} | ||
|
||
type WorkbookConnectionListResponse struct { | ||
WorkbookConnectionsResponse WorkbookConnectionsResponse `json:"connections"` | ||
} | ||
|
||
func (c *Client) GetWorkbookConnections(workbookID string) ([]WorkbookConnection, error) { | ||
req, err := http.NewRequest("GET", fmt.Sprintf("%s/workbooks/%s/connections", c.ApiUrl, workbookID), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := c.doRequest(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
workbookConnectionsListResponse := WorkbookConnectionListResponse{} | ||
err = json.Unmarshal(body, &workbookConnectionsListResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// workbook connections don't seem to have pagination | ||
allWorkbookConnections := workbookConnectionsListResponse.WorkbookConnectionsResponse.WorkbookConnections | ||
for idx := range allWorkbookConnections { | ||
allWorkbookConnections[idx].WorkbookID = workbookID | ||
} | ||
return allWorkbookConnections, 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,148 @@ | ||
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 = &workbookConnectionsDataSource{} | ||
_ datasource.DataSourceWithConfigure = &workbookConnectionsDataSource{} | ||
) | ||
|
||
func WorkbookConnectionsDataSource() datasource.DataSource { | ||
return &workbookConnectionsDataSource{} | ||
} | ||
|
||
type workbookConnectionsDataSource struct { | ||
client *Client | ||
} | ||
|
||
type workbookConnectionNestedDataModel struct { | ||
ID types.String `tfsdk:"id"` | ||
Type types.String `tfsdk:"type"` | ||
DatasourceID types.String `tfsdk:"datasource_id"` | ||
ServerAddress types.String `tfsdk:"server_address"` | ||
ServerPort types.String `tfsdk:"server_port"` | ||
UserName types.String `tfsdk:"username"` | ||
EmbedPassword types.Bool `tfsdk:"embed_password"` | ||
QueryTaggingEnabled types.Bool `tfsdk:"query_tagging_enabled"` | ||
AuthenticationType types.String `tfsdk:"authentication_type"` | ||
UseOAuthManagedKeychain types.Bool `tfsdk:"use_oauth_managed_keychain"` | ||
} | ||
|
||
type workbookConnectionsDataSourceModel struct { | ||
ID types.String `tfsdk:"id"` | ||
Connections []workbookConnectionNestedDataModel `tfsdk:"connections"` | ||
} | ||
|
||
func (d *workbookConnectionsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_workbook_connections" | ||
} | ||
|
||
func (d *workbookConnectionsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Description: "Retrieve workbook connections details", | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Required: true, | ||
Description: "ID of the workbook", | ||
}, | ||
"connections": schema.ListNestedAttribute{ | ||
Description: "List workbook connections and their attributes", | ||
Computed: true, | ||
NestedObject: schema.NestedAttributeObject{ | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "ID of the workbook connection", | ||
}, | ||
"type": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "Database connection type", | ||
}, | ||
"datasource_id": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "ID of datasource", | ||
}, | ||
"server_address": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "Server address", | ||
}, | ||
"server_port": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "Server port", | ||
}, | ||
"username": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "Username", | ||
}, | ||
"embed_password": schema.BoolAttribute{ | ||
Computed: true, | ||
Description: "Embed database password into connection", | ||
}, | ||
"query_tagging_enabled": schema.BoolAttribute{ | ||
Computed: true, | ||
Description: "Query tagging enabled", | ||
}, | ||
"authentication_type": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "Authentication type", | ||
}, | ||
"use_oauth_managed_keychain": schema.BoolAttribute{ | ||
Computed: true, | ||
Description: "Use OAuth managed keychain", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *workbookConnectionsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var state workbookConnectionsDataSourceModel | ||
|
||
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...) | ||
|
||
connections, err := d.client.GetWorkbookConnections(state.ID.ValueString()) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Unable to Read Tableau Workbook Connections", | ||
err.Error(), | ||
) | ||
return | ||
} | ||
for _, connection := range connections { | ||
workbookConnection := workbookConnectionNestedDataModel{ | ||
ID: types.StringValue(connection.ID), | ||
Type: types.StringValue(connection.Type), | ||
DatasourceID: types.StringValue(connection.DataSourceID.ID), | ||
ServerAddress: types.StringValue(connection.ServerAddress), | ||
ServerPort: types.StringValue(connection.ServerPort), | ||
UserName: types.StringValue(connection.UserName), | ||
EmbedPassword: types.BoolValue(connection.EmbedPassword), | ||
QueryTaggingEnabled: types.BoolValue(connection.QueryTaggingEnabled), | ||
AuthenticationType: types.StringValue(connection.AuthenticationType), | ||
UseOAuthManagedKeychain: types.BoolValue(connection.UseOAuthManagedKeychain), | ||
} | ||
state.Connections = append(state.Connections, workbookConnection) | ||
} | ||
|
||
diags := resp.State.Set(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} | ||
|
||
func (d *workbookConnectionsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
d.client = req.ProviderData.(*Client) | ||
} |