本文整理汇总了Golang中github.com/juju/errors.IsAlreadyExists函数的典型用法代码示例。如果您正苦于以下问题:Golang IsAlreadyExists函数的具体用法?Golang IsAlreadyExists怎么用?Golang IsAlreadyExists使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了IsAlreadyExists函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: SetInstanceInfo
// SetInstanceInfo is used to provision a machine and in one steps set
// it's instance id, nonce, hardware characteristics, add networks and
// network interfaces as needed.
//
// TODO(dimitern) Do all the operations described in a single
// transaction, rather than using separate calls. Alternatively,
// we can add all the things to create/set in a document in some
// collection and have a worker that takes care of the actual work.
// Merge SetProvisioned() in here or drop it at that point.
func (m *Machine) SetInstanceInfo(
id instance.Id, nonce string, characteristics *instance.HardwareCharacteristics,
networks []NetworkInfo, interfaces []NetworkInterfaceInfo,
volumes map[names.VolumeTag]VolumeInfo,
volumeAttachments map[names.VolumeTag]VolumeAttachmentInfo,
) error {
// Add the networks and interfaces first.
for _, network := range networks {
_, err := m.st.AddNetwork(network)
if err != nil && errors.IsAlreadyExists(err) {
// Ignore already existing networks.
continue
} else if err != nil {
return errors.Trace(err)
}
}
for _, iface := range interfaces {
_, err := m.AddNetworkInterface(iface)
if err != nil && errors.IsAlreadyExists(err) {
// Ignore already existing network interfaces.
continue
} else if err != nil {
return errors.Trace(err)
}
}
if err := setProvisionedVolumeInfo(m.st, volumes); err != nil {
return errors.Trace(err)
}
if err := setMachineVolumeAttachmentInfo(m.st, m.Id(), volumeAttachments); err != nil {
return errors.Trace(err)
}
return m.SetProvisioned(id, nonce, characteristics)
}
开发者ID:vonwenm,项目名称:juju,代码行数:43,代码来源:machine.go
示例2: SetInstanceInfo
// SetInstanceInfo is used to provision a machine and in one steps set
// it's instance id, nonce, hardware characteristics, add networks and
// network interfaces as needed.
//
// TODO(dimitern) Do all the operations described in a single
// transaction, rather than using separate calls. Alternatively,
// we can add all the things to create/set in a document in some
// collection and have a worker that takes care of the actual work.
// Merge SetProvisioned() in here or drop it at that point.
func (m *Machine) SetInstanceInfo(
id instance.Id, nonce string, characteristics *instance.HardwareCharacteristics,
networks []NetworkInfo, interfaces []NetworkInterfaceInfo) error {
// Add the networks and interfaces first.
for _, network := range networks {
_, err := m.st.AddNetwork(network)
if err != nil && errors.IsAlreadyExists(err) {
// Ignore already existing networks.
continue
} else if err != nil {
return err
}
}
for _, iface := range interfaces {
_, err := m.AddNetworkInterface(iface)
if err != nil && errors.IsAlreadyExists(err) {
// Ignore already existing network interfaces.
continue
} else if err != nil {
return err
}
}
return m.SetProvisioned(id, nonce, characteristics)
}
开发者ID:rogpeppe,项目名称:juju,代码行数:34,代码来源:machine.go
示例3: TestNewEnvironmentSameUserSameNameFails
func (s *EnvironSuite) TestNewEnvironmentSameUserSameNameFails(c *gc.C) {
cfg, _ := s.createTestEnvConfig(c)
owner := s.factory.MakeUser(c, nil).UserTag()
// Create the first environment.
_, st1, err := s.State.NewEnvironment(cfg, owner)
c.Assert(err, jc.ErrorIsNil)
defer st1.Close()
// Attempt to create another environment with a different UUID but the
// same owner and name as the first.
newUUID, err := utils.NewUUID()
c.Assert(err, jc.ErrorIsNil)
cfg2 := testing.CustomEnvironConfig(c, testing.Attrs{
"name": cfg.Name(),
"uuid": newUUID.String(),
})
_, _, err = s.State.NewEnvironment(cfg2, owner)
errMsg := fmt.Sprintf("environment %q for %s already exists", cfg2.Name(), owner.Username())
c.Assert(err, gc.ErrorMatches, errMsg)
c.Assert(errors.IsAlreadyExists(err), jc.IsTrue)
// Remove the first environment.
err = st1.RemoveAllEnvironDocs()
c.Assert(err, jc.ErrorIsNil)
// We should now be able to create the other environment.
env2, st2, err := s.State.NewEnvironment(cfg2, owner)
c.Assert(err, jc.ErrorIsNil)
defer st2.Close()
c.Assert(env2, gc.NotNil)
c.Assert(st2, gc.NotNil)
}
开发者ID:Pankov404,项目名称:juju,代码行数:33,代码来源:environ_test.go
示例4: ServerError
// ServerError returns an error suitable for returning to an API
// client, with an error code suitable for various kinds of errors
// generated in packages outside the API.
func ServerError(err error) *params.Error {
if err == nil {
return nil
}
logger.Tracef("server RPC error %v", errors.Details(err))
msg := err.Error()
// Skip past annotations when looking for the code.
err = errors.Cause(err)
code, ok := singletonCode(err)
var info *params.ErrorInfo
switch {
case ok:
case errors.IsUnauthorized(err):
code = params.CodeUnauthorized
case errors.IsNotFound(err):
code = params.CodeNotFound
case errors.IsUserNotFound(err):
code = params.CodeUserNotFound
case errors.IsAlreadyExists(err):
code = params.CodeAlreadyExists
case errors.IsNotAssigned(err):
code = params.CodeNotAssigned
case state.IsHasAssignedUnitsError(err):
code = params.CodeHasAssignedUnits
case state.IsHasHostedModelsError(err):
code = params.CodeHasHostedModels
case isNoAddressSetError(err):
code = params.CodeNoAddressSet
case errors.IsNotProvisioned(err):
code = params.CodeNotProvisioned
case IsUpgradeInProgressError(err):
code = params.CodeUpgradeInProgress
case state.IsHasAttachmentsError(err):
code = params.CodeMachineHasAttachedStorage
case isUnknownModelError(err):
code = params.CodeModelNotFound
case errors.IsNotSupported(err):
code = params.CodeNotSupported
case errors.IsBadRequest(err):
code = params.CodeBadRequest
case errors.IsMethodNotAllowed(err):
code = params.CodeMethodNotAllowed
default:
if err, ok := err.(*DischargeRequiredError); ok {
code = params.CodeDischargeRequired
info = ¶ms.ErrorInfo{
Macaroon: err.Macaroon,
// One macaroon fits all.
MacaroonPath: "/",
}
break
}
code = params.ErrCode(err)
}
return ¶ms.Error{
Message: msg,
Code: code,
Info: info,
}
}
开发者ID:bac,项目名称:juju,代码行数:63,代码来源:errors.go
示例5: grantControllerAccess
func grantControllerAccess(accessor *state.State, targetUserTag, apiUser names.UserTag, access permission.Access) error {
_, err := accessor.AddControllerUser(state.UserAccessSpec{User: targetUserTag, CreatedBy: apiUser, Access: access})
if errors.IsAlreadyExists(err) {
controllerTag := accessor.ControllerTag()
controllerUser, err := accessor.UserAccess(targetUserTag, controllerTag)
if errors.IsNotFound(err) {
// Conflicts with prior check, must be inconsistent state.
err = txn.ErrExcessiveContention
}
if err != nil {
return errors.Annotate(err, "could not look up controller access for user")
}
// Only set access if greater access is being granted.
if controllerUser.Access.EqualOrGreaterControllerAccessThan(access) {
return errors.Errorf("user already has %q access or greater", access)
}
if _, err = accessor.SetUserAccess(controllerUser.UserTag, controllerUser.Object, access); err != nil {
return errors.Annotate(err, "could not set controller access for user")
}
return nil
}
if err != nil {
return errors.Trace(err)
}
return nil
}
开发者ID:bac,项目名称:juju,代码行数:28,代码来源:controller.go
示例6: TestNewModelSameUserSameNameFails
func (s *ModelSuite) TestNewModelSameUserSameNameFails(c *gc.C) {
cfg, _ := s.createTestModelConfig(c)
owner := s.Factory.MakeUser(c, nil).UserTag()
// Create the first model.
_, st1, err := s.State.NewModel(state.ModelArgs{
CloudName: "dummy",
CloudRegion: "dummy-region",
Config: cfg,
Owner: owner,
StorageProviderRegistry: storage.StaticProviderRegistry{},
})
c.Assert(err, jc.ErrorIsNil)
defer st1.Close()
// Attempt to create another model with a different UUID but the
// same owner and name as the first.
newUUID, err := utils.NewUUID()
c.Assert(err, jc.ErrorIsNil)
cfg2 := testing.CustomModelConfig(c, testing.Attrs{
"name": cfg.Name(),
"uuid": newUUID.String(),
})
_, _, err = s.State.NewModel(state.ModelArgs{
CloudName: "dummy",
CloudRegion: "dummy-region",
Config: cfg2,
Owner: owner,
StorageProviderRegistry: storage.StaticProviderRegistry{},
})
errMsg := fmt.Sprintf("model %q for %s already exists", cfg2.Name(), owner.Canonical())
c.Assert(err, gc.ErrorMatches, errMsg)
c.Assert(errors.IsAlreadyExists(err), jc.IsTrue)
// Remove the first model.
env1, err := st1.Model()
c.Assert(err, jc.ErrorIsNil)
err = env1.Destroy()
c.Assert(err, jc.ErrorIsNil)
// Destroy only sets the model to dying and RemoveAllModelDocs can
// only be called on a dead model. Normally, the environ's lifecycle
// would be set to dead after machines and services have been cleaned up.
err = state.SetModelLifeDead(st1, env1.ModelTag().Id())
c.Assert(err, jc.ErrorIsNil)
err = st1.RemoveAllModelDocs()
c.Assert(err, jc.ErrorIsNil)
// We should now be able to create the other model.
env2, st2, err := s.State.NewModel(state.ModelArgs{
CloudName: "dummy",
CloudRegion: "dummy-region",
Config: cfg2,
Owner: owner,
StorageProviderRegistry: storage.StaticProviderRegistry{},
})
c.Assert(err, jc.ErrorIsNil)
defer st2.Close()
c.Assert(env2, gc.NotNil)
c.Assert(st2, gc.NotNil)
}
开发者ID:kat-co,项目名称:juju,代码行数:60,代码来源:model_test.go
示例7: ServerError
// ServerError returns an error suitable for returning to an API
// client, with an error code suitable for various kinds of errors
// generated in packages outside the API.
func ServerError(err error) *params.Error {
if err == nil {
return nil
}
code, ok := singletonCode(err)
switch {
case ok:
case errors.IsUnauthorized(err):
code = params.CodeUnauthorized
case errors.IsNotFound(err):
code = params.CodeNotFound
case errors.IsAlreadyExists(err):
code = params.CodeAlreadyExists
case state.IsNotAssigned(err):
code = params.CodeNotAssigned
case state.IsHasAssignedUnitsError(err):
code = params.CodeHasAssignedUnits
case IsNoAddressSetError(err):
code = params.CodeNoAddressSet
case state.IsNotProvisionedError(err):
code = params.CodeNotProvisioned
case IsUnknownEnviromentError(err):
code = params.CodeNotFound
default:
code = params.ErrCode(err)
}
return ¶ms.Error{
Message: err.Error(),
Code: code,
}
}
开发者ID:klyachin,项目名称:juju,代码行数:34,代码来源:errors.go
示例8: TestCaseSensitiveUsersErrors
func (s *UserSuite) TestCaseSensitiveUsersErrors(c *gc.C) {
s.Factory.MakeUser(c, &factory.UserParams{Name: "Bob"})
_, err := s.State.AddUser(
"boB", "ignored", "ignored", "ignored")
c.Assert(errors.IsAlreadyExists(err), jc.IsTrue)
c.Assert(err, gc.ErrorMatches, "user already exists")
}
开发者ID:kat-co,项目名称:juju,代码行数:8,代码来源:user_test.go
示例9: TestCaseSensitiveEnvUserErrors
func (s *EnvUserSuite) TestCaseSensitiveEnvUserErrors(c *gc.C) {
env, err := s.State.Environment()
c.Assert(err, jc.ErrorIsNil)
s.Factory.MakeEnvUser(c, &factory.EnvUserParams{User: "[email protected]"})
_, err = s.State.AddEnvironmentUser(names.NewUserTag("[email protected]"), env.Owner(), "")
c.Assert(err, gc.ErrorMatches, `environment user "[email protected]" already exists`)
c.Assert(errors.IsAlreadyExists(err), jc.IsTrue)
}
开发者ID:snailwalker,项目名称:juju,代码行数:9,代码来源:envuser_test.go
示例10: PickNewAddress
// PickNewAddress returns a new IPAddress that isn't in use for the subnet.
// The address starts with AddressStateUnknown, for later allocation.
// This will fail if the subnet is not alive.
func (s *Subnet) PickNewAddress() (*IPAddress, error) {
for {
addr, err := s.attemptToPickNewAddress()
if err == nil {
return addr, err
}
if !errors.IsAlreadyExists(err) {
return addr, err
}
}
}
开发者ID:OSBI,项目名称:juju,代码行数:14,代码来源:subnets.go
示例11: TestCaseSensitiveModelUserErrors
func (s *ModelUserSuite) TestCaseSensitiveModelUserErrors(c *gc.C) {
model, err := s.State.Model()
c.Assert(err, jc.ErrorIsNil)
s.Factory.MakeModelUser(c, &factory.ModelUserParams{User: "[email protected]"})
_, err = s.State.AddModelUser(state.ModelUserSpec{
User: names.NewUserTag("[email protected]"),
CreatedBy: model.Owner()})
c.Assert(err, gc.ErrorMatches, `model user "[email protected]" already exists`)
c.Assert(errors.IsAlreadyExists(err), jc.IsTrue)
}
开发者ID:exekias,项目名称:juju,代码行数:11,代码来源:modeluser_test.go
示例12: Start
// Start implements Service.
func (s *Service) Start() error {
err := s.start()
if errors.IsAlreadyExists(err) {
logger.Debugf("service %q already running", s.Name())
return nil
} else if err != nil {
logger.Errorf("service %q failed to start: %v", s.Name(), err)
return err
}
logger.Debugf("service %q successfully started", s.Name())
return nil
}
开发者ID:bac,项目名称:juju,代码行数:13,代码来源:service.go
示例13: Install
// Install implements Service.
func (s *Service) Install() error {
if s.NoConf() {
return s.errorf(nil, "missing conf")
}
err := s.install()
if errors.IsAlreadyExists(err) {
logger.Debugf("service %q already installed", s.Name())
return nil
} else if err != nil {
logger.Errorf("failed to install service %q: %v", s.Name(), err)
return err
}
logger.Debugf("service %q successfully installed", s.Name())
return nil
}
开发者ID:bac,项目名称:juju,代码行数:17,代码来源:service.go
示例14: createOrFetchStateSubnet
func (p *ProvisionerAPI) createOrFetchStateSubnet(subnetInfo network.SubnetInfo) (*state.Subnet, error) {
stateSubnetInfo := state.SubnetInfo{
ProviderId: subnetInfo.ProviderId,
CIDR: subnetInfo.CIDR,
VLANTag: subnetInfo.VLANTag,
AllocatableIPHigh: subnetInfo.AllocatableIPHigh.String(),
AllocatableIPLow: subnetInfo.AllocatableIPLow.String(),
}
subnet, err := p.st.AddSubnet(stateSubnetInfo)
if err != nil {
if errors.IsAlreadyExists(err) {
subnet, err = p.st.Subnet(subnetInfo.CIDR)
}
if err != nil {
return subnet, errors.Trace(err)
}
}
return subnet, nil
}
开发者ID:AlexisBruemmer,项目名称:juju,代码行数:19,代码来源:provisioner.go
示例15: TestNewEnvironmentSameUserSameNameFails
func (s *EnvironSuite) TestNewEnvironmentSameUserSameNameFails(c *gc.C) {
cfg, _ := s.createTestEnvConfig(c)
owner := s.Factory.MakeUser(c, nil).UserTag()
// Create the first environment.
_, st1, err := s.State.NewEnvironment(cfg, owner)
c.Assert(err, jc.ErrorIsNil)
defer st1.Close()
// Attempt to create another environment with a different UUID but the
// same owner and name as the first.
newUUID, err := utils.NewUUID()
c.Assert(err, jc.ErrorIsNil)
cfg2 := testing.CustomEnvironConfig(c, testing.Attrs{
"name": cfg.Name(),
"uuid": newUUID.String(),
})
_, _, err = s.State.NewEnvironment(cfg2, owner)
errMsg := fmt.Sprintf("environment %q for %s already exists", cfg2.Name(), owner.Canonical())
c.Assert(err, gc.ErrorMatches, errMsg)
c.Assert(errors.IsAlreadyExists(err), jc.IsTrue)
// Remove the first environment.
env1, err := st1.Environment()
c.Assert(err, jc.ErrorIsNil)
err = env1.Destroy()
c.Assert(err, jc.ErrorIsNil)
// Destroy only sets the environment to dying and RemoveAllEnvironDocs can
// only be called on a dead environment. Normally, the environ's lifecycle
// would be set to dead after machines and services have been cleaned up.
err = state.SetEnvLifeDead(st1, env1.EnvironTag().Id())
c.Assert(err, jc.ErrorIsNil)
err = st1.RemoveAllEnvironDocs()
c.Assert(err, jc.ErrorIsNil)
// We should now be able to create the other environment.
env2, st2, err := s.State.NewEnvironment(cfg2, owner)
c.Assert(err, jc.ErrorIsNil)
defer st2.Close()
c.Assert(env2, gc.NotNil)
c.Assert(st2, gc.NotNil)
}
开发者ID:imoapps,项目名称:juju,代码行数:42,代码来源:environ_test.go
示例16: ServerError
// ServerError returns an error suitable for returning to an API
// client, with an error code suitable for various kinds of errors
// generated in packages outside the API.
func ServerError(err error) *params.Error {
if err == nil {
return nil
}
msg := err.Error()
// Skip past annotations when looking for the code.
err = errors.Cause(err)
code, ok := singletonCode(err)
switch {
case ok:
case errors.IsUnauthorized(err):
code = params.CodeUnauthorized
case errors.IsNotFound(err):
code = params.CodeNotFound
case errors.IsAlreadyExists(err):
code = params.CodeAlreadyExists
case errors.IsNotAssigned(err):
code = params.CodeNotAssigned
case state.IsHasAssignedUnitsError(err):
code = params.CodeHasAssignedUnits
case IsNoAddressSetError(err):
code = params.CodeNoAddressSet
case errors.IsNotProvisioned(err):
code = params.CodeNotProvisioned
case state.IsUpgradeInProgressError(err):
code = params.CodeUpgradeInProgress
case state.IsHasAttachmentsError(err):
code = params.CodeMachineHasAttachedStorage
case IsUnknownEnviromentError(err):
code = params.CodeNotFound
case errors.IsNotSupported(err):
code = params.CodeNotSupported
default:
code = params.ErrCode(err)
}
return ¶ms.Error{
Message: msg,
Code: code,
}
}
开发者ID:ktsakalozos,项目名称:juju,代码行数:43,代码来源:errors.go
示例17: Run
//.........这里部分代码省略.........
hostedModelUUID, err := utils.NewUUID()
if err != nil {
return errors.Trace(err)
}
controllerUUID, err := utils.NewUUID()
if err != nil {
return errors.Trace(err)
}
// Create an environment config from the cloud and credentials.
configAttrs := map[string]interface{}{
"type": cloud.Type,
"name": environs.ControllerModelName,
config.UUIDKey: controllerUUID.String(),
config.ControllerUUIDKey: controllerUUID.String(),
}
userConfigAttrs, err := c.config.ReadAttrs(ctx)
if err != nil {
return errors.Trace(err)
}
for k, v := range userConfigAttrs {
configAttrs[k] = v
}
logger.Debugf("preparing controller with config: %v", configAttrs)
// Read existing current controller, account, model so we can clean up on error.
var oldCurrentController string
oldCurrentController, err = modelcmd.ReadCurrentController()
if err != nil {
return errors.Annotate(err, "error reading current controller")
}
defer func() {
if resultErr == nil || errors.IsAlreadyExists(resultErr) {
return
}
if oldCurrentController != "" {
if err := modelcmd.WriteCurrentController(oldCurrentController); err != nil {
logger.Warningf(
"cannot reset current controller to %q: %v",
oldCurrentController, err,
)
}
}
if err := store.RemoveController(c.controllerName); err != nil {
logger.Warningf(
"cannot destroy newly created controller %q details: %v",
c.controllerName, err,
)
}
}()
environ, err := environsPrepare(
modelcmd.BootstrapContext(ctx), store,
environs.PrepareParams{
BaseConfig: configAttrs,
ControllerName: c.controllerName,
CloudName: c.Cloud,
CloudRegion: region.Name,
CloudEndpoint: region.Endpoint,
CloudStorageEndpoint: region.StorageEndpoint,
Credential: *credential,
CredentialName: credentialName,
},
)
if err != nil {
开发者ID:makyo,项目名称:juju,代码行数:67,代码来源:bootstrap.go
示例18: changeModelAccess
// changeModelAccess performs the requested access grant or revoke action for the
// specified user on the specified model.
func changeModelAccess(accessor common.ModelManagerBackend, modelTag names.ModelTag, apiUser, targetUserTag names.UserTag, action params.ModelAction, access permission.Access, userIsAdmin bool) error {
st, err := accessor.ForModel(modelTag)
if err != nil {
return errors.Annotate(err, "could not lookup model")
}
defer st.Close()
if err := userAuthorizedToChangeAccess(st, userIsAdmin, apiUser); err != nil {
return errors.Trace(err)
}
switch action {
case params.GrantModelAccess:
_, err = st.AddModelUser(modelTag.Id(), state.UserAccessSpec{User: targetUserTag, CreatedBy: apiUser, Access: access})
if errors.IsAlreadyExists(err) {
modelUser, err := st.UserAccess(targetUserTag, modelTag)
if errors.IsNotFound(err) {
// Conflicts with prior check, must be inconsistent state.
err = txn.ErrExcessiveContention
}
if err != nil {
return errors.Annotate(err, "could not look up model access for user")
}
// Only set access if greater access is being granted.
if modelUser.Access.EqualOrGreaterModelAccessThan(access) {
return errors.Errorf("user already has %q access or greater", access)
}
if _, err = st.SetUserAccess(modelUser.UserTag, modelUser.Object, access); err != nil {
return errors.Annotate(err, "could not set model access for user")
}
return nil
}
return errors.Annotate(err, "could not grant model access")
case params.RevokeModelAccess:
switch access {
case permission.ReadAccess:
// Revoking read access removes all access.
err := st.RemoveUserAccess(targetUserTag, modelTag)
return errors.Annotate(err, "could not revoke model access")
case permission.WriteAccess:
// Revoking write access sets read-only.
modelUser, err := st.UserAccess(targetUserTag, modelTag)
if err != nil {
return errors.Annotate(err, "could not look up model access for user")
}
_, err = st.SetUserAccess(modelUser.UserTag, modelUser.Object, permission.ReadAccess)
return errors.Annotate(err, "could not set model access to read-only")
case permission.AdminAccess:
// Revoking admin access sets read-write.
modelUser, err := st.UserAccess(targetUserTag, modelTag)
if err != nil {
return errors.Annotate(err, "could not look up model access for user")
}
_, err = st.SetUserAccess(modelUser.UserTag, modelUser.Object, permission.WriteAccess)
return errors.Annotate(err, "could not set model access to read-write")
default:
return errors.Errorf("don't know how to revoke %q access", access)
}
default:
return errors.Errorf("unknown action %q", action)
}
}
开发者ID:bac,项目名称:juju,代码行数:68,代码来源:modelmanager.go
示例19: ChangeModelAccess
// ChangeModelAccess performs the requested access grant or revoke action for the
// specified user on the specified model.
func ChangeModelAccess(accessor Backend, modelTag names.ModelTag, apiUser, targetUserTag names.UserTag, action params.ModelAction, access permission.ModelAccess, userIsAdmin bool) error {
st, err := accessor.ForModel(modelTag)
if err != nil {
return errors.Annotate(err, "could not lookup model")
}
defer st.Close()
if err := userAuthorizedToChangeAccess(st, userIsAdmin, apiUser); err != nil {
return errors.Trace(err)
}
stateAccess, err := resolveStateAccess(access)
if err != nil {
return errors.Annotate(err, "could not resolve model access")
}
switch action {
case params.GrantModelAccess:
_, err = st.AddModelUser(state.ModelUserSpec{User: targetUserTag, CreatedBy: apiUser, Access: stateAccess})
if errors.IsAlreadyExists(err) {
modelUser, err := st.ModelUser(targetUserTag)
if errors.IsNotFound(err) {
// Conflicts with prior check, must be inconsistent state.
err = txn.ErrExcessiveContention
}
if err != nil {
return errors.Annotate(err, "could not look up model access for user")
}
// Only set access if greater access is being granted.
if isGreaterAccess(modelUser.Access(), stateAccess) {
err = modelUser.SetAccess(stateAccess)
if err != nil {
return errors.Annotate(err, "could not set model access for user")
}
} else {
return errors.Errorf("user already has %q access", modelUser.Access())
}
return nil
}
return errors.Annotate(err, "could not grant model access")
case params.RevokeModelAccess:
if stateAccess == state.ModelReadAccess {
// Revoking read access removes all access.
err := st.RemoveModelUser(targetUserTag)
return errors.Annotate(err, "could not revoke model access")
} else if stateAccess == state.ModelAdminAccess {
// Revoking admin access sets read-only.
modelUser, err := st.ModelUser(targetUserTag)
if err != nil {
return errors.Annotate(err, "could not look up model access for user")
}
err = modelUser.SetAccess(state.ModelReadAccess)
return errors.Annotate(err, "could not set model access to read-only")
} else {
return errors.Errorf("don't know how to revoke %q access", stateAccess)
}
default:
return errors.Errorf("unknown action %q", action)
}
}
开发者ID:makyo,项目名称:juju,代码行数:67,代码来源:modelmanager.go
示例20: Run
//.........这里部分代码省略.........
case bootstrap.IsBootstrapAttribute(k):
bootstrapConfigAttrs[k] = v
delete(modelConfigAttrs, k)
case controller.ControllerOnlyAttribute(k):
controllerConfigAttrs[k] = v
delete(modelConfigAttrs, k)
}
}
bootstrapConfig, err := bootstrap.NewConfig(bootstrapConfigAttrs)
if err != nil {
return errors.Annotate(err, "constructing bootstrap config")
}
controllerConfig, err := controller.NewConfig(
controllerUUID.String(), bootstrapConfig.CACert, controllerConfigAttrs,
)
if err != nil {
return errors.Annotate(err, "constructing controller config")
}
if err := common.FinalizeAuthorizedKeys(ctx, modelConfigAttrs); err != nil {
return errors.Annotate(err, "finalizing authorized-keys")
}
logger.Debugf("preparing controller with config: %v", modelConfigAttrs)
// Read existing current controller so we can clean up on error.
var oldCurrentController string
oldCurrentController, err = store.CurrentController()
if errors.IsNotFound(err) {
oldCurrentController = ""
} else if err != nil {
return errors.Annotate(err, "error reading current controller")
}
defer func() {
if resultErr == nil || errors.IsAlreadyExists(resultErr) {
return
}
if oldCurrentController != "" {
if err := store.SetCurrentController(oldCurrentController); err != nil {
logger.Errorf(
"cannot reset current controller to %q: %v",
oldCurrentController, err,
)
}
}
if err := store.RemoveController(c.controllerName); err != nil {
logger.Errorf(
"cannot destroy newly created controller %q details: %v",
c.controllerName, err,
)
}
}()
bootstrapModelConfig := make(map[string]interface{})
for k, v := range inheritedControllerAttrs {
bootstrapModelConfig[k] = v
}
for k, v := range modelConfigAttrs {
bootstrapModelConfig[k] = v
}
// Add in any default attribute values if not already
// specified, making the recorded bootstrap config
// immutable to changes in Juju.
for k, v := range config.ConfigDefaults() {
if _, ok := bootstrapModelConfig[k]; !ok {
bootstrapModelConfig[k] = v
}
开发者ID:kat-co,项目名称:juju,代码行数:67,代码来源:bootstrap.go
注:本文中的github.com/juju/errors.IsAlreadyExists函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论