summaryrefslogtreecommitdiffstats
path: root/utils.go
diff options
context:
space:
mode:
Diffstat (limited to 'utils.go')
-rw-r--r--utils.go73
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
+}