Skip to content

Commit

Permalink
feat(resources): refactor managed software update configuration to ro…
Browse files Browse the repository at this point in the history
…ot level attributes and simplify state management
  • Loading branch information
ShocOne committed Sep 17, 2024
1 parent 5f21d27 commit 161a805
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 98 deletions.
56 changes: 22 additions & 34 deletions internal/resources/managedsoftwareupdates/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,44 +41,32 @@ func construct(d *schema.ResourceData) (*jamfpro.ResourceManagedSoftwareUpdatePl
}
}

// Handle config
if v, ok := d.GetOk("config"); ok {
configList := v.([]interface{})
if len(configList) > 0 {
config := configList[0].(map[string]interface{})
// Now handle the fields that were previously inside the config block
resource.Config.UpdateAction = d.Get("update_action").(string)
resource.Config.VersionType = d.Get("version_type").(string)

// Set common required fields
resource.Config.UpdateAction = config["update_action"].(string)
resource.Config.VersionType = config["version_type"].(string)

// Set SpecificVersion, default to "NO_SPECIFIC_VERSION"
if v, ok := config["specific_version"]; ok && v.(string) != "" {
resource.Config.SpecificVersion = v.(string)
} else {
resource.Config.SpecificVersion = "NO_SPECIFIC_VERSION"
}
if v, ok := d.GetOk("specific_version"); ok {
resource.Config.SpecificVersion = v.(string)
} else {
resource.Config.SpecificVersion = "NO_SPECIFIC_VERSION"
}

// Set BuildVersion, default to empty string if not present
if v, ok := config["build_version"]; ok && v.(string) != "" {
resource.Config.BuildVersion = v.(string)
} else {
resource.Config.BuildVersion = ""
}
if v, ok := d.GetOk("build_version"); ok {
resource.Config.BuildVersion = v.(string)
} else {
resource.Config.BuildVersion = ""
}

// Set MaxDeferrals, default to 0 if not applicable
if v, ok := config["max_deferrals"]; ok {
resource.Config.MaxDeferrals = v.(int)
} else {
resource.Config.MaxDeferrals = 0
}
if v, ok := d.GetOk("max_deferrals"); ok {
resource.Config.MaxDeferrals = v.(int)
} else {
resource.Config.MaxDeferrals = 0
}

// Set ForceInstallLocalDateTime, default to empty string if not present
if v, ok := config["force_install_local_date_time"]; ok && v.(string) != "" {
resource.Config.ForceInstallLocalDateTime = v.(string)
} else {
resource.Config.ForceInstallLocalDateTime = ""
}
}
if v, ok := d.GetOk("force_install_local_date_time"); ok {
resource.Config.ForceInstallLocalDateTime = v.(string)
} else {
resource.Config.ForceInstallLocalDateTime = ""
}

// Debug logging for config specifically
Expand Down
21 changes: 4 additions & 17 deletions internal/resources/managedsoftwareupdates/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,14 @@ func create(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.

func read(ctx context.Context, d *schema.ResourceData, meta interface{}, cleanup bool) diag.Diagnostics {
client := meta.(*jamfpro.Client)
resourceUUID := d.Id()
var diags diag.Diagnostics
resourceUUID := d.Id()

var response *jamfpro.ResponseManagedSoftwareUpdatePlan

var response *jamfpro.ResponseManagedSoftwareUpdatePlanList
err := retry.RetryContext(ctx, d.Timeout(schema.TimeoutRead), func() *retry.RetryError {
var apiErr error
groupId := d.Get("group.0.group_id").(string)
objectType := d.Get("group.0.object_type").(string)
if groupId != "" && objectType != "" {
response, apiErr = client.GetManagedSoftwareUpdatePlansByGroupID(groupId, objectType)
} else {
// Assuming there's a method to get a single plan by UUID
var singlePlan *jamfpro.ResponseManagedSoftwareUpdatePlan
singlePlan, apiErr = client.GetManagedSoftwareUpdatePlanByUUID(resourceUUID)
if singlePlan != nil {
response = &jamfpro.ResponseManagedSoftwareUpdatePlanList{
TotalCount: 1,
Results: []jamfpro.ResponseManagedSoftwareUpdatePlan{*singlePlan},
}
}
}
response, apiErr = client.GetManagedSoftwareUpdatePlanByUUID(resourceUUID)
if apiErr != nil {
return retry.RetryableError(apiErr)
}
Expand Down
81 changes: 37 additions & 44 deletions internal/resources/managedsoftwareupdates/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,50 +58,43 @@ func ResourceJamfProManagedSoftwareUpdate() *schema.Resource {
},
},
},
"config": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"update_action": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"DOWNLOAD_ONLY", "DOWNLOAD_INSTALL", "DOWNLOAD_INSTALL_ALLOW_DEFERRAL", "DOWNLOAD_INSTALL_RESTART", "DOWNLOAD_INSTALL_SCHEDULE", "UNKNOWN"}, false),
Description: "The software update action to perform.",
},
"version_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"LATEST_MAJOR", "LATEST_MINOR", "LATEST_ANY", "SPECIFIC_VERSION", "CUSTOM_VERSION", "UNKNOWN"}, false),
Description: "The type of version to update to.",
},
"specific_version": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"15.0", "14.7", "14.6.1", "14.6", "14.5", "13.7", "13.6.9", "13.6.8", "13.6.7", "12.7.6", "12.7.5", "11.7.10", "NO_SPECIFIC_VERSION"}, false),
Description: "Optional. Indicates the specific version to update to. Only available when the version type is set to specific version or custom version, otherwise defaults to NO_SPECIFIC_VERSION.",
},
"build_version": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Optional. Indicates the build version to update to. Only available when the version type is set to CUSTOM_VERSION.",
},
"max_deferrals": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntAtLeast(0),
Description: "Required when the provided update_action is DOWNLOAD_INSTALL_ALLOW_DEFERRAL, not applicable to all managed software update plans",
},
"force_install_local_date_time": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Optional. Indicates the local date and time of the device to force update by.",
},
},
},

