Skip to content

Commit

Permalink
Pass the required factory argument to an API for all device commands
Browse files Browse the repository at this point in the history
Signed-off-by: Volodymyr Khoroz <volodymyr.khoroz@foundries.io>
  • Loading branch information
vkhoroz committed Dec 2, 2021
1 parent 7432807 commit 98b53c3
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 46 deletions.
53 changes: 30 additions & 23 deletions client/foundries.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,9 @@ func (a *Api) Delete(url string, data []byte) (*[]byte, error) {
return readResponse(res, log)
}

func (a *Api) DeviceGet(device string) (*Device, error) {
body, err := a.Get(a.serverUrl + "/ota/devices/" + device + "/")
func (a *Api) DeviceGet(factory, device string) (*Device, error) {
url := a.serverUrl + "/ota/devices/" + device + "/?factory=" + factory
body, err := a.Get(url)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -680,44 +681,49 @@ func (a *Api) DeviceListCont(url string) (*DeviceList, error) {
return &devices, nil
}

func (a *Api) DeviceChown(name, owner string) error {
func (a *Api) DeviceChown(factory, name, owner string) error {
body := map[string]string{"owner": owner}
data, err := json.Marshal(body)
if err != nil {
return err
}
_, err = a.Patch(a.serverUrl+"/ota/devices/"+name+"/", data)
url := a.serverUrl + "/ota/devices/" + name + "/?factory=" + factory
_, err = a.Patch(url, data)
return err
}

func (a *Api) DeviceRename(curName, newName string) error {
func (a *Api) DeviceRename(factory, curName, newName string) error {
body := map[string]string{"name": newName}
data, err := json.Marshal(body)
if err != nil {
return err
}
_, err = a.Patch(a.serverUrl+"/ota/devices/"+curName+"/", data)
url := a.serverUrl + "/ota/devices/" + curName + "/?factory=" + factory
_, err = a.Patch(url, data)
return err
}

func (a *Api) DeviceSetGroup(device string, group string) error {
func (a *Api) DeviceSetGroup(factory, device, group string) error {
body := map[string]string{"group": group}
data, err := json.Marshal(body)
if err != nil {
return err
}
_, err = a.Patch(a.serverUrl+"/ota/devices/"+device+"/", data)
url := a.serverUrl + "/ota/devices/" + device + "/?factory=" + factory
_, err = a.Patch(url, data)
return err
}

func (a *Api) DeviceDelete(device string) error {
func (a *Api) DeviceDelete(factory, device string) error {
bytes := []byte{}
_, err := a.Delete(a.serverUrl+"/ota/devices/"+device+"/", bytes)
url := a.serverUrl + "/ota/devices/" + device + "/?factory=" + factory
_, err := a.Delete(url, bytes)
return err
}

func (a *Api) DeviceListUpdates(device string) (*UpdateList, error) {
return a.DeviceListUpdatesCont(a.serverUrl + "/ota/devices/" + device + "/updates/")
func (a *Api) DeviceListUpdates(factory, device string) (*UpdateList, error) {
url := a.serverUrl + "/ota/devices/" + device + "/updates/?factory=" + factory
return a.DeviceListUpdatesCont(url)
}

func (a *Api) DeviceListUpdatesCont(url string) (*UpdateList, error) {
Expand All @@ -734,9 +740,10 @@ func (a *Api) DeviceListUpdatesCont(url string) (*UpdateList, error) {
return &updates, nil
}

func (a *Api) DeviceUpdateEvents(device, correlationId string) ([]UpdateEvent, error) {
func (a *Api) DeviceUpdateEvents(factory, device, correlationId string) ([]UpdateEvent, error) {
var events []UpdateEvent
body, err := a.Get(a.serverUrl + "/ota/devices/" + device + "/updates/" + correlationId + "/")
url := a.serverUrl + "/ota/devices/" + device + "/updates/" + correlationId + "/?factory=" + factory
body, err := a.Get(url)
if err != nil {
return nil, err
}
Expand All @@ -747,35 +754,35 @@ func (a *Api) DeviceUpdateEvents(device, correlationId string) ([]UpdateEvent, e
return events, nil
}

func (a *Api) DeviceCreateConfig(device string, cfg ConfigCreateRequest) error {
func (a *Api) DeviceCreateConfig(factory, device string, cfg ConfigCreateRequest) error {
data, err := json.Marshal(cfg)
if err != nil {
return err
}

url := a.serverUrl + "/ota/devices/" + device + "/config/"
url := a.serverUrl + "/ota/devices/" + device + "/config/?factory=" + factory
logrus.Debug("Creating new device config")
_, err = a.Post(url, data)
return err
}

func (a *Api) DevicePatchConfig(device string, cfg ConfigCreateRequest, force bool) error {
func (a *Api) DevicePatchConfig(factory, device string, cfg ConfigCreateRequest, force bool) error {
data, err := json.Marshal(cfg)
if err != nil {
return err
}

url := a.serverUrl + "/ota/devices/" + device + "/config/"
url := a.serverUrl + "/ota/devices/" + device + "/config/?factory=" + factory
if force {
url += "?force=1"
url += "&force=1"
}
logrus.Debug("Patching device config")
_, err = a.Patch(url, data)
return err
}

func (a *Api) DeviceListConfig(device string) (*DeviceConfigList, error) {
url := a.serverUrl + "/ota/devices/" + device + "/config/"
func (a *Api) DeviceListConfig(factory, device string) (*DeviceConfigList, error) {
url := a.serverUrl + "/ota/devices/" + device + "/config/?factory=" + factory
logrus.Debugf("DeviceListConfig with url: %s", url)
return a.DeviceListConfigCont(url)
}
Expand All @@ -794,8 +801,8 @@ func (a *Api) DeviceListConfigCont(url string) (*DeviceConfigList, error) {
return &config, nil
}

func (a *Api) DeviceDeleteConfig(device, filename string) error {
url := a.serverUrl + "/ota/devices/" + device + "/config/" + filename + "/"
func (a *Api) DeviceDeleteConfig(factory, device, filename string) error {
url := a.serverUrl + "/ota/devices/" + device + "/config/" + filename + "/?factory=" + factory
logrus.Debugf("Deleting config file: %s", url)
_, err := a.Delete(url, nil)
return err
Expand Down
4 changes: 3 additions & 1 deletion subcommands/devices/chown.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/foundriesio/fioctl/subcommands"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func init() {
Expand All @@ -18,9 +19,10 @@ and owners. The new owner-id can be found by running 'fioctl users'`,
}

func doChown(cmd *cobra.Command, args []string) {
factory := viper.GetString("factory")
logrus.Debug("Chown %r", args)
device := args[0]
owner := args[1]

subcommands.DieNotNil(api.DeviceChown(device, owner))
subcommands.DieNotNil(api.DeviceChown(factory, device, owner))
}
4 changes: 3 additions & 1 deletion subcommands/devices/config_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package devices
import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/foundriesio/fioctl/subcommands"
)
Expand All @@ -17,7 +18,8 @@ func init() {
}

func doConfigDelete(cmd *cobra.Command, args []string) {
factory := viper.GetString("factory")
logrus.Debug("Deleting file from device config")

subcommands.DieNotNil(api.DeviceDeleteConfig(args[0], args[1]))
subcommands.DieNotNil(api.DeviceDeleteConfig(factory, args[0], args[1]))
}
4 changes: 3 additions & 1 deletion subcommands/devices/config_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/foundriesio/fioctl/subcommands"
)
Expand All @@ -21,6 +22,7 @@ func init() {
}

func doConfigGroup(cmd *cobra.Command, args []string) {
factory := viper.GetString("factory")
device := args[0]
unset, _ := cmd.Flags().GetBool("unset")
var group string
Expand All @@ -38,6 +40,6 @@ func doConfigGroup(cmd *cobra.Command, args []string) {
logrus.Debugf("Assigning device %s to group %s", device, group)
}

err := api.DeviceSetGroup(device, group)
err := api.DeviceSetGroup(factory, device, group)
subcommands.DieNotNil(err)
}
4 changes: 3 additions & 1 deletion subcommands/devices/config_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package devices
import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/foundriesio/fioctl/client"
"github.com/foundriesio/fioctl/subcommands"
Expand All @@ -20,6 +21,7 @@ func init() {
}

func doConfigLog(cmd *cobra.Command, args []string) {
factory := viper.GetString("factory")
device := args[0]
listLimit, _ := cmd.Flags().GetInt("limit")
logrus.Debugf("Showing device config log for %s", device)
Expand All @@ -28,7 +30,7 @@ func doConfigLog(cmd *cobra.Command, args []string) {
Limit: listLimit,
ShowAppliedAt: true,
ListFunc: func() (*client.DeviceConfigList, error) {
return api.DeviceListConfig(device)
return api.DeviceListConfig(factory, device)
},
ListContFunc: api.DeviceListConfigCont,
})
Expand Down
8 changes: 5 additions & 3 deletions subcommands/devices/config_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/foundriesio/fioctl/client"
"github.com/foundriesio/fioctl/subcommands"
Expand Down Expand Up @@ -93,14 +94,15 @@ func eciesEncrypt(content string, pubkey *ecies.PublicKey) string {
}

func doConfigSet(cmd *cobra.Command, args []string) {
factory := viper.GetString("factory")
name := args[0]
reason, _ := cmd.Flags().GetString("reason")
isRaw, _ := cmd.Flags().GetBool("raw")
shouldCreate, _ := cmd.Flags().GetBool("create")

logrus.Debugf("Creating new device config for %s", name)
// Ensure the device has a public key we can encrypt with
device, err := api.DeviceGet(name)
device, err := api.DeviceGet(factory, name)
subcommands.DieNotNil(err)
if len(device.PublicKey) == 0 {
subcommands.DieNotNil(fmt.Errorf("Device has no public key to encrypt with"))
Expand All @@ -113,9 +115,9 @@ func doConfigSet(cmd *cobra.Command, args []string) {
IsRawFile: isRaw,
SetFunc: func(cfg client.ConfigCreateRequest) error {
if shouldCreate {
return api.DeviceCreateConfig(device.Name, cfg)
return api.DeviceCreateConfig(factory, device.Name, cfg)
} else {
return api.DevicePatchConfig(device.Name, cfg, false)
return api.DevicePatchConfig(factory, device.Name, cfg, false)
}
},
EncryptFunc: func(value string) string {
Expand Down
8 changes: 5 additions & 3 deletions subcommands/devices/config_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package devices
import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/foundriesio/fioctl/client"
"github.com/foundriesio/fioctl/subcommands"
Expand Down Expand Up @@ -39,6 +40,7 @@ currently configured and reporting.`,
}

func doConfigUpdates(cmd *cobra.Command, args []string) {
factory := viper.GetString("factory")
name := args[0]
updateApps, _ := cmd.Flags().GetString("apps")
updateTag, _ := cmd.Flags().GetString("tag")
Expand All @@ -51,7 +53,7 @@ func doConfigUpdates(cmd *cobra.Command, args []string) {

logrus.Debugf("Configuring device updates for %s", name)

device, err := api.DeviceGet(name)
device, err := api.DeviceGet(factory, name)
subcommands.DieNotNil(err, "Failed to fetch a device:")

subcommands.SetUpdatesConfig(&subcommands.SetUpdatesConfigOptions{
Expand All @@ -61,10 +63,10 @@ func doConfigUpdates(cmd *cobra.Command, args []string) {
IsForced: isForced,
Device: device,
ListFunc: func() (*client.DeviceConfigList, error) {
return api.DeviceListConfig(name)
return api.DeviceListConfig(factory, name)
},
SetFunc: func(cfg client.ConfigCreateRequest, force bool) error {
return api.DevicePatchConfig(name, cfg, force)
return api.DevicePatchConfig(factory, name, cfg, force)
},
},
device.Tag, device.DockerApps)
Expand Down
15 changes: 7 additions & 8 deletions subcommands/devices/config_wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/foundriesio/fioctl/client"
"github.com/foundriesio/fioctl/subcommands"
Expand Down Expand Up @@ -59,8 +60,8 @@ func (w *WireguardClientConfig) Unmarshall(configVal string) {
}
}

func loadWireguardClientConfig(device string) WireguardClientConfig {
dcl, err := api.DeviceListConfig(device)
func loadWireguardClientConfig(factory, device string) WireguardClientConfig {
dcl, err := api.DeviceListConfig(factory, device)
wcc := WireguardClientConfig{}
if err != nil {
return wcc
Expand Down Expand Up @@ -128,13 +129,11 @@ func findVpnAddress(factory string) string {
}

func doConfigWireguard(cmd *cobra.Command, args []string) {
factory := viper.GetString("factory")
logrus.Debug("Configuring wireguard")

// Ensure the device has a public key we can encrypt with
device, err := api.DeviceGet(args[0])
subcommands.DieNotNil(err)

wcc := loadWireguardClientConfig(args[0])
wcc := loadWireguardClientConfig(factory, args[0])
if len(args) == 1 {
fmt.Println("Enabled:", wcc.Enabled)
if len(wcc.Address) > 0 {
Expand Down Expand Up @@ -168,11 +167,11 @@ func doConfigWireguard(cmd *cobra.Command, args []string) {
wcc.Enabled = true
if len(wcc.Address) == 0 {
fmt.Println("Finding a unique VPN address ...")
wcc.Address = findVpnAddress(device.Factory)
wcc.Address = findVpnAddress(factory)
}
} else {
wcc.Enabled = false
}
cfg.Files[0].Value = wcc.Marshall()
subcommands.DieNotNil(api.DevicePatchConfig(args[0], cfg, false))
subcommands.DieNotNil(api.DevicePatchConfig(factory, args[0], cfg, false))
}
4 changes: 3 additions & 1 deletion subcommands/devices/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func init() {
Expand All @@ -18,11 +19,12 @@ func init() {
}

func doDelete(cmd *cobra.Command, args []string) {
factory := viper.GetString("factory")
logrus.Debug("Deleting %r", args)

for _, name := range args {
fmt.Printf("Deleting %s .. ", name)
if err := api.DeviceDelete(name); err != nil {
if err := api.DeviceDelete(factory, name); err != nil {
fmt.Printf("failed\n%s", err)
os.Exit(1)
} else {
Expand Down
4 changes: 3 additions & 1 deletion subcommands/devices/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func init() {
Expand All @@ -18,9 +19,10 @@ func init() {
}

func doRename(cmd *cobra.Command, args []string) {
factory := viper.GetString("factory")
logrus.Debugf("Renaming %s -> %s", args[0], args[1])

if err := api.DeviceRename(args[0], args[1]); err != nil {
if err := api.DeviceRename(factory, args[0], args[1]); err != nil {
fmt.Printf("failed\n%s", err)
os.Exit(1)
}
Expand Down
Loading

0 comments on commit 98b53c3

Please sign in to comment.