@@ -2,6 +2,7 @@ package enaptercli
22
33import (
44 "bytes"
5+ "cmp"
56 "context"
67 "encoding/json"
78 "fmt"
@@ -12,6 +13,7 @@ import (
1213
1314type cmdDeviceCreateLua struct {
1415 cmdDeviceCreate
16+ siteID string
1517 deviceName string
1618 deviceSlug string
1719 runtimeID string
@@ -36,6 +38,10 @@ func buildCmdDeviceCreateLua() *cli.Command {
3638func (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+ }
0 commit comments