github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v2/users/requests.go (about) 1 package users 2 3 import ( 4 "context" 5 6 "github.com/vnpaycloud-console/gophercloud/v2" 7 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 8 ) 9 10 // List lists the existing users. 11 func List(client *gophercloud.ServiceClient) pagination.Pager { 12 return pagination.NewPager(client, rootURL(client), func(r pagination.PageResult) pagination.Page { 13 return UserPage{pagination.SinglePageBase(r)} 14 }) 15 } 16 17 // CommonOpts are the parameters that are shared between CreateOpts and 18 // UpdateOpts 19 type CommonOpts struct { 20 // Either a name or username is required. When provided, the value must be 21 // unique or a 409 conflict error will be returned. If you provide a name but 22 // omit a username, the latter will be set to the former; and vice versa. 23 Name string `json:"name,omitempty"` 24 Username string `json:"username,omitempty"` 25 26 // TenantID is the ID of the tenant to which you want to assign this user. 27 TenantID string `json:"tenantId,omitempty"` 28 29 // Enabled indicates whether this user is enabled or not. 30 Enabled *bool `json:"enabled,omitempty"` 31 32 // Email is the email address of this user. 33 Email string `json:"email,omitempty"` 34 } 35 36 // CreateOpts represents the options needed when creating new users. 37 type CreateOpts CommonOpts 38 39 // CreateOptsBuilder allows extensions to add additional parameters to the 40 // Create request. 41 type CreateOptsBuilder interface { 42 ToUserCreateMap() (map[string]any, error) 43 } 44 45 // ToUserCreateMap assembles a request body based on the contents of a 46 // CreateOpts. 47 func (opts CreateOpts) ToUserCreateMap() (map[string]any, error) { 48 if opts.Name == "" && opts.Username == "" { 49 err := gophercloud.ErrMissingInput{} 50 err.Argument = "users.CreateOpts.Name/users.CreateOpts.Username" 51 err.Info = "Either a Name or Username must be provided" 52 return nil, err 53 } 54 return gophercloud.BuildRequestBody(opts, "user") 55 } 56 57 // Create is the operation responsible for creating new users. 58 func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 59 b, err := opts.ToUserCreateMap() 60 if err != nil { 61 r.Err = err 62 return 63 } 64 resp, err := client.Post(ctx, rootURL(client), b, &r.Body, &gophercloud.RequestOpts{ 65 OkCodes: []int{200, 201}, 66 }) 67 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 68 return 69 } 70 71 // Get requests details on a single user, either by ID or Name. 72 func Get(ctx context.Context, client *gophercloud.ServiceClient, id string) (r GetResult) { 73 resp, err := client.Get(ctx, ResourceURL(client, id), &r.Body, nil) 74 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 75 return 76 } 77 78 // UpdateOptsBuilder allows extensions to add additional parameters to the 79 // Update request. 80 type UpdateOptsBuilder interface { 81 ToUserUpdateMap() (map[string]any, error) 82 } 83 84 // UpdateOpts specifies the base attributes that may be updated on an 85 // existing server. 86 type UpdateOpts CommonOpts 87 88 // ToUserUpdateMap formats an UpdateOpts structure into a request body. 89 func (opts UpdateOpts) ToUserUpdateMap() (map[string]any, error) { 90 return gophercloud.BuildRequestBody(opts, "user") 91 } 92 93 // Update is the operation responsible for updating exist users by their ID. 94 func Update(ctx context.Context, client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 95 b, err := opts.ToUserUpdateMap() 96 if err != nil { 97 r.Err = err 98 return 99 } 100 resp, err := client.Put(ctx, ResourceURL(client, id), &b, &r.Body, &gophercloud.RequestOpts{ 101 OkCodes: []int{200}, 102 }) 103 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 104 return 105 } 106 107 // Delete is the operation responsible for permanently deleting a User. 108 func Delete(ctx context.Context, client *gophercloud.ServiceClient, id string) (r DeleteResult) { 109 resp, err := client.Delete(ctx, ResourceURL(client, id), nil) 110 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 111 return 112 } 113 114 // ListRoles lists the existing roles that can be assigned to users. 115 func ListRoles(client *gophercloud.ServiceClient, tenantID, userID string) pagination.Pager { 116 return pagination.NewPager(client, listRolesURL(client, tenantID, userID), func(r pagination.PageResult) pagination.Page { 117 return RolePage{pagination.SinglePageBase(r)} 118 }) 119 }