func (s *permissionSuite) TestParseModelAccessValid(c *gc.C) { var ( access permission.ModelAccess err error ) _, err = permission.ParseModelAccess("") c.Check(err, gc.ErrorMatches, "invalid model access permission.*") access, err = permission.ParseModelAccess("read") c.Check(err, jc.ErrorIsNil) c.Check(access, gc.Equals, permission.ModelReadAccess) access, err = permission.ParseModelAccess("write") c.Check(err, jc.ErrorIsNil) c.Check(access, gc.Equals, permission.ModelWriteAccess) access, err = permission.ParseModelAccess("orange") c.Check(err, gc.ErrorMatches, "invalid model access permission.*") }
// Init implements Command.Init. func (c *addCommand) Init(args []string) error { if len(args) == 0 { return fmt.Errorf("no username supplied") } _, err := permission.ParseModelAccess(c.ModelAccess) if err != nil { return err } c.User, args = args[0], args[1:] if len(args) > 0 { c.DisplayName, args = args[0], args[1:] } return cmd.CheckEmpty(args) }
// Init implements cmd.Command. func (c *accessCommand) Init(args []string) error { if len(args) < 1 { return errors.New("no user specified") } if len(args) < 2 { return errors.New("no model specified") } _, err := permission.ParseModelAccess(c.ModelAccess) if err != nil { return err } c.User = args[0] c.ModelNames = args[1:] return nil }
// ParseModelAccess parses an access permission argument into // a type suitable for making an API facade call. func ParseModelAccess(access string) (params.ModelAccessPermission, error) { var fail params.ModelAccessPermission modelAccess, err := permission.ParseModelAccess(access) if err != nil { return fail, errors.Trace(err) } var accessPermission params.ModelAccessPermission switch modelAccess { case permission.ModelReadAccess: accessPermission = params.ModelReadAccess case permission.ModelWriteAccess: accessPermission = params.ModelWriteAccess default: return fail, errors.Errorf("unsupported model access permission %v", modelAccess) } return accessPermission, nil }
func (s *permissionSuite) TestParseModelAccessInvalid(c *gc.C) { _, err := permission.ParseModelAccess("preposterous") c.Check(err, gc.ErrorMatches, "invalid model access permission.*") }