Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ad5ec28

Browse files
committedJul 3, 2024·
Added apply template
1 parent 73f98cf commit ad5ec28

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed
 

‎command/template/apply.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package template
2020
import (
2121
"context"
2222
"fmt"
23+
"strings"
2324

2425
"github.com/arduino/arduino-cli/cli/feedback"
2526
"github.com/arduino/arduino-cloud-cli/config"
@@ -92,7 +93,7 @@ func resolveDeviceNetworkConfigurations(ctx context.Context, cl *iot.Client, dev
9293
discoveredCredentials[credential.GetSecretName()] = credential
9394
if credential.Required {
9495
if _, ok := networkCredentials[credential.GetSecretName()]; !ok {
95-
return nil, fmt.Errorf("missing mandatory network credential: %s", credential.GetSecretName())
96+
return nil, fmt.Errorf("missing mandatory network credential: %s. Available: %s", credential.GetSecretName(), humanReadableCredentials(credentials))
9697
}
9798
}
9899
}
@@ -105,3 +106,15 @@ func resolveDeviceNetworkConfigurations(ctx context.Context, cl *iot.Client, dev
105106

106107
return networkCredentials, nil
107108
}
109+
110+
func humanReadableCredentials(cred []iotclient.ArduinoCredentialsv1) string {
111+
var buf strings.Builder
112+
for _, c := range cred {
113+
if c.Required {
114+
buf.WriteString(fmt.Sprintf(" - %s (required)", c.GetSecretName()))
115+
} else {
116+
buf.WriteString(fmt.Sprintf(" - %s", c.GetSecretName()))
117+
}
118+
}
119+
return buf.String()
120+
}

‎internal/iot/client.go

+23
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,29 @@ func (cl *Client) DashboardDelete(ctx context.Context, id string) error {
533533
return nil
534534
}
535535

536+
// TemplateApply apply a given template, creating associated resources like things and dashboards.
537+
func (cl *Client) TemplateApply(ctx context.Context, id, thingId, prefix string, credentials map[string]string) (*iotclient.ArduinoTemplate, error) {
538+
ctx, err := ctxWithToken(ctx, cl.token)
539+
if err != nil {
540+
return nil, err
541+
}
542+
543+
req := cl.api.TemplatesApi.TemplatesApply(ctx)
544+
req = req.Template(iotclient.Template{
545+
PrefixName: toStringPointer(prefix),
546+
CustomTemplateId: toStringPointer(id),
547+
ThingsOptions: map[string]interface{}{
548+
thingId: credentials,
549+
},
550+
})
551+
dev, _, err := cl.api.TemplatesApi.TemplatesApplyExecute(req)
552+
if err != nil {
553+
err = fmt.Errorf("retrieving device, %w", errorDetail(err))
554+
return nil, err
555+
}
556+
return dev, nil
557+
}
558+
536559
func (cl *Client) setup(client, secret, organization string) error {
537560
baseURL := GetArduinoAPIBaseURL()
538561

0 commit comments

Comments
 (0)
Please sign in to comment.