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