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

     1  package chef
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  
     9  	chefc "github.com/go-chef/chef"
    10  )
    11  
    12  func resourceChefNode() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: CreateNode,
    15  		Update: UpdateNode,
    16  		Read:   ReadNode,
    17  		Delete: DeleteNode,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"name": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  				ForceNew: true,
    24  			},
    25  			"environment_name": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Optional: true,
    28  				Default:  "_default",
    29  			},
    30  			"automatic_attributes_json": &schema.Schema{
    31  				Type:      schema.TypeString,
    32  				Optional:  true,
    33  				Default:   "{}",
    34  				StateFunc: jsonStateFunc,
    35  			},
    36  			"normal_attributes_json": &schema.Schema{
    37  				Type:      schema.TypeString,
    38  				Optional:  true,
    39  				Default:   "{}",
    40  				StateFunc: jsonStateFunc,
    41  			},
    42  			"default_attributes_json": &schema.Schema{
    43  				Type:      schema.TypeString,
    44  				Optional:  true,
    45  				Default:   "{}",
    46  				StateFunc: jsonStateFunc,
    47  			},
    48  			"override_attributes_json": &schema.Schema{
    49  				Type:      schema.TypeString,
    50  				Optional:  true,
    51  				Default:   "{}",
    52  				StateFunc: jsonStateFunc,
    53  			},
    54  			"run_list": &schema.Schema{
    55  				Type:     schema.TypeList,
    56  				Optional: true,
    57  				Elem: &schema.Schema{
    58  					Type:      schema.TypeString,
    59  					StateFunc: runListEntryStateFunc,
    60  				},
    61  			},
    62  		},
    63  	}
    64  }
    65  
    66  func CreateNode(d *schema.ResourceData, meta interface{}) error {
    67  	client := meta.(*chefc.Client)
    68  
    69  	node, err := nodeFromResourceData(d)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	_, err = client.Nodes.Post(*node)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	d.SetId(node.Name)
    80  	return ReadNode(d, meta)
    81  }
    82  
    83  func UpdateNode(d *schema.ResourceData, meta interface{}) error {
    84  	client := meta.(*chefc.Client)
    85  
    86  	node, err := nodeFromResourceData(d)
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	_, err = client.Nodes.Put(*node)
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	d.SetId(node.Name)
    97  	return ReadNode(d, meta)
    98  }
    99  
   100  func ReadNode(d *schema.ResourceData, meta interface{}) error {
   101  	client := meta.(*chefc.Client)
   102  
   103  	name := d.Id()
   104  
   105  	node, err := client.Nodes.Get(name)
   106  	if err != nil {
   107  		if errRes, ok := err.(*chefc.ErrorResponse); ok {
   108  			if errRes.Response.StatusCode == 404 {
   109  				d.SetId("")
   110  				return nil
   111  			}
   112  		} else {
   113  			return err
   114  		}
   115  	}
   116  
   117  	d.Set("name", node.Name)
   118  	d.Set("environment_name", node.Environment)
   119  
   120  	automaticAttrJson, err := json.Marshal(node.AutomaticAttributes)
   121  	if err != nil {
   122  		return err
   123  	}
   124  	d.Set("automatic_attributes_json", automaticAttrJson)
   125  
   126  	normalAttrJson, err := json.Marshal(node.NormalAttributes)
   127  	if err != nil {
   128  		return err
   129  	}
   130  	d.Set("normal_attributes_json", normalAttrJson)
   131  
   132  	defaultAttrJson, err := json.Marshal(node.DefaultAttributes)
   133  	if err != nil {
   134  		return err
   135  	}
   136  	d.Set("default_attributes_json", defaultAttrJson)
   137  
   138  	overrideAttrJson, err := json.Marshal(node.OverrideAttributes)
   139  	if err != nil {
   140  		return err
   141  	}
   142  	d.Set("override_attributes_json", overrideAttrJson)
   143  
   144  	runListI := make([]interface{}, len(node.RunList))
   145  	for i, v := range node.RunList {
   146  		runListI[i] = v
   147  	}
   148  	d.Set("run_list", runListI)
   149  
   150  	return nil
   151  }
   152  
   153  func DeleteNode(d *schema.ResourceData, meta interface{}) error {
   154  	client := meta.(*chefc.Client)
   155  
   156  	name := d.Id()
   157  	err := client.Nodes.Delete(name)
   158  
   159  	if err == nil {
   160  		d.SetId("")
   161  	}
   162  
   163  	return err
   164  }
   165  
   166  func nodeFromResourceData(d *schema.ResourceData) (*chefc.Node, error) {
   167  
   168  	node := &chefc.Node{
   169  		Name:        d.Get("name").(string),
   170  		Environment: d.Get("environment_name").(string),
   171  		ChefType:    "node",
   172  		JsonClass:   "Chef::Node",
   173  	}
   174  
   175  	var err error
   176  
   177  	err = json.Unmarshal(
   178  		[]byte(d.Get("automatic_attributes_json").(string)),
   179  		&node.AutomaticAttributes,
   180  	)
   181  	if err != nil {
   182  		return nil, fmt.Errorf("automatic_attributes_json: %s", err)
   183  	}
   184  
   185  	err = json.Unmarshal(
   186  		[]byte(d.Get("normal_attributes_json").(string)),
   187  		&node.NormalAttributes,
   188  	)
   189  	if err != nil {
   190  		return nil, fmt.Errorf("normal_attributes_json: %s", err)
   191  	}
   192  
   193  	err = json.Unmarshal(
   194  		[]byte(d.Get("default_attributes_json").(string)),
   195  		&node.DefaultAttributes,
   196  	)
   197  	if err != nil {
   198  		return nil, fmt.Errorf("default_attributes_json: %s", err)
   199  	}
   200  
   201  	err = json.Unmarshal(
   202  		[]byte(d.Get("override_attributes_json").(string)),
   203  		&node.OverrideAttributes,
   204  	)
   205  	if err != nil {
   206  		return nil, fmt.Errorf("override_attributes_json: %s", err)
   207  	}
   208  
   209  	runListI := d.Get("run_list").([]interface{})
   210  	node.RunList = make([]string, len(runListI))
   211  	for i, vI := range runListI {
   212  		node.RunList[i] = vI.(string)
   213  	}
   214  
   215  	return node, nil
   216  }