github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/chef/resource_environment.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 resourceChefEnvironment() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: CreateEnvironment,
    15  		Update: UpdateEnvironment,
    16  		Read:   ReadEnvironment,
    17  		Delete: DeleteEnvironment,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"name": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  				ForceNew: true,
    24  			},
    25  			"description": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Optional: true,
    28  				Default:  "Managed by Terraform",
    29  			},
    30  			"default_attributes_json": &schema.Schema{
    31  				Type:      schema.TypeString,
    32  				Optional:  true,
    33  				Default:   "{}",
    34  				StateFunc: jsonStateFunc,
    35  			},
    36  			"override_attributes_json": &schema.Schema{
    37  				Type:      schema.TypeString,
    38  				Optional:  true,
    39  				Default:   "{}",
    40  				StateFunc: jsonStateFunc,
    41  			},
    42  			"cookbook_constraints": &schema.Schema{
    43  				Type:     schema.TypeMap,
    44  				Optional: true,
    45  				Elem: &schema.Schema{
    46  					Type: schema.TypeString,
    47  				},
    48  			},
    49  		},
    50  	}
    51  }
    52  
    53  func CreateEnvironment(d *schema.ResourceData, meta interface{}) error {
    54  	client := meta.(*chefc.Client)
    55  
    56  	env, err := environmentFromResourceData(d)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	_, err = client.Environments.Create(env)
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	d.SetId(env.Name)
    67  	return ReadEnvironment(d, meta)
    68  }
    69  
    70  func UpdateEnvironment(d *schema.ResourceData, meta interface{}) error {
    71  	client := meta.(*chefc.Client)
    72  
    73  	env, err := environmentFromResourceData(d)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	_, err = client.Environments.Put(env)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	d.SetId(env.Name)
    84  	return ReadEnvironment(d, meta)
    85  }
    86  
    87  func ReadEnvironment(d *schema.ResourceData, meta interface{}) error {
    88  	client := meta.(*chefc.Client)
    89  
    90  	name := d.Id()
    91  
    92  	env, err := client.Environments.Get(name)
    93  	if err != nil {
    94  		if errRes, ok := err.(*chefc.ErrorResponse); ok {
    95  			if errRes.Response.StatusCode == 404 {
    96  				d.SetId("")
    97  				return nil
    98  			}
    99  		} else {
   100  			return err
   101  		}
   102  	}
   103  
   104  	d.Set("name", env.Name)
   105  	d.Set("description", env.Description)
   106  
   107  	defaultAttrJson, err := json.Marshal(env.DefaultAttributes)
   108  	if err != nil {
   109  		return err
   110  	}
   111  	d.Set("default_attributes_json", defaultAttrJson)
   112  
   113  	overrideAttrJson, err := json.Marshal(env.OverrideAttributes)
   114  	if err != nil {
   115  		return err
   116  	}
   117  	d.Set("override_attributes_json", overrideAttrJson)
   118  
   119  	cookbookVersionsI := map[string]interface{}{}
   120  	for k, v := range env.CookbookVersions {
   121  		cookbookVersionsI[k] = v
   122  	}
   123  	d.Set("cookbook_constraints", cookbookVersionsI)
   124  
   125  	return nil
   126  }
   127  
   128  func DeleteEnvironment(d *schema.ResourceData, meta interface{}) error {
   129  	client := meta.(*chefc.Client)
   130  
   131  	name := d.Id()
   132  
   133  	// For some reason Environments.Delete is not exposed by the
   134  	// underlying client library, so we have to do this manually.
   135  
   136  	path := fmt.Sprintf("environments/%s", name)
   137  
   138  	httpReq, err := client.NewRequest("DELETE", path, nil)
   139  	if err != nil {
   140  		return err
   141  	}
   142  
   143  	_, err = client.Do(httpReq, nil)
   144  	if err == nil {
   145  		d.SetId("")
   146  	}
   147  
   148  	return err
   149  }
   150  
   151  func environmentFromResourceData(d *schema.ResourceData) (*chefc.Environment, error) {
   152  
   153  	env := &chefc.Environment{
   154  		Name:        d.Get("name").(string),
   155  		Description: d.Get("description").(string),
   156  		ChefType:    "environment",
   157  	}
   158  
   159  	var err error
   160  
   161  	err = json.Unmarshal(
   162  		[]byte(d.Get("default_attributes_json").(string)),
   163  		&env.DefaultAttributes,
   164  	)
   165  	if err != nil {
   166  		return nil, fmt.Errorf("default_attributes_json: %s", err)
   167  	}
   168  
   169  	err = json.Unmarshal(
   170  		[]byte(d.Get("override_attributes_json").(string)),
   171  		&env.OverrideAttributes,
   172  	)
   173  	if err != nil {
   174  		return nil, fmt.Errorf("override_attributes_json: %s", err)
   175  	}
   176  
   177  	env.CookbookVersions = make(map[string]string)
   178  	for k, vI := range d.Get("cookbook_constraints").(map[string]interface{}) {
   179  		env.CookbookVersions[k] = vI.(string)
   180  	}
   181  
   182  	return env, nil
   183  }