github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/cobbler/resource_cobbler_kickstart_file.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 resourceKickstartFile() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceKickstartFileCreate,
    14  		Read:   resourceKickstartFileRead,
    15  		Update: resourceKickstartFileUpdate,
    16  		Delete: resourceKickstartFileDelete,
    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 resourceKickstartFileCreate(d *schema.ResourceData, meta interface{}) error {
    34  	config := meta.(*Config)
    35  
    36  	ks := cobbler.KickstartFile{
    37  		Name: d.Get("name").(string),
    38  		Body: d.Get("body").(string),
    39  	}
    40  
    41  	log.Printf("[DEBUG] Cobbler KickstartFile: Create Options: %#v", ks)
    42  
    43  	if err := config.cobblerClient.CreateKickstartFile(ks); err != nil {
    44  		return fmt.Errorf("Cobbler KickstartFile: Error Creating: %s", err)
    45  	}
    46  
    47  	d.SetId(ks.Name)
    48  
    49  	return resourceKickstartFileRead(d, meta)
    50  }
    51  
    52  func resourceKickstartFileRead(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 resourceKickstartFileUpdate(d *schema.ResourceData, meta interface{}) error {
    59  	config := meta.(*Config)
    60  
    61  	ks := cobbler.KickstartFile{
    62  		Name: d.Id(),
    63  		Body: d.Get("body").(string),
    64  	}
    65  
    66  	log.Printf("[DEBUG] Cobbler KickstartFile: Updating Kickstart (%s) with options: %+v", d.Id(), ks)
    67  
    68  	if err := config.cobblerClient.CreateKickstartFile(ks); err != nil {
    69  		return fmt.Errorf("Cobbler KickstartFile: Error Updating (%s): %s", d.Id(), err)
    70  	}
    71  
    72  	return resourceKickstartFileRead(d, meta)
    73  }
    74  
    75  func resourceKickstartFileDelete(d *schema.ResourceData, meta interface{}) error {
    76  	config := meta.(*Config)
    77  
    78  	if err := config.cobblerClient.DeleteKickstartFile(d.Id()); err != nil {
    79  		return fmt.Errorf("Cobbler KickstartFile: Error Deleting (%s): %s", d.Id(), err)
    80  	}
    81  
    82  	return nil
    83  }