Jamie Hannaford | 2a13024 | 2014-10-28 11:19:46 +0100 | [diff] [blame] | 1 | package users |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 2 | |
| 3 | import ( |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 4 | "errors" |
| 5 | |
| 6 | "github.com/racker/perigee" |
Jamie Hannaford | 929bd00 | 2014-10-29 11:14:25 +0100 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
| 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | ) |
| 10 | |
| 11 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 12 | createPage := func(r pagination.PageResult) pagination.Page { |
| 13 | return UserPage{pagination.SinglePageBase(r)} |
| 14 | } |
| 15 | |
| 16 | return pagination.NewPager(client, rootURL(client), createPage) |
| 17 | } |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 18 | |
| 19 | // EnabledState represents whether the user is enabled or not. |
| 20 | type EnabledState *bool |
| 21 | |
| 22 | // Useful variables to use when creating or updating users. |
| 23 | var ( |
| 24 | iTrue = true |
| 25 | iFalse = false |
| 26 | |
| 27 | Enabled EnabledState = &iTrue |
| 28 | Disabled EnabledState = &iFalse |
| 29 | ) |
| 30 | |
| 31 | // CreateOpts represents the options needed when creating new users. |
| 32 | type CreateOpts struct { |
| 33 | // Either a name or username is required. When provided, the value must be |
| 34 | // unique or a 409 conflict error will be returned. If you provide a name but |
| 35 | // omit a username, the latter will be set to the former; and vice versa. |
| 36 | Name, Username string |
| 37 | |
| 38 | // The ID of the tenant to which you want to assign this user. |
| 39 | TenantID string |
| 40 | |
| 41 | // Indicates whether this user is enabled or not. |
| 42 | Enabled EnabledState |
| 43 | |
| 44 | // The email address of this user. |
| 45 | Email string |
| 46 | } |
| 47 | |
| 48 | // CreateOptsBuilder describes struct types that can be accepted by the Create call. |
| 49 | type CreateOptsBuilder interface { |
| 50 | ToUserCreateMap() (map[string]interface{}, error) |
| 51 | } |
| 52 | |
| 53 | // ToUserCreateMap assembles a request body based on the contents of a CreateOpts. |
| 54 | func (opts CreateOpts) ToUserCreateMap() (map[string]interface{}, error) { |
| 55 | m := make(map[string]interface{}) |
| 56 | |
| 57 | if opts.Name == "" && opts.Username == "" { |
| 58 | return m, errors.New("Either a Name or Username must be provided") |
| 59 | } |
| 60 | |
| 61 | if opts.Name != "" { |
| 62 | m["name"] = opts.Name |
| 63 | } |
| 64 | if opts.Username != "" { |
| 65 | m["username"] = opts.Username |
| 66 | } |
| 67 | if opts.Enabled != nil { |
| 68 | m["enabled"] = &opts.Enabled |
| 69 | } |
| 70 | if opts.Email != "" { |
| 71 | m["email"] = opts.Email |
| 72 | } |
| 73 | |
| 74 | return m, nil |
| 75 | } |
| 76 | |
| 77 | // Create is the operation responsible for creating new users. |
| 78 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
| 79 | var res CreateResult |
| 80 | |
| 81 | reqBody, err := opts.ToUserCreateMap() |
| 82 | if err != nil { |
| 83 | res.Err = err |
| 84 | return res |
| 85 | } |
| 86 | |
| 87 | _, res.Err = perigee.Request("POST", rootURL(client), perigee.Options{ |
| 88 | Results: &res.Body, |
| 89 | ReqBody: reqBody, |
| 90 | MoreHeaders: client.AuthenticatedHeaders(), |
| 91 | OkCodes: []int{200, 201}, |
| 92 | }) |
| 93 | |
| 94 | return res |
| 95 | } |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame^] | 96 | |
| 97 | // Get requests details on a single user, either by ID or username. |
| 98 | func Get(client *gophercloud.ServiceClient, identifier string) GetResult { |
| 99 | var result GetResult |
| 100 | |
| 101 | _, result.Err = perigee.Request("GET", resourceURL(client, identifier), perigee.Options{ |
| 102 | Results: &result.Body, |
| 103 | MoreHeaders: client.AuthenticatedHeaders(), |
| 104 | }) |
| 105 | |
| 106 | return result |
| 107 | } |