Esempio n. 1
0
File: register.go Progetto: bac/juju
// updateController prompts for a controller name and updates the
// controller and account details in the given client store.
// It returns the name of the updated controller.
func (c *registerCommand) updateController(
	ctx *cmd.Context,
	store jujuclient.ClientStore,
	defaultControllerName string,
	controllerDetails jujuclient.ControllerDetails,
	accountDetails jujuclient.AccountDetails,
) (string, error) {
	// Check that the same controller isn't already stored, so that we
	// can avoid needlessly asking for a controller name in that case.
	all, err := store.AllControllers()
	if err != nil {
		return "", errors.Trace(err)
	}
	for name, ctl := range all {
		if ctl.ControllerUUID == controllerDetails.ControllerUUID {
			// TODO(rogpeppe) lp#1614010 Succeed but override the account details in this case?
			return "", errors.Errorf("controller is already registered as %q", name)
		}
	}
	controllerName, err := c.promptControllerName(store, defaultControllerName, ctx.Stderr, ctx.Stdin)
	if err != nil {
		return "", errors.Trace(err)
	}

	if err := store.AddController(controllerName, controllerDetails); err != nil {
		return "", errors.Trace(err)
	}
	if err := store.UpdateAccount(controllerName, accountDetails); err != nil {
		return "", errors.Annotatef(err, "cannot update account information: %v", err)
	}
	return controllerName, nil
}
Esempio n. 2
0
func translateControllerError(store jujuclient.ClientStore, err error) error {
	if !errors.IsNotFound(err) {
		return err
	}
	controllers, err2 := store.AllControllers()
	if err2 != nil {
		return err2
	}
	if len(controllers) == 0 {
		return errors.Wrap(err, ErrNoControllersDefined)
	}
	return errors.Wrap(err, ErrNoCurrentController)
}
Esempio n. 3
0
func newCacheStore(store jujuclient.ClientStore) (*cacheStore, error) {
	controllers, err := store.AllControllers()
	if err != nil {
		return nil, errors.Trace(err)
	}
	accounts := make(map[string]jujuclient.AccountDetails)
	for controller := range controllers {
		acct, err := store.AccountDetails(controller)
		if err != nil {
			if !errors.IsNotFound(err) {
				return nil, errors.Annotatef(err, "cannot get account details for %q", controller)
			}
		} else {
			accounts[controller] = *acct
		}
	}
	return &cacheStore{
		accounts:        accounts,
		origStore:       store,
		controllers:     controllers,
		updatedAccounts: make(map[string]bool),
	}, nil
}