github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/cobbler/resource_cobbler_distro.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 resourceDistro() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceDistroCreate,
    14  		Read:   resourceDistroRead,
    15  		Update: resourceDistroUpdate,
    16  		Delete: resourceDistroDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"arch": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Optional: true,
    22  				ForceNew: true,
    23  				Computed: true,
    24  			},
    25  
    26  			"breed": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  				ForceNew: true,
    30  			},
    31  
    32  			"boot_files": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Optional: true,
    35  				ForceNew: true,
    36  				Computed: true,
    37  			},
    38  
    39  			"comment": &schema.Schema{
    40  				Type:     schema.TypeString,
    41  				Optional: true,
    42  				Computed: true,
    43  			},
    44  
    45  			"fetchable_files": &schema.Schema{
    46  				Type:     schema.TypeString,
    47  				Optional: true,
    48  				Computed: true,
    49  			},
    50  
    51  			"kernel": &schema.Schema{
    52  				Type:     schema.TypeString,
    53  				Required: true,
    54  			},
    55  
    56  			"kernel_options": &schema.Schema{
    57  				Type:     schema.TypeString,
    58  				Optional: true,
    59  				Computed: true,
    60  			},
    61  
    62  			"kernel_options_post": &schema.Schema{
    63  				Type:     schema.TypeString,
    64  				Optional: true,
    65  				Computed: true,
    66  			},
    67  
    68  			"initrd": &schema.Schema{
    69  				Type:     schema.TypeString,
    70  				Required: true,
    71  			},
    72  
    73  			"mgmt_classes": &schema.Schema{
    74  				Type:     schema.TypeList,
    75  				Optional: true,
    76  				Elem:     &schema.Schema{Type: schema.TypeString},
    77  				Computed: true,
    78  			},
    79  
    80  			"name": &schema.Schema{
    81  				Type:     schema.TypeString,
    82  				Required: true,
    83  			},
    84  
    85  			"os_version": &schema.Schema{
    86  				Type:     schema.TypeString,
    87  				Required: true,
    88  			},
    89  
    90  			"owners": &schema.Schema{
    91  				Type:     schema.TypeList,
    92  				Optional: true,
    93  				Elem:     &schema.Schema{Type: schema.TypeString},
    94  				Computed: true,
    95  			},
    96  
    97  			"redhat_management_key": &schema.Schema{
    98  				Type:     schema.TypeString,
    99  				Optional: true,
   100  				Computed: true,
   101  			},
   102  
   103  			"redhat_management_server": &schema.Schema{
   104  				Type:     schema.TypeString,
   105  				Optional: true,
   106  				Computed: true,
   107  			},
   108  
   109  			"template_files": &schema.Schema{
   110  				Type:     schema.TypeString,
   111  				Optional: true,
   112  				Computed: true,
   113  			},
   114  		},
   115  	}
   116  }
   117  
   118  func resourceDistroCreate(d *schema.ResourceData, meta interface{}) error {
   119  	config := meta.(*Config)
   120  
   121  	// Create a cobblerclient.Distro
   122  	distro := buildDistro(d, config)
   123  
   124  	// Attempte to create the Distro
   125  	log.Printf("[DEBUG] Cobbler Distro: Create Options: %#v", distro)
   126  	newDistro, err := config.cobblerClient.CreateDistro(distro)
   127  	if err != nil {
   128  		return fmt.Errorf("Cobbler Distro: Error Creating: %s", err)
   129  	}
   130  
   131  	d.SetId(newDistro.Name)
   132  
   133  	return resourceDistroRead(d, meta)
   134  }
   135  
   136  func resourceDistroRead(d *schema.ResourceData, meta interface{}) error {
   137  	config := meta.(*Config)
   138  
   139  	// Retrieve the distro from cobbler
   140  	distro, err := config.cobblerClient.GetDistro(d.Id())
   141  	if err != nil {
   142  		return fmt.Errorf("Cobbler Distro: Error Reading (%s): %s", d.Id(), err)
   143  	}
   144  
   145  	// Set all fields
   146  	d.Set("arch", distro.Arch)
   147  	d.Set("breed", distro.Breed)
   148  	d.Set("boot_files", distro.BootFiles)
   149  	d.Set("comment", distro.Comment)
   150  	d.Set("fetchable_files", distro.FetchableFiles)
   151  	d.Set("kernel", distro.Kernel)
   152  	d.Set("kernel_options", distro.KernelOptions)
   153  	d.Set("kernel_options_post", distro.KernelOptionsPost)
   154  	d.Set("initrd", distro.Initrd)
   155  	d.Set("mgmt_classes", distro.MGMTClasses)
   156  	d.Set("os_version", distro.OSVersion)
   157  	d.Set("owners", distro.Owners)
   158  	d.Set("redhat_management_key", distro.RedHatManagementKey)
   159  	d.Set("redhat_management_server", distro.RedHatManagementServer)
   160  	d.Set("template_files", distro.TemplateFiles)
   161  
   162  	return nil
   163  }
   164  
   165  func resourceDistroUpdate(d *schema.ResourceData, meta interface{}) error {
   166  	config := meta.(*Config)
   167  
   168  	// create a cobblerclient.Distro
   169  	distro := buildDistro(d, config)
   170  
   171  	// Attempt to updateh the distro with new information
   172  	log.Printf("[DEBUG] Cobbler Distro: Updating Distro (%s) with options: %+v", d.Id(), distro)
   173  	err := config.cobblerClient.UpdateDistro(&distro)
   174  	if err != nil {
   175  		return fmt.Errorf("Cobbler Distro: Error Updating (%s): %s", d.Id(), err)
   176  	}
   177  
   178  	return resourceDistroRead(d, meta)
   179  }
   180  
   181  func resourceDistroDelete(d *schema.ResourceData, meta interface{}) error {
   182  	config := meta.(*Config)
   183  
   184  	// Attempt to delete the distro
   185  	if err := config.cobblerClient.DeleteDistro(d.Id()); err != nil {
   186  		return fmt.Errorf("Cobbler Distro: Error Deleting (%s): %s", d.Id(), err)
   187  	}
   188  
   189  	return nil
   190  }
   191  
   192  // buildDistro builds a cobbler.Distro from the Terraform attributes
   193  func buildDistro(d *schema.ResourceData, meta interface{}) cobbler.Distro {
   194  	mgmtClasses := []string{}
   195  	for _, i := range d.Get("mgmt_classes").([]interface{}) {
   196  		mgmtClasses = append(mgmtClasses, i.(string))
   197  	}
   198  
   199  	owners := []string{}
   200  	for _, i := range d.Get("owners").([]interface{}) {
   201  		owners = append(owners, i.(string))
   202  	}
   203  
   204  	distro := cobbler.Distro{
   205  		Arch:                   d.Get("arch").(string),
   206  		Breed:                  d.Get("breed").(string),
   207  		BootFiles:              d.Get("boot_files").(string),
   208  		Comment:                d.Get("comment").(string),
   209  		FetchableFiles:         d.Get("fetchable_files").(string),
   210  		Kernel:                 d.Get("kernel").(string),
   211  		KernelOptions:          d.Get("kernel_options").(string),
   212  		KernelOptionsPost:      d.Get("kernel_options_post").(string),
   213  		Initrd:                 d.Get("initrd").(string),
   214  		MGMTClasses:            mgmtClasses,
   215  		Name:                   d.Get("name").(string),
   216  		OSVersion:              d.Get("os_version").(string),
   217  		Owners:                 owners,
   218  		RedHatManagementKey:    d.Get("redhat_management_key").(string),
   219  		RedHatManagementServer: d.Get("redhat_management_server").(string),
   220  		TemplateFiles:          d.Get("template_files").(string),
   221  	}
   222  
   223  	return distro
   224  }