2 * Copyright (C) 2017 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 "github.com/franciscocpg/reflectme"
26 "github.com/iotbzh/xds-agent/lib/xaapiv1"
27 "github.com/urfave/cli"
30 var cacheXdsVersion *xaapiv1.XDSVersion
31 var cacheData = xaapiv1.XDSVersion{}
33 // XdsVersionGet Get version of XDS agent & server
34 func XdsVersionGet(ver *xaapiv1.XDSVersion) error {
36 if cacheXdsVersion == nil {
37 if err := HTTPCli.Get("/version", &cacheData); err != nil {
41 reflectme.Copy(&cacheData, ver)
45 // XdsConfigGet Get current XDS Agent config
46 func XdsConfigGet(cfg *xaapiv1.APIConfig) error {
47 return HTTPCli.Get("/config", cfg)
50 // XdsConfigSet Set XDS Agent config
51 func XdsConfigSet(cfg xaapiv1.APIConfig) error {
55 newCfg := xaapiv1.APIConfig{}
56 if err := HTTPCli.Post("/config", cfg, &newCfg); err != nil {
60 idx := XdsServerIndexGet()
61 if !newCfg.Servers[idx].Connected {
62 return fmt.Errorf("XDS server %s still not connected", cfg.Servers[idx].URL)
68 // XdsServerIDGet returns the XDS Server ID
69 func XdsServerIDGet() string {
70 ver := xaapiv1.XDSVersion{}
71 if err := XdsVersionGet(&ver); err != nil {
74 if len(ver.Server) < 1 {
77 return ver.Server[XdsServerIndexGet()].ID
80 // XdsServerIndexGet returns the index number of XDS Server
81 func XdsServerIndexGet() int {
82 // FIXME support multiple server
86 // ProjectsListGet Get the list of existing projects
87 func ProjectsListGet(prjs *[]xaapiv1.ProjectConfig) error {
89 if err := HTTPCli.HTTPGet("/projects", &data); err != nil {
92 Log.Debugf("Result of /projects: %v", string(data[:]))
94 return json.Unmarshal(data, &prjs)
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))
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 != "" {