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