github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/cobbler/resource_cobbler_profile.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 resourceProfile() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceProfileCreate, 14 Read: resourceProfileRead, 15 Update: resourceProfileUpdate, 16 Delete: resourceProfileDelete, 17 18 Schema: map[string]*schema.Schema{ 19 "boot_files": &schema.Schema{ 20 Type: schema.TypeString, 21 Optional: true, 22 Computed: true, 23 }, 24 25 "comment": &schema.Schema{ 26 Type: schema.TypeString, 27 Optional: true, 28 Computed: true, 29 }, 30 31 "distro": &schema.Schema{ 32 Type: schema.TypeString, 33 Required: true, 34 }, 35 36 "enable_gpxe": &schema.Schema{ 37 Type: schema.TypeBool, 38 Optional: true, 39 Computed: true, 40 }, 41 42 "enable_menu": &schema.Schema{ 43 Type: schema.TypeBool, 44 Optional: true, 45 Computed: true, 46 }, 47 48 "fetchable_files": &schema.Schema{ 49 Type: schema.TypeString, 50 Optional: true, 51 Computed: true, 52 }, 53 54 "kernel_options": &schema.Schema{ 55 Type: schema.TypeString, 56 Optional: true, 57 Computed: true, 58 }, 59 60 "kernel_options_post": &schema.Schema{ 61 Type: schema.TypeString, 62 Optional: true, 63 Computed: true, 64 }, 65 66 "kickstart": &schema.Schema{ 67 Type: schema.TypeString, 68 Optional: true, 69 Computed: true, 70 }, 71 72 "ks_meta": &schema.Schema{ 73 Type: schema.TypeString, 74 Optional: true, 75 Computed: true, 76 }, 77 78 "mgmt_classes": &schema.Schema{ 79 Type: schema.TypeList, 80 Optional: true, 81 Computed: true, 82 Elem: &schema.Schema{Type: schema.TypeString}, 83 }, 84 85 "mgmt_parameters": &schema.Schema{ 86 Type: schema.TypeString, 87 Optional: true, 88 Computed: true, 89 }, 90 91 "name": &schema.Schema{ 92 Type: schema.TypeString, 93 Required: true, 94 ForceNew: true, 95 }, 96 97 "name_servers_search": &schema.Schema{ 98 Type: schema.TypeList, 99 Optional: true, 100 Computed: true, 101 Elem: &schema.Schema{Type: schema.TypeString}, 102 }, 103 104 "name_servers": &schema.Schema{ 105 Type: schema.TypeList, 106 Optional: true, 107 Computed: true, 108 Elem: &schema.Schema{Type: schema.TypeString}, 109 }, 110 111 "owners": &schema.Schema{ 112 Type: schema.TypeList, 113 Optional: true, 114 Computed: true, 115 Elem: &schema.Schema{Type: schema.TypeString}, 116 }, 117 118 "parent": &schema.Schema{ 119 Type: schema.TypeString, 120 Optional: true, 121 Computed: true, 122 }, 123 124 "proxy": &schema.Schema{ 125 Type: schema.TypeString, 126 Optional: true, 127 Computed: true, 128 }, 129 130 "redhat_management_key": &schema.Schema{ 131 Type: schema.TypeString, 132 Optional: true, 133 Computed: true, 134 }, 135 136 "redhat_management_server": &schema.Schema{ 137 Type: schema.TypeString, 138 Optional: true, 139 Computed: true, 140 }, 141 142 "repos": &schema.Schema{ 143 Type: schema.TypeList, 144 Optional: true, 145 Computed: true, 146 Elem: &schema.Schema{Type: schema.TypeString}, 147 }, 148 149 "server": &schema.Schema{ 150 Type: schema.TypeString, 151 Optional: true, 152 Computed: true, 153 }, 154 155 "template_files": &schema.Schema{ 156 Type: schema.TypeString, 157 Optional: true, 158 Computed: true, 159 }, 160 161 "template_remote_kickstarts": &schema.Schema{ 162 Type: schema.TypeInt, 163 Optional: true, 164 Computed: true, 165 }, 166 167 "virt_auto_boot": &schema.Schema{ 168 Type: schema.TypeString, 169 Optional: true, 170 Computed: true, 171 }, 172 173 "virt_bridge": &schema.Schema{ 174 Type: schema.TypeString, 175 Optional: true, 176 Computed: true, 177 }, 178 179 "virt_cpus": &schema.Schema{ 180 Type: schema.TypeString, 181 Optional: true, 182 Computed: true, 183 }, 184 185 "virt_disk_driver": &schema.Schema{ 186 Type: schema.TypeString, 187 Optional: true, 188 Computed: true, 189 }, 190 191 "virt_file_size": &schema.Schema{ 192 Type: schema.TypeString, 193 Optional: true, 194 Computed: true, 195 }, 196 197 "virt_path": &schema.Schema{ 198 Type: schema.TypeString, 199 Optional: true, 200 Computed: true, 201 }, 202 203 "virt_ram": &schema.Schema{ 204 Type: schema.TypeString, 205 Optional: true, 206 Computed: true, 207 }, 208 209 "virt_type": &schema.Schema{ 210 Type: schema.TypeString, 211 Optional: true, 212 Computed: true, 213 }, 214 }, 215 } 216 } 217 218 func resourceProfileCreate(d *schema.ResourceData, meta interface{}) error { 219 config := meta.(*Config) 220 221 // Create a cobblerclient.Profile struct 222 profile := buildProfile(d, config) 223 224 // Attempt to create the Profile 225 log.Printf("[DEBUG] Cobbler Profile: Create Options: %#v", profile) 226 newProfile, err := config.cobblerClient.CreateProfile(profile) 227 if err != nil { 228 return fmt.Errorf("Cobbler Profile: Error Creating: %s", err) 229 } 230 231 d.SetId(newProfile.Name) 232 233 return resourceProfileRead(d, meta) 234 } 235 236 func resourceProfileRead(d *schema.ResourceData, meta interface{}) error { 237 config := meta.(*Config) 238 239 // Retrieve the profile entry from Cobbler 240 profile, err := config.cobblerClient.GetProfile(d.Id()) 241 if err != nil { 242 return fmt.Errorf("Cobbler Profile: Error Reading (%s): %s", d.Id(), err) 243 } 244 245 // Set all fields 246 d.Set("boot_files", profile.BootFiles) 247 d.Set("comment", profile.Comment) 248 d.Set("distro", profile.Distro) 249 d.Set("enable_gpxe", profile.EnableGPXE) 250 d.Set("enable_menu", profile.EnableMenu) 251 d.Set("fetchable_files", profile.FetchableFiles) 252 d.Set("kernel_options", profile.KernelOptions) 253 d.Set("kernel_options_post", profile.KernelOptionsPost) 254 d.Set("kickstart", profile.Kickstart) 255 d.Set("ks_meta", profile.KSMeta) 256 d.Set("mgmt_classes", profile.MGMTClasses) 257 d.Set("mgmt_parameters", profile.MGMTParameters) 258 d.Set("name_servers_search", profile.NameServersSearch) 259 d.Set("name_servers", profile.NameServers) 260 d.Set("owners", profile.Owners) 261 d.Set("proxy", profile.Proxy) 262 d.Set("redhat_management_key", profile.RedHatManagementKey) 263 d.Set("redhat_management_server", profile.RedHatManagementServer) 264 d.Set("repos", profile.Repos) 265 d.Set("template_files", profile.TemplateFiles) 266 d.Set("template_remote_kickstarts", profile.TemplateRemoteKickstarts) 267 d.Set("virt_auto_boot", profile.VirtAutoBoot) 268 d.Set("virt_bridge", profile.VirtBridge) 269 d.Set("virt_cpus", profile.VirtCPUs) 270 d.Set("virt_disk_driver", profile.VirtDiskDriver) 271 d.Set("virt_file_size", profile.VirtFileSize) 272 d.Set("virt_path", profile.VirtPath) 273 d.Set("virt_ram", profile.VirtRam) 274 d.Set("virt_type", profile.VirtType) 275 276 return nil 277 } 278 279 func resourceProfileUpdate(d *schema.ResourceData, meta interface{}) error { 280 config := meta.(*Config) 281 282 // Create a cobblerclient.Profile struct 283 profile := buildProfile(d, config) 284 285 // Attempt to update the profile with new information 286 log.Printf("[DEBUG] Cobbler Profile: Updating Profile (%s) with options: %+v", d.Id(), profile) 287 err := config.cobblerClient.UpdateProfile(&profile) 288 if err != nil { 289 return fmt.Errorf("Cobbler Profile: Error Updating (%s): %s", d.Id(), err) 290 } 291 292 return resourceProfileRead(d, meta) 293 } 294 295 func resourceProfileDelete(d *schema.ResourceData, meta interface{}) error { 296 config := meta.(*Config) 297 298 // Attempt to delete the profile 299 if err := config.cobblerClient.DeleteProfile(d.Id()); err != nil { 300 return fmt.Errorf("Cobbler Profile: Error Deleting (%s): %s", d.Id(), err) 301 } 302 303 return nil 304 } 305 306 // buildProfile builds a cobblerclient.Profile out of the Terraform attributes 307 func buildProfile(d *schema.ResourceData, meta interface{}) cobbler.Profile { 308 mgmtClasses := []string{} 309 for _, i := range d.Get("mgmt_classes").([]interface{}) { 310 mgmtClasses = append(mgmtClasses, i.(string)) 311 } 312 313 nameServersSearch := []string{} 314 for _, i := range d.Get("name_servers_search").([]interface{}) { 315 nameServersSearch = append(nameServersSearch, i.(string)) 316 } 317 318 nameServers := []string{} 319 for _, i := range d.Get("name_servers").([]interface{}) { 320 nameServers = append(nameServers, i.(string)) 321 } 322 323 owners := []string{} 324 for _, i := range d.Get("owners").([]interface{}) { 325 owners = append(owners, i.(string)) 326 } 327 328 repos := []string{} 329 for _, i := range d.Get("repos").([]interface{}) { 330 repos = append(repos, i.(string)) 331 } 332 333 profile := cobbler.Profile{ 334 BootFiles: d.Get("boot_files").(string), 335 Comment: d.Get("comment").(string), 336 Distro: d.Get("distro").(string), 337 EnableGPXE: d.Get("enable_gpxe").(bool), 338 EnableMenu: d.Get("enable_menu").(bool), 339 FetchableFiles: d.Get("fetchable_files").(string), 340 KernelOptions: d.Get("kernel_options").(string), 341 KernelOptionsPost: d.Get("kernel_options_post").(string), 342 Kickstart: d.Get("kickstart").(string), 343 KSMeta: d.Get("ks_meta").(string), 344 MGMTClasses: mgmtClasses, 345 MGMTParameters: d.Get("mgmt_parameters").(string), 346 Name: d.Get("name").(string), 347 NameServersSearch: nameServersSearch, 348 NameServers: nameServers, 349 Owners: owners, 350 Parent: d.Get("parent").(string), 351 Proxy: d.Get("proxy").(string), 352 RedHatManagementKey: d.Get("redhat_management_key").(string), 353 RedHatManagementServer: d.Get("redhat_management_server").(string), 354 Repos: repos, 355 Server: d.Get("server").(string), 356 TemplateFiles: d.Get("template_files").(string), 357 TemplateRemoteKickstarts: d.Get("template_remote_kickstarts").(int), 358 VirtAutoBoot: d.Get("virt_auto_boot").(string), 359 VirtBridge: d.Get("virt_bridge").(string), 360 VirtCPUs: d.Get("virt_cpus").(string), 361 VirtDiskDriver: d.Get("virt_disk_driver").(string), 362 VirtFileSize: d.Get("virt_file_size").(string), 363 VirtPath: d.Get("virt_path").(string), 364 VirtRam: d.Get("virt_ram").(string), 365 VirtType: d.Get("virt_type").(string), 366 } 367 368 return profile 369 }