diff options
author | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-11-03 18:32:24 +0100 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-11-06 15:35:18 +0100 |
commit | c35d7a0fc8bbb1f9123bb41a7b66e45ea2564dd2 (patch) | |
tree | 38e0ab51a68548bb61cf6ca0de01a268f41a5701 /utils.go |
Initial commitv0.0.1
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'utils.go')
-rw-r--r-- | utils.go | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..6a05ef3 --- /dev/null +++ b/utils.go @@ -0,0 +1,73 @@ +package main + +import ( + "encoding/json" + + "github.com/iotbzh/xds-agent/lib/apiv1" + "github.com/urfave/cli" +) + +var cacheXdsVersion *apiv1.XDSVersion + +// XdsVersionGet Get version of XDS agent & server +func XdsVersionGet(ver *apiv1.XDSVersion) error { + // Use cached data + if cacheXdsVersion != nil { + ver = cacheXdsVersion + return nil + } + + dataVer := apiv1.XDSVersion{} + if err := HTTPCli.Get("/version", &dataVer); err != nil { + return err + } + + cacheXdsVersion = &dataVer + *ver = dataVer + return nil +} + +// XdsServerIDGet returns the XDS Server ID +func XdsServerIDGet() string { + ver := apiv1.XDSVersion{} + if err := XdsVersionGet(&ver); err != nil { + return "" + } + if len(ver.Server) < 1 { + return "" + } + return ver.Server[XdsServerIndexGet()].ID +} + +// XdsServerIndexGet returns the index number of XDS Server +func XdsServerIndexGet() int { + // FIXME support multiple server + return 0 +} + +// ProjectsListGet Get the list of existing projects +func ProjectsListGet(prjs *[]apiv1.ProjectConfig) error { + var data []byte + if err := HTTPCli.HTTPGet("/projects", &data); err != nil { + return err + } + Log.Debugf("Result of /projects: %v", string(data[:])) + + return json.Unmarshal(data, &prjs) +} + +// LogPost Helper to log a POST request +func LogPost(format string, data interface{}) { + b, _ := json.Marshal(data) + Log.Infof(format, string(b)) +} + +// GetID Return a string ID set with --id option or as simple parameter +func GetID(ctx *cli.Context) string { + id := ctx.String("id") + idArgs := ctx.Args().First() + if id == "" && idArgs != "" { + id = idArgs + } + return id +} |