github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/ns1/resource_datafeed.go (about)

     1  package ns1
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/helper/schema"
     5  
     6  	ns1 "gopkg.in/ns1/ns1-go.v2/rest"
     7  	"gopkg.in/ns1/ns1-go.v2/rest/model/data"
     8  )
     9  
    10  func dataFeedResource() *schema.Resource {
    11  	return &schema.Resource{
    12  		Schema: map[string]*schema.Schema{
    13  			"id": &schema.Schema{
    14  				Type:     schema.TypeString,
    15  				Computed: true,
    16  			},
    17  			"source_id": &schema.Schema{
    18  				Type:     schema.TypeString,
    19  				Required: true,
    20  				ForceNew: true,
    21  			},
    22  			"name": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  			},
    26  			"config": &schema.Schema{
    27  				Type:     schema.TypeMap,
    28  				Optional: true,
    29  			},
    30  		},
    31  		Create: DataFeedCreate,
    32  		Read:   DataFeedRead,
    33  		Update: DataFeedUpdate,
    34  		Delete: DataFeedDelete,
    35  	}
    36  }
    37  
    38  func dataFeedToResourceData(d *schema.ResourceData, f *data.Feed) {
    39  	d.SetId(f.ID)
    40  	d.Set("name", f.Name)
    41  	d.Set("config", f.Config)
    42  }
    43  
    44  func resourceDataToDataFeed(d *schema.ResourceData) *data.Feed {
    45  	return &data.Feed{
    46  		Name:     d.Get("name").(string),
    47  		SourceID: d.Get("source_id").(string),
    48  		Config:   d.Get("config").(map[string]interface{}),
    49  	}
    50  }
    51  
    52  // DataFeedCreate creates an ns1 datafeed
    53  func DataFeedCreate(d *schema.ResourceData, meta interface{}) error {
    54  	client := meta.(*ns1.Client)
    55  	f := resourceDataToDataFeed(d)
    56  	if _, err := client.DataFeeds.Create(d.Get("source_id").(string), f); err != nil {
    57  		return err
    58  	}
    59  	dataFeedToResourceData(d, f)
    60  	return nil
    61  }
    62  
    63  // DataFeedRead reads the datafeed for the given ID from ns1
    64  func DataFeedRead(d *schema.ResourceData, meta interface{}) error {
    65  	client := meta.(*ns1.Client)
    66  	f, _, err := client.DataFeeds.Get(d.Get("source_id").(string), d.Id())
    67  	if err != nil {
    68  		return err
    69  	}
    70  	dataFeedToResourceData(d, f)
    71  	return nil
    72  }
    73  
    74  // DataFeedDelete delets the given datafeed from ns1
    75  func DataFeedDelete(d *schema.ResourceData, meta interface{}) error {
    76  	client := meta.(*ns1.Client)
    77  	_, err := client.DataFeeds.Delete(d.Get("source_id").(string), d.Id())
    78  	d.SetId("")
    79  	return err
    80  }
    81  
    82  // DataFeedUpdate updates the given datafeed with modified parameters
    83  func DataFeedUpdate(d *schema.ResourceData, meta interface{}) error {
    84  	client := meta.(*ns1.Client)
    85  	f := resourceDataToDataFeed(d)
    86  	f.ID = d.Id()
    87  	if _, err := client.DataFeeds.Update(d.Get("source_id").(string), f); err != nil {
    88  		return err
    89  	}
    90  	dataFeedToResourceData(d, f)
    91  	return nil
    92  }