Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/commands/pro_platform_device_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func newPDGApplyCmd(cliCtx *registry.CLIContext) *cobra.Command {

dg := devicegroups.New(cliCtx.PlatformSDKClient)
id, resolveErr := dg.ResolveDeviceGroupIDByName(ctx, createReq.Name)
if resolveErr != nil && !platform.IsNotFound(resolveErr) {
return resolveErr
}
if resolveErr != nil {
// Not found — create
result, err := devicegroups.New(cliCtx.PlatformSDKClient).CreateDeviceGroup(ctx, &createReq)
Expand Down
18 changes: 15 additions & 3 deletions internal/platform/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

package platform

import "errors"
import (
"errors"

"github.com/Jamf-Concepts/jamfplatform-go-sdk/jamfplatform"
)

// ErrNotFound is returned when a resource name cannot be resolved to an ID.
//
Expand All @@ -20,7 +24,15 @@ import "errors"
// platform.IsNotFound assertions across the codebase.
var ErrNotFound = errors.New("not found")

// IsNotFound reports whether err is or wraps ErrNotFound.
// IsNotFound reports whether err is or wraps ErrNotFound, or is a 404
// *APIResponseError from the Platform SDK (returned by Resolve* methods when
// a name lookup yields zero results).
func IsNotFound(err error) bool {
return errors.Is(err, ErrNotFound)
if errors.Is(err, ErrNotFound) {
return true
}
if apiErr := jamfplatform.AsAPIError(err); apiErr != nil && apiErr.HasStatus(404) {
return true
}
return false
}
24 changes: 22 additions & 2 deletions internal/platform/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"errors"
"fmt"
"testing"

"github.com/Jamf-Concepts/jamfplatform-go-sdk/jamfplatform"
)

func TestIsNotFound(t *testing.T) {
Expand All @@ -14,10 +16,28 @@ func TestIsNotFound(t *testing.T) {
t.Error("expected IsNotFound(ErrNotFound) = true")
}

// Wrapped sentinel (as produced by ResolveBlueprintID)
// Wrapped sentinel
wrapped := fmt.Errorf("blueprint %q not found: %w", "test", ErrNotFound)
if !IsNotFound(wrapped) {
t.Error("expected IsNotFound on wrapped error = true")
t.Error("expected IsNotFound on wrapped sentinel = true")
}

// SDK *APIResponseError 404 (returned by ResolveBlueprintIDByName on empty results)
api404 := &jamfplatform.APIResponseError{StatusCode: 404}
if !IsNotFound(api404) {
t.Error("expected IsNotFound(*APIResponseError{404}) = true")
}

// SDK *APIResponseError 404 wrapped in fmt.Errorf (as the SDK wraps it)
wrapped404 := fmt.Errorf("ResolveBlueprintIDByName(Brand New Blueprint): %w", api404)
if !IsNotFound(wrapped404) {
t.Error("expected IsNotFound on wrapped *APIResponseError{404} = true")
}

// SDK *APIResponseError non-404 must not match
api500 := &jamfplatform.APIResponseError{StatusCode: 500}
if IsNotFound(api500) {
t.Error("expected IsNotFound(*APIResponseError{500}) = false")
}

// Non-matching error
Expand Down