github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/grafana/resource_data_source.go (about)

     1  package grafana
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  
     9  	gapi "github.com/apparentlymart/go-grafana-api"
    10  )
    11  
    12  func ResourceDataSource() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: CreateDataSource,
    15  		Update: UpdateDataSource,
    16  		Delete: DeleteDataSource,
    17  		Read:   ReadDataSource,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"id": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Computed: true,
    23  			},
    24  
    25  			"type": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  			},
    29  
    30  			"name": &schema.Schema{
    31  				Type:     schema.TypeString,
    32  				Required: true,
    33  			},
    34  
    35  			"url": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Required: true,
    38  			},
    39  
    40  			"is_default": &schema.Schema{
    41  				Type:     schema.TypeBool,
    42  				Optional: true,
    43  				Default:  false,
    44  			},
    45  
    46  			"basic_auth_enabled": &schema.Schema{
    47  				Type:     schema.TypeBool,
    48  				Optional: true,
    49  				Default:  false,
    50  			},
    51  
    52  			"basic_auth_username": &schema.Schema{
    53  				Type:     schema.TypeString,
    54  				Optional: true,
    55  				Default:  "",
    56  			},
    57  
    58  			"basic_auth_password": &schema.Schema{
    59  				Type:     schema.TypeString,
    60  				Optional: true,
    61  				Default:  "",
    62  			},
    63  
    64  			"username": &schema.Schema{
    65  				Type:     schema.TypeString,
    66  				Optional: true,
    67  				Default:  "",
    68  			},
    69  
    70  			"password": &schema.Schema{
    71  				Type:     schema.TypeString,
    72  				Optional: true,
    73  				Default:  "",
    74  			},
    75  
    76  			"database_name": &schema.Schema{
    77  				Type:     schema.TypeString,
    78  				Optional: true,
    79  				Default:  "",
    80  			},
    81  
    82  			"access_mode": &schema.Schema{
    83  				Type:     schema.TypeString,
    84  				Optional: true,
    85  				Default:  "proxy",
    86  			},
    87  		},
    88  	}
    89  }
    90  
    91  func CreateDataSource(d *schema.ResourceData, meta interface{}) error {
    92  	client := meta.(*gapi.Client)
    93  
    94  	dataSource, err := makeDataSource(d)
    95  	if err != nil {
    96  		return err
    97  	}
    98  
    99  	id, err := client.NewDataSource(dataSource)
   100  	if err != nil {
   101  		return err
   102  	}
   103  
   104  	d.SetId(strconv.FormatInt(id, 10))
   105  
   106  	return ReadDataSource(d, meta)
   107  }
   108  
   109  func UpdateDataSource(d *schema.ResourceData, meta interface{}) error {
   110  	client := meta.(*gapi.Client)
   111  
   112  	dataSource, err := makeDataSource(d)
   113  	if err != nil {
   114  		return err
   115  	}
   116  
   117  	return client.UpdateDataSource(dataSource)
   118  }
   119  
   120  func ReadDataSource(d *schema.ResourceData, meta interface{}) error {
   121  	client := meta.(*gapi.Client)
   122  
   123  	idStr := d.Id()
   124  	id, err := strconv.ParseInt(idStr, 10, 64)
   125  	if err != nil {
   126  		return fmt.Errorf("Invalid id: %#v", idStr)
   127  	}
   128  
   129  	dataSource, err := client.DataSource(id)
   130  	if err != nil {
   131  		return err
   132  	}
   133  
   134  	d.Set("id", dataSource.Id)
   135  	d.Set("access_mode", dataSource.Access)
   136  	d.Set("basic_auth_enabled", dataSource.BasicAuth)
   137  	d.Set("basic_auth_username", dataSource.BasicAuthUser)
   138  	d.Set("basic_auth_password", dataSource.BasicAuthPassword)
   139  	d.Set("database_name", dataSource.Database)
   140  	d.Set("is_default", dataSource.IsDefault)
   141  	d.Set("name", dataSource.Name)
   142  	d.Set("password", dataSource.Password)
   143  	d.Set("type", dataSource.Type)
   144  	d.Set("url", dataSource.URL)
   145  	d.Set("username", dataSource.User)
   146  
   147  	return nil
   148  }
   149  
   150  func DeleteDataSource(d *schema.ResourceData, meta interface{}) error {
   151  	client := meta.(*gapi.Client)
   152  
   153  	idStr := d.Id()
   154  	id, err := strconv.ParseInt(idStr, 10, 64)
   155  	if err != nil {
   156  		return fmt.Errorf("Invalid id: %#v", idStr)
   157  	}
   158  
   159  	return client.DeleteDataSource(id)
   160  }
   161  
   162  func makeDataSource(d *schema.ResourceData) (*gapi.DataSource, error) {
   163  	idStr := d.Id()
   164  	var id int64
   165  	var err error
   166  	if idStr != "" {
   167  		id, err = strconv.ParseInt(idStr, 10, 64)
   168  	}
   169  
   170  	return &gapi.DataSource{
   171  		Id:                id,
   172  		Name:              d.Get("name").(string),
   173  		Type:              d.Get("type").(string),
   174  		URL:               d.Get("url").(string),
   175  		Access:            d.Get("access_mode").(string),
   176  		Database:          d.Get("database_name").(string),
   177  		User:              d.Get("username").(string),
   178  		Password:          d.Get("password").(string),
   179  		IsDefault:         d.Get("is_default").(bool),
   180  		BasicAuth:         d.Get("basic_auth_enabled").(bool),
   181  		BasicAuthUser:     d.Get("basic_auth_username").(string),
   182  		BasicAuthPassword: d.Get("basic_auth_password").(string),
   183  	}, err
   184  }