X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=utils.go;h=d6324151d9af855fd29b397cf5d85af6f34d676e;hb=6f71a805b2f6f961761b5906a21d2a234b84aa4c;hp=b21fc19a36d5ba4d4a0d83154ad3a034a8738464;hpb=75416fe01aa98b53c57315a050fc58f0879a238d;p=src%2Fxds%2Fxds-cli.git diff --git a/utils.go b/utils.go index b21fc19..d632415 100644 --- a/utils.go +++ b/utils.go @@ -1,29 +1,67 @@ +/* + * Copyright (C) 2017 "IoT.bzh" + * Author Sebastien Douheret + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + package main import ( "encoding/json" + "fmt" + "github.com/franciscocpg/reflectme" "github.com/iotbzh/xds-agent/lib/xaapiv1" "github.com/urfave/cli" ) var cacheXdsVersion *xaapiv1.XDSVersion +var cacheData = xaapiv1.XDSVersion{} // XdsVersionGet Get version of XDS agent & server func XdsVersionGet(ver *xaapiv1.XDSVersion) error { - // Use cached data - if cacheXdsVersion != nil { - ver = cacheXdsVersion - return nil + // Update cached data + if cacheXdsVersion == nil { + if err := HTTPCli.Get("/version", &cacheData); err != nil { + return err + } } + reflectme.Copy(&cacheData, ver) + return nil +} + +// XdsConfigGet Get current XDS Agent config +func XdsConfigGet(cfg *xaapiv1.APIConfig) error { + return HTTPCli.Get("/config", cfg) +} + +// XdsConfigSet Set XDS Agent config +func XdsConfigSet(cfg xaapiv1.APIConfig) error { + // clear cache + cacheXdsVersion = nil - dataVer := xaapiv1.XDSVersion{} - if err := HTTPCli.Get("/version", &dataVer); err != nil { + newCfg := xaapiv1.APIConfig{} + if err := HTTPCli.Post("/config", cfg, &newCfg); err != nil { return err } - cacheXdsVersion = &dataVer - *ver = dataVer + idx := XdsServerIndexGet() + if !newCfg.Servers[idx].Connected { + return fmt.Errorf("XDS server %s still not connected", cfg.Servers[idx].URL) + } + return nil }