Esempio n. 1
0
// Run implements Command.Run
func (c *showControllerCommand) Run(ctx *cmd.Context) error {
	controllerNames := c.controllerNames
	if len(controllerNames) == 0 {
		currentController, err := modelcmd.ReadCurrentController()
		if err != nil {
			return errors.Trace(err)
		}
		if currentController == "" {
			return errors.New("there is no active controller")
		}
		controllerNames = []string{currentController}
	}
	controllers := make(map[string]ShowControllerDetails)
	for _, name := range controllerNames {
		actualName, err := modelcmd.ResolveControllerName(c.store, name)
		if err != nil {
			return err
		}
		one, err := c.store.ControllerByName(actualName)
		if err != nil {
			return err
		}
		controllers[name] = c.convertControllerForShow(actualName, one)
	}
	return c.out.Write(ctx, controllers)
}
Esempio n. 2
0
func (c *migrateCommand) getMigrationSpec() (*controller.ModelMigrationSpec, error) {
	store := c.ClientStore()

	modelInfo, err := store.ModelByName(c.ControllerName(), c.AccountName(), c.model)
	if err != nil {
		return nil, err
	}

	controllerName, err := modelcmd.ResolveControllerName(store, c.targetController)
	if err != nil {
		return nil, err
	}

	controllerInfo, err := store.ControllerByName(controllerName)
	if err != nil {
		return nil, err
	}

	accountName, err := store.CurrentAccount(controllerName)
	if err != nil {
		return nil, err
	}

	accountInfo, err := store.AccountByName(controllerName, accountName)
	if err != nil {
		return nil, err
	}

	return &controller.ModelMigrationSpec{
		ModelUUID:            modelInfo.ModelUUID,
		TargetControllerUUID: controllerInfo.ControllerUUID,
		TargetAddrs:          controllerInfo.APIEndpoints,
		TargetCACert:         controllerInfo.CACert,
		TargetUser:           accountInfo.User,
		TargetPassword:       accountInfo.Password,
	}, nil
}
Esempio n. 3
0
func (s *ControllerSuite) TestNotFound(c *gc.C) {
	_, err := modelcmd.ResolveControllerName(s.store, "foo")
	c.Assert(err, jc.Satisfies, errors.IsNotFound)
	// We should report on the passed in controller name.
	c.Assert(err, gc.ErrorMatches, ".* foo .*")
}
Esempio n. 4
0
func (s *ControllerSuite) TestOnlyLocalController(c *gc.C) {
	name, err := modelcmd.ResolveControllerName(s.store, "local.anothercontroller")
	c.Assert(name, gc.DeepEquals, "local.anothercontroller")
	c.Assert(err, jc.ErrorIsNil)
}
Esempio n. 5
0
func (s *ControllerSuite) TestLocalNameFallback(c *gc.C) {
	name, err := modelcmd.ResolveControllerName(s.store, "controller")
	c.Assert(name, gc.DeepEquals, "local.controller")
	c.Assert(err, jc.ErrorIsNil)
}
Esempio n. 6
0
func (c *switchCommand) Run(ctx *cmd.Context) (resultErr error) {

	// Get the current name for logging the transition or printing
	// the current controller/model.
	currentControllerName, err := c.ReadCurrentController()
	if err != nil {
		return errors.Trace(err)
	}
	if c.Target == "" {
		currentName, err := c.name(currentControllerName, true)
		if err != nil {
			return errors.Trace(err)
		}
		if currentName == "" {
			return errors.New("no currently specified model")
		}
		fmt.Fprintf(ctx.Stdout, "%s\n", currentName)
		return nil
	}
	currentName, err := c.name(currentControllerName, false)
	if err != nil {
		return errors.Trace(err)
	}

	var newName string
	defer func() {
		if resultErr != nil {
			return
		}
		logSwitch(ctx, currentName, &newName)
	}()

	// Switch is an alternative way of dealing with environments than using
	// the JUJU_MODEL environment setting, and as such, doesn't play too well.
	// If JUJU_MODEL is set we should report that as the current environment,
	// and not allow switching when it is set.
	if model := os.Getenv(osenv.JujuModelEnvKey); model != "" {
		return errors.Errorf("cannot switch when JUJU_MODEL is overriding the model (set to %q)", model)
	}

	// If the target identifies a controller, then set that as the current controller.
	var newControllerName string
	if newControllerName, err = modelcmd.ResolveControllerName(c.Store, c.Target); err == nil {
		if newControllerName == currentControllerName {
			newName = currentName
			return nil
		} else {
			newName, err = c.name(newControllerName, false)
			if err != nil {
				return errors.Trace(err)
			}
			return errors.Trace(c.WriteCurrentController(newControllerName))
		}
	} else if !errors.IsNotFound(err) {
		return errors.Trace(err)
	}

	// The target is not a controller, so check for a model with
	// the given name. The name can be qualified with the controller
	// name (<controller>:<model>), or unqualified; in the latter
	// case, the model must exist in the current controller.
	newControllerName, modelName := modelcmd.SplitModelName(c.Target)
	if newControllerName != "" {
		// A controller was specified so see if we should use a local version.
		newControllerName, err = modelcmd.ResolveControllerName(c.Store, newControllerName)
		if err != nil {
			return errors.Trace(err)
		}
		newName = modelcmd.JoinModelName(newControllerName, modelName)
	} else {
		if currentControllerName == "" {
			return unknownSwitchTargetError(c.Target)
		}
		newControllerName = currentControllerName
		newName = modelcmd.JoinModelName(newControllerName, modelName)
	}

	accountName, err := c.Store.CurrentAccount(newControllerName)
	if err != nil {
		return errors.Trace(err)
	}
	err = c.Store.SetCurrentModel(newControllerName, accountName, modelName)
	if errors.IsNotFound(err) {
		// The model isn't known locally, so we must query the controller.
		if err := c.RefreshModels(c.Store, newControllerName, accountName); err != nil {
			return errors.Annotate(err, "refreshing models cache")
		}
		err := c.Store.SetCurrentModel(newControllerName, accountName, modelName)
		if errors.IsNotFound(err) {
			return unknownSwitchTargetError(c.Target)
		} else if err != nil {
			return errors.Trace(err)
		}
	} else if err != nil {
		return errors.Trace(err)
	}
	if currentControllerName != newControllerName {
		if err := c.WriteCurrentController(newControllerName); err != nil {
			return errors.Trace(err)
		}
	}
	return nil
}