Skip to content

Commit

Permalink
kibana: Add support for dashboard references (#65)
Browse files Browse the repository at this point in the history
Add support for dashboard references
  • Loading branch information
pengux authored Mar 10, 2021
1 parent 721df6d commit 08dabed
Show file tree
Hide file tree
Showing 7 changed files with 459 additions and 82 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/Microsoft/go-winio v0.4.15 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/containerd/continuity v0.0.0-20200928162600-f2cc35102c2a // indirect
github.com/ewilde/go-kibana v0.0.0-20210224091751-f7a3bb655958
github.com/ewilde/go-kibana v0.0.0-20210308173242-50256c0d763b
github.com/hashicorp/hcl v0.0.0-20171017181929-23c074d0eceb // indirect
github.com/hashicorp/terraform-plugin-sdk v1.16.0
github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2
Expand Down
92 changes: 21 additions & 71 deletions go.sum

Large diffs are not rendered by default.

54 changes: 52 additions & 2 deletions kibana/resource_kibana_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package kibana

import (
"fmt"
"log"

"github.com/ewilde/go-kibana"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/structure"
"log"
)

func resourceKibanaDashboard() *schema.Resource {
Expand Down Expand Up @@ -88,6 +89,27 @@ func resourceKibanaDashboard() *schema.Resource {
return newJson == oldJson
},
},
"references": {
Type: schema.TypeSet,
Description: "A list of references",
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}
Expand Down Expand Up @@ -125,6 +147,7 @@ func resourceKibanaDashboardRead(d *schema.ResourceData, meta interface{}) error
d.Set("options_json", response.Attributes.OptionsJson)
d.Set("ui_state_json", response.Attributes.UiStateJSON)
d.Set("time_restore", response.Attributes.TimeRestore)
d.Set("references", flattenDashboardReferences(response.References))

if response.Attributes.KibanaSavedObjectMeta != nil {
d.Set("search_source_json", response.Attributes.KibanaSavedObjectMeta.SearchSourceJSON)
Expand All @@ -141,7 +164,7 @@ func resourceKibanaDashboardUpdate(d *schema.ResourceData, meta interface{}) err

log.Printf("[INFO] Creating Kibana dashboard %s", dashboardRequest.Attributes.Title)

_, err = meta.(*kibana.KibanaClient).Dashboard().Update(d.Id(), &kibana.UpdateDashboardRequest{Attributes: dashboardRequest.Attributes})
_, err = meta.(*kibana.KibanaClient).Dashboard().Update(d.Id(), &kibana.UpdateDashboardRequest{Attributes: dashboardRequest.Attributes, References: dashboardRequest.References})

if err != nil {
return fmt.Errorf("failed to update kibana saved dashboard: %v error: %v", dashboardRequest, err)
Expand Down Expand Up @@ -178,5 +201,32 @@ func createKibanaDashboardCreateRequestFromResourceData(d *schema.ResourceData)
request.WithKibanaSavedObjectMeta(&kibana.SearchKibanaSavedObjectMeta{SearchSourceJSON: searchMeta})
}

references := readDashboardReferencesFromResource(d)
if len(references) > 0 {
request.WithReferences(references)
}

return request.Build()
}

func flattenDashboardReferences(refs []*kibana.DashboardReferences) []interface{} {
if refs == nil {
return nil
}

out := make([]interface{}, 0)

for _, ref := range refs {
if ref == nil {
continue
}

out = append(out, map[string]interface{}{
"id": ref.Id,
"name": ref.Name,
"type": ref.Type.String(),
})
}

return out
}
Loading

0 comments on commit 08dabed

Please sign in to comment.