github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/chef/resource_role.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 resourceChefRole() *schema.Resource { 13 return &schema.Resource{ 14 Create: CreateRole, 15 Update: UpdateRole, 16 Read: ReadRole, 17 Delete: DeleteRole, 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 "run_list": &schema.Schema{ 43 Type: schema.TypeList, 44 Optional: true, 45 Elem: &schema.Schema{ 46 Type: schema.TypeString, 47 StateFunc: runListEntryStateFunc, 48 }, 49 }, 50 }, 51 } 52 } 53 54 func CreateRole(d *schema.ResourceData, meta interface{}) error { 55 client := meta.(*chefc.Client) 56 57 role, err := roleFromResourceData(d) 58 if err != nil { 59 return err 60 } 61 62 _, err = client.Roles.Create(role) 63 if err != nil { 64 return err 65 } 66 67 d.SetId(role.Name) 68 return ReadRole(d, meta) 69 } 70 71 func UpdateRole(d *schema.ResourceData, meta interface{}) error { 72 client := meta.(*chefc.Client) 73 74 role, err := roleFromResourceData(d) 75 if err != nil { 76 return err 77 } 78 79 _, err = client.Roles.Put(role) 80 if err != nil { 81 return err 82 } 83 84 d.SetId(role.Name) 85 return ReadRole(d, meta) 86 } 87 88 func ReadRole(d *schema.ResourceData, meta interface{}) error { 89 client := meta.(*chefc.Client) 90 91 name := d.Id() 92 93 role, err := client.Roles.Get(name) 94 if err != nil { 95 if errRes, ok := err.(*chefc.ErrorResponse); ok { 96 if errRes.Response.StatusCode == 404 { 97 d.SetId("") 98 return nil 99 } 100 } else { 101 return err 102 } 103 } 104 105 d.Set("name", role.Name) 106 d.Set("description", role.Description) 107 108 defaultAttrJson, err := json.Marshal(role.DefaultAttributes) 109 if err != nil { 110 return err 111 } 112 d.Set("default_attributes_json", defaultAttrJson) 113 114 overrideAttrJson, err := json.Marshal(role.OverrideAttributes) 115 if err != nil { 116 return err 117 } 118 d.Set("override_attributes_json", overrideAttrJson) 119 120 runListI := make([]interface{}, len(role.RunList)) 121 for i, v := range role.RunList { 122 runListI[i] = v 123 } 124 d.Set("run_list", runListI) 125 126 return nil 127 } 128 129 func DeleteRole(d *schema.ResourceData, meta interface{}) error { 130 client := meta.(*chefc.Client) 131 132 name := d.Id() 133 134 // For some reason Roles.Delete is not exposed by the 135 // underlying client library, so we have to do this manually. 136 137 path := fmt.Sprintf("roles/%s", name) 138 139 httpReq, err := client.NewRequest("DELETE", path, nil) 140 if err != nil { 141 return err 142 } 143 144 _, err = client.Do(httpReq, nil) 145 if err == nil { 146 d.SetId("") 147 } 148 149 return err 150 } 151 152 func roleFromResourceData(d *schema.ResourceData) (*chefc.Role, error) { 153 154 role := &chefc.Role{ 155 Name: d.Get("name").(string), 156 Description: d.Get("description").(string), 157 ChefType: "role", 158 } 159 160 var err error 161 162 err = json.Unmarshal( 163 []byte(d.Get("default_attributes_json").(string)), 164 &role.DefaultAttributes, 165 ) 166 if err != nil { 167 return nil, fmt.Errorf("default_attributes_json: %s", err) 168 } 169 170 err = json.Unmarshal( 171 []byte(d.Get("override_attributes_json").(string)), 172 &role.OverrideAttributes, 173 ) 174 if err != nil { 175 return nil, fmt.Errorf("override_attributes_json: %s", err) 176 } 177 178 runListI := d.Get("run_list").([]interface{}) 179 role.RunList = make([]string, len(runListI)) 180 for i, vI := range runListI { 181 role.RunList[i] = vI.(string) 182 } 183 184 return role, nil 185 }