Skip to content

Commit a3382fe

Browse files
committed
moved clone thing to iot-api implementation
1 parent dbc9171 commit a3382fe

File tree

2 files changed

+19
-37
lines changed

2 files changed

+19
-37
lines changed

command/thing/clone.go

+2-37
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323

2424
"github.com/arduino/arduino-cloud-cli/config"
2525
"github.com/arduino/arduino-cloud-cli/internal/iot"
26-
iotclient "github.com/arduino/iot-client-go"
2726
)
2827

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

42-
thing, err := retrieve(ctx, iotClient, params.CloneID)
41+
newThing, err := iotClient.ThingClone(ctx, params.CloneID, params.Name)
4342
if err != nil {
44-
return nil, err
45-
}
46-
47-
thing.Name = &params.Name
48-
force := true
49-
newThing, err := iotClient.ThingCreate(ctx, thing, force)
50-
if err != nil {
51-
return nil, err
43+
return nil, fmt.Errorf("cloning thing %s: %w", params.CloneID, err)
5244
}
5345

5446
t, err := getThingInfo(newThing)
@@ -57,30 +49,3 @@ func Clone(ctx context.Context, params *CloneParams, cred *config.Credentials) (
5749
}
5850
return t, nil
5951
}
60-
61-
type thingFetcher interface {
62-
ThingShow(ctx context.Context, id string) (*iotclient.ArduinoThing, error)
63-
}
64-
65-
func retrieve(ctx context.Context, fetcher thingFetcher, thingID string) (*iotclient.ThingCreate, error) {
66-
clone, err := fetcher.ThingShow(ctx, thingID)
67-
if err != nil {
68-
return nil, fmt.Errorf("%s: %w", "retrieving the thing to be cloned", err)
69-
}
70-
71-
thing := &iotclient.ThingCreate{}
72-
73-
// Copy variables
74-
for _, p := range clone.Properties {
75-
thing.Properties = append(thing.Properties, iotclient.Property{
76-
Name: p.Name,
77-
Permission: p.Permission,
78-
UpdateParameter: p.UpdateParameter,
79-
UpdateStrategy: p.UpdateStrategy,
80-
Type: p.Type,
81-
VariableName: p.VariableName,
82-
})
83-
}
84-
85-
return thing, nil
86-
}

internal/iot/client.go

+17
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,23 @@ func (cl *Client) ThingShow(ctx context.Context, id string) (*iotclient.ArduinoT
363363
return thing, nil
364364
}
365365

366+
// ThingClone allows to clone a specific thing, given its id from Arduino IoT Cloud.
367+
func (cl *Client) ThingClone(ctx context.Context, id, newName string) (*iotclient.ArduinoThing, error) {
368+
ctx, err := ctxWithToken(ctx, cl.token)
369+
if err != nil {
370+
return nil, err
371+
}
372+
373+
req := cl.api.ThingsV2Api.ThingsV2Clone(ctx, id)
374+
includeTags := true
375+
req = req.ThingClone(iotclient.ThingClone{Name: newName, IncludeTags: &includeTags})
376+
thing, _, err := cl.api.ThingsV2Api.ThingsV2CloneExecute(req)
377+
if err != nil {
378+
return nil, fmt.Errorf("cloning thing thing, %w", errorDetail(err))
379+
}
380+
return thing, nil
381+
}
382+
366383
// ThingList returns a list of things on Arduino IoT Cloud.
367384
func (cl *Client) ThingList(ctx context.Context, ids []string, device *string, props bool, tags map[string]string) ([]iotclient.ArduinoThing, error) {
368385
ctx, err := ctxWithToken(ctx, cl.token)

0 commit comments

Comments
 (0)