Added sdks install / uninstall / abort commands.
[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         "strconv"
25
26         "github.com/franciscocpg/reflectme"
27         "github.com/iotbzh/xds-agent/lib/xaapiv1"
28         "github.com/urfave/cli"
29 )
30
31 var cacheXdsVersion *xaapiv1.XDSVersion
32 var cacheData = xaapiv1.XDSVersion{}
33
34 // XdsVersionGet Get version of XDS agent & server
35 func XdsVersionGet(ver *xaapiv1.XDSVersion) error {
36         // Update cached data
37         if cacheXdsVersion == nil {
38                 if err := HTTPCli.Get("/version", &cacheData); err != nil {
39                         return err
40                 }
41         }
42         reflectme.Copy(&cacheData, ver)
43         return nil
44 }
45
46 // XdsConfigGet Get current XDS Agent config
47 func XdsConfigGet(cfg *xaapiv1.APIConfig) error {
48         return HTTPCli.Get("/config", cfg)
49 }
50
51 // XdsConfigSet Set XDS Agent config
52 func XdsConfigSet(cfg xaapiv1.APIConfig) error {
53         // clear cache
54         cacheXdsVersion = nil
55
56         newCfg := xaapiv1.APIConfig{}
57         if err := HTTPCli.Post("/config", cfg, &newCfg); err != nil {
58                 return err
59         }
60
61         idx := XdsServerIndexGet()
62         if !newCfg.Servers[idx].Connected {
63                 return fmt.Errorf("XDS server %s still not connected", cfg.Servers[idx].URL)
64         }
65
66         return nil
67 }
68
69 // XdsServerIDGet returns the XDS Server ID
70 func XdsServerIDGet() string {
71         ver := xaapiv1.XDSVersion{}
72         if err := XdsVersionGet(&ver); err != nil {
73                 return ""
74         }
75         if len(ver.Server) < 1 {
76                 return ""
77         }
78         return ver.Server[XdsServerIndexGet()].ID
79 }
80
81 // XdsServerIndexGet returns the index number of XDS Server
82 func XdsServerIndexGet() int {
83         // FIXME support multiple server
84         return 0
85 }
86
87 // XdsServerComputeURL computes the URL used to access to XDS Server API
88 func XdsServerComputeURL(endURL string) string {
89         return "servers/" + strconv.Itoa(XdsServerIndexGet()) + endURL
90 }
91
92 // ProjectsListGet Get the list of existing projects
93 func ProjectsListGet(prjs *[]xaapiv1.ProjectConfig) error {
94         var data []byte
95         if err := HTTPCli.HTTPGet("/projects", &data); err != nil {
96                 return err
97         }
98         Log.Debugf("Result of /projects: %v", string(data[:]))
99
100         return json.Unmarshal(data, &prjs)
101 }
102
103 // LogPost Helper to log a POST request
104 func LogPost(format string, data interface{}) {
105         b, _ := json.Marshal(data)
106         Log.Infof(format, string(b))
107 }
108
109 // GetID Return a string ID set with --id option or as simple parameter
110 func GetID(ctx *cli.Context) string {
111         id := ctx.String("id")
112         idArgs := ctx.Args().First()
113         if id == "" && idArgs != "" {
114                 id = idArgs
115         }
116         return id
117 }