// UpdateAccount implements AccountUpdater. func (c *MemStore) UpdateAccount(controllerName, accountName string, details jujuclient.AccountDetails) error { if err := jujuclient.ValidateControllerName(controllerName); err != nil { return err } if err := jujuclient.ValidateAccountName(accountName); err != nil { return err } if err := jujuclient.ValidateAccountDetails(details); err != nil { return err } accounts, ok := c.Accounts[controllerName] if !ok { accounts = &jujuclient.ControllerAccounts{ Accounts: make(map[string]jujuclient.AccountDetails), } c.Accounts[controllerName] = accounts } if len(accounts.Accounts) > 0 { if _, ok := accounts.Accounts[accountName]; !ok { return errors.AlreadyExistsf( "alternative account for controller %s", controllerName, ) } } accounts.Accounts[accountName] = details return nil }
// UpdateModel implements ModelUpdater. func (c *MemStore) UpdateModel(controller, account, model string, details jujuclient.ModelDetails) error { if err := jujuclient.ValidateControllerName(controller); err != nil { return err } if err := jujuclient.ValidateAccountName(account); err != nil { return err } if err := jujuclient.ValidateModelName(model); err != nil { return err } if err := jujuclient.ValidateModelDetails(details); err != nil { return err } controllerAccountModels, ok := c.Models[controller] if !ok { controllerAccountModels.AccountModels = make(map[string]*jujuclient.AccountModels) c.Models[controller] = controllerAccountModels } accountModels, ok := controllerAccountModels.AccountModels[account] if !ok { accountModels = &jujuclient.AccountModels{ Models: make(map[string]jujuclient.ModelDetails), } controllerAccountModels.AccountModels[account] = accountModels } accountModels.Models[model] = details return nil }
// RemoveModel implements ModelRemover. func (c *MemStore) RemoveModel(controller, account, model string) error { if err := jujuclient.ValidateControllerName(controller); err != nil { return err } if err := jujuclient.ValidateAccountName(account); err != nil { return err } if err := jujuclient.ValidateModelName(model); err != nil { return err } controllerAccountModels, ok := c.Models[controller] if !ok { return errors.NotFoundf("models for controller %s", controller) } accountModels, ok := controllerAccountModels.AccountModels[account] if !ok { return errors.NotFoundf( "models for account %s on controller %s", account, controller, ) } if _, ok := accountModels.Models[model]; !ok { return errors.NotFoundf("model %s:%s:%s", controller, account, model) } delete(accountModels.Models, model) if accountModels.CurrentModel == model { accountModels.CurrentModel = "" } return nil }
// SetCurrentModel implements ModelUpdater. func (c *MemStore) SetCurrentModel(controllerName, accountName, modelName string) error { if err := jujuclient.ValidateControllerName(controllerName); err != nil { return errors.Trace(err) } if err := jujuclient.ValidateAccountName(accountName); err != nil { return err } if err := jujuclient.ValidateModelName(modelName); err != nil { return errors.Trace(err) } controllerAccountModels, ok := c.Models[controllerName] if !ok { return errors.NotFoundf("models for controller %s", controllerName) } accountModels, ok := controllerAccountModels.AccountModels[accountName] if !ok { return errors.NotFoundf( "models for account %s on controller %s", accountName, controllerName, ) } if _, ok := accountModels.Models[modelName]; !ok { return errors.NotFoundf("model %s:%s:%s", controllerName, accountName, modelName) } accountModels.CurrentModel = modelName return nil }
// AccountByName implements AccountGetter. func (c *MemStore) AccountByName(controllerName, accountName string) (*jujuclient.AccountDetails, error) { if err := jujuclient.ValidateControllerName(controllerName); err != nil { return nil, err } if err := jujuclient.ValidateAccountName(accountName); err != nil { return nil, err } accounts, ok := c.Accounts[controllerName] if !ok { return nil, errors.NotFoundf("accounts for controller %s", controllerName) } details, ok := accounts.Accounts[accountName] if !ok { return nil, errors.NotFoundf("account %s:%s", controllerName, accountName) } return &details, nil }
// SetCurrentAccount implements AccountUpdater. func (c *MemStore) SetCurrentAccount(controllerName, accountName string) error { if err := jujuclient.ValidateControllerName(controllerName); err != nil { return err } if err := jujuclient.ValidateAccountName(accountName); err != nil { return err } accounts, ok := c.Accounts[controllerName] if !ok { return errors.NotFoundf("accounts for controller %s", controllerName) } if _, ok := accounts.Accounts[accountName]; !ok { return errors.NotFoundf("account %s:%s", controllerName, accountName) } accounts.CurrentAccount = accountName return nil }
// AllModels implements ModelGetter. func (c *MemStore) AllModels(controller, account string) (map[string]jujuclient.ModelDetails, error) { if err := jujuclient.ValidateControllerName(controller); err != nil { return nil, err } if err := jujuclient.ValidateAccountName(account); err != nil { return nil, err } controllerAccountModels, ok := c.Models[controller] if !ok { return nil, errors.NotFoundf("models for controller %s", controller) } accountModels, ok := controllerAccountModels.AccountModels[account] if !ok { return nil, errors.NotFoundf("models for account %s on controller %s", account, controller) } return accountModels.Models, nil }
// CurrentModel implements ModelGetter. func (c *MemStore) CurrentModel(controller, account string) (string, error) { if err := jujuclient.ValidateControllerName(controller); err != nil { return "", err } if err := jujuclient.ValidateAccountName(account); err != nil { return "", err } controllerAccountModels, ok := c.Models[controller] if !ok { return "", errors.NotFoundf("models for controller %s", controller) } accountModels, ok := controllerAccountModels.AccountModels[account] if !ok { return "", errors.NotFoundf("models for account %s on controller %s", account, controller) } if accountModels.CurrentModel == "" { return "", errors.NotFoundf("current model for account %s on controller %s", account, controller) } return accountModels.CurrentModel, nil }
// ModelByName implements ModelGetter. func (c *MemStore) ModelByName(controller, account, model string) (*jujuclient.ModelDetails, error) { if err := jujuclient.ValidateControllerName(controller); err != nil { return nil, err } if err := jujuclient.ValidateAccountName(account); err != nil { return nil, err } if err := jujuclient.ValidateModelName(model); err != nil { return nil, err } controllerAccountModels, ok := c.Models[controller] if !ok { return nil, errors.NotFoundf("models for controller %s", controller) } accountModels, ok := controllerAccountModels.AccountModels[account] if !ok { return nil, errors.NotFoundf("models for account %s on controller %s", account, controller) } details, ok := accountModels.Models[model] if !ok { return nil, errors.NotFoundf("model %s:%s:%s", controller, account, model) } return &details, nil }