Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for dashboard- and chart-level workflow links #232

Merged
merged 7 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's this whole []any business? 🧐

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type casting with an unexpected shape?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that d.Get function is getting the list of workflow links from the terraform state -- but all we told terraform is the schema, so it doesnt have a strongly-typed representation of the data

im honestly not totally sure why we traditionally cast to []any first, and then do a subsequent cast (down here) to get the elements of the array as maps. i think maybe i dont like that pattern and im gonna break it here?? let's see how this goes...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 all for seeing how it goes in the face of uncertainty 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it did not go great 😂

panic: interface conversion: interface {} is []interface {}, not []map[string]string

i guess go makes you cast in two steps??

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even just this doesnt work -- i guess generic arrays and maps omit type info, so their elements must be cast individually 😞
CleanShot 2024-08-08 at 14 40 15@2x

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
Loading