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