Skip to content

Commit

Permalink
Merge pull request #21 from squaredup/work/ss/dashboard-timeframe
Browse files Browse the repository at this point in the history
Support for Dashboard Timeframe
  • Loading branch information
shaswot77 authored Dec 19, 2023
2 parents 2206492 + c4dc328 commit 80aa8f8
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 6 deletions.
15 changes: 13 additions & 2 deletions .vscode/settings.json → cspell.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"cSpell.words": [
"version": "0.2",
"language": "en,en-US",
"allowCompoundWords": true,
"dictionaries": [
"en_US",
"en_GB",
"softwareTerms",
"golang",
"bash"
],
"words": [
"basetypes",
"cbroglie",
"datasource",
Expand All @@ -16,7 +26,8 @@
"stringvalidator",
"tfprotov",
"tfsdk",
"timeframe",
"unmarshalling",
"visualisation"
]
}
}
2 changes: 2 additions & 0 deletions docs/resources/dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ EOT
})
workspace_id = squaredup_workspace.application_workspace.id
display_name = "Sample Dashboard"
timeframe = "last12hours"
}
```

Expand All @@ -144,6 +145,7 @@ EOT
### Optional

- `template_bindings` (String) Template Bindings used for replacing mustache template in the dashboard template. Needs to be a JSON encoded string.
- `timeframe` (String) The timeframe of the dashboard. It should be one of the following: last1hour, last12hours, last24hours, last7days, last30days, thisMonth, thisQuarter, thisYear, lastMonth, lastQuarter, lastYear

### Read-Only

Expand Down
1 change: 1 addition & 0 deletions examples/resources/squaredup_dashboard/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,5 @@ EOT
})
workspace_id = squaredup_workspace.application_workspace.id
display_name = "Sample Dashboard"
timeframe = "last12hours"
}
6 changes: 4 additions & 2 deletions internal/provider/client_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"strings"
)

func (c *SquaredUpClient) CreateDashboard(displayName string, workspaceId string, dashboardContent string) (*Dashboard, error) {
func (c *SquaredUpClient) CreateDashboard(displayName string, workspaceId string, timeframe string, dashboardContent string) (*Dashboard, error) {

DashboardPayload := map[string]interface{}{
"displayName": displayName,
"workspaceId": workspaceId,
"timeframe": timeframe,
}

rb, err := json.Marshal(DashboardPayload)
Expand Down Expand Up @@ -59,10 +60,11 @@ func (c *SquaredUpClient) GetDashboard(dashboardId string) (*Dashboard, error) {
return &newDashboard, nil
}

func (c *SquaredUpClient) UpdateDashboard(dashboardId string, displayName string, workspaceId string, dashboardContent string) (*Dashboard, error) {
func (c *SquaredUpClient) UpdateDashboard(dashboardId string, displayName string, workspaceId string, timeframe string, dashboardContent string) (*Dashboard, error) {
DashboardPayload := map[string]interface{}{
"displayName": displayName,
"workspaceId": workspaceId,
"timeframe": timeframe,
}

rb, err := json.Marshal(DashboardPayload)
Expand Down
1 change: 1 addition & 0 deletions internal/provider/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type Dashboard struct {
Group string `json:"group,omitempty"`
Name string `json:"name"`
SchemaVersion string `json:"schemaVersion"`
Timeframe string `json:"timeframe,omitempty"`
}

type SquaredupGremlinQuery struct {
Expand Down
28 changes: 26 additions & 2 deletions internal/provider/resource_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (

"github.com/cbroglie/mustache"
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)

Expand All @@ -36,6 +38,7 @@ type squaredupDashboard struct {
DashboardTemplate jsontypes.Normalized `tfsdk:"dashboard_template"`
TemplateBindings jsontypes.Normalized `tfsdk:"template_bindings"`
DashboardContent jsontypes.Normalized `tfsdk:"dashboard_content"`
Timeframe types.String `tfsdk:"timeframe"`
Group types.String `tfsdk:"group"`
Name types.String `tfsdk:"name"`
SchemaVersion types.String `tfsdk:"schema_version"`
Expand Down Expand Up @@ -81,6 +84,24 @@ func (r *DashboardResource) Schema(_ context.Context, _ resource.SchemaRequest,
Computed: true,
CustomType: jsontypes.NormalizedType{},
},
"timeframe": schema.StringAttribute{
Description: "The timeframe of the dashboard. It should be one of the following: last1hour, last12hours, last24hours, last7days, last30days, thisMonth, thisQuarter, thisYear, lastMonth, lastQuarter, lastYear",
Optional: true,
Computed: true,
Validators: []validator.String{stringvalidator.OneOf(
"last1hour",
"last12hours",
"last24hours",
"last7days",
"last30days",
"thisMonth",
"thisQuarter",
"thisYear",
"lastMonth",
"lastQuarter",
"lastYear",
)},
},
"group": schema.StringAttribute{
Description: "The group of the dashboard",
Computed: true,
Expand Down Expand Up @@ -149,7 +170,7 @@ func (r *DashboardResource) Create(ctx context.Context, req resource.CreateReque
plan.TemplateBindings = jsontypes.NewNormalizedNull()
}

dashboard, err := r.client.CreateDashboard(plan.DisplayName.ValueString(), plan.WorkspaceID.ValueString(), updatedDashboard)
dashboard, err := r.client.CreateDashboard(plan.DisplayName.ValueString(), plan.WorkspaceID.ValueString(), plan.Timeframe.ValueString(), updatedDashboard)
if err != nil {
resp.Diagnostics.AddError(
"Unable to create dashboard",
Expand All @@ -165,6 +186,7 @@ func (r *DashboardResource) Create(ctx context.Context, req resource.CreateReque
DashboardTemplate: plan.DashboardTemplate,
TemplateBindings: plan.TemplateBindings,
DashboardContent: jsontypes.NewNormalizedValue(updatedDashboard),
Timeframe: types.StringValue(dashboard.Timeframe),
Group: types.StringValue(dashboard.Group),
Name: types.StringValue(dashboard.Name),
SchemaVersion: types.StringValue(dashboard.SchemaVersion),
Expand Down Expand Up @@ -203,6 +225,7 @@ func (r *DashboardResource) Read(ctx context.Context, req resource.ReadRequest,
DashboardTemplate: state.DashboardTemplate,
TemplateBindings: state.TemplateBindings,
DashboardContent: state.DashboardContent,
Timeframe: types.StringValue(dashboard.Timeframe),
Group: types.StringValue(dashboard.Group),
Name: types.StringValue(dashboard.Name),
SchemaVersion: types.StringValue(dashboard.SchemaVersion),
Expand Down Expand Up @@ -253,7 +276,7 @@ func (r *DashboardResource) Update(ctx context.Context, req resource.UpdateReque
plan.TemplateBindings = jsontypes.NewNormalizedNull()
}

dashboard, err := r.client.UpdateDashboard(state.DashboardID.ValueString(), plan.DisplayName.ValueString(), state.DashboardID.String(), updatedDashboard)
dashboard, err := r.client.UpdateDashboard(state.DashboardID.ValueString(), plan.DisplayName.ValueString(), state.DashboardID.String(), plan.Timeframe.ValueString(), updatedDashboard)
if err != nil {
resp.Diagnostics.AddError(
"Unable to update dashboard",
Expand All @@ -269,6 +292,7 @@ func (r *DashboardResource) Update(ctx context.Context, req resource.UpdateReque
DashboardTemplate: plan.DashboardTemplate,
TemplateBindings: plan.TemplateBindings,
DashboardContent: jsontypes.NewNormalizedValue(updatedDashboard),
Timeframe: types.StringValue(dashboard.Timeframe),
Group: types.StringValue(dashboard.Group),
Name: types.StringValue(dashboard.Name),
SchemaVersion: types.StringValue(dashboard.SchemaVersion),
Expand Down

0 comments on commit 80aa8f8

Please sign in to comment.