Skip to content

Commit 669d4a8

Browse files
committed
feat: use associated site-id for provisioning commands
1 parent 8e40885 commit 669d4a8

3 files changed

Lines changed: 57 additions & 2 deletions

File tree

internal/app/enaptercli/cmd_device_create_lua_device.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package enaptercli
22

33
import (
44
"bytes"
5+
"cmp"
56
"context"
67
"encoding/json"
78
"fmt"
@@ -12,6 +13,7 @@ import (
1213

1314
type cmdDeviceCreateLua struct {
1415
cmdDeviceCreate
16+
siteID string
1517
deviceName string
1618
deviceSlug string
1719
runtimeID string
@@ -36,6 +38,10 @@ func buildCmdDeviceCreateLua() *cli.Command {
3638
func (c *cmdDeviceCreateLua) Flags() []cli.Flag {
3739
flags := c.cmdDeviceCreate.Flags()
3840
return append(flags, &cli.StringFlag{
41+
Name: "site-id",
42+
Usage: "site ID",
43+
Destination: &c.siteID,
44+
}, &cli.StringFlag{
3945
Name: "runtime-id",
4046
Aliases: []string{"r"},
4147
Usage: "UCM device ID where the new Lua device will run",
@@ -85,8 +91,14 @@ func (c *cmdDeviceCreateLua) do(ctx context.Context) error {
8591
c.blueprintID = blueprintID
8692
}
8793

94+
// Cloud API does not allow slugs as runtime ID for now
95+
runtimeID, err := c.resolveRuntimeID(ctx)
96+
if err != nil {
97+
return fmt.Errorf("resolve runtime ID: %w", err)
98+
}
99+
88100
body, err := json.Marshal(map[string]interface{}{
89-
"runtime_id": c.runtimeID,
101+
"runtime_id": runtimeID,
90102
"name": c.deviceName,
91103
"slug": c.deviceSlug,
92104
"blueprint_id": c.blueprintID,
@@ -101,3 +113,35 @@ func (c *cmdDeviceCreateLua) do(ctx context.Context) error {
101113
ContentType: contentTypeJSON,
102114
})
103115
}
116+
117+
func (c *cmdDeviceCreateLua) resolveRuntimeID(ctx context.Context) (string, error) {
118+
if c.siteID != "" && c.cmdBase.siteID != "" && c.cmdBase.siteID != c.siteID {
119+
return "", errSiteIDMismatch
120+
}
121+
122+
siteID := cmp.Or(c.siteID, c.cmdBase.siteID)
123+
if siteID == "" {
124+
return c.runtimeID, nil
125+
}
126+
127+
var resp struct {
128+
Device struct {
129+
ID string `json:"id"`
130+
} `json:"device"`
131+
}
132+
133+
if err := c.doHTTPRequest(ctx, doHTTPRequestParams{
134+
Method: http.MethodGet,
135+
Path: "/sites/" + siteID + "/devices/" + c.runtimeID,
136+
RespProcessor: func(r *http.Response) error {
137+
if r.StatusCode != http.StatusOK {
138+
return cli.Exit(parseRespErrorMessage(r), 1)
139+
}
140+
return json.NewDecoder(r.Body).Decode(&resp)
141+
},
142+
}); err != nil {
143+
return "", err
144+
}
145+
146+
return resp.Device.ID, nil
147+
}

internal/app/enaptercli/cmd_device_create_standalone.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package enaptercli
22

33
import (
44
"bytes"
5+
"cmp"
56
"context"
67
"encoding/json"
78
"fmt"
@@ -52,8 +53,17 @@ func (c *cmdDeviceCreateStandalone) Flags() []cli.Flag {
5253
}
5354

5455
func (c *cmdDeviceCreateStandalone) do(ctx context.Context) error {
56+
if c.siteID != "" && c.cmdBase.siteID != "" && c.cmdBase.siteID != c.siteID {
57+
return errSiteIDMismatch
58+
}
59+
60+
siteID := cmp.Or(c.siteID, c.cmdBase.siteID)
61+
if siteID == "" {
62+
return errSiteIDMissing
63+
}
64+
5565
body, err := json.Marshal(map[string]any{
56-
"site_id": c.siteID,
66+
"site_id": siteID,
5767
"name": c.deviceName,
5868
"slug": c.deviceSlug,
5969
})

internal/app/enaptercli/testdata/helps/enapter device create lua-device

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ OPTIONS:
88
--connection value, -c value name of the connection to use
99
--api-allow-insecure allow insecure connections to the Enapter API (default: false) [$ENAPTER3_API_ALLOW_INSECURE]
1010
--verbose log extra details about the operation (default: false)
11+
--site-id value site ID
1112
--runtime-id value, -r value UCM device ID where the new Lua device will run
1213
--device-name value, -n value name for the new Lua device
1314
--device-slug value slug for the new Lua device

0 commit comments

Comments
 (0)