// decorateAndWriteInfo decorates the info struct with information // from the given cfg, and the writes that out to the filesystem. func decorateAndWriteInfo( store jujuclient.ClientStore, details prepareDetails, controllerName, modelName string, ) error { accountName := details.User if err := store.UpdateController(controllerName, details.ControllerDetails); err != nil { return errors.Trace(err) } if err := store.UpdateBootstrapConfig(controllerName, details.BootstrapConfig); err != nil { return errors.Trace(err) } if err := store.UpdateAccount(controllerName, accountName, details.AccountDetails); err != nil { return errors.Trace(err) } if err := store.SetCurrentAccount(controllerName, accountName); err != nil { return errors.Trace(err) } if err := store.UpdateModel(controllerName, accountName, modelName, details.ModelDetails); err != nil { return errors.Trace(err) } if err := store.SetCurrentModel(controllerName, accountName, modelName); err != nil { return errors.Trace(err) } return nil }
// decorateAndWriteInfo decorates the info struct with information // from the given cfg, and the writes that out to the filesystem. func decorateAndWriteInfo( store jujuclient.ClientStore, details prepareDetails, controllerName, modelName string, ) error { qualifiedModelName := jujuclient.JoinOwnerModelName( names.NewUserTag(details.AccountDetails.User), modelName, ) if err := store.AddController(controllerName, details.ControllerDetails); err != nil { return errors.Trace(err) } if err := store.UpdateBootstrapConfig(controllerName, details.BootstrapConfig); err != nil { return errors.Trace(err) } if err := store.UpdateAccount(controllerName, details.AccountDetails); err != nil { return errors.Trace(err) } if err := store.UpdateModel(controllerName, qualifiedModelName, details.ModelDetails); err != nil { return errors.Trace(err) } if err := store.SetCurrentModel(controllerName, qualifiedModelName); err != nil { return errors.Trace(err) } return nil }
func (c *registerCommand) maybeSetCurrentModel(ctx *cmd.Context, store jujuclient.ClientStore, controllerName, userName string, models []base.UserModel) error { if len(models) == 0 { fmt.Fprintf(ctx.Stderr, "\n%s\n\n", errNoModels.Error()) return nil } // If we get to here, there is at least one model. if len(models) == 1 { // There is exactly one model shared, // so set it as the current model. model := models[0] owner := names.NewUserTag(model.Owner) modelName := jujuclient.JoinOwnerModelName(owner, model.Name) err := store.SetCurrentModel(controllerName, modelName) if err != nil { return errors.Trace(err) } fmt.Fprintf(ctx.Stderr, "\nCurrent model set to %q.\n\n", modelName) } else { fmt.Fprintf(ctx.Stderr, ` There are %d models available. Use "juju switch" to select one of them: `, len(models)) user := names.NewUserTag(userName) ownerModelNames := make(set.Strings) otherModelNames := make(set.Strings) for _, model := range models { if model.Owner == userName { ownerModelNames.Add(model.Name) continue } owner := names.NewUserTag(model.Owner) modelName := common.OwnerQualifiedModelName(model.Name, owner, user) otherModelNames.Add(modelName) } for _, modelName := range ownerModelNames.SortedValues() { fmt.Fprintf(ctx.Stderr, " - juju switch %s\n", modelName) } for _, modelName := range otherModelNames.SortedValues() { fmt.Fprintf(ctx.Stderr, " - juju switch %s\n", modelName) } fmt.Fprintln(ctx.Stderr) } return nil }
func (c *JujuCommandBase) missingModelError(store jujuclient.ClientStore, controllerName, modelName string) error { // First, we'll try and clean up the missing model from the local cache. err := store.RemoveModel(controllerName, modelName) if err != nil { logger.Warningf("cannot remove unknown model from cache: %v", err) } currentModel, err := store.CurrentModel(controllerName) if err != nil { logger.Warningf("cannot read current model: %v", err) } else if currentModel == modelName { if err := store.SetCurrentModel(controllerName, ""); err != nil { logger.Warningf("cannot reset current model: %v", err) } } errorMessage := "model %q has been removed from the controller, run 'juju models' and switch to one of them." modelInfoMessage := "\nThere are %d accessible models on controller %q." models, err := store.AllModels(controllerName) if err == nil { modelInfoMessage = fmt.Sprintf(modelInfoMessage, len(models), controllerName) } else { modelInfoMessage = "" } return errors.Errorf(errorMessage+modelInfoMessage, modelName) }