Skip to content

Commit 788b838

Browse files
committed
Use organization ID in iot-client calls
1 parent 0c63960 commit 788b838

21 files changed

+30
-25
lines changed

command/dashboard/create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type CreateParams struct {
3434

3535
// Create allows to create a new dashboard.
3636
func Create(params *CreateParams, cred *config.Credentials) (*DashboardInfo, error) {
37-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
37+
iotClient, err := iot.NewClient(cred)
3838
if err != nil {
3939
return nil, err
4040
}

command/dashboard/delete.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type DeleteParams struct {
3131
// Delete command is used to delete a dashboard
3232
// from Arduino IoT Cloud.
3333
func Delete(params *DeleteParams, cred *config.Credentials) error {
34-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
34+
iotClient, err := iot.NewClient(cred)
3535
if err != nil {
3636
return err
3737
}

command/dashboard/extract.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type ExtractParams struct {
3434
// Extract command is used to extract a dashboard template
3535
// from a dashboard on Arduino IoT Cloud.
3636
func Extract(params *ExtractParams, cred *config.Credentials) (map[string]interface{}, error) {
37-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
37+
iotClient, err := iot.NewClient(cred)
3838
if err != nil {
3939
return nil, err
4040
}

command/dashboard/list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
// List command is used to list
2626
// the dashboards of Arduino IoT Cloud.
2727
func List(cred *config.Credentials) ([]DashboardInfo, error) {
28-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
28+
iotClient, err := iot.NewClient(cred)
2929
if err != nil {
3030
return nil, err
3131
}

command/device/create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func Create(params *CreateParams, cred *config.Credentials) (*DeviceInfo, error)
6363
)
6464
}
6565

66-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
66+
iotClient, err := iot.NewClient(cred)
6767
if err != nil {
6868
return nil, err
6969
}

command/device/creategeneric.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type DeviceGenericInfo struct {
4444

4545
// CreateGeneric command is used to add a new generic device to Arduino IoT Cloud.
4646
func CreateGeneric(params *CreateGenericParams, cred *config.Credentials) (*DeviceGenericInfo, error) {
47-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
47+
iotClient, err := iot.NewClient(cred)
4848
if err != nil {
4949
return nil, err
5050
}

command/device/createlora.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func CreateLora(params *CreateLoraParams, cred *config.Credentials) (*DeviceLora
107107
return nil, err
108108
}
109109

110-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
110+
iotClient, err := iot.NewClient(cred)
111111
if err != nil {
112112
return nil, err
113113
}

command/device/delete.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func Delete(params *DeleteParams, cred *config.Credentials) error {
4343
return errors.New("cannot use both ID and Tags. only one of them should be not nil")
4444
}
4545

46-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
46+
iotClient, err := iot.NewClient(cred)
4747
if err != nil {
4848
return err
4949
}

command/device/list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type ListParams struct {
3333
// List command is used to list
3434
// the devices of Arduino IoT Cloud.
3535
func List(params *ListParams, cred *config.Credentials) ([]DeviceInfo, error) {
36-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
36+
iotClient, err := iot.NewClient(cred)
3737
if err != nil {
3838
return nil, err
3939
}

command/device/listfrequency.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type FrequencyPlanInfo struct {
3434
// ListFrequencyPlans command is used to list
3535
// the supported LoRa frequency plans.
3636
func ListFrequencyPlans(cred *config.Credentials) ([]FrequencyPlanInfo, error) {
37-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
37+
iotClient, err := iot.NewClient(cred)
3838
if err != nil {
3939
return nil, err
4040
}

command/ota/massupload.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func MassUpload(params *MassUploadParams, cred *config.Credentials) ([]Result, e
7171
return nil, fmt.Errorf("%s: %w", "cannot generate .ota file", err)
7272
}
7373

74-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
74+
iotClient, err := iot.NewClient(cred)
7575
if err != nil {
7676
return nil, err
7777
}

command/ota/upload.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type UploadParams struct {
4545
// Upload command is used to upload a firmware OTA,
4646
// on a device of Arduino IoT Cloud.
4747
func Upload(params *UploadParams, cred *config.Credentials) error {
48-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
48+
iotClient, err := iot.NewClient(cred)
4949
if err != nil {
5050
return err
5151
}

command/tag/create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type CreateTagsParams struct {
3535
// CreateTags allows to create or overwrite tags
3636
// on a resource of Arduino IoT Cloud.
3737
func CreateTags(params *CreateTagsParams, cred *config.Credentials) error {
38-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
38+
iotClient, err := iot.NewClient(cred)
3939
if err != nil {
4040
return err
4141
}

command/tag/delete.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type DeleteTagsParams struct {
3535
// DeleteTags command is used to delete tags of a device
3636
// from Arduino IoT Cloud.
3737
func DeleteTags(params *DeleteTagsParams, cred *config.Credentials) error {
38-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
38+
iotClient, err := iot.NewClient(cred)
3939
if err != nil {
4040
return err
4141
}

command/thing/bind.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type BindParams struct {
3333
// Bind command is used to bind a thing to a device
3434
// on Arduino IoT Cloud.
3535
func Bind(params *BindParams, cred *config.Credentials) error {
36-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
36+
iotClient, err := iot.NewClient(cred)
3737
if err != nil {
3838
return err
3939
}

command/thing/clone.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type CloneParams struct {
3333

3434
// Clone allows to create a new thing from an already existing one.
3535
func Clone(params *CloneParams, cred *config.Credentials) (*ThingInfo, error) {
36-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
36+
iotClient, err := iot.NewClient(cred)
3737
if err != nil {
3838
return nil, err
3939
}

command/thing/create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type CreateParams struct {
3434

3535
// Create allows to create a new thing.
3636
func Create(params *CreateParams, cred *config.Credentials) (*ThingInfo, error) {
37-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
37+
iotClient, err := iot.NewClient(cred)
3838
if err != nil {
3939
return nil, err
4040
}

command/thing/delete.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func Delete(params *DeleteParams, cred *config.Credentials) error {
4343
return errors.New("cannot use both ID and Tags. only one of them should be not nil")
4444
}
4545

46-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
46+
iotClient, err := iot.NewClient(cred)
4747
if err != nil {
4848
return err
4949
}

command/thing/extract.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type ExtractParams struct {
3434
// Extract command is used to extract a thing template
3535
// from a thing on Arduino IoT Cloud.
3636
func Extract(params *ExtractParams, cred *config.Credentials) (map[string]interface{}, error) {
37-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
37+
iotClient, err := iot.NewClient(cred)
3838
if err != nil {
3939
return nil, err
4040
}

command/thing/list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type ListParams struct {
3636
// List command is used to list
3737
// the things of Arduino IoT Cloud.
3838
func List(params *ListParams, cred *config.Credentials) ([]ThingInfo, error) {
39-
iotClient, err := iot.NewClient(cred.Client, cred.Secret)
39+
iotClient, err := iot.NewClient(cred)
4040
if err != nil {
4141
return nil, err
4242
}

internal/iot/client.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os"
2424

2525
"github.com/antihax/optional"
26+
"github.com/arduino/arduino-cloud-cli/internal/config"
2627
iotclient "github.com/arduino/iot-client-go"
2728
)
2829

@@ -33,10 +34,10 @@ type Client struct {
3334
}
3435

3536
// NewClient returns a new client implementing the Client interface.
36-
// It needs a ClientID and SecretID for cloud authentication.
37-
func NewClient(clientID, secretID string) (*Client, error) {
37+
// It needs client Credentials for cloud authentication.
38+
func NewClient(cred *config.Credentials) (*Client, error) {
3839
cl := &Client{}
39-
err := cl.setup(clientID, secretID)
40+
err := cl.setup(cred.Client, cred.Secret, cred.Organization)
4041
if err != nil {
4142
err = fmt.Errorf("instantiate new iot client: %w", err)
4243
return nil, err
@@ -350,7 +351,7 @@ func (cl *Client) DashboardDelete(id string) error {
350351
return nil
351352
}
352353

353-
func (cl *Client) setup(client, secret string) error {
354+
func (cl *Client) setup(client, secret, organization string) error {
354355
// Get the access token in exchange of client_id and client_secret
355356
tok, err := token(client, secret)
356357
if err != nil {
@@ -363,7 +364,11 @@ func (cl *Client) setup(client, secret string) error {
363364

364365
// Create an instance of the iot-api Go client, we pass an empty config
365366
// because defaults are ok
366-
cl.api = iotclient.NewAPIClient(iotclient.NewConfiguration())
367+
config := iotclient.NewConfiguration()
368+
if organization != "" {
369+
config.DefaultHeader = map[string]string{"X-Organization": organization}
370+
}
371+
cl.api = iotclient.NewAPIClient(config)
367372

368373
return nil
369374
}

0 commit comments

Comments
 (0)