github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/cobbler/resource_cobbler_snippet.go (about)

     1  package cobbler
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	cobbler "github.com/jtopjian/cobblerclient"
     9  )
    10  
    11  func resourceSnippet() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceSnippetCreate,
    14  		Read:   resourceSnippetRead,
    15  		Update: resourceSnippetUpdate,
    16  		Delete: resourceSnippetDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"name": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"body": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  			},
    29  		},
    30  	}
    31  }
    32  
    33  func resourceSnippetCreate(d *schema.ResourceData, meta interface{}) error {
    34  	config := meta.(*Config)
    35  
    36  	snippet := cobbler.Snippet{
    37  		Name: d.Get("name").(string),
    38  		Body: d.Get("body").(string),
    39  	}
    40  
    41  	log.Printf("[DEBUG] Cobbler Snippet: Create Options: %#v", snippet)
    42  
    43  	if err := config.cobblerClient.CreateSnippet(snippet); err != nil {
    44  		return fmt.Errorf("Cobbler Snippet: Error Creating: %s", err)
    45  	}
    46  
    47  	d.SetId(snippet.Name)
    48  
    49  	return resourceSnippetRead(d, meta)
    50  }
    51  
    52  func resourceSnippetRead(d *schema.ResourceData, meta interface{}) error {
    53  	// Since all attributes are required and not computed,
    54  	// there's no reason to read.
    55  	return nil
    56  }
    57  
    58  func resourceSnippetUpdate(d *schema.ResourceData, meta interface{}) error {
    59  	config := meta.(*Config)
    60  
    61  	snippet := cobbler.Snippet{
    62  		Name: d.Id(),
    63  		Body: d.Get("body").(string),
    64  	}
    65  
    66  	log.Printf("[DEBUG] Cobbler Snippet: Updating Snippet (%s) with options: %+v", d.Id(), snippet)
    67  
    68  	if err := config.cobblerClient.CreateSnippet(snippet); err != nil {
    69  		return fmt.Errorf("Cobbler Snippet: Error Updating (%s): %s", d.Id(), err)
    70  	}
    71  
    72  	return resourceSnippetRead(d, meta)
    73  }
    74  
    75  func resourceSnippetDelete(d *schema.ResourceData, meta interface{}) error {
    76  	config := meta.(*Config)
    77  
    78  	if err := config.cobblerClient.DeleteSnippet(d.Id()); err != nil {
    79  		return fmt.Errorf("Cobbler Snippet: Error Deleting (%s): %s", d.Id(), err)
    80  	}
    81  
    82  	return nil
    83  }