d6324151d9af855fd29b397cf5d85af6f34d676e
[src/xds/xds-cli.git] / utils.go
1 /*
2  * Copyright (C) 2017 "IoT.bzh"
3  * Author Sebastien Douheret <sebastien@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 package main
20
21 import (
22         "encoding/json"
23         "fmt"
24
25         "github.com/franciscocpg/reflectme"
26         "github.com/iotbzh/xds-agent/lib/xaapiv1"
27         "github.com/urfave/cli"
28 )
29
30 var cacheXdsVersion *xaapiv1.XDSVersion
31 var cacheData = xaapiv1.XDSVersion{}
32
33 // XdsVersionGet Get version of XDS agent & server
34 func XdsVersionGet(ver *xaapiv1.XDSVersion) error {
35         // Update cached data
36         if cacheXdsVersion == nil {
37                 if err := HTTPCli.Get("/version", &cacheData); err != nil {
38                         return err
39                 }
40         }
41         reflectme.Copy(&cacheData, ver)
42         return nil
43 }
44
45 // XdsConfigGet Get current XDS Agent config
46 func XdsConfigGet(cfg *xaapiv1.APIConfig) error {
47         return HTTPCli.Get("/config", cfg)
48 }
49
50 // XdsConfigSet Set XDS Agent config
51 func XdsConfigSet(cfg xaapiv1.APIConfig) error {
52         // clear cache
53         cacheXdsVersion = nil
54
55         newCfg := xaapiv1.APIConfig{}
56         if err := HTTPCli.Post("/config", cfg, &newCfg); err != nil {
57                 return err
58         }
59
60         idx := XdsServerIndexGet()
61         if !newCfg.Servers[idx].Connected {
62                 return fmt.Errorf("XDS server %s still not connected", cfg.Servers[idx].URL)
63         }
64
65         return nil
66 }
67
68 // XdsServerIDGet returns the XDS Server ID
69 func XdsServerIDGet() string {
70         ver := xaapiv1.XDSVersion{}
71         if err := XdsVersionGet(&ver); err != nil {
72                 return ""
73         }
74         if len(ver.Server) < 1 {
75                 return ""
76         }
77         return ver.Server[XdsServerIndexGet()].ID
78 }
79
80 // XdsServerIndexGet returns the index number of XDS Server
81 func XdsServerIndexGet() int {
82         // FIXME support multiple server
83         return 0
84 }
85
86 // ProjectsListGet Get the list of existing projects
87 func ProjectsListGet(prjs *[]xaapiv1.ProjectConfig) error {
88         var data []byte
89         if err := HTTPCli.HTTPGet("/projects", &data); err != nil {
90                 return err
91         }
92         Log.Debugf("Result of /projects: %v", string(data[:]))
93
94         return json.Unmarshal(data, &prjs)
95 }
96
97 // LogPost Helper to log a POST request
98 func LogPost(format string, data interface{}) {
99         b, _ := json.Marshal(data)
100         Log.Infof(format, string(b))
101 }
102
103 // GetID Return a string ID set with --id option or as simple parameter
104 func GetID(ctx *cli.Context) string {
105         id := ctx.String("id")
106         idArgs := ctx.Args().First()
107         if id == "" && idArgs != "" {
108                 id = idArgs
109         }
110         return id
111 }