Skip to content

Commit 42e447e

Browse files
authored
Merge pull request #1014 from TSPARR/main
feat: Add Get Computer Inventory by Serial Number function and return additional inventory data
2 parents 66036a6 + 00b1148 commit 42e447e

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"log"
7+
8+
"github.com/deploymenttheory/go-api-sdk-jamfpro/sdk/jamfpro"
9+
)
10+
11+
func main() {
12+
// Define the path to the JSON configuration file
13+
configFilePath := "/Users/dafyddwatkins/localtesting/jamfpro/clientconfig.json"
14+
15+
// Initialize the Jamf Pro client with the HTTP client configuration
16+
client, err := jamfpro.BuildClientWithConfigFile(configFilePath)
17+
if err != nil {
18+
log.Fatalf("Failed to initialize Jamf Pro client: %v", err)
19+
}
20+
21+
// Define the serial number of the computer inventory you want to retrieve
22+
serialNumber := "C02ABC123DEF"
23+
24+
// Call the GetComputerInventoryBySerialNumber function
25+
computerInventory, err := client.GetComputerInventoryBySerialNumber(serialNumber)
26+
if err != nil {
27+
log.Fatalf("Error fetching computer inventory by serial number: %v", err)
28+
}
29+
30+
// Pretty print the response
31+
prettyJSON, err := json.MarshalIndent(computerInventory, "", " ")
32+
if err != nil {
33+
log.Fatalf("Failed to generate pretty JSON: %v", err)
34+
}
35+
fmt.Printf("%s\n", prettyJSON)
36+
}

sdk/jamfpro/jamfproapi_computer_inventory.go

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ import (
2020

2121
const uriComputersInventory = "/api/v1/computers-inventory"
2222

23+
// computerInventorySections contains all available sections for computer inventory API requests
24+
var computerInventorySections = []string{
25+
"GENERAL", "DISK_ENCRYPTION", "PURCHASING", "APPLICATIONS", "STORAGE",
26+
"USER_AND_LOCATION", "CONFIGURATION_PROFILES", "PRINTERS", "SERVICES",
27+
"HARDWARE", "LOCAL_USER_ACCOUNTS", "CERTIFICATES", "ATTACHMENTS",
28+
"PLUGINS", "PACKAGE_RECEIPTS", "FONTS", "SECURITY", "OPERATING_SYSTEM",
29+
"LICENSED_SOFTWARE", "IBEACONS", "SOFTWARE_UPDATES", "EXTENSION_ATTRIBUTES",
30+
"CONTENT_CACHING", "GROUP_MEMBERSHIPS",
31+
}
32+
2333
// List
2434

2535
// ResponseComputerInventoryList represents the top-level JSON response structure.
@@ -107,7 +117,7 @@ type ComputerInventorySubsetGeneralRemoteManagement struct {
107117
type ComputerInventorySubsetGeneralMdmCapable struct {
108118
Capable bool `json:"capable"`
109119
CapableUsers []string `json:"capableUsers"`
110-
UserManagementInfo [][]struct {
120+
UserManagementInfo []struct {
111121
CapableUser string `json:"capableUser"`
112122
ManagementId string `json:"managementId"`
113123
} `json:"userManagementInfo"`
@@ -598,9 +608,17 @@ func (c *Client) GetComputersInventory(params url.Values) (*ResponseComputerInve
598608
func (c *Client) GetComputerInventoryByID(id string) (*ResourceComputerInventory, error) {
599609
endpoint := fmt.Sprintf("%s/%s", uriComputersInventory, id)
600610

611+
// Add all section parameters to get complete inventory data
612+
params := url.Values{}
613+
for _, section := range computerInventorySections {
614+
params.Add("section", section)
615+
}
616+
617+
endpointWithParams := fmt.Sprintf("%s?%s", endpoint, params.Encode())
618+
601619
// Fetch the computer inventory by ID
602620
var responseInventory ResourceComputerInventory
603-
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &responseInventory)
621+
resp, err := c.HTTP.DoRequest("GET", endpointWithParams, nil, &responseInventory)
604622
if err != nil {
605623
return nil, fmt.Errorf(errMsgFailedGetByID, "computer inventory", id, err)
606624
}
@@ -614,7 +632,13 @@ func (c *Client) GetComputerInventoryByID(id string) (*ResourceComputerInventory
614632

615633
// GetComputerInventoryByName retrieves a specific computer's inventory information by its name.
616634
func (c *Client) GetComputerInventoryByName(name string) (*ResourceComputerInventory, error) {
617-
inventories, err := c.GetComputersInventory(nil)
635+
// Add all section parameters to get complete inventory data
636+
params := url.Values{}
637+
for _, section := range computerInventorySections {
638+
params.Add("section", section)
639+
}
640+
641+
inventories, err := c.GetComputersInventory(params)
618642
if err != nil {
619643
return nil, fmt.Errorf(errMsgFailedPaginatedGet, "computer inventory", err)
620644
}
@@ -628,6 +652,27 @@ func (c *Client) GetComputerInventoryByName(name string) (*ResourceComputerInven
628652
return nil, fmt.Errorf(errMsgFailedGetByName, "computer inventory", name, err)
629653
}
630654

655+
// GetComputerInventoryBySerialNumber retrieves a specific computer's inventory information by its serial number.
656+
func (c *Client) GetComputerInventoryBySerialNumber(serialNumber string) (*ResourceComputerInventory, error) {
657+
// Add filter and all section parameters to get complete inventory data
658+
params := url.Values{}
659+
params.Set("filter", fmt.Sprintf("hardware.serialNumber==\"%s\"", serialNumber))
660+
for _, section := range computerInventorySections {
661+
params.Add("section", section)
662+
}
663+
664+
inventories, err := c.GetComputersInventory(params)
665+
if err != nil {
666+
return nil, fmt.Errorf(errMsgFailedPaginatedGet, "computer inventory", err)
667+
}
668+
669+
if len(inventories.Results) == 0 {
670+
return nil, fmt.Errorf("failed to find computer inventory by serial number '%s'", serialNumber)
671+
}
672+
673+
return &inventories.Results[0], nil
674+
}
675+
631676
// UpdateComputerInventoryByID updates a specific computer's inventory information by its ID.
632677
func (c *Client) UpdateComputerInventoryByID(id string, inventoryUpdate *ResourceComputerInventory) (*ResourceComputerInventory, error) {
633678
endpoint := fmt.Sprintf("%s/%s", uriComputersInventory, id)

0 commit comments

Comments
 (0)