github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/romulus/api.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package romulus 5 6 import ( 7 "github.com/juju/errors" 8 9 "github.com/juju/juju/api/controller" 10 "github.com/juju/juju/cmd/modelcmd" 11 ) 12 13 // GetMeteringURLForModelCmd returns the controller-configured metering URL for 14 // a given model command. 15 var GetMeteringURLForModelCmd = getMeteringURLForModelCmdImpl 16 17 // GetMeteringURLForControllerCmd returns the controller-configured metering URL for 18 // a given controller command. 19 var GetMeteringURLForControllerCmd = getMeteringURLForControllerCmdImpl 20 21 func getMeteringURLForModelCmdImpl(c *modelcmd.ModelCommandBase) (string, error) { 22 controllerAPIRoot, err := c.NewControllerAPIRoot() 23 if err != nil { 24 return "", errors.Trace(err) 25 } 26 controllerAPI := controller.NewClient(controllerAPIRoot) 27 controllerCfg, err := controllerAPI.ControllerConfig() 28 if err != nil { 29 return "", errors.Trace(err) 30 } 31 return controllerCfg.MeteringURL(), nil 32 } 33 34 func getMeteringURLForControllerCmdImpl(c *modelcmd.ControllerCommandBase) (string, error) { 35 controllerAPIRoot, err := c.NewAPIRoot() 36 if err != nil { 37 return "", errors.Trace(err) 38 } 39 controllerAPI := controller.NewClient(controllerAPIRoot) 40 controllerCfg, err := controllerAPI.ControllerConfig() 41 if err != nil { 42 return "", errors.Trace(err) 43 } 44 return controllerCfg.MeteringURL(), nil 45 }