Skip to content

Commit

Permalink
Merge pull request #26 from squaredup/work/ss/dashboard-share
Browse files Browse the repository at this point in the history
Support for Dashboard Sharing
  • Loading branch information
shaswot77 authored Jan 22, 2024
2 parents df49b21 + 2dcced4 commit ec7c6e3
Show file tree
Hide file tree
Showing 8 changed files with 631 additions and 0 deletions.
90 changes: 90 additions & 0 deletions docs/resources/dashboard_share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "squaredup_dashboard_share Resource - terraform-provider-squaredup"
subcategory: ""
description: |-
Enable sharing for a dashboard
---

# squaredup_dashboard_share (Resource)

Enable sharing for a dashboard

## Example Usage

```terraform
resource "squaredup_workspace" "application_workspace" {
display_name = "Application Team"
description = "Workspace with Dashboards for Application Team"
open_access_enabled = true
}
resource "squaredup_dashboard" "sample_dashboard" {
dashboard_template = <<EOT
{
"_type": "layout/grid",
"contents": [
{
"x": 0,
"h": 2,
"i": "1",
"y": 0,
"config": {
"title": "",
"description": "",
"_type": "tile/text",
"visualisation": {
"config": {
"content": "Sample Tile",
"autoSize": true,
"fontSize": 16,
"align": "center"
}
}
},
"w": 4
}
],
"columns": 1,
"version": 1
}
EOT
workspace_id = squaredup_workspace.application_workspace.id
display_name = "Sample Dashboard"
}
resource "squaredup_dashboard_share" "sample_dashboard_share" {
dashboard_id = squaredup_dashboard.sample_dashboard.id
workspace_id = squaredup_workspace.application_workspace.id
require_authentication = true
enabled = true
}
```

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

### Required

- `dashboard_id` (String) The ID of the dashboard to share
- `require_authentication` (Boolean) If false, the dashboard will be accessible to anyone with the link
- `workspace_id` (String) The ID of the workspace where the dashboard is located

### Optional

- `enabled` (Boolean) If false, sharing of the dashboard is disabled

### Read-Only

- `dashboard_share_link` (String) Shareable link for the dashboard
- `id` (String) The ID of the dashboard share
- `last_updated` (String) The last time the Dashboard Share was updated

## Import

Import is supported using the following syntax:

```shell
# Dashboard share can be imported using the OA ID.
terraform import squaredup_dashboard.example openacc-123
```
2 changes: 2 additions & 0 deletions examples/resources/squaredup_dashboard_share/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Dashboard share can be imported using the OA ID.
terraform import squaredup_dashboard.example openacc-123
46 changes: 46 additions & 0 deletions examples/resources/squaredup_dashboard_share/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
resource "squaredup_workspace" "application_workspace" {
display_name = "Application Team"
description = "Workspace with Dashboards for Application Team"
open_access_enabled = true
}

resource "squaredup_dashboard" "sample_dashboard" {
dashboard_template = <<EOT
{
"_type": "layout/grid",
"contents": [
{
"x": 0,
"h": 2,
"i": "1",
"y": 0,
"config": {
"title": "",
"description": "",
"_type": "tile/text",
"visualisation": {
"config": {
"content": "Sample Tile",
"autoSize": true,
"fontSize": 16,
"align": "center"
}
}
},
"w": 4
}
],
"columns": 1,
"version": 1
}
EOT
workspace_id = squaredup_workspace.application_workspace.id
display_name = "Sample Dashboard"
}

resource "squaredup_dashboard_share" "sample_dashboard_share" {
dashboard_id = squaredup_dashboard.sample_dashboard.id
workspace_id = squaredup_workspace.application_workspace.id
require_authentication = true
enabled = true
}
85 changes: 85 additions & 0 deletions internal/provider/client_dashboard_share.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package provider

import (
"encoding/json"
"net/http"
"strings"
)

func (c *SquaredUpClient) CreateSharedDashboard(dashboardShare DashboardShare) (*DashboardShare, error) {
rb, err := json.Marshal(dashboardShare)
if err != nil {
return nil, err
}

req, err := http.NewRequest("POST", c.baseURL+"/api/openaccess/shares", strings.NewReader(string(rb)))
if err != nil {
return nil, err
}

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

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

return &sharedDashboard, nil
}

func (c *SquaredUpClient) GetSharedDashboard(sharedDashboardId string) (*DashboardShare, error) {
req, err := http.NewRequest("GET", c.baseURL+"/api/openaccess/shares/"+sharedDashboardId, nil)
if err != nil {
return nil, err
}

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

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

return &sharedDashboard, nil
}

func (c *SquaredUpClient) UpdateSharedDashboard(sharedDashboardId string, dashboardShare DashboardShare) error {
rb, err := json.Marshal(dashboardShare)
if err != nil {
return err
}

req, err := http.NewRequest("PUT", c.baseURL+"/api/openaccess/shares/"+sharedDashboardId, strings.NewReader(string(rb)))
if err != nil {
return err
}

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

return nil
}

func (c *SquaredUpClient) DeleteSharedDashboard(sharedDashboardId string) error {
req, err := http.NewRequest("DELETE", c.baseURL+"/api/openaccess/shares/"+sharedDashboardId, nil)
if err != nil {
return err
}

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

return nil
}
13 changes: 13 additions & 0 deletions internal/provider/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,16 @@ type GremlinQueryResult struct {
TenantId []string `json:"__tenantId"`
ConfigId []string `json:"__configId"`
}

type DashboardShare struct {
LastUpdated string `json:"lastUpdated,omitempty"`
ID string `json:"id,omitempty"`
TargetID string `json:"targetId"`
WorkspaceID string `json:"workspaceId"`
Properties DashboardShareProperties `json:"properties"`
}

type DashboardShareProperties struct {
Enabled bool `json:"enabled"`
RequireAuthentication bool `json:"requireAuthentication"`
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,6 @@ func (p *squaredupProvider) Resources(_ context.Context) []func() resource.Resou
SquaredupDataSourceResource,
SquaredupWorkspaceResource,
SquaredUpDashboardResource,
SquaredUpDashboardShareResource,
}
}
Loading

0 comments on commit ec7c6e3

Please sign in to comment.