Skip to content

Commit

Permalink
Merge pull request #31 from vantage-sh/ig/dashboard-saved-filters
Browse files Browse the repository at this point in the history
Add saved filters support for dashboards.
  • Loading branch information
igeagonz authored Mar 11, 2024
2 parents bf99444 + 7995799 commit c28eebd
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 29 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/dashboards.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Read-Only:
- `title` (String)
- `token` (String)
- `widget_tokens` (List of String)
- `saved_filter_tokens` (List of String)
- `workspace_token` (String)


1 change: 1 addition & 0 deletions docs/resources/dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ resource "vantage_dashboard" "demo_dashboard" {
- `date_interval` (String) Determines the date range in the Dashboard. Guaranteed to be set to 'custom' if 'start_date' and 'end_date' are set.
- `end_date` (String) The end date for the date range for CostReports in the Dashboard. ISO 8601 Formatted. Overwrites 'date_interval' if set.
- `start_date` (String) The start date for the date range for CostReports in the Dashboard. ISO 8601 Formatted. Overwrites 'date_interval' if set.
- `saved_filter_tokens` (List of String) The tokens for SavedFilters that are applied to CostReports in the Dashboard.

### Read-Only

Expand Down
1 change: 1 addition & 0 deletions examples/cost_report/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ resource "vantage_dashboard" "demo_dashboard" {
title = "Demo Dashboard"
date_interval = "last_month"
workspace_token = "wrkspc_47c3254c790e9351"
saved_filter_tokens = [vantage_saved_filter.demo_filter.token]
}

resource "vantage_team" "demo_team" {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/hashicorp/terraform-plugin-docs v0.14.1
github.com/hashicorp/terraform-plugin-framework v1.5.0
github.com/hashicorp/terraform-plugin-testing v1.6.0
github.com/vantage-sh/vantage-go v0.0.6
github.com/vantage-sh/vantage-go v0.0.10
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/vantage-sh/vantage-go v0.0.6 h1:wf4WlUIldghn8d8ZnQR4KIUZcLYpBOFpXe2hl1owqag=
github.com/vantage-sh/vantage-go v0.0.6/go.mod h1:MsSI4gX/Wvkzo9kqWphZfE6puolmnbHcXSaoGkDCmXg=
github.com/vantage-sh/vantage-go v0.0.10 h1:sfbTMoR0ND7YN7n7C4eXAgGQcxBdAVhNm0KUY+KhSrU=
github.com/vantage-sh/vantage-go v0.0.10/go.mod h1:MsSI4gX/Wvkzo9kqWphZfE6puolmnbHcXSaoGkDCmXg=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down
68 changes: 50 additions & 18 deletions vantage/dashboard_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ func NewDashboardResource() resource.Resource {
}

type DashboardResourceModel struct {
Token types.String `tfsdk:"token"`
Title types.String `tfsdk:"title"`
WidgetTokens types.List `tfsdk:"widget_tokens"`
DateBin types.String `tfsdk:"date_bin"`
DateInterval types.String `tfsdk:"date_interval"`
StartDate types.String `tfsdk:"start_date"`
EndDate types.String `tfsdk:"end_date"`
WorkspaceToken types.String `tfsdk:"workspace_token"`
Token types.String `tfsdk:"token"`
Title types.String `tfsdk:"title"`
WidgetTokens types.List `tfsdk:"widget_tokens"`
DateBin types.String `tfsdk:"date_bin"`
DateInterval types.String `tfsdk:"date_interval"`
StartDate types.String `tfsdk:"start_date"`
EndDate types.String `tfsdk:"end_date"`
WorkspaceToken types.String `tfsdk:"workspace_token"`
SavedFilterTokens types.List `tfsdk:"saved_filter_tokens"`
}

func (r *DashboardResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
Expand Down Expand Up @@ -93,6 +94,11 @@ func (r DashboardResource) Schema(ctx context.Context, req resource.SchemaReques
stringplanmodifier.UseStateForUnknown(),
},
},
"saved_filter_tokens": schema.ListAttribute{
ElementType: types.StringType,
MarkdownDescription: "Tokens of the saved filters used in the Dashboard.",
Optional: true,
},
},
MarkdownDescription: "Manages a Dashboard.",
}
Expand All @@ -114,15 +120,25 @@ func (r DashboardResource) Create(ctx context.Context, req resource.CreateReques
}
}

savedFilterTokens := []types.String{}
if !data.SavedFilterTokens.IsNull() && !data.SavedFilterTokens.IsUnknown() {
savedFilterTokens = make([]types.String, 0, len(data.SavedFilterTokens.Elements()))
resp.Diagnostics.Append(data.SavedFilterTokens.ElementsAs(ctx, &savedFilterTokens, false)...)
if resp.Diagnostics.HasError() {
return
}
}

params := dashboardsv2.NewCreateDashboardParams()
body := &modelsv2.PostDashboards{
Title: data.Title.ValueStringPointer(),
WidgetTokens: fromStringsValue(widgetTokens),
DateBin: data.DateBin.ValueString(),
DateInterval: data.DateInterval.ValueString(),
StartDate: data.StartDate.ValueString(),
EndDate: data.EndDate.ValueStringPointer(),
WorkspaceToken: data.WorkspaceToken.ValueString(),
Title: data.Title.ValueStringPointer(),
WidgetTokens: fromStringsValue(widgetTokens),
DateBin: data.DateBin.ValueString(),
DateInterval: data.DateInterval.ValueString(),
StartDate: data.StartDate.ValueString(),
EndDate: data.EndDate.ValueStringPointer(),
WorkspaceToken: data.WorkspaceToken.ValueString(),
SavedFilterTokens: fromStringsValue(savedFilterTokens),
}
params.WithDashboards(body)
out, err := r.client.V2.Dashboards.CreateDashboard(params, r.client.Auth)
Expand Down Expand Up @@ -180,6 +196,12 @@ func (r DashboardResource) Read(ctx context.Context, req resource.ReadRequest, r
return
}
state.WidgetTokens = widgets
saved_filters, diag := types.ListValueFrom(ctx, types.StringType, out.Payload.SavedFilterTokens)
if diag.HasError() {
resp.Diagnostics.Append(diag...)
return
}
state.SavedFilterTokens = saved_filters

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
Expand All @@ -200,11 +222,21 @@ func (r DashboardResource) Update(ctx context.Context, req resource.UpdateReques
}
}

savedFilterTokens := []types.String{}
if !data.SavedFilterTokens.IsNull() && !data.SavedFilterTokens.IsUnknown() {
savedFilterTokens = make([]types.String, 0, len(data.SavedFilterTokens.Elements()))
resp.Diagnostics.Append(data.SavedFilterTokens.ElementsAs(ctx, &savedFilterTokens, false)...)
if resp.Diagnostics.HasError() {
return
}
}

params := dashboardsv2.NewUpdateDashboardParams()
body := &modelsv2.PutDashboards{
Title: data.Title.ValueString(),
WidgetTokens: fromStringsValue(widgetTokens),
DateBin: data.DateBin.ValueString(),
Title: data.Title.ValueString(),
WidgetTokens: fromStringsValue(widgetTokens),
SavedFilterTokens: fromStringsValue(savedFilterTokens),
DateBin: data.DateBin.ValueString(),
}
if data.DateInterval.ValueString() == "" || data.DateInterval.ValueString() == "custom" {
body.StartDate = data.StartDate.ValueString()
Expand Down
29 changes: 21 additions & 8 deletions vantage/dashboards_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ func NewDashboardsDataSource() datasource.DataSource {
}

type dashboardDataSourceModel struct {
Token types.String `tfsdk:"token"`
Title types.String `tfsdk:"title"`
WidgetTokens types.List `tfsdk:"widget_tokens"`
DateBin types.String `tfsdk:"date_bin"`
DateInterval types.String `tfsdk:"date_interval"`
StartDate types.String `tfsdk:"start_date"`
EndDate types.String `tfsdk:"end_date"`
WorkspaceToken types.String `tfsdk:"workspace_token"`
Token types.String `tfsdk:"token"`
Title types.String `tfsdk:"title"`
WidgetTokens types.List `tfsdk:"widget_tokens"`
DateBin types.String `tfsdk:"date_bin"`
DateInterval types.String `tfsdk:"date_interval"`
StartDate types.String `tfsdk:"start_date"`
EndDate types.String `tfsdk:"end_date"`
WorkspaceToken types.String `tfsdk:"workspace_token"`
SavedFilterTokens types.List `tfsdk:"saved_filter_tokens"`
}

type dashboardsDataSourceModel struct {
Expand Down Expand Up @@ -79,6 +80,14 @@ func (d *dashboardsDataSource) Read(ctx context.Context, req datasource.ReadRequ
return
}
dashboardModel.WidgetTokens = widgetTokens

savedFilterTokens, diag := types.ListValueFrom(ctx, types.StringType, dashboard.SavedFilterTokens)
if diag.HasError() {
resp.Diagnostics.Append(diag...)
return
}
dashboardModel.SavedFilterTokens = savedFilterTokens

state.Dashboards = append(state.Dashboards, dashboardModel)
}

Expand Down Expand Up @@ -120,6 +129,10 @@ func (d *dashboardsDataSource) Schema(ctx context.Context, req datasource.Schema
"workspace_token": schema.StringAttribute{
Computed: true,
},
"saved_filter_tokens": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
},
},
},
Computed: true,
Expand Down

0 comments on commit c28eebd

Please sign in to comment.