github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/grafana/resource_dashboard.go (about) 1 package grafana 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 9 gapi "github.com/apparentlymart/go-grafana-api" 10 ) 11 12 func ResourceDashboard() *schema.Resource { 13 return &schema.Resource{ 14 Create: CreateDashboard, 15 Delete: DeleteDashboard, 16 Read: ReadDashboard, 17 18 Schema: map[string]*schema.Schema{ 19 "slug": &schema.Schema{ 20 Type: schema.TypeString, 21 Computed: true, 22 }, 23 24 "config_json": &schema.Schema{ 25 Type: schema.TypeString, 26 Required: true, 27 ForceNew: true, 28 StateFunc: NormalizeDashboardConfigJSON, 29 ValidateFunc: ValidateDashboardConfigJSON, 30 }, 31 }, 32 } 33 } 34 35 func CreateDashboard(d *schema.ResourceData, meta interface{}) error { 36 client := meta.(*gapi.Client) 37 38 model := prepareDashboardModel(d.Get("config_json").(string)) 39 40 resp, err := client.SaveDashboard(model, false) 41 if err != nil { 42 return err 43 } 44 45 d.SetId(resp.Slug) 46 47 return ReadDashboard(d, meta) 48 } 49 50 func ReadDashboard(d *schema.ResourceData, meta interface{}) error { 51 client := meta.(*gapi.Client) 52 53 slug := d.Id() 54 55 dashboard, err := client.Dashboard(slug) 56 if err != nil { 57 return err 58 } 59 60 configJSONBytes, err := json.Marshal(dashboard.Model) 61 if err != nil { 62 return err 63 } 64 65 configJSON := NormalizeDashboardConfigJSON(string(configJSONBytes)) 66 67 d.SetId(dashboard.Meta.Slug) 68 d.Set("slug", dashboard.Meta.Slug) 69 d.Set("config_json", configJSON) 70 71 return nil 72 } 73 74 func DeleteDashboard(d *schema.ResourceData, meta interface{}) error { 75 client := meta.(*gapi.Client) 76 77 slug := d.Id() 78 return client.DeleteDashboard(slug) 79 } 80 81 func prepareDashboardModel(configJSON string) map[string]interface{} { 82 configMap := map[string]interface{}{} 83 err := json.Unmarshal([]byte(configJSON), &configMap) 84 if err != nil { 85 // The validate function should've taken care of this. 86 panic(fmt.Errorf("Invalid JSON got into prepare func")) 87 } 88 89 delete(configMap, "id") 90 configMap["version"] = 0 91 92 return configMap 93 } 94 95 func ValidateDashboardConfigJSON(configI interface{}, k string) ([]string, []error) { 96 configJSON := configI.(string) 97 configMap := map[string]interface{}{} 98 err := json.Unmarshal([]byte(configJSON), &configMap) 99 if err != nil { 100 return nil, []error{err} 101 } 102 return nil, nil 103 } 104 105 func NormalizeDashboardConfigJSON(configI interface{}) string { 106 configJSON := configI.(string) 107 108 configMap := map[string]interface{}{} 109 err := json.Unmarshal([]byte(configJSON), &configMap) 110 if err != nil { 111 // The validate function should've taken care of this. 112 return "" 113 } 114 115 // Some properties are managed by this provider and are thus not 116 // significant when included in the JSON. 117 delete(configMap, "id") 118 delete(configMap, "version") 119 120 ret, err := json.Marshal(configMap) 121 if err != nil { 122 // Should never happen. 123 return configJSON 124 } 125 126 return string(ret) 127 }