Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
w0de committed Mar 26, 2024
1 parent 61adc65 commit 6c390e0
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 90 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/yohan460/terraform-provider-jamf
go 1.18

require (
github.com/avast/retry-go v3.0.0+incompatible
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/terraform-plugin-sdk/v2 v2.16.0
github.com/mitchellh/go-homedir v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJE
github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw=
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHSxpiH9JdtuBj0=
github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
github.com/bradfitz/slice v0.0.0-20180809154707-2b758aa73013 h1:/P9/RL0xgWE+ehnCUUN5h3RpG3dmoMCOONO1CCvq23Y=
github.com/bradfitz/slice v0.0.0-20180809154707-2b758aa73013/go.mod h1:pccXHIvs3TV/TUqSNyEvF99sxjX2r4FFRIyw6TZY9+w=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
Expand Down
29 changes: 12 additions & 17 deletions jamf/data_source_jamf_computer_extension_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func dataSourceJamfComputerExtensionAttribute() *schema.Resource {
Default: "Extension Attributes",
ValidateFunc: validation.StringInSlice([]string{"General", "Hardware", "Operating System", "User and Location", "Purchasing", "Extension Attributes"}, false),
},
"recon_display": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"General", "Hardware", "Operating System", "User and Location", "Purchasing", "Extension Attributes"}, false),
},
"script": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -129,23 +134,13 @@ func deconstructJamfComputerExtensionAttributeStruct(d *schema.ResourceData, in
// Input Type
switch inputType := in.InputType.Type; inputType {
case "script":
scriptInterface := map[string]interface{}{
"enabled": in.Enabled,
"platform": in.InputType.Platform,
}

if s, ok := d.GetOk("script"); ok {
for _, v := range s.([]interface{}) {
script := v.(map[string]interface{})

// since file_path is always set in TypeList
if script["file_path"] == "" {
scriptInterface["script_contents"] = in.InputType.Script
}
}
}

d.Set("script", scriptInterface)
d.Set("script", []interface{}{
map[string]interface{}{
"enabled": in.Enabled,
"platform": in.InputType.Platform,
"script_contents": in.InputType.Script,
},
})
case "Text Field":
d.Set("text_field", []interface{}{
map[string]interface{}{
Expand Down
129 changes: 70 additions & 59 deletions jamf/resource_jamf_computer_extension_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"fmt"
"strconv"
"time"

"github.com/avast/retry-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -45,6 +47,11 @@ func resourceJamfComputerExtensionAttribute() *schema.Resource {
Default: "Extension Attributes",
ValidateFunc: validation.StringInSlice([]string{"General", "Hardware", "Operating System", "User and Location", "Purchasing", "Extension Attributes"}, false),
},
"recon_display": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"General", "Hardware", "Operating System", "User and Location", "Purchasing", "Extension Attributes"}, false),
},
"script": {
Type: schema.TypeList,
Optional: true,
Expand All @@ -64,14 +71,8 @@ func resourceJamfComputerExtensionAttribute() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{"Mac", "Windows"}, false),
},
"script_contents": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"script.0.file_path"},
},
"file_path": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"script.0.script_contents"},
Type: schema.TypeString,
Optional: true,
},
},
},
Expand Down Expand Up @@ -128,56 +129,21 @@ func buildJamfComputerExtensionAttributeStruct(d *schema.ResourceData) (*jamf.Co
}

if s, ok := d.GetOk("script"); ok {
for _, v := range s.([]interface{}) {
script := v.(map[string]interface{})

out.InputType.Type = "script"

if v, ok := script["enabled"]; ok {
out.Enabled = v.(bool)
}
out.InputType.Type = "script"

if v, ok := script["platform"]; ok {
out.InputType.Platform = v.(string)
}
script := s.([]interface{})[0].(map[string]interface{})

filePath, hasFilePath := script["file_path"]
if hasFilePath {
if filePath == "" {
hasFilePath = false // since file_path is always set in TypeList
}
}
scriptContents, hasScriptContents := script["script_contents"]
if hasScriptContents {
if scriptContents == "" {
hasScriptContents = false // since script_contents is always set in TypeList
}
}

if hasFilePath && !hasScriptContents {
content, err := loadFileContent(filePath.(string))
if err != nil {
return &out, err
}
out.InputType.Script = content
} else if !hasFilePath && hasScriptContents && scriptContents != "" {
out.InputType.Script = scriptContents.(string)
} else {
return &out, fmt.Errorf("only one of file_path and script_contents must be set")
}
}
}

if _, ok := d.GetOk("text_field"); ok {
out.Enabled = script["enabled"].(bool)
out.InputType.Platform = script["platform"].(string)
out.InputType.Script = script["script_contents"].(string)
} else if _, ok := d.GetOk("text_field"); ok {
out.InputType.Type = "Text Field"
}
} else if s, ok := d.GetOk("popup_menu"); ok {
out.InputType.Type = "Pop-up Menu"

if s, ok := d.GetOk("popup_menu"); ok {
val := s.(*schema.Set).List()
popup := val[0].(map[string]interface{})

out.InputType.Type = "Pop-up Menu"

if v, ok := popup["choices"]; ok {
choices := v.([]interface{})
choicesList := make([]string, len(choices))
Expand Down Expand Up @@ -219,17 +185,39 @@ func resourceJamfComputerExtensionAttributeCreate(ctx context.Context, d *schema

func resourceJamfComputerExtensionAttributeRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
var resp *jamf.ComputerExtensionAttribute
c := m.(*jamf.Client)

id, _ := strconv.Atoi(d.Id())
resp, err := c.GetComputerExtensionAttribute(id)
id, err := strconv.Atoi(d.Id())
if err != nil {
return diag.FromErr(err)
}

err = retry.Do(
func() error {
resp, err = c.GetComputerExtensionAttribute(id)
if err != nil {
return err
}

return nil
},
retry.RetryIf(
func(e error) bool {
if jamfErr, ok := e.(jamf.Error); ok && jamfErr.StatusCode() == 404 {
return true
} else {
return false
}
},
),
retry.Attempts(3),
retry.Delay(2*time.Second),
retry.MaxJitter(1*time.Second),
)

if err != nil {
if jamfErr, ok := err.(jamf.Error); ok && jamfErr.StatusCode() == 404 {
d.SetId("")
} else {
return diag.FromErr(err)
}
return diag.FromErr(err)
} else {
deconstructJamfComputerExtensionAttributeStruct(d, resp)
}
Expand Down Expand Up @@ -260,7 +248,30 @@ func resourceJamfComputerExtensionAttributeDelete(ctx context.Context, d *schema
return diag.FromErr(err)
}

if _, err := c.DeleteComputerExtensionAttribute(id); err != nil {
err = retry.Do(
func() error {
_, err := c.DeleteComputerExtensionAttribute(id)
if err != nil {
return err
}

return nil
},
retry.RetryIf(
func(e error) bool {
if jamfErr, ok := e.(jamf.Error); ok && jamfErr.StatusCode() == 404 {
return true
} else {
return false
}
},
),
retry.Attempts(3),
retry.Delay(2*time.Second),
retry.MaxJitter(1*time.Second),
)

if err != nil {
return diag.FromErr(err)
}

Expand Down
21 changes: 7 additions & 14 deletions jamf/resource_jamf_computer_extension_attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,41 @@ func TestAccJamfComputerExtensionAttribute_basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: resource.ComposeTestCheckFunc(
testAccCheckJamfComputerExtensionAttributeExists("jamf_computer_extension_attribute.extensionattribute-test"),
),
Steps: []resource.TestStep{
{
Config: testAccCheckJamfComputerExtensionAttributeScript,
Check: resource.ComposeTestCheckFunc(
testAccCheckJamfComputerExtensionAttributeExists("jamf_computer_extension_attribute.extensionattribute-script"),
),
},
{
Config: testAccCheckJamfComputerExtensionAttributeTextField,
Check: resource.ComposeTestCheckFunc(
testAccCheckJamfComputerExtensionAttributeExists("jamf_computer_extension_attribute.extensionattribute-textfield"),
),
},
{
Config: testAccCheckJamfComputerExtensionAttributePopup,
Check: resource.ComposeTestCheckFunc(
testAccCheckJamfComputerExtensionAttributeExists("jamf_computer_extension_attribute.extensionattribute-popup"),
),
},
},
})
}

const (
testAccCheckJamfComputerExtensionAttributeScript = `resource "jamf_computer_extension_attribute" "extensionattribute-script" {
testAccCheckJamfComputerExtensionAttributeScript = `resource "jamf_computer_extension_attribute" "extensionattribute-test" {
name = "Terraform test script"
description = "testing jamf extension attribute resource"
data_type = "string"
data_type = "String"
inventory_display = "Extension Attributes"
script {
enabled = false
script_contents = "#!/bin/bash\nprint(\"hello world\")"
}
}`

testAccCheckJamfComputerExtensionAttributeTextField = `resource "jamf_computer_extension_attribute" "extensionattribute-textfield" {
testAccCheckJamfComputerExtensionAttributeTextField = `resource "jamf_computer_extension_attribute" "extensionattribute-test" {
name = "Terraform test textfield"
text_field {}
}`

testAccCheckJamfComputerExtensionAttributePopup = `resource "jamf_computer_extension_attribute" "extensionattribute-popup" {
testAccCheckJamfComputerExtensionAttributePopup = `resource "jamf_computer_extension_attribute" "extensionattribute-test" {
name = "Terraform test popup"
popup_menu {
choices = ["choice1", "choice2"]
Expand Down

0 comments on commit 6c390e0

Please sign in to comment.