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.
26 "github.com/franciscocpg/reflectme"
27 "github.com/iotbzh/xds-agent/lib/xaapiv1"
28 "github.com/urfave/cli"
31 var cacheXdsVersion *xaapiv1.XDSVersion
32 var cacheData = xaapiv1.XDSVersion{}
34 // XdsVersionGet Get version of XDS agent & server
35 func XdsVersionGet(ver *xaapiv1.XDSVersion) error {
37 if cacheXdsVersion == nil {
38 if err := HTTPCli.Get("/version", &cacheData); err != nil {
42 reflectme.Copy(&cacheData, ver)
46 // XdsConfigGet Get current XDS Agent config
47 func XdsConfigGet(cfg *xaapiv1.APIConfig) error {
48 return HTTPCli.Get("/config", cfg)
51 // XdsConfigSet Set XDS Agent config
52 func XdsConfigSet(cfg xaapiv1.APIConfig) error {
56 newCfg := xaapiv1.APIConfig{}
57 if err := HTTPCli.Post("/config", cfg, &newCfg); err != nil {
61 idx := XdsServerIndexGet()
62 if !newCfg.Servers[idx].Connected {
63 return fmt.Errorf("XDS server %s still not connected", cfg.Servers[idx].URL)
69 // XdsServerIDGet returns the XDS Server ID
70 func XdsServerIDGet() string {
71 ver := xaapiv1.XDSVersion{}
72 if err := XdsVersionGet(&ver); err != nil {
75 if len(ver.Server) < 1 {
78 return ver.Server[XdsServerIndexGet()].ID
81 // XdsServerIndexGet returns the index number of XDS Server
82 func XdsServerIndexGet() int {
83 // FIXME support multiple server
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
92 // ProjectsListGet Get the list of existing projects
93 func ProjectsListGet(prjs *[]xaapiv1.ProjectConfig) error {
95 if err := HTTPCli.HTTPGet("/projects", &data); err != nil {
98 Log.Debugf("Result of /projects: %v", string(data[:]))
100 return json.Unmarshal(data, &prjs)
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))
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 != "" {