// Root level attributes, previously in the config block
"update_action": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"DOWNLOAD_ONLY", "DOWNLOAD_INSTALL", "DOWNLOAD_INSTALL_ALLOW_DEFERRAL", "DOWNLOAD_INSTALL_RESTART", "DOWNLOAD_INSTALL_SCHEDULE", "UNKNOWN"}, false),
Description: "The software update action to perform.",
},
"version_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"LATEST_MAJOR", "LATEST_MINOR", "LATEST_ANY", "SPECIFIC_VERSION", "CUSTOM_VERSION", "UNKNOWN"}, false),
Description: "The type of version to update to.",
},
"specific_version": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"15.0", "14.7", "14.6.1", "14.6", "14.5", "13.7", "13.6.9", "13.6.8", "13.6.7", "12.7.6", "12.7.5", "11.7.10", "NO_SPECIFIC_VERSION"}, false),
Description: "Optional. Indicates the specific version to update to. Only available when the version type is set to specific version or custom version, otherwise defaults to NO_SPECIFIC_VERSION.",
},
"build_version": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Optional. Indicates the build version to update to. Only available when the version type is set to CUSTOM_VERSION.",
},
"max_deferrals": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntAtLeast(0),
Description: "Required when the provided update_action is DOWNLOAD_INSTALL_ALLOW_DEFERRAL, not applicable to all managed software update plans.",
},
"force_install_local_date_time": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Optional. Indicates the local date and time of the device to force update by.",
},
},
}
Expand Down
5 changes: 2 additions & 3 deletions internal/resources/managedsoftwareupdates/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import (

// updateState updates the Terraform state with the latest ResponseManagedSoftwareUpdatePlan
// information from the Jamf Pro API.
func updateState(d *schema.ResourceData, response *jamfpro.ResponseManagedSoftwareUpdatePlanList) diag.Diagnostics {
if response == nil || len(response.Results) == 0 {
func updateState(d *schema.ResourceData, plan *jamfpro.ResponseManagedSoftwareUpdatePlan) diag.Diagnostics {
if plan == nil {
return diag.Errorf("no managed software update plan found in the response")
}

plan := response.Results[0]
d.SetId(plan.PlanUuid)
if err := d.Set("plan_uuid", plan.PlanUuid); err != nil {
return diag.FromErr(fmt.Errorf("error setting plan_uuid: %v", err))
Expand Down

0 comments on commit 161a805

Please sign in to comment.