Skip to content

[WIRE-419] Update iot-client to latest generated artifact #155

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

Merged
merged 15 commits into from
Jun 21, 2024
Merged
Prev Previous commit
Next Next commit
moved clone thing to iot-api implementation
  • Loading branch information
rjtokenring committed Jun 19, 2024
commit a3382fe6e7e91b5357a7737923ac04c5ad974aaa
39 changes: 2 additions & 37 deletions command/thing/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"github.com/arduino/arduino-cloud-cli/config"
"github.com/arduino/arduino-cloud-cli/internal/iot"
iotclient "github.com/arduino/iot-client-go"
)

// CloneParams contains the parameters needed to clone a thing.
Expand All @@ -39,16 +38,9 @@ func Clone(ctx context.Context, params *CloneParams, cred *config.Credentials) (
return nil, err
}

thing, err := retrieve(ctx, iotClient, params.CloneID)
newThing, err := iotClient.ThingClone(ctx, params.CloneID, params.Name)
if err != nil {
return nil, err
}

thing.Name = &params.Name
force := true
newThing, err := iotClient.ThingCreate(ctx, thing, force)
if err != nil {
return nil, err
return nil, fmt.Errorf("cloning thing %s: %w", params.CloneID, err)
}

t, err := getThingInfo(newThing)
Expand All @@ -57,30 +49,3 @@ func Clone(ctx context.Context, params *CloneParams, cred *config.Credentials) (
}
return t, nil
}

type thingFetcher interface {
ThingShow(ctx context.Context, id string) (*iotclient.ArduinoThing, error)
}

func retrieve(ctx context.Context, fetcher thingFetcher, thingID string) (*iotclient.ThingCreate, error) {
clone, err := fetcher.ThingShow(ctx, thingID)
if err != nil {
return nil, fmt.Errorf("%s: %w", "retrieving the thing to be cloned", err)
}

thing := &iotclient.ThingCreate{}

// Copy variables
for _, p := range clone.Properties {
thing.Properties = append(thing.Properties, iotclient.Property{
Name: p.Name,
Permission: p.Permission,
UpdateParameter: p.UpdateParameter,
UpdateStrategy: p.UpdateStrategy,
Type: p.Type,
VariableName: p.VariableName,
})
}

return thing, nil
}
17 changes: 17 additions & 0 deletions internal/iot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,23 @@ func (cl *Client) ThingShow(ctx context.Context, id string) (*iotclient.ArduinoT
return thing, nil
}

// ThingClone allows to clone a specific thing, given its id from Arduino IoT Cloud.
func (cl *Client) ThingClone(ctx context.Context, id, newName string) (*iotclient.ArduinoThing, error) {
ctx, err := ctxWithToken(ctx, cl.token)
if err != nil {
return nil, err
}

req := cl.api.ThingsV2Api.ThingsV2Clone(ctx, id)
includeTags := true
req = req.ThingClone(iotclient.ThingClone{Name: newName, IncludeTags: &includeTags})
thing, _, err := cl.api.ThingsV2Api.ThingsV2CloneExecute(req)
if err != nil {
return nil, fmt.Errorf("cloning thing thing, %w", errorDetail(err))
}
return thing, nil
}

// ThingList returns a list of things on Arduino IoT Cloud.
func (cl *Client) ThingList(ctx context.Context, ids []string, device *string, props bool, tags map[string]string) ([]iotclient.ArduinoThing, error) {
ctx, err := ctxWithToken(ctx, cl.token)
Expand Down
Loading