Exemple #1
0
// QualifiedModelName returns a Qualified model name, given either
// an unqualified or qualified model name. If the input is a
// fully qualified name, it is returned untouched; otherwise it is
// return qualified with the logged-in user name.
func (s QualifyingClientStore) QualifiedModelName(controllerName, modelName string) (string, error) {
	if !jujuclient.IsQualifiedModelName(modelName) {
		details, err := s.ClientStore.AccountDetails(controllerName)
		if err != nil {
			return "", errors.Annotate(err, "getting account details for qualifying model name")
		}
		owner := names.NewUserTag(details.User)
		modelName = jujuclient.JoinOwnerModelName(owner, modelName)
	} else {
		unqualifiedModelName, owner, err := jujuclient.SplitModelName(modelName)
		if err != nil {
			return "", errors.Trace(err)
		}
		owner = names.NewUserTag(owner.Id())
		modelName = jujuclient.JoinOwnerModelName(owner, unqualifiedModelName)
	}
	return modelName, nil
}
Exemple #2
0
// Run implements Command.Run
func (c *whoAmICommand) Run(ctx *cmd.Context) error {
	controllerName, err := c.store.CurrentController()
	if err != nil && !errors.IsNotFound(err) {
		return err
	}
	if err != nil {
		fmt.Fprintln(ctx.Stderr, "There is no current controller.\nRun juju list-controllers to see available controllers.")
		return nil
	}
	modelName, err := c.store.CurrentModel(controllerName)
	if err != nil && !errors.IsNotFound(err) {
		return err
	}
	userDetails, err := c.store.AccountDetails(controllerName)
	if err != nil && !errors.IsNotFound(err) {
		return err
	}
	if err != nil {
		fmt.Fprintf(ctx.Stderr, "You are not logged in to controller %q and model %q.\nRun juju login if you want to login.\n", controllerName, modelName)
		return nil
	}
	// Only qualify model name if there is a current model.
	if modelName != "" {
		if unqualifiedModelName, owner, err := jujuclient.SplitModelName(modelName); err == nil {
			user := names.NewUserTag(userDetails.User)
			modelName = common.OwnerQualifiedModelName(unqualifiedModelName, owner, user)
		}
	}

	result := whoAmI{
		ControllerName: controllerName,
		ModelName:      modelName,
		UserName:       userDetails.User,
	}
	return c.out.Write(ctx, result)
}
// convertControllerDetails takes a map of Controllers and
// the recently used model for each and creates a list of
// amalgamated controller and model details.
func (c *listControllersCommand) convertControllerDetails(storeControllers map[string]jujuclient.ControllerDetails) (map[string]ControllerItem, []string) {
	if len(storeControllers) == 0 {
		return nil, nil
	}

	errs := []string{}
	addError := func(msg, controllerName string, err error) {
		logger.Errorf(fmt.Sprintf("getting current %s for controller %s: %v", msg, controllerName, err))
		errs = append(errs, msg)
	}

	controllers := map[string]ControllerItem{}
	for controllerName, details := range storeControllers {
		serverName := ""
		// The most recently connected-to address
		// is the first in the list.
		if len(details.APIEndpoints) > 0 {
			serverName = details.APIEndpoints[0]
		}

		var userName, access string
		accountDetails, err := c.store.AccountDetails(controllerName)
		if err != nil {
			if !errors.IsNotFound(err) {
				addError("account details", controllerName, err)
				continue
			}
		} else {
			userName = accountDetails.User
			access = accountDetails.LastKnownAccess
		}

		var modelName string
		currentModel, err := c.store.CurrentModel(controllerName)
		if err != nil {
			if !errors.IsNotFound(err) {
				addError("model", controllerName, err)
				continue
			}
		} else {
			modelName = currentModel
			if userName != "" {
				// There's a user logged in, so display the
				// model name relative to that user.
				if unqualifiedModelName, owner, err := jujuclient.SplitModelName(modelName); err == nil {
					user := names.NewUserTag(userName)
					modelName = common.OwnerQualifiedModelName(unqualifiedModelName, owner, user)
				}
			}
		}

		item := ControllerItem{
			ModelName:      modelName,
			User:           userName,
			Access:         access,
			Server:         serverName,
			APIEndpoints:   details.APIEndpoints,
			ControllerUUID: details.ControllerUUID,
			CACert:         details.CACert,
			Cloud:          details.Cloud,
			CloudRegion:    details.CloudRegion,
			AgentVersion:   details.AgentVersion,
		}
		if details.MachineCount != nil && *details.MachineCount > 0 {
			item.MachineCount = details.MachineCount
		}
		if details.ModelCount != nil && *details.ModelCount > 0 {
			item.ModelCount = details.ModelCount
		}
		if details.ControllerMachineCount > 0 {
			item.ControllerMachines = &ControllerMachines{
				Total:  details.ControllerMachineCount,
				Active: details.ActiveControllerMachineCount,
			}
		}
		controllers[controllerName] = item
	}
	return controllers, errs
}
Exemple #4
0
// Run implements Command.Run
func (c *modelsCommand) Run(ctx *cmd.Context) error {
	accountDetails, err := c.ClientStore().AccountDetails(c.ControllerName())
	if err != nil {
		return err
	}
	c.loggedInUser = accountDetails.User

	// First get a list of the models.
	var models []base.UserModel
	if c.all {
		models, err = c.getAllModels()
	} else {
		if c.user == "" {
			c.user = accountDetails.User
		}
		models, err = c.getUserModels()
	}
	if err != nil {
		return errors.Annotate(err, "cannot list models")
	}

	// And now get the full details of the models.
	paramsModelInfo, err := c.getModelInfo(models)
	if err != nil {
		return errors.Annotate(err, "cannot get model details")
	}

	// TODO(perrito666) 2016-05-02 lp:1558657
	now := time.Now()
	modelInfo := make([]common.ModelInfo, 0, len(models))
	for _, info := range paramsModelInfo {
		model, err := common.ModelInfoFromParams(info, now)
		if err != nil {
			return errors.Trace(err)
		}
		model.ControllerName = c.ControllerName()
		modelInfo = append(modelInfo, model)
	}

	modelSet := ModelSet{Models: modelInfo}
	current, err := c.ClientStore().CurrentModel(c.ControllerName())
	if err != nil && !errors.IsNotFound(err) {
		return err
	}
	modelSet.CurrentModelQualified = current
	modelSet.CurrentModel = current
	if c.user != "" {
		userForListing := names.NewUserTag(c.user)
		unqualifiedModelName, owner, err := jujuclient.SplitModelName(current)
		if err == nil {
			modelSet.CurrentModel = common.OwnerQualifiedModelName(
				unqualifiedModelName, owner, userForListing,
			)
		}
	}

	if err := c.out.Write(ctx, modelSet); err != nil {
		return err
	}
	if len(models) == 0 && c.out.Name() == "tabular" {
		// When the output is tabular, we inform the user when there
		// are no models available, and tell them how to go about
		// creating or granting access to them.
		fmt.Fprintln(ctx.Stderr, noModelsMessage)
	}
	return nil
}