Skip to content

Commit

Permalink
Switch to structured login
Browse files Browse the repository at this point in the history
  • Loading branch information
SchoolGuy committed Oct 9, 2024
1 parent fb98fbe commit 8ddc43b
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 56 deletions.
19 changes: 11 additions & 8 deletions cobbler/resource_cobbler_distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package cobbler

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"log"

cobbler "github.com/cobbler/cobblerclient"
"github.com/fatih/structs"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -125,8 +125,10 @@ func resourceDistroCreate(ctx context.Context, d *schema.ResourceData, meta inte
// Create a cobblerclient.Distro
distro := buildDistro(d, config)

// Attempte to create the Distro
log.Printf("[DEBUG] Cobbler Distro: Create Options: %#v", distro)
// Attempt to create the Distro
tflog.Debug(ctx, "Cobbler Distro Create Options", map[string]interface{}{
"options": structs.Map(distro),
})
newDistro, err := config.cobblerClient.CreateDistro(distro)
if err != nil {
return diag.Errorf("Cobbler Distro: Error Creating: %s", err)
Expand All @@ -143,7 +145,6 @@ func resourceDistroRead(ctx context.Context, d *schema.ResourceData, meta interf
// Retrieve the distro from cobbler
distro, err := config.cobblerClient.GetDistro(d.Id())
if err != nil {
//goland:noinspection GoErrorStringFormat
return diag.Errorf("Cobbler Distro: Error Reading (%s): %s", d.Id(), err)
}

Expand Down Expand Up @@ -174,10 +175,12 @@ func resourceDistroUpdate(ctx context.Context, d *schema.ResourceData, meta inte
distro := buildDistro(d, config)

// Attempt to updateh the distro with new information
log.Printf("[DEBUG] Cobbler Distro: Updating Distro (%s) with options: %+v", d.Id(), distro)
tflog.Debug(ctx, "Cobbler Distro: Updating Distro", map[string]interface{}{
"distro": d.Id(),
"options": structs.Map(distro),
})
err := config.cobblerClient.UpdateDistro(&distro)
if err != nil {
//goland:noinspection GoErrorStringFormat
return diag.Errorf("Cobbler Distro: Error Updating (%s): %s", d.Id(), err)
}

Expand Down
17 changes: 10 additions & 7 deletions cobbler/resource_cobbler_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package cobbler
import (
"context"
cobbler "github.com/cobbler/cobblerclient"
"github.com/fatih/structs"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"log"
)

func resourceProfile() *schema.Resource {
Expand Down Expand Up @@ -229,10 +230,11 @@ func resourceProfileCreate(ctx context.Context, d *schema.ResourceData, meta int
profile := buildProfile(d, config)

// Attempt to create the Profile
log.Printf("[DEBUG] Cobbler Profile: Create Options: %#v", profile)
tflog.Debug(ctx, "Cobbler Profile: Create Options", map[string]interface{}{
"options": structs.Map(profile),
})
newProfile, err := config.cobblerClient.CreateProfile(profile)
if err != nil {
//goland:noinspection GoErrorStringFormat
return diag.Errorf("Cobbler Profile: Error Creating: %s", err)
}

Expand All @@ -247,7 +249,6 @@ func resourceProfileRead(ctx context.Context, d *schema.ResourceData, meta inter
// Retrieve the profile entry from Cobbler
profile, err := config.cobblerClient.GetProfile(d.Id())
if err != nil {
//goland:noinspection GoErrorStringFormat
return diag.Errorf("Cobbler Profile: Error Reading (%s): %s", d.Id(), err)
}

Expand Down Expand Up @@ -293,11 +294,13 @@ func resourceProfileUpdate(ctx context.Context, d *schema.ResourceData, meta int
profile := buildProfile(d, config)

// Attempt to update the profile with new information
log.Printf("[DEBUG] Cobbler Profile: Updating Profile (%s) with options: %+v", d.Id(), profile)
tflog.Debug(ctx, "Cobbler Profile: Updating Profile with options", map[string]interface{}{
"profile": d.Id(),
"options": structs.Map(profile),
})
err := config.cobblerClient.UpdateProfile(&profile)
if err != nil {
//goland:noinspection GoErrorStringFormat
return diag.Errorf("Cobbler Profile: Error Updating (%s): %s", d.Id(), err)
return diag.Errorf("error updating Cobbler Profile: Error Updating (%s): %s", d.Id(), err)
}

return resourceProfileRead(ctx, d, meta)
Expand Down
31 changes: 22 additions & 9 deletions cobbler/resource_cobbler_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package cobbler

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"log"

cobbler "github.com/cobbler/cobblerclient"
"github.com/fatih/structs"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -116,7 +116,9 @@ func resourceRepoCreate(ctx context.Context, d *schema.ResourceData, meta interf
repo := buildRepo(d, config)

// Attempt to create the Repo
log.Printf("[DEBUG] Cobbler Repo: Create Options: %#v", repo)
tflog.Debug(ctx, "Cobbler Repo: Create Options", map[string]interface{}{
"options": structs.Map(repo),
})
newRepo, err := config.cobblerClient.CreateRepo(repo)
if err != nil {
return diag.Errorf("Cobbler Repo: Error Creating: %s", err)
Expand Down Expand Up @@ -149,22 +151,30 @@ func resourceRepoRead(ctx context.Context, d *schema.ResourceData, meta interfac

err = d.Set("apt_components", repo.AptComponents)
if err != nil {
log.Printf("[DEBUG] Unable to set apt_components: %s", err)
tflog.Debug(ctx, "Unable to set apt_components", map[string]interface{}{
"error": err,
})
}

err = d.Set("apt_dists", repo.AptDists)
if err != nil {
log.Printf("[DEBUG] Unable to set apt_dists: %s", err)
tflog.Debug(ctx, "Unable to set apt_dists", map[string]interface{}{
"error": err,
})
}

err = d.Set("owners", repo.Owners)
if err != nil {
log.Printf("[DEBUG] Unable to set owners: %s", err)
tflog.Debug(ctx, "Unable to set owners", map[string]interface{}{
"error": err,
})
}

err = d.Set("rpm_list", repo.RpmList)
if err != nil {
log.Printf("[DEBUG] Unable to set rpm_list: %s", err)
tflog.Debug(ctx, "Unable to set rpm_list", map[string]interface{}{
"error": err,
})
}

return nil
Expand All @@ -177,7 +187,10 @@ func resourceRepoUpdate(ctx context.Context, d *schema.ResourceData, meta interf
repo := buildRepo(d, config)

// Attempt to updateh the repo with new information
log.Printf("[DEBUG] Cobbler Repo: Updating Repo (%s) with options: %+v", d.Id(), repo)
tflog.Debug(ctx, "Cobbler Repo: Updating Repo with options", map[string]interface{}{
"repo": d.Id(),
"options": structs.Map(repo),
})
err := config.cobblerClient.UpdateRepo(&repo)
if err != nil {
return diag.Errorf("Cobbler Repo: Error Updating (%s): %s", d.Id(), err)
Expand Down
18 changes: 10 additions & 8 deletions cobbler/resource_cobbler_snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package cobbler

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"log"

cobbler "github.com/cobbler/cobblerclient"
"github.com/fatih/structs"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -41,10 +41,11 @@ func resourceSnippetCreate(ctx context.Context, d *schema.ResourceData, meta int
Body: d.Get("body").(string),
}

log.Printf("[DEBUG] Cobbler Snippet: Create Options: %#v", snippet)
tflog.Debug(ctx, "Cobbler Snippet: Create Options", map[string]interface{}{
"options": structs.Map(snippet),
})

if err := config.cobblerClient.CreateSnippet(snippet); err != nil {
//goland:noinspection GoErrorStringFormat
return diag.Errorf("Cobbler Snippet: Error Creating: %s", err)
}

Expand All @@ -67,10 +68,12 @@ func resourceSnippetUpdate(ctx context.Context, d *schema.ResourceData, meta int
Body: d.Get("body").(string),
}

log.Printf("[DEBUG] Cobbler Snippet: Updating Snippet (%s) with options: %+v", d.Id(), snippet)
tflog.Debug(ctx, "Cobbler Snippet: Updating Snippet with options", map[string]interface{}{
"snippet": d.Id(),
"options": structs.Map(snippet),
})

if err := config.cobblerClient.CreateSnippet(snippet); err != nil {
//goland:noinspection GoErrorStringFormat
return diag.Errorf("Cobbler Snippet: Error Updating (%s): %s", d.Id(), err)
}

Expand All @@ -81,7 +84,6 @@ func resourceSnippetDelete(ctx context.Context, d *schema.ResourceData, meta int
config := meta.(*Config)

if err := config.cobblerClient.DeleteSnippet(d.Id()); err != nil {
//goland:noinspection GoErrorStringFormat
return diag.Errorf("Cobbler Snippet: Error Deleting (%s): %s", d.Id(), err)
}

Expand Down
59 changes: 43 additions & 16 deletions cobbler/resource_cobbler_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"context"
"fmt"
"github.com/fatih/structs"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"log"
Expand Down Expand Up @@ -432,7 +434,9 @@ func resourceSystemCreate(ctx context.Context, d *schema.ResourceData, meta inte
system := buildSystem(d)

// Attempt to create the System
log.Printf("[DEBUG] Cobbler System: Create Options: %#v", system)
tflog.Debug(ctx, "Cobbler System: Create Options", map[string]interface{}{
"options": structs.Map(system),
})
newSystem, err := config.cobblerClient.CreateSystem(system)
if err != nil {
return diag.Errorf("Cobbler System: Error Creating: %s", err)
Expand All @@ -443,16 +447,21 @@ func resourceSystemCreate(ctx context.Context, d *schema.ResourceData, meta inte

// Add each interface to the system
for interfaceName, interfaceInfo := range interfaces {
log.Printf("[DEBUG] Cobbler System Interface %#v: %#v", interfaceName, interfaceInfo)
tflog.Debug(ctx, "Cobbler System Interface", map[string]interface{}{
"interface": interfaceName,
"options": structs.Map(interfaceInfo),
})
if err := newSystem.CreateInterface(interfaceName, interfaceInfo); err != nil {
return diag.Errorf("Cobbler System: Error adding Interface %s to %s: %s", interfaceName, newSystem.Name, err)
}
}

log.Printf("[DEBUG] Cobbler System: Created System: %#v", newSystem)
tflog.Debug(ctx, "Cobbler System: Created System", map[string]interface{}{
"system": structs.Map(newSystem),
})
d.SetId(newSystem.Name)

log.Printf("[DEBUG] Cobbler System: syncing system")
tflog.Debug(ctx, "Cobbler System: syncing system")
if err := config.cobblerClient.Sync(); err != nil {
return diag.Errorf("Cobbler System: Error syncing system: %s", err)
}
Expand All @@ -462,13 +471,17 @@ func resourceSystemCreate(ctx context.Context, d *schema.ResourceData, meta inte

func resourceSystemRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
log.Printf("[DEBUG] Reading Cobbler system %s\n", d.Id())
tflog.Debug(ctx, "Reading Cobbler system", map[string]interface{}{
"system": d.Id(),
})

// Retrieve the system entry from Cobbler
system, err := config.cobblerClient.GetSystem(d.Id())
if err != nil {
if strings.Contains(err.Error(), "not found") {
log.Printf("[WARN] Cobbler System (%s) not found, removing from state", d.Id())
tflog.Warn(ctx, "Cobbler System not found, removing from state", map[string]interface{}{
"system": d.Id(),
})
d.SetId("")
return nil
}
Expand Down Expand Up @@ -519,14 +532,16 @@ func resourceSystemRead(ctx context.Context, d *schema.ResourceData, meta interf
// Get all interfaces that the System has
allInterfaces, err := system.GetInterfaces()
if err != nil {
//goland:noinspection GoErrorStringFormat
return diag.Errorf("Cobbler System %s: Error getting interfaces: %s", system.Name, err)
}

// Build a generic map array with the interface attributes
var systemInterfaces []map[string]interface{}
for interfaceName, interfaceInfo := range allInterfaces {
log.Printf("[DEBUG] Cobbler System Interface %#v: %#v", interfaceName, interfaceInfo)
tflog.Debug(ctx, "Cobbler System Interface", map[string]interface{}{
"interface": interfaceName,
"options": structs.Map(interfaceInfo),
})
iface := make(map[string]interface{})
iface["name"] = interfaceName
iface["cnames"] = interfaceInfo.CNAMEs
Expand Down Expand Up @@ -554,7 +569,6 @@ func resourceSystemRead(ctx context.Context, d *schema.ResourceData, meta interf

err = d.Set("interface", systemInterfaces)
if err != nil {
//goland:noinspection GoErrorStringFormat
return diag.Errorf("Cobbler System %s: Error appending interface to : %s", system.Name, err)
}

Expand All @@ -570,7 +584,6 @@ func resourceSystemUpdate(ctx context.Context, d *schema.ResourceData, meta inte
// Retrieve the existing system entry from Cobbler
system, err := config.cobblerClient.GetSystem(d.Id())
if err != nil {
//goland:noinspection GoErrorStringFormat
return diag.Errorf("Cobbler System: Error Reading (%s): %s", d.Id(), err)
}

Expand All @@ -579,16 +592,24 @@ func resourceSystemUpdate(ctx context.Context, d *schema.ResourceData, meta inte
if err != nil {
return diag.Errorf("error getting interfaces: %s", err)
}
log.Printf("[DEBUG] Cobbler System Interfaces: %#v", currentInterfaces)
interfaceMap := make(map[string]map[string]interface{})
for interfaceName, interfaceInfo := range currentInterfaces {
interfaceMap[interfaceName] = structs.Map(interfaceInfo)
}
tflog.Debug(ctx, "Cobbler System Interfaces", map[string]interface{}{
"interfaces": interfaceMap,
})

// Create a new cobblerclient.System struct with the new information
newSystem := buildSystem(d)

// Attempt to update the system with new information
log.Printf("[DEBUG] Cobbler System: Updating System (%s) with options: %+v", d.Id(), system)
tflog.Debug(ctx, "Cobbler System: Updating System with options", map[string]interface{}{
"system": d.Id(),
"options": structs.Map(system),
})
err = config.cobblerClient.UpdateSystem(&newSystem)
if err != nil {
//goland:noinspection GoErrorStringFormat
return diag.Errorf("Cobbler System: Error Updating (%s): %s", d.Id(), err)
}

Expand All @@ -604,7 +625,10 @@ func resourceSystemUpdate(ctx context.Context, d *schema.ResourceData, meta inte
for interfaceName, interfaceInfo := range oldIfaces {
if _, ok := newIfaces[interfaceName]; !ok {
// Interface does not exist in the new set, so it has been removed from terraform.
log.Printf("[DEBUG] Cobbler System: Deleting Interface %#v: %#v", interfaceName, interfaceInfo)
tflog.Debug(ctx, "Cobbler System: Deleting Interface", map[string]interface{}{
"interface": interfaceName,
"options": structs.Map(interfaceInfo),
})

if err := system.DeleteInterface(interfaceName); err != nil {
return diag.Errorf("Cobbler System: Error deleting Interface %s to %s: %s", interfaceName, system.Name, err)
Expand All @@ -614,15 +638,18 @@ func resourceSystemUpdate(ctx context.Context, d *schema.ResourceData, meta inte

// Modify interfaces that have changed
for interfaceName, interfaceInfo := range newIfaces {
log.Printf("[DEBUG] Cobbler System: New Interface %#v: %#v", interfaceName, interfaceInfo)
tflog.Debug(ctx, "Cobbler System: New Interface", map[string]interface{}{
"interface": interfaceName,
"options": structs.Map(interfaceInfo),
})

if err := system.CreateInterface(interfaceName, interfaceInfo); err != nil {
return diag.Errorf("Cobbler System: Error adding Interface %s to %s: %s", interfaceName, system.Name, err)
}
}
}

log.Printf("[DEBUG] Cobbler System: syncing system")
tflog.Debug(ctx, "Cobbler System: syncing system")
if err := config.cobblerClient.Sync(); err != nil {
return diag.Errorf("Cobbler System: Error syncing system: %s", err)
}
Expand Down
Loading

0 comments on commit 8ddc43b

Please sign in to comment.