github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/builtin/providers/datadog/resource_datadog_timeboard.go (about) 1 package datadog 2 3 import ( 4 "fmt" 5 "log" 6 "strconv" 7 "strings" 8 9 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/zorkian/go-datadog-api" 11 ) 12 13 func resourceDatadogTimeboard() *schema.Resource { 14 15 request := &schema.Schema{ 16 Type: schema.TypeList, 17 Required: true, 18 Elem: &schema.Resource{ 19 Schema: map[string]*schema.Schema{ 20 "q": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 }, 24 "stacked": &schema.Schema{ 25 Type: schema.TypeBool, 26 Optional: true, 27 Default: false, 28 }, 29 }, 30 }, 31 } 32 33 graph := &schema.Schema{ 34 Type: schema.TypeList, 35 Required: true, 36 Description: "A list of graph definitions.", 37 Elem: &schema.Resource{ 38 Schema: map[string]*schema.Schema{ 39 "title": &schema.Schema{ 40 Type: schema.TypeString, 41 Required: true, 42 Description: "The name of the graph.", 43 }, 44 "viz": &schema.Schema{ 45 Type: schema.TypeString, 46 Required: true, 47 }, 48 "request": request, 49 }, 50 }, 51 } 52 53 template_variable := &schema.Schema{ 54 Type: schema.TypeList, 55 Optional: true, 56 Description: "A list of template variables for using Dashboard templating.", 57 Elem: &schema.Resource{ 58 Schema: map[string]*schema.Schema{ 59 "name": &schema.Schema{ 60 Type: schema.TypeString, 61 Required: true, 62 Description: "The name of the variable.", 63 }, 64 "prefix": &schema.Schema{ 65 Type: schema.TypeString, 66 Optional: true, 67 Description: "The tag prefix associated with the variable. Only tags with this prefix will appear in the variable dropdown.", 68 }, 69 "default": &schema.Schema{ 70 Type: schema.TypeString, 71 Optional: true, 72 Description: "The default value for the template variable on dashboard load.", 73 }, 74 }, 75 }, 76 } 77 78 return &schema.Resource{ 79 Create: resourceDatadogTimeboardCreate, 80 Update: resourceDatadogTimeboardUpdate, 81 Read: resourceDatadogTimeboardRead, 82 Delete: resourceDatadogTimeboardDelete, 83 Exists: resourceDatadogTimeboardExists, 84 85 Schema: map[string]*schema.Schema{ 86 "title": &schema.Schema{ 87 Type: schema.TypeString, 88 Required: true, 89 Description: "The name of the dashboard.", 90 }, 91 "description": &schema.Schema{ 92 Type: schema.TypeString, 93 Required: true, 94 Description: "A description of the dashboard's content.", 95 }, 96 "read_only": &schema.Schema{ 97 Type: schema.TypeBool, 98 Optional: true, 99 Default: false, 100 }, 101 "graph": graph, 102 "template_variable": template_variable, 103 }, 104 } 105 } 106 107 func buildTemplateVariables(terraformTemplateVariables *[]interface{}) *[]datadog.TemplateVariable { 108 datadogTemplateVariables := make([]datadog.TemplateVariable, len(*terraformTemplateVariables)) 109 for i, t_ := range *terraformTemplateVariables { 110 t := t_.(map[string]interface{}) 111 datadogTemplateVariables[i] = datadog.TemplateVariable{ 112 Name: t["name"].(string), 113 Prefix: t["prefix"].(string), 114 Default: t["default"].(string)} 115 } 116 return &datadogTemplateVariables 117 } 118 119 func appendRequests(datadogGraph *datadog.Graph, terraformRequests *[]interface{}) { 120 for _, t_ := range *terraformRequests { 121 t := t_.(map[string]interface{}) 122 d := struct { 123 Query string `json:"q"` 124 Stacked bool `json:"stacked"` 125 }{Query: t["q"].(string)} 126 if stacked, ok := t["stacked"]; ok { 127 d.Stacked = stacked.(bool) 128 } 129 datadogGraph.Definition.Requests = append(datadogGraph.Definition.Requests, d) 130 } 131 } 132 133 func buildGraphs(terraformGraphs *[]interface{}) *[]datadog.Graph { 134 datadogGraphs := make([]datadog.Graph, len(*terraformGraphs)) 135 for i, t_ := range *terraformGraphs { 136 t := t_.(map[string]interface{}) 137 datadogGraphs[i] = datadog.Graph{Title: t["title"].(string)} 138 d := &datadogGraphs[i] 139 d.Definition.Viz = t["viz"].(string) 140 terraformRequests := t["request"].([]interface{}) 141 appendRequests(d, &terraformRequests) 142 } 143 return &datadogGraphs 144 } 145 146 func buildTimeboard(d *schema.ResourceData) (*datadog.Dashboard, error) { 147 var id int 148 if d.Id() != "" { 149 var err error 150 id, err = strconv.Atoi(d.Id()) 151 if err != nil { 152 return nil, err 153 } 154 } 155 terraformGraphs := d.Get("graph").([]interface{}) 156 terraformTemplateVariables := d.Get("template_variable").([]interface{}) 157 return &datadog.Dashboard{ 158 Id: id, 159 Title: d.Get("title").(string), 160 Description: d.Get("description").(string), 161 ReadOnly: d.Get("read_only").(bool), 162 Graphs: *buildGraphs(&terraformGraphs), 163 TemplateVariables: *buildTemplateVariables(&terraformTemplateVariables), 164 }, nil 165 } 166 167 func resourceDatadogTimeboardCreate(d *schema.ResourceData, meta interface{}) error { 168 timeboard, err := buildTimeboard(d) 169 if err != nil { 170 return fmt.Errorf("Failed to parse resource configuration: %s", err.Error()) 171 } 172 timeboard, err = meta.(*datadog.Client).CreateDashboard(timeboard) 173 if err != nil { 174 return fmt.Errorf("Failed to create timeboard using Datadog API: %s", err.Error()) 175 } 176 d.SetId(strconv.Itoa(timeboard.Id)) 177 return nil 178 } 179 180 func resourceDatadogTimeboardUpdate(d *schema.ResourceData, meta interface{}) error { 181 timeboard, err := buildTimeboard(d) 182 if err != nil { 183 return fmt.Errorf("Failed to parse resource configuration: %s", err.Error()) 184 } 185 if err = meta.(*datadog.Client).UpdateDashboard(timeboard); err != nil { 186 return fmt.Errorf("Failed to update timeboard using Datadog API: %s", err.Error()) 187 } 188 return resourceDatadogTimeboardRead(d, meta) 189 } 190 191 func resourceDatadogTimeboardRead(d *schema.ResourceData, meta interface{}) error { 192 id, err := strconv.Atoi(d.Id()) 193 if err != nil { 194 return err 195 } 196 timeboard, err := meta.(*datadog.Client).GetDashboard(id) 197 if err != nil { 198 return err 199 } 200 log.Printf("[DEBUG] timeboard: %v", timeboard) 201 d.Set("title", timeboard.Title) 202 d.Set("description", timeboard.Description) 203 d.Set("graphs", timeboard.Graphs) 204 d.Set("template_variables", timeboard.TemplateVariables) 205 return nil 206 } 207 208 func resourceDatadogTimeboardDelete(d *schema.ResourceData, meta interface{}) error { 209 id, err := strconv.Atoi(d.Id()) 210 if err != nil { 211 return err 212 } 213 if err = meta.(*datadog.Client).DeleteDashboard(id); err != nil { 214 return err 215 } 216 return nil 217 } 218 219 func resourceDatadogTimeboardExists(d *schema.ResourceData, meta interface{}) (b bool, e error) { 220 id, err := strconv.Atoi(d.Id()) 221 if err != nil { 222 return false, err 223 } 224 if _, err = meta.(*datadog.Client).GetDashboard(id); err != nil { 225 if strings.Contains(err.Error(), "404 Not Found") { 226 return false, nil 227 } 228 return false, err 229 } 230 return true, nil 231 }