Skip to content

Commit c86447f

Browse files
committed
Adde template call
1 parent ad5ec28 commit c86447f

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

cli/template/apply.go

+20-11
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,26 @@ func runTemplateApplyCommand(flags *applyFlags) error {
6868
return fmt.Errorf("retrieving credentials: %w", err)
6969
}
7070

71-
deviceNetCredentials := make(map[string]string)
72-
if flags.netCredentials != "" {
73-
configNetArray := strings.Split(strings.Trim(flags.netCredentials, " "), ",")
74-
for _, netConfig := range configNetArray {
75-
netConfigArray := strings.Split(netConfig, "=")
76-
if len(netConfigArray) != 2 {
77-
return fmt.Errorf("invalid network configuration: %s", netConfig)
78-
}
79-
deviceNetCredentials[netConfigArray[0]] = netConfigArray[1]
80-
}
71+
deviceNetCredentials, err := parseCredentials(flags.netCredentials)
72+
if err != nil {
73+
return fmt.Errorf("parsing network credentials: %w", err)
8174
}
82-
75+
8376
return template.ApplyCustomTemplates(cred, flags.templateId, flags.deviceId, flags.templatePrefix, deviceNetCredentials)
8477
}
78+
79+
func parseCredentials(credentials string) (map[string]string, error) {
80+
credentialsMap := make(map[string]string)
81+
if credentials == "" {
82+
return credentialsMap, nil
83+
}
84+
credentialsArray := strings.Split(credentials, ",")
85+
for _, credential := range credentialsArray {
86+
credentialArray := strings.Split(credential, "=")
87+
if len(credentialArray) != 2 {
88+
return nil, fmt.Errorf("invalid network credential: %s", credential)
89+
}
90+
credentialsMap[credentialArray[0]] = credentialArray[1]
91+
}
92+
return credentialsMap, nil
93+
}

command/template/apply.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,22 @@ func ApplyCustomTemplates(cred *config.Credentials, templateId, deviceId, prefix
5757
if len(cstTemplate.ThingTemplates) <= 0 {
5858
return fmt.Errorf("template %s has no thing template", templateId)
5959
}
60-
mainThing := cstTemplate.ThingTemplates[0]
61-
logrus.Debug("Main thing template - id: ", mainThing.Id)
60+
thingTemplateIdentifier := cstTemplate.ThingTemplates[0]
61+
logrus.Debug("Main thing template - id: ", thingTemplateIdentifier.Id)
6262

6363
// Get device and check its connectivity
6464
secrets, err := resolveDeviceNetworkConfigurations(ctx, iotClient, deviceId, networkCredentials)
6565
if err != nil {
6666
return err
6767
}
68-
for key, value := range secrets {
69-
logrus.Info(fmt.Sprintf("Secret %s: %s", key, value))
68+
69+
// Apply the template
70+
_, err = iotClient.TemplateApply(ctx, templateId, thingTemplateIdentifier.Id, prefix, deviceId, secrets)
71+
if err != nil {
72+
return err
7073
}
74+
feedback.Printf("Template applied successfully to device %s", deviceId)
75+
7176
return nil
7277
}
7378

internal/iot/client.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -534,18 +534,22 @@ func (cl *Client) DashboardDelete(ctx context.Context, id string) error {
534534
}
535535

536536
// 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) {
537+
func (cl *Client) TemplateApply(ctx context.Context, id, thingId, prefix, deviceId string, credentials map[string]string) (*iotclient.ArduinoTemplate, error) {
538538
ctx, err := ctxWithToken(ctx, cl.token)
539539
if err != nil {
540540
return nil, err
541541
}
542542

543+
thingOption := make(map[string]any)
544+
thingOption["device_id"] = deviceId
545+
thingOption["secrets"] = credentials
546+
543547
req := cl.api.TemplatesApi.TemplatesApply(ctx)
544548
req = req.Template(iotclient.Template{
545549
PrefixName: toStringPointer(prefix),
546550
CustomTemplateId: toStringPointer(id),
547551
ThingsOptions: map[string]interface{}{
548-
thingId: credentials,
552+
thingId: thingOption,
549553
},
550554
})
551555
dev, _, err := cl.api.TemplatesApi.TemplatesApplyExecute(req)

0 commit comments

Comments
 (0)