Skip to content

Commit

Permalink
feat: add computer extension attributes resources and data sources (#14)
Browse files Browse the repository at this point in the history
* feat: computer extension attribute schema

* fix: schema validation and add platform to script

* feat: computer extension attribute resource functions

* fix: recognize text_field properly

* test: computer extension attributes

* feat: computer extension attribute data source

* docs: add documentation for computer extension attributes

* fix: set default value for text field input_type

* bump go-jamf-api version

* bump go-jamf-api version
  • Loading branch information
forevanyeung committed Apr 11, 2023
1 parent 5b85f34 commit 3c84c39
Show file tree
Hide file tree
Showing 8 changed files with 647 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.3
github.com/mitchellh/go-homedir v1.1.0
github.com/yohan460/go-jamf-api v0.0.0-20221116181400-9279a31bfd4c
github.com/yohan460/go-jamf-api v0.0.0-20230411151701-d1c2c274032b
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70=
github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=
github.com/yohan460/go-jamf-api v0.0.0-20221116181400-9279a31bfd4c h1:8hHLXtVHpCxV0vIvT09RZbrRWJrkVt7lhh7nnor+c+c=
github.com/yohan460/go-jamf-api v0.0.0-20221116181400-9279a31bfd4c/go.mod h1:tBRMKJ3SjrouWaToIt9vqf7R63qtoi3UeQ486vcYz38=
github.com/yohan460/go-jamf-api v0.0.0-20230411151701-d1c2c274032b h1:4EkYhRnTZxYkUYWhQySBA0wxdKGBioUJ8MB0gkyhNQE=
github.com/yohan460/go-jamf-api v0.0.0-20230411151701-d1c2c274032b/go.mod h1:tBRMKJ3SjrouWaToIt9vqf7R63qtoi3UeQ486vcYz38=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
164 changes: 164 additions & 0 deletions jamf/data_source_jamf_computer_extension_attribute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package jamf

import (
"context"
"strconv"

"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"
"github.com/yohan460/go-jamf-api"
)

func dataSourceJamfComputerExtensionAttribute() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceJamfComputerExtensionAttributeRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"data_type": {
Type: schema.TypeString,
Optional: true,
Default: "String",
ValidateFunc: validation.StringInSlice([]string{"String", "Integer", "Date"}, false),
},
"inventory_display": {
Type: schema.TypeString,
Optional: true,
Default: "Extension Attributes",
ValidateFunc: validation.StringInSlice([]string{"General", "Hardware", "Operating System", "User and Location", "Purchasing", "Extension Attributes"}, false),
},
"script": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ExactlyOneOf: []string{"script", "text_field", "popup_menu"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"platform": {
Type: schema.TypeString,
Optional: true,
Default: "Mac",
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"},
},
},
},
},
"text_field": {
Type: schema.TypeSet,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// set as a placeholder to `text_field` is recognized,
// this schema is not used anywhere
"input_type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"popup_menu": {
Type: schema.TypeSet,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"choices": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
}
}

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

resp, err := c.GetComputerExtensionAttributeByName(d.Get("name").(string))
if err != nil {
return diag.FromErr(err)
}

deconstructJamfComputerExtensionAttributeStruct(d, resp)

return diags
}

func deconstructJamfComputerExtensionAttributeStruct(d *schema.ResourceData, in *jamf.ComputerExtensionAttribute) {
d.SetId(strconv.Itoa(in.Id))
d.Set("name", in.Name)
d.Set("description", in.Description)
d.Set("data_type", in.DataType)
d.Set("inventory_display", in.InventoryDisplay)
d.Set("recon_display", in.ReconDisplay)

// 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)
case "Text Field":
d.Set("text_field", []interface{}{
map[string]interface{}{
"input_type": "text_field",
},
})
case "Pop-up Menu":
d.Set("popup_menu", []interface{}{
map[string]interface{}{
"choices": in.InputType.Choices,
},
})
}

return
}
2 changes: 2 additions & 0 deletions jamf/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func Provider() *schema.Provider {
"jamf_script": resourceJamfScript(),
"jamf_policy": resourceJamfPolicy(),
"jamf_computer_configuration_profile": resourceJamfComputerConfigurationProfile(),
"jamf_computer_extension_attribute": resourceJamfComputerExtensionAttribute(),
},

DataSourcesMap: map[string]*schema.Resource{
Expand All @@ -56,6 +57,7 @@ func Provider() *schema.Provider {
"jamf_package": dataSourceJamfPackage(),
"jamf_policy": dataSourceJamfPolicy(),
"jamf_computer_configuration_profile": dataSourceJamfComputerConfigurationProfile(),
"jamf_computer_extension_attribute": dataSourceJamfComputerExtensionAttribute(),
},
ConfigureContextFunc: providerConfigure,
}
Expand Down

0 comments on commit 3c84c39

Please sign in to comment.