Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: propagate fetcher errors back to the user #425

Merged
merged 2 commits into from
Jan 22, 2025
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
8 changes: 6 additions & 2 deletions pkg/c8yfetcher/c8yfetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ func lookupEntity(fetch EntityFetcher, values []string, getID bool, format strin
Data: resultSet,
})
}
} else {
// Propagate errors back to the caller
return entities, err
}
// TODO: Handle error
} else {
entities = append(entities, entityReference{
ID: applyFormatter(id, format),
Expand All @@ -150,8 +152,10 @@ func lookupEntity(fetch EntityFetcher, values []string, getID bool, format strin
Data: resultSet,
})
}
} else {
// Propagate errors back to the caller
return entities, err
}
// TODO: Handle error
}

return entities, nil
Expand Down
7 changes: 6 additions & 1 deletion pkg/c8yfetcher/childAdditionFetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package c8yfetcher

import (
"context"

"github.com/pkg/errors"
"github.com/reubenmiller/go-c8y/pkg/c8y"
)
Expand Down Expand Up @@ -41,9 +42,13 @@ func (f *ChildAdditionFetcher) getByID(id string) ([]fetcherResultSet, error) {
}

func (f *ChildAdditionFetcher) getByName(name string) ([]fetcherResultSet, error) {
var err error
query := "name eq '" + name + "'"
if f.Query != nil {
query = f.Query(name)
query, err = f.Query(name)
if err != nil {
return nil, NewQueryBuildErr(err)
}
}
mcol, _, err := f.client.Inventory.GetChildAdditions(
c8y.WithDisabledDryRunContext(context.Background()),
Expand Down
4 changes: 2 additions & 2 deletions pkg/c8yfetcher/configurationFetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func NewConfigurationFetcher(factory *cmdutil.Factory) *ConfigurationFetcher {
CumulocityFetcher: &CumulocityFetcher{
factory: factory,
},
Query: func(s string) string {
return fmt.Sprintf("(type eq 'c8y_ConfigurationDump') and name eq '%s'", s)
Query: func(s string) (string, error) {
return fmt.Sprintf("(type eq 'c8y_ConfigurationDump') and name eq '%s'", s), nil
},
},
}
Expand Down
13 changes: 8 additions & 5 deletions pkg/c8yfetcher/deviceServiceFetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,30 @@ func NewDeviceServiceFetcher(factory *cmdutil.Factory, device string) *DeviceSer
CumulocityFetcher: &CumulocityFetcher{
factory: factory,
},
Query: func(s string) string {
Query: func(s string) (string, error) {

client, err := factory.Client()
if err != nil {
return ""
return "", err
}

if !IsID(device) {
// Lookup software by name
moDevice, _, err := client.Inventory.GetDevicesByName(c8y.WithDisabledDryRunContext(context.Background()), device, &c8y.PaginationOptions{
PageSize: 5,
})
if err == nil && moDevice != nil && len(moDevice.ManagedObjects) > 0 {
if err != nil {
return "", NewQueryBuildErr(err)
}
if moDevice != nil && len(moDevice.ManagedObjects) > 0 {
device = moDevice.ManagedObjects[0].ID
}
}

if IsID(device) {
return fmt.Sprintf("(type eq 'c8y_Service') and name eq '%s' and (bygroupid(%s))", s, device)
return fmt.Sprintf("(type eq 'c8y_Service') and name eq '%s' and (bygroupid(%s))", s, device), nil
}
return fmt.Sprintf("(type eq 'c8y_Service') and name eq '%s'", s)
return fmt.Sprintf("(type eq 'c8y_Service') and name eq '%s'", s), nil
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/c8yfetcher/firmwareFetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func NewFirmwareFetcher(factory *cmdutil.Factory) *FirmwareFetcher {
CumulocityFetcher: &CumulocityFetcher{
factory: factory,
},
Query: func(s string) string {
return fmt.Sprintf("(type eq 'c8y_Firmware') and name eq '%s'", s)
Query: func(s string) (string, error) {
return fmt.Sprintf("(type eq 'c8y_Firmware') and name eq '%s'", s), nil
},
},
}
Expand Down
17 changes: 12 additions & 5 deletions pkg/c8yfetcher/firmwareVersionFetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,25 @@ func NewFirmwareVersionFetcher(factory *cmdutil.Factory, firmware string, includ
CumulocityFetcher: &CumulocityFetcher{
factory: factory,
},
Query: func(s string) string {
Query: func(s string) (string, error) {
client, err := factory.Client()
if err != nil {
return ""
return "", err
}
var firmwareID string

// Use default value of a non-existent id so that the query does not return anything
// Use a default (non-existent) managed object to ensure the inventory
// query is still valid when the firmware is not found (for whatever reason)
firmwareID := "0"
if IsID(firmware) {
firmwareID = firmware
} else {
// Lookup firmware by name
res, _, err := client.Firmware.GetFirmwareByName(c8y.WithDisabledDryRunContext(context.Background()), firmware, c8y.NewPaginationOptions(5))
if err == nil && len(res.ManagedObjects) > 0 {
if err != nil {
return "", NewQueryBuildErr(err)
}
if len(res.ManagedObjects) > 0 {
firmwareID = res.ManagedObjects[0].ID
}
}
Expand All @@ -39,7 +46,7 @@ func NewFirmwareVersionFetcher(factory *cmdutil.Factory, firmware string, includ
patchFilter = "not(" + patchFilter + ")"
}

return fmt.Sprintf("(type eq 'c8y_FirmwareBinary') and %s and c8y_Firmware.version eq '%s' and (bygroupid(%s))", patchFilter, s, firmwareID)
return fmt.Sprintf("(type eq 'c8y_FirmwareBinary') and %s and c8y_Firmware.version eq '%s' and (bygroupid(%s))", patchFilter, s, firmwareID), nil
},
},
}
Expand Down
13 changes: 11 additions & 2 deletions pkg/c8yfetcher/managedObjectFetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package c8yfetcher

import (
"context"

"github.com/pkg/errors"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/cmdutil"
"github.com/reubenmiller/go-c8y/pkg/c8y"
)

type QueryFilter func(string) string
func NewQueryBuildErr(err error) error {
return errors.Wrap(err, "Fetcher could not build a query")
}

type QueryFilter func(string) (string, error)

type ManagedObjectFetcher struct {
Query QueryFilter
Expand Down Expand Up @@ -43,9 +48,13 @@ func (f *ManagedObjectFetcher) getByID(id string) ([]fetcherResultSet, error) {
}

func (f *ManagedObjectFetcher) getByName(name string) ([]fetcherResultSet, error) {
var err error
query := "name eq '" + name + "'"
if f.Query != nil {
query = f.Query(name)
query, err = f.Query(name)
if err != nil {
return nil, errors.Wrap(err, "Fetcher could not build a valid query")
}
}
mcol, _, err := f.Client().Inventory.GetManagedObjects(
c8y.WithDisabledDryRunContext(context.Background()),
Expand Down
4 changes: 2 additions & 2 deletions pkg/c8yfetcher/softwareFetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func NewSoftwareFetcher(factory *cmdutil.Factory) *SoftwareFetcher {
CumulocityFetcher: &CumulocityFetcher{
factory: factory,
},
Query: func(s string) string {
return fmt.Sprintf("(type eq 'c8y_Software') and name eq '%s'", s)
Query: func(s string) (string, error) {
return fmt.Sprintf("(type eq 'c8y_Software') and name eq '%s'", s), nil
},
},
}
Expand Down
13 changes: 8 additions & 5 deletions pkg/c8yfetcher/softwareVersionFetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,30 @@ func NewSoftwareVersionFetcher(factory *cmdutil.Factory, software string) *Softw
CumulocityFetcher: &CumulocityFetcher{
factory: factory,
},
Query: func(s string) string {
Query: func(s string) (string, error) {
// Check
client, err := factory.Client()
if err != nil {
return ""
return "", err
}

if !IsID(software) {
// Lookup software by name
moSoftware, _, err := client.Software.GetSoftwareByName(c8y.WithDisabledDryRunContext(context.Background()), software, &c8y.PaginationOptions{
PageSize: 5,
})
if err == nil && moSoftware != nil && len(moSoftware.ManagedObjects) > 0 {
if err != nil {
return "", NewQueryBuildErr(err)
}
if moSoftware != nil && len(moSoftware.ManagedObjects) > 0 {
software = moSoftware.ManagedObjects[0].ID
}
}

if IsID(software) {
return fmt.Sprintf("(type eq 'c8y_SoftwareBinary') and not(has(c8y_Patch)) and c8y_Software.version eq '%s' and (bygroupid(%s))", s, software)
return fmt.Sprintf("(type eq 'c8y_SoftwareBinary') and not(has(c8y_Patch)) and c8y_Software.version eq '%s' and (bygroupid(%s))", s, software), nil
}
return fmt.Sprintf("(type eq 'c8y_SoftwareBinary') and not(has(c8y_Patch)) and c8y_Software.version eq '%s'", s)
return fmt.Sprintf("(type eq 'c8y_SoftwareBinary') and not(has(c8y_Patch)) and c8y_Software.version eq '%s'", s), nil
},
},
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/mapbuilder/mapbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1060,10 +1060,8 @@ func (b *MapBuilder) MarshalJSONObject() (body []byte, err error) {
Logger.Debugf("body iterator. path=%s, value=%s", it.Path, value)

if itErr != nil {
if itErr == io.EOF {
err = itErr
return
}
err = itErr
return
} else {
switch extInput := input.(type) {
case []byte:
Expand Down
Loading