本文整理汇总了Golang中github.com/juju/juju/api.Client类的典型用法代码示例。如果您正苦于以下问题:Golang Client类的具体用法?Golang Client怎么用?Golang Client使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Client类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: addCharmViaAPI
// addCharmViaAPI calls the appropriate client API calls to add the
// given charm URL to state. Also displays the charm URL of the added
// charm on stdout.
func addCharmViaAPI(client *api.Client, ctx *cmd.Context, curl *charm.URL, repo charm.Repository) (*charm.URL, error) {
if curl.Revision < 0 {
latest, err := charm.Latest(repo, curl)
if err != nil {
return nil, err
}
curl = curl.WithRevision(latest)
}
switch curl.Schema {
case "local":
ch, err := repo.Get(curl)
if err != nil {
return nil, err
}
stateCurl, err := client.AddLocalCharm(curl, ch)
if err != nil {
return nil, err
}
curl = stateCurl
case "cs":
err := client.AddCharm(curl)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unsupported charm URL schema: %q", curl.Schema)
}
ctx.Infof("Added charm %q to the environment.", curl)
return curl, nil
}
示例2: addLocalCharm
func addLocalCharm(c *gc.C, client *api.Client, name string) (*charm.URL, *charm.CharmArchive) {
charmArchive := testcharms.Repo.CharmArchive(c.MkDir(), name)
curl := charm.MustParseURL(fmt.Sprintf("local:quantal/%s-%d", charmArchive.Meta().Name, charmArchive.Revision()))
_, err := client.AddLocalCharm(curl, charmArchive)
c.Assert(err, jc.ErrorIsNil)
return curl, charmArchive
}
示例3: runMachineUpdate
// runMachineUpdate connects via ssh to the machine and runs the update script
func runMachineUpdate(client *api.Client, id string, sshArg string) error {
progress("updating machine: %v\n", id)
addr, err := client.PublicAddress(id)
if err != nil {
return errors.Annotate(err, "no public address found")
}
return runViaSsh(addr, sshArg)
}
示例4: getMetaResources
func getMetaResources(cURL *charm.URL, client *api.Client) (map[string]charmresource.Meta, error) {
// this gets the charm info that was added to the controller using addcharm.
charmInfo, err := client.CharmInfo(cURL.String())
if err != nil {
return nil, errors.Trace(err)
}
return charmInfo.Meta.Resources, nil
}
示例5: destroyEnv
func (c *destroyEnvironmentCommand) destroyEnv(apiclient *api.Client) (result error) {
defer func() {
result = c.ensureUserFriendlyErrorLog(result)
}()
err := apiclient.DestroyEnvironment()
if cmdErr := processDestroyError(err); cmdErr != nil {
return cmdErr
}
return nil
}
示例6: addCharmFromURL
// addCharmFromURL calls the appropriate client API calls to add the
// given charm URL to state. For non-public charm URLs, this function also
// handles the macaroon authorization process using the given csClient.
// The resulting charm URL of the added charm is displayed on stdout.
func addCharmFromURL(client *api.Client, curl *charm.URL, channel csparams.Channel, csClient *csclient.Client) (*charm.URL, *macaroon.Macaroon, error) {
var csMac *macaroon.Macaroon
if err := client.AddCharm(curl, channel); err != nil {
if !params.IsCodeUnauthorized(err) {
return nil, nil, errors.Trace(err)
}
m, err := authorizeCharmStoreEntity(csClient, curl)
if err != nil {
return nil, nil, maybeTermsAgreementError(err)
}
if err := client.AddCharmWithAuthorization(curl, channel, m); err != nil {
return nil, nil, errors.Trace(err)
}
csMac = m
}
return curl, csMac, nil
}
示例7: addCharmFromURL
// addCharmFromURL calls the appropriate client API calls to add the
// given charm URL to state. For non-public charm URLs, this function also
// handles the macaroon authorization process using the given csClient.
// The resulting charm URL of the added charm is displayed on stdout.
func addCharmFromURL(client *api.Client, curl *charm.URL, repo charmrepo.Interface, csclient *csClient) (*charm.URL, error) {
switch curl.Schema {
case "local":
ch, err := repo.Get(curl)
if err != nil {
return nil, err
}
stateCurl, err := client.AddLocalCharm(curl, ch)
if err != nil {
return nil, err
}
curl = stateCurl
case "cs":
if err := client.AddCharm(curl); err != nil {
if !params.IsCodeUnauthorized(err) {
return nil, errors.Trace(err)
}
m, err := csclient.authorize(curl)
if err != nil {
return nil, maybeTermsAgreementError(err)
}
if err := client.AddCharmWithAuthorization(curl, m); err != nil {
return nil, errors.Trace(err)
}
}
default:
return nil, fmt.Errorf("unsupported charm URL schema: %q", curl.Schema)
}
return curl, nil
}
示例8: addCharmViaAPI
// addCharmViaAPI calls the appropriate client API calls to add the
// given charm URL to state. For non-public charm URLs, this function also
// handles the macaroon authorization process using the given csClient.
// The resulting charm URL of the added charm is displayed on stdout.
func addCharmViaAPI(client *api.Client, ctx *cmd.Context, curl *charm.URL, repo charmrepo.Interface, csclient *csClient) (*charm.URL, error) {
switch curl.Schema {
case "local":
ch, err := repo.Get(curl)
if err != nil {
return nil, err
}
stateCurl, err := client.AddLocalCharm(curl, ch)
if err != nil {
return nil, err
}
curl = stateCurl
case "cs":
if err := client.AddCharm(curl); err != nil {
if !params.IsCodeUnauthorized(err) {
return nil, errors.Mask(err)
}
m, err := csclient.authorize(curl)
if err != nil {
return nil, errors.Mask(err)
}
if err := client.AddCharmWithAuthorization(curl, m); err != nil {
return nil, errors.Mask(err)
}
}
default:
return nil, fmt.Errorf("unsupported charm URL schema: %q", curl.Schema)
}
ctx.Infof("Added charm %q to the environment.", curl)
return curl, nil
}
示例9: upgradeCharm
// upgradeCharm upgrades the charm for the given service to the given charm id.
// If the service is already deployed using the given charm id, do nothing.
// This function returns an error if the existing charm and the target one are
// incompatible, meaning an upgrade from one to the other is not allowed.
func upgradeCharm(client *api.Client, log deploymentLogger, service, id string) error {
existing, err := client.ServiceGetCharmURL(service)
if err != nil {
return errors.Annotatef(err, "cannot retrieve info for service %q", service)
}
if existing.String() == id {
log.Infof("reusing service %s (charm: %s)", service, id)
return nil
}
url, err := charm.ParseURL(id)
if err != nil {
return errors.Annotatef(err, "cannot parse charm URL %q", id)
}
if url.WithRevision(-1).Path() != existing.WithRevision(-1).Path() {
return errors.Errorf("bundle charm %q is incompatible with existing charm %q", id, existing)
}
if err := client.ServiceSetCharm(service, id, false); err != nil {
return errors.Annotatef(err, "cannot upgrade charm to %q", id)
}
log.Infof("upgraded charm for existing service %s (from %s to %s)", service, existing, id)
return nil
}
示例10: testMinVer
func testMinVer(client *api.Client, t minverTest, c *gc.C) {
charmMinVer := version.MustParse(t.charm)
jujuVer := version.MustParse(t.juju)
cleanup := api.PatchClientFacadeCall(client,
func(request string, paramsIn interface{}, response interface{}) error {
c.Assert(paramsIn, gc.IsNil)
if response, ok := response.(*params.AgentVersionResult); ok {
response.Version = jujuVer
} else {
c.Log("wrong output structure")
c.Fail()
}
return nil
},
)
defer cleanup()
charmArchive := testcharms.Repo.CharmArchive(c.MkDir(), "dummy")
curl := charm.MustParseURL(
fmt.Sprintf("local:quantal/%s-%d", charmArchive.Meta().Name, charmArchive.Revision()),
)
charmArchive.Meta().MinJujuVersion = charmMinVer
_, err := client.AddLocalCharm(curl, charmArchive)
if t.ok {
if err != nil {
c.Errorf("Unexpected non-nil error for jujuver %v, minver %v: %#v", t.juju, t.charm, err)
}
} else {
if err == nil {
c.Errorf("Unexpected nil error for jujuver %v, minver %v", t.juju, t.charm)
} else if !api.IsMinVersionError(err) {
c.Errorf("Wrong error for jujuver %v, minver %v: expected minVersionError, got: %#v", t.juju, t.charm, err)
}
}
}
示例11: resolveCharmURL
// resolveCharmURL returns a resolved charm URL, given a charm location string.
// If the series is not resolved, the environment default-series is used, or if
// not set, the series is resolved with the state server.
func resolveCharmURL(url string, client *api.Client, conf *config.Config) (*charm.URL, error) {
ref, err := charm.ParseReference(url)
if err != nil {
return nil, err
}
// If series is not set, use configured default series
if ref.Series == "" {
if defaultSeries, ok := conf.DefaultSeries(); ok {
ref.Series = defaultSeries
}
}
if ref.Series != "" {
return ref.URL("")
}
// Otherwise, look up the best supported series for this charm
if ref.Schema != "local" {
return client.ResolveCharm(ref)
}
possibleURL := *ref
possibleURL.Series = "precise"
logger.Errorf("The series is not specified in the environment (default-series) or with the charm. Did you mean:\n\t%s", &possibleURL)
return nil, fmt.Errorf("cannot resolve series for charm: %q", ref)
}
示例12: initVersions
// initVersions collects state relevant to an upgrade decision. The returned
// agent and client versions, and the list of currently available tools, will
// always be accurate; the chosen version, and the flag indicating development
// mode, may remain blank until uploadTools or validate is called.
func (c *UpgradeJujuCommand) initVersions(client *api.Client, cfg *config.Config) (*upgradeContext, error) {
agent, ok := cfg.AgentVersion()
if !ok {
// Can't happen. In theory.
return nil, fmt.Errorf("incomplete environment configuration")
}
if c.Version == agent {
return nil, errUpToDate
}
clientVersion := version.Current.Number
findResult, err := client.FindTools(clientVersion.Major, -1, "", "")
if err != nil {
return nil, err
}
err = findResult.Error
if findResult.Error != nil {
if !params.IsCodeNotFound(err) {
return nil, err
}
if !c.UploadTools {
// No tools found and we shouldn't upload any, so if we are not asking for a
// major upgrade, pretend there is no more recent version available.
if c.Version == version.Zero && agent.Major == clientVersion.Major {
return nil, errUpToDate
}
return nil, err
}
}
return &upgradeContext{
agent: agent,
client: clientVersion,
chosen: c.Version,
tools: findResult.List,
apiClient: client,
config: cfg,
}, nil
}
示例13: addCharmFromURL
// addCharmFromURL calls the appropriate client API calls to add the
// given charm URL to state. For non-public charm URLs, this function also
// handles the macaroon authorization process using the given csClient.
// The resulting charm URL of the added charm is displayed on stdout.
//
// The repo holds the charm repository associated with with the URL
// by resolveCharmStoreEntityURL.
func addCharmFromURL(client *api.Client, curl *charm.URL, channel csparams.Channel, repo charmrepo.Interface) (*charm.URL, *macaroon.Macaroon, error) {
var csMac *macaroon.Macaroon
switch curl.Schema {
case "local":
ch, err := repo.Get(curl)
if err != nil {
return nil, nil, err
}
stateCurl, err := client.AddLocalCharm(curl, ch)
if err != nil {
return nil, nil, err
}
curl = stateCurl
case "cs":
repo, ok := repo.(*charmrepo.CharmStore)
if !ok {
return nil, nil, errors.Errorf("(cannot happen) cs-schema URL with unexpected repo type %T", repo)
}
csClient := repo.Client()
if err := client.AddCharm(curl, channel); err != nil {
if !params.IsCodeUnauthorized(err) {
return nil, nil, errors.Trace(err)
}
m, err := authorizeCharmStoreEntity(csClient, curl)
if err != nil {
return nil, nil, maybeTermsAgreementError(err)
}
if err := client.AddCharmWithAuthorization(curl, channel, m); err != nil {
return nil, nil, errors.Trace(err)
}
csMac = m
}
default:
return nil, nil, fmt.Errorf("unsupported charm URL schema: %q", curl.Schema)
}
return curl, csMac, nil
}
示例14: addCharm
// addCharm interprets the new charmRef and adds the specified charm if the new charm is different
// to what's already deployed as specified by oldURL.
func (c *upgradeCharmCommand) addCharm(
oldURL *charm.URL,
charmRef string,
client *api.Client,
resolver *charmURLResolver,
) (charmstore.CharmID, *macaroon.Macaroon, error) {
var id charmstore.CharmID
// Charm may have been supplied via a path reference.
ch, newURL, err := charmrepo.NewCharmAtPathForceSeries(charmRef, oldURL.Series, c.ForceSeries)
if err == nil {
_, newName := filepath.Split(charmRef)
if newName != oldURL.Name {
return id, nil, fmt.Errorf("cannot upgrade %q to %q", oldURL.Name, newName)
}
addedURL, err := client.AddLocalCharm(newURL, ch)
id.URL = addedURL
return id, nil, err
}
if _, ok := err.(*charmrepo.NotFoundError); ok {
return id, nil, errors.Errorf("no charm found at %q", charmRef)
}
// If we get a "not exists" or invalid path error then we attempt to interpret
// the supplied charm reference as a URL below, otherwise we return the error.
if err != os.ErrNotExist && !charmrepo.IsInvalidPathError(err) {
return id, nil, err
}
// Charm has been supplied as a URL so we resolve and deploy using the store.
newURL, channel, supportedSeries, repo, err := resolver.resolve(charmRef)
if err != nil {
return id, nil, errors.Trace(err)
}
id.Channel = channel
if !c.ForceSeries && oldURL.Series != "" && newURL.Series == "" && !isSeriesSupported(oldURL.Series, supportedSeries) {
series := []string{"no series"}
if len(supportedSeries) > 0 {
series = supportedSeries
}
return id, nil, errors.Errorf(
"cannot upgrade from single series %q charm to a charm supporting %q. Use --force-series to override.",
oldURL.Series, series,
)
}
// If no explicit revision was set with either SwitchURL
// or Revision flags, discover the latest.
if *newURL == *oldURL {
newRef, _ := charm.ParseURL(charmRef)
if newRef.Revision != -1 {
return id, nil, fmt.Errorf("already running specified charm %q", newURL)
}
if newURL.Schema == "cs" {
// No point in trying to upgrade a charm store charm when
// we just determined that's the latest revision
// available.
return id, nil, fmt.Errorf("already running latest charm %q", newURL)
}
}
curl, csMac, err := addCharmFromURL(client, newURL, channel, repo)
if err != nil {
return id, nil, errors.Trace(err)
}
id.URL = curl
return id, csMac, nil
}
示例15: deployBundle
// deployBundle deploys the given bundle data using the given API client and
// charm store client. The deployment is not transactional, and its progress is
// notified using the given deployment logger.
func deployBundle(
data *charm.BundleData, client *api.Client, serviceDeployer *serviceDeployer,
csclient *csClient, repoPath string, conf *config.Config, log deploymentLogger,
bundleStorage map[string]map[string]storage.Constraints,
) error {
verifyConstraints := func(s string) error {
_, err := constraints.Parse(s)
return err
}
verifyStorage := func(s string) error {
_, err := storage.ParseConstraints(s)
return err
}
if err := data.Verify(verifyConstraints, verifyStorage); err != nil {
return errors.Annotate(err, "cannot deploy bundle")
}
// Retrieve bundle changes.
changes := bundlechanges.FromData(data)
numChanges := len(changes)
// Initialize the unit status.
status, err := client.Status(nil)
if err != nil {
return errors.Annotate(err, "cannot get model status")
}
unitStatus := make(map[string]string, numChanges)
for _, serviceData := range status.Services {
for unit, unitData := range serviceData.Units {
unitStatus[unit] = unitData.Machine
}
}
// Instantiate a watcher used to follow the deployment progress.
watcher, err := watchAll(client)
if err != nil {
return errors.Annotate(err, "cannot watch model")
}
defer watcher.Stop()
serviceClient, err := serviceDeployer.newServiceAPIClient()
if err != nil {
return errors.Annotate(err, "cannot get service client")
}
annotationsClient, err := serviceDeployer.newAnnotationsAPIClient()
if err != nil {
return errors.Annotate(err, "cannot get annotations client")
}
// Instantiate the bundle handler.
h := &bundleHandler{
changes: changes,
results: make(map[string]string, numChanges),
client: client,
serviceClient: serviceClient,
annotationsClient: annotationsClient,
serviceDeployer: serviceDeployer,
bundleStorage: bundleStorage,
csclient: csclient,
repoPath: repoPath,
conf: conf,
log: log,
data: data,
unitStatus: unitStatus,
ignoredMachines: make(map[string]bool, len(data.Services)),
ignoredUnits: make(map[string]bool, len(data.Services)),
watcher: watcher,
}
// Deploy the bundle.
for _, change := range changes {
switch change := change.(type) {
case *bundlechanges.AddCharmChange:
err = h.addCharm(change.Id(), change.Params)
case *bundlechanges.AddMachineChange:
err = h.addMachine(change.Id(), change.Params)
case *bundlechanges.AddRelationChange:
err = h.addRelation(change.Id(), change.Params)
case *bundlechanges.AddServiceChange:
err = h.addService(change.Id(), change.Params)
case *bundlechanges.AddUnitChange:
err = h.addUnit(change.Id(), change.Params)
case *bundlechanges.ExposeChange:
err = h.exposeService(change.Id(), change.Params)
case *bundlechanges.SetAnnotationsChange:
err = h.setAnnotations(change.Id(), change.Params)
default:
return errors.Errorf("unknown change type: %T", change)
}
if err != nil {
return errors.Annotate(err, "cannot deploy bundle")
}
}
return nil
}