Skip to content

Commit

Permalink
add support for dashboard- and chart-level workflow links (#232)
Browse files Browse the repository at this point in the history
* idk something like this

* test?

* sick

* verz

* added terraform docs

* dont work so hard

---------

Co-authored-by: MisterSquishy <[email protected]>
  • Loading branch information
MisterSquishy and MisterSquishy authored Aug 8, 2024
1 parent 5652a21 commit 2fc3ecf
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.96.0
1.97.0
8 changes: 8 additions & 0 deletions client/metric_dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type UnifiedDashboardAttributes struct {
Labels []Label `json:"labels"`
TemplateVariables []TemplateVariable `json:"template_variables"`
EventQueryIDs []string `json:"event_query_ids"`
WorkflowLinks []WorkflowLink `json:"workflow_links"`
}

type UnifiedGroup struct {
Expand Down Expand Up @@ -53,6 +54,7 @@ type UnifiedChart struct {
Text string `json:"text"`
Thresholds []Threshold `json:"thresholds"`
Subtitle *string `json:"subtitle,omitempty"`
WorkflowLinks []WorkflowLink `json:"workflow_links"`
}

type Label struct {
Expand Down Expand Up @@ -93,6 +95,11 @@ type TemplateVariable struct {
SuggestionAttributeKey string `json:"suggestion_attribute_key"`
}

type WorkflowLink struct {
Name string `json:"name"`
URL string `json:"url"`
}

func getUnifiedDashboardURL(project, id string) string {
path := fmt.Sprintf(
"projects/%s/metric_dashboards",
Expand Down Expand Up @@ -124,6 +131,7 @@ func (c *Client) CreateUnifiedDashboard(
Labels: dashboard.Attributes.Labels,
TemplateVariables: dashboard.Attributes.TemplateVariables,
EventQueryIDs: dashboard.Attributes.EventQueryIDs,
WorkflowLinks: dashboard.Attributes.WorkflowLinks,
},
})

Expand Down
30 changes: 30 additions & 0 deletions docs/resources/dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ resource "lightstep_dashboard" "customer_charges" {
- `group` (Block Set) (see [below for nested schema](#nestedblock--group))
- `label` (Block Set) Labels can be key/value pairs or standalone values. (see [below for nested schema](#nestedblock--label))
- `template_variable` (Block Set) Variable to be used in dashboard queries for dynamically filtering telemetry data (see [below for nested schema](#nestedblock--template_variable))
- `workflow_link` (Block List) Links to other resources (see [below for nested schema](#nestedblock--workflow_link))

### Read-Only

Expand All @@ -94,6 +95,7 @@ Optional:
- `subtitle` (String) Subtitle to show beneath big number, unused in other chart types
- `threshold` (Block List) (see [below for nested schema](#nestedblock--chart--threshold))
- `width` (Number)
- `workflow_link` (Block List) Links to other resources (see [below for nested schema](#nestedblock--chart--workflow_link))
- `x_pos` (Number)
- `y_axis` (Block List, Max: 1, Deprecated) (see [below for nested schema](#nestedblock--chart--y_axis))
- `y_pos` (Number)
Expand Down Expand Up @@ -158,6 +160,15 @@ Optional:
- `label` (String)


<a id="nestedblock--chart--workflow_link"></a>
### Nested Schema for `chart.workflow_link`

Required:

- `name` (String)
- `url` (String)


<a id="nestedblock--chart--y_axis"></a>
### Nested Schema for `chart.y_axis`

Expand Down Expand Up @@ -261,6 +272,7 @@ Optional:
- `subtitle` (String) Subtitle to show beneath big number, unused in other chart types
- `threshold` (Block List) (see [below for nested schema](#nestedblock--group--chart--threshold))
- `width` (Number)
- `workflow_link` (Block List) Links to other resources (see [below for nested schema](#nestedblock--group--chart--workflow_link))
- `x_pos` (Number)
- `y_axis` (Block List, Max: 1, Deprecated) (see [below for nested schema](#nestedblock--group--chart--y_axis))
- `y_pos` (Number)
Expand Down Expand Up @@ -325,6 +337,15 @@ Optional:
- `label` (String)


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

Required:

- `name` (String)
- `url` (String)


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

Expand Down Expand Up @@ -405,3 +426,12 @@ Required:
- `default_values` (List of String) One or more values to set the template variable to by default (if none are provided, defaults to all possible values)
- `name` (String) Unique (per dashboard) name for template variable, beginning with a letter or underscore and only containing letters, numbers, and underscores
- `suggestion_attribute_key` (String) Attribute key used as source for suggested template variable values appearing in Lightstep UI


<a id="nestedblock--workflow_link"></a>
### Nested Schema for `workflow_link`

Required:

- `name` (String)
- `url` (String)
30 changes: 30 additions & 0 deletions docs/resources/metric_dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ resource "lightstep_metric_dashboard" "customer_charges" {
- `group` (Block Set) (see [below for nested schema](#nestedblock--group))
- `label` (Block Set) Labels can be key/value pairs or standalone values. (see [below for nested schema](#nestedblock--label))
- `template_variable` (Block Set) Variable to be used in dashboard queries for dynamically filtering telemetry data (see [below for nested schema](#nestedblock--template_variable))
- `workflow_link` (Block List) Links to other resources (see [below for nested schema](#nestedblock--workflow_link))

### Read-Only

Expand All @@ -117,6 +118,7 @@ Optional:
- `subtitle` (String) Subtitle to show beneath big number, unused in other chart types
- `threshold` (Block List) (see [below for nested schema](#nestedblock--chart--threshold))
- `width` (Number)
- `workflow_link` (Block List) Links to other resources (see [below for nested schema](#nestedblock--chart--workflow_link))
- `x_pos` (Number)
- `y_axis` (Block List, Max: 1, Deprecated) (see [below for nested schema](#nestedblock--chart--y_axis))
- `y_pos` (Number)
Expand Down Expand Up @@ -195,6 +197,15 @@ Optional:
- `label` (String)


<a id="nestedblock--chart--workflow_link"></a>
### Nested Schema for `chart.workflow_link`

Required:

- `name` (String)
- `url` (String)


<a id="nestedblock--chart--y_axis"></a>
### Nested Schema for `chart.y_axis`

Expand Down Expand Up @@ -298,6 +309,7 @@ Optional:
- `subtitle` (String) Subtitle to show beneath big number, unused in other chart types
- `threshold` (Block List) (see [below for nested schema](#nestedblock--group--chart--threshold))
- `width` (Number)
- `workflow_link` (Block List) Links to other resources (see [below for nested schema](#nestedblock--group--chart--workflow_link))
- `x_pos` (Number)
- `y_axis` (Block List, Max: 1, Deprecated) (see [below for nested schema](#nestedblock--group--chart--y_axis))
- `y_pos` (Number)
Expand Down Expand Up @@ -376,6 +388,15 @@ Optional:
- `label` (String)


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

Required:

- `name` (String)
- `url` (String)


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

Expand Down Expand Up @@ -456,3 +477,12 @@ Required:
- `default_values` (List of String) One or more values to set the template variable to by default (if none are provided, defaults to all possible values)
- `name` (String) Unique (per dashboard) name for template variable, beginning with a letter or underscore and only containing letters, numbers, and underscores
- `suggestion_attribute_key` (String) Attribute key used as source for suggested template variable values appearing in Lightstep UI


<a id="nestedblock--workflow_link"></a>
### Nested Schema for `workflow_link`

Required:

- `name` (String)
- `url` (String)
88 changes: 82 additions & 6 deletions lightstep/resource_metric_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ func resourceUnifiedDashboard(chartSchemaType ChartSchemaType) *schema.Resource
Elem: &schema.Schema{Type: schema.TypeString},
Description: "IDs of the event queries to display on this dashboard",
},
"workflow_link": {
Description: "Links to other resources",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"url": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}
Expand Down Expand Up @@ -301,6 +318,23 @@ func getChartSchema(chartSchemaType ChartSchemaType) map[string]*schema.Schema {
Schema: getThresholdSchema(),
},
},
"workflow_link": {
Description: "Links to other resources",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"url": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
)
}
Expand Down Expand Up @@ -471,13 +505,17 @@ func getUnifiedDashboardAttributesFromResource(d *schema.ResourceData) (*client.
eventQueriesSet := d.Get("event_query_ids").(*schema.Set)
eventQueries := buildStringSlice(eventQueriesSet.List())

workflowLinksList := d.Get("workflow_link").([]any)
workflowLinks := buildWorkflowLinks(workflowLinksList)

attributes := &client.UnifiedDashboardAttributes{
Name: d.Get("dashboard_name").(string),
Description: d.Get("dashboard_description").(string),
Groups: groups,
Labels: labels,
TemplateVariables: templateVariables,
EventQueryIDs: eventQueries,
WorkflowLinks: workflowLinks,
}

return attributes, hasLegacyChartsIn, nil
Expand Down Expand Up @@ -573,13 +611,17 @@ func buildCharts(chartsIn []interface{}) ([]client.UnifiedChart, error) {
}

for _, chart := range charts {
workflowLinksList := chart["workflow_link"].([]any)
workflowLinks := buildWorkflowLinks(workflowLinksList)

c := client.UnifiedChart{
Title: chart["name"].(string),
Description: chart["description"].(string),
Rank: chart["rank"].(int),
Position: buildPosition(chart),
ID: chart["id"].(string),
ChartType: chart["type"].(string),
Title: chart["name"].(string),
Description: chart["description"].(string),
Rank: chart["rank"].(int),
Position: buildPosition(chart),
ID: chart["id"].(string),
ChartType: chart["type"].(string),
WorkflowLinks: workflowLinks,
}

queries, err := buildQueries(chart["query"].([]interface{}))
Expand Down Expand Up @@ -688,6 +730,21 @@ func buildTemplateVariables(templateVariablesIn []interface{}) []client.Template
return newTemplateVariables
}

func buildWorkflowLinks(workflowLinksIn []interface{}) []client.WorkflowLink {
var newWorkflowLinks []client.WorkflowLink
for _, wfLink := range workflowLinksIn {
wfMap := wfLink.(map[string]interface{})
name := wfMap["name"].(string)
url := wfMap["url"].(string)

newWorkflowLinks = append(newWorkflowLinks, client.WorkflowLink{
Name: name,
URL: url,
})
}
return newWorkflowLinks
}

func buildStringSlice(valuesIn []interface{}) []string {
vals := make([]string, 0, len(valuesIn))
for _, v := range valuesIn {
Expand Down Expand Up @@ -770,6 +827,16 @@ func (p *resourceUnifiedDashboardImp) setResourceDataFromUnifiedDashboard(projec
if err := d.Set("event_query_ids", dash.Attributes.EventQueryIDs); err != nil {
return fmt.Errorf("unable to set event_query_ids resource field: %v", err)
}
resourceWorkflowLinks := make([]map[string]string, 0, len(dash.Attributes.WorkflowLinks))
for _, workflowLink := range dash.Attributes.WorkflowLinks {
resourceWorkflowLinks = append(resourceWorkflowLinks, map[string]string{
"name": workflowLink.Name,
"url": workflowLink.URL,
})
}
if err := d.Set("workflow_link", resourceWorkflowLinks); err != nil {
return fmt.Errorf("unable to set workflow_link resource field: %v", err)
}

return nil
}
Expand Down Expand Up @@ -895,6 +962,15 @@ func assembleCharts(

resource["threshold"] = getThresholdsFromResourceData(c.Thresholds)

resourceWorkflowLinks := make([]map[string]string, 0, len(c.WorkflowLinks))
for _, workflowLink := range c.WorkflowLinks {
resourceWorkflowLinks = append(resourceWorkflowLinks, map[string]string{
"name": workflowLink.Name,
"url": workflowLink.URL,
})
}
resource["workflow_link"] = resourceWorkflowLinks

chartResources = append(chartResources, resource)
}
return chartResources, nil
Expand Down
Loading

0 comments on commit 2fc3ecf

Please sign in to comment.