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 | |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame^] | 31 | type commonOpts struct { |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 32 | // Either a name or username is required. When provided, the value must be |
| 33 | // unique or a 409 conflict error will be returned. If you provide a name but |
| 34 | // omit a username, the latter will be set to the former; and vice versa. |
| 35 | Name, Username string |
| 36 | |
| 37 | // The ID of the tenant to which you want to assign this user. |
| 38 | TenantID string |
| 39 | |
| 40 | // Indicates whether this user is enabled or not. |
| 41 | Enabled EnabledState |
| 42 | |
| 43 | // The email address of this user. |
| 44 | Email string |
| 45 | } |
| 46 | |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame^] | 47 | // CreateOpts represents the options needed when creating new users. |
| 48 | type CreateOpts commonOpts |
| 49 | |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 50 | // CreateOptsBuilder describes struct types that can be accepted by the Create call. |
| 51 | type CreateOptsBuilder interface { |
| 52 | ToUserCreateMap() (map[string]interface{}, error) |
| 53 | } |
| 54 | |
| 55 | // ToUserCreateMap assembles a request body based on the contents of a CreateOpts. |
| 56 | func (opts CreateOpts) ToUserCreateMap() (map[string]interface{}, error) { |
| 57 | m := make(map[string]interface{}) |
| 58 | |
| 59 | if opts.Name == "" && opts.Username == "" { |
| 60 | return m, errors.New("Either a Name or Username must be provided") |
| 61 | } |
| 62 | |
| 63 | if opts.Name != "" { |
| 64 | m["name"] = opts.Name |
| 65 | } |
| 66 | if opts.Username != "" { |
| 67 | m["username"] = opts.Username |
| 68 | } |
| 69 | if opts.Enabled != nil { |
| 70 | m["enabled"] = &opts.Enabled |
| 71 | } |
| 72 | if opts.Email != "" { |
| 73 | m["email"] = opts.Email |
| 74 | } |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame^] | 75 | if opts.TenantID != "" { |
| 76 | m["tenant_id"] = opts.TenantID |
| 77 | } |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 78 | |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame^] | 79 | return map[string]interface{}{"user": m}, nil |
Jamie Hannaford | 9c7bb8e | 2014-10-29 11:47:34 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // Create is the operation responsible for creating new users. |
| 83 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
| 84 | var res CreateResult |
| 85 | |
| 86 | reqBody, err := opts.ToUserCreateMap() |
| 87 | if err != nil { |
| 88 | res.Err = err |
| 89 | return res |
| 90 | } |
| 91 | |
| 92 | _, res.Err = perigee.Request("POST", rootURL(client), perigee.Options{ |
| 93 | Results: &res.Body, |
| 94 | ReqBody: reqBody, |
| 95 | MoreHeaders: client.AuthenticatedHeaders(), |
| 96 | OkCodes: []int{200, 201}, |
| 97 | }) |
| 98 | |
| 99 | return res |
| 100 | } |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 101 | |
Jamie Hannaford | 69c1fe9 | 2014-10-29 13:28:58 +0100 | [diff] [blame] | 102 | // Get requests details on a single user, either by ID. |
| 103 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 104 | var result GetResult |
| 105 | |
Jamie Hannaford | 69c1fe9 | 2014-10-29 13:28:58 +0100 | [diff] [blame] | 106 | _, result.Err = perigee.Request("GET", resourceURL(client, id), perigee.Options{ |
Jamie Hannaford | 2ad98bd | 2014-10-29 13:26:47 +0100 | [diff] [blame] | 107 | Results: &result.Body, |
| 108 | MoreHeaders: client.AuthenticatedHeaders(), |
| 109 | }) |
| 110 | |
| 111 | return result |
| 112 | } |
Jamie Hannaford | 7e04adf | 2014-10-29 13:47:58 +0100 | [diff] [blame^] | 113 | |
| 114 | // UpdateOptsBuilder allows extentions to add additional attributes to the Update request. |
| 115 | type UpdateOptsBuilder interface { |
| 116 | ToUserUpdateMap() map[string]interface{} |
| 117 | } |
| 118 | |
| 119 | // UpdateOpts specifies the base attributes that may be updated on an existing server. |
| 120 | type UpdateOpts commonOpts |
| 121 | |
| 122 | // ToUserUpdateMap formats an UpdateOpts structure into a request body. |
| 123 | func (opts UpdateOpts) ToUserUpdateMap() map[string]interface{} { |
| 124 | m := make(map[string]interface{}) |
| 125 | |
| 126 | if opts.Name != "" { |
| 127 | m["name"] = opts.Name |
| 128 | } |
| 129 | if opts.Username != "" { |
| 130 | m["username"] = opts.Username |
| 131 | } |
| 132 | if opts.Enabled != nil { |
| 133 | m["enabled"] = &opts.Enabled |
| 134 | } |
| 135 | if opts.Email != "" { |
| 136 | m["email"] = opts.Email |
| 137 | } |
| 138 | if opts.TenantID != "" { |
| 139 | m["tenant_id"] = opts.TenantID |
| 140 | } |
| 141 | |
| 142 | return map[string]interface{}{"user": m} |
| 143 | } |
| 144 | |
| 145 | // Update is the operation responsible for updating exist users by their UUID. |
| 146 | func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult { |
| 147 | var result UpdateResult |
| 148 | |
| 149 | _, result.Err = perigee.Request("PUT", resourceURL(client, id), perigee.Options{ |
| 150 | Results: &result.Body, |
| 151 | ReqBody: opts.ToUserUpdateMap(), |
| 152 | MoreHeaders: client.AuthenticatedHeaders(), |
| 153 | }) |
| 154 | |
| 155 | return result |
| 156 | } |