Skip to content

Commit

Permalink
[LS-53603] terraform support for service health panel (#194)
Browse files Browse the repository at this point in the history
Add support for the new service health panel

---------

Co-authored-by: adam.burvill <[email protected]>
  • Loading branch information
TheRealAkhil and amburvill authored Nov 28, 2023
1 parent 3c2d118 commit fbb2413
Show file tree
Hide file tree
Showing 9 changed files with 334 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.84.4
1.85.0
9 changes: 9 additions & 0 deletions client/metric_dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type UnifiedGroup struct {
VisibilityType string `json:"visibility_type"`
Charts []UnifiedChart `json:"charts"`
Labels []Label `json:"labels"`
Panels []Panel `json:"panels"`
}

type UnifiedPosition struct {
Expand Down Expand Up @@ -57,6 +58,14 @@ type Label struct {
Value string `json:"label_value"`
}

type Panel struct {
ID string `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
Position UnifiedPosition `json:"position"`
Body map[string]any `json:"body"`
}

type YAxis struct {
Min float64 `json:"min"`
Max float64 `json:"max"`
Expand Down
29 changes: 29 additions & 0 deletions docs/resources/dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ Required:
Optional:

- `chart` (Block Set) (see [below for nested schema](#nestedblock--group--chart))
- `service_health_panel` (Block Set) A dashboard panel to view the health of your services (see [below for nested schema](#nestedblock--group--service_health_panel))
- `text_panel` (Block List) (see [below for nested schema](#nestedblock--group--text_panel))
- `title` (String)

Expand Down Expand Up @@ -240,6 +241,34 @@ Required:



<a id="nestedblock--group--service_health_panel"></a>
### Nested Schema for `group.service_health_panel`

Optional:

- `height` (Number)
- `name` (String)
- `panel_options` (Block Set, Max: 1) custom options for the service health panel (see [below for nested schema](#nestedblock--group--service_health_panel--panel_options))
- `width` (Number)
- `x_pos` (Number)
- `y_pos` (Number)

Read-Only:

- `id` (String) The ID of this resource.

<a id="nestedblock--group--service_health_panel--panel_options"></a>
### Nested Schema for `group.service_health_panel.panel_options`

Optional:

- `change_since` (String)
- `percentile` (String)
- `sort_by` (String)
- `sort_direction` (String)



<a id="nestedblock--group--text_panel"></a>
### Nested Schema for `group.text_panel`

Expand Down
29 changes: 29 additions & 0 deletions docs/resources/metric_dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ Required:
Optional:

- `chart` (Block Set) (see [below for nested schema](#nestedblock--group--chart))
- `service_health_panel` (Block Set) A dashboard panel to view the health of your services (see [below for nested schema](#nestedblock--group--service_health_panel))
- `text_panel` (Block List) (see [below for nested schema](#nestedblock--group--text_panel))
- `title` (String)

Expand Down Expand Up @@ -297,6 +298,34 @@ Required:



<a id="nestedblock--group--service_health_panel"></a>
### Nested Schema for `group.service_health_panel`

Optional:

- `height` (Number)
- `name` (String)
- `panel_options` (Block Set, Max: 1) custom options for the service health panel (see [below for nested schema](#nestedblock--group--service_health_panel--panel_options))
- `width` (Number)
- `x_pos` (Number)
- `y_pos` (Number)

Read-Only:

- `id` (String) The ID of this resource.

<a id="nestedblock--group--service_health_panel--panel_options"></a>
### Nested Schema for `group.service_health_panel.panel_options`

Optional:

- `change_since` (String)
- `percentile` (String)
- `sort_by` (String)
- `sort_direction` (String)



<a id="nestedblock--group--text_panel"></a>
### Nested Schema for `group.text_panel`

Expand Down
11 changes: 1 addition & 10 deletions lightstep/resource_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func getQueriesFromUnifiedDashboardResourceData(
qs := map[string]interface{}{
"hidden": q.Hidden,
"display": q.Display,
"display_type_options": displayTypeOptionsFromResourceData(q.DisplayTypeOptions),
"display_type_options": convertNestedMapToSchemaSet(q.DisplayTypeOptions),
"query_name": q.Name,
"query_string": q.TQLQuery,
"dependency_map_options": getDependencyMapOptions(q.DependencyMapOptions),
Expand All @@ -155,15 +155,6 @@ func getQueriesFromUnifiedDashboardResourceData(
return queries, nil
}

func displayTypeOptionsFromResourceData(opts map[string]interface{}) *schema.Set {
// "display_type_options" is a set that always has at most one element, so
// the hash function is trivial
f := func(i interface{}) int {
return 1
}
return schema.NewSet(f, []interface{}{opts})
}

func getDependencyMapOptions(options *client.DependencyMapOptions) []interface{} {
if options == nil {
return nil
Expand Down
44 changes: 30 additions & 14 deletions lightstep/resource_metric_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"net/http"
"strings"

"github.com/lightstep/terraform-provider-lightstep/client"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/lightstep/terraform-provider-lightstep/client"
)

type ChartSchemaType int
Expand Down Expand Up @@ -129,11 +129,12 @@ func getGroupSchema(chartSchemaType ChartSchemaType) map[string]*schema.Schema {
"text_panel": {
Type: schema.TypeList,
Optional: true,
Computed: true, // the panels can be mutated individually; chart mutations should not trigger group updates
Computed: true, // the panels can be mutated individually; text panel mutations should not trigger group updates
Elem: &schema.Resource{
Schema: getTextPanelSchema(),
},
},
ServiceHealthPanel: getServiceHealthPanelSchema(),
}
}

Expand Down Expand Up @@ -168,14 +169,26 @@ func getPanelSchema(isNameRequired bool) map[string]*schema.Schema {
}
}

return map[string]*schema.Schema{
// Alias for what we refer to as title elsewhere
"name": nameSchema(),
"description": {
Type: schema.TypeString,
Optional: true,
Default: "",
return mergeSchemas(
getPositionSchema(),
map[string]*schema.Schema{
// Alias for what we refer to as title elsewhere
"name": nameSchema(),
"description": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"id": {
Type: schema.TypeString,
Computed: true,
},
},
)
}

func getPositionSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"x_pos": {
Type: schema.TypeInt,
ValidateFunc: validation.IntAtLeast(0),
Expand All @@ -200,10 +213,6 @@ func getPanelSchema(isNameRequired bool) map[string]*schema.Schema {
Default: 0,
Optional: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
},
}
}

Expand Down Expand Up @@ -487,13 +496,18 @@ func buildGroups(groupsIn []interface{}, legacyChartsIn []interface{}) ([]client
if err != nil {
return nil, hasLegacyChartsIn, err
}
serviceHealthPanels, err := convertServiceHealthFromResourceToApiRequest(group[ServiceHealthPanel])
if err != nil {
return nil, hasLegacyChartsIn, err
}

g := client.UnifiedGroup{
ID: group["id"].(string),
Rank: group["rank"].(int),
Title: group["title"].(string),
VisibilityType: group["visibility_type"].(string),
Charts: append(chartPanels, textPanels...),
Panels: serviceHealthPanels,
}
newGroups = append(newGroups, g)
}
Expand Down Expand Up @@ -663,6 +677,8 @@ func (p *resourceUnifiedDashboardImp) setResourceDataFromUnifiedDashboard(projec
group["chart"] = groupCharts
group["text_panel"] = groupTextPanels

group[ServiceHealthPanel] = convertServiceHealthfromApiRequestToResource(g.Panels)

groups = append(groups, group)
}
if err := d.Set("group", groups); err != nil {
Expand Down
62 changes: 62 additions & 0 deletions lightstep/resource_metric_dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,68 @@ resource "lightstep_metric_dashboard" "test" {
})
}

func TestAccDashboardServiceHealthPanel(t *testing.T) {
var dashboard client.UnifiedDashboard

dashboardConfig := `
resource "lightstep_metric_dashboard" "test" {
project_name = "` + testProject + `"
dashboard_name = "Acceptance Test Dashboard (TestAccDashboardServiceHealthPanel)"
dashboard_description = "Dashboard to test the service health panel"
group {
rank = 0
visibility_type = "implicit"
service_health_panel {
name = "test_service_health_panel"
x_pos = 0
y_pos = 0
width = 10
height = 10
panel_options {
sort_direction = "asc"
sort_by = "latency"
}
}
}
}
`
// Change the chart name and metric name
updatedConfig := strings.Replace(dashboardConfig, "asc", "desc", -1)

resourceName := "lightstep_metric_dashboard.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testGetMetricDashboardDestroy,
Steps: []resource.TestStep{
{
// Create the initial dashboard with a service health panel. verify the name and panel_options.percentile
Config: dashboardConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckMetricDashboardExists(resourceName, &dashboard),
resource.TestCheckResourceAttr(resourceName, "group.0.service_health_panel.0.name", "test_service_health_panel"),
resource.TestCheckResourceAttr(resourceName, "group.0.service_health_panel.0.panel_options.0.sort_by", "latency"),
resource.TestCheckResourceAttr(resourceName, "group.0.service_health_panel.0.panel_options.0.sort_direction", "asc"),
),
},
{
// Updated config will contain the new metric and chart name
Config: updatedConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckMetricDashboardExists(resourceName, &dashboard),
resource.TestCheckResourceAttr(resourceName, "group.0.service_health_panel.0.name", "test_service_health_panel"),
resource.TestCheckResourceAttr(resourceName, "group.0.service_health_panel.0.panel_options.0.sort_direction", "desc"),
),
},
},
})
}

func Test_buildLabels(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading

0 comments on commit fbb2413

Please sign in to comment.