blob: 8c667cbddad4158706ab98c4ee0b2aaa67329912 [file] [log] [blame]
jrperrittc5c590a2016-11-04 14:41:15 -05001package members
2
3import (
4 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
6)
7
8// Create member for specific image
9//
10// Preconditions
11// The specified images must exist.
12// You can only add a new member to an image which 'visibility' attribute is private.
13// You must be the owner of the specified image.
14// Synchronous Postconditions
15// With correct permissions, you can see the member status of the image as pending through API calls.
16//
17// More details here: http://developer.openstack.org/api-ref-image-v2.html#createImageMember-v2
18func Create(client *gophercloud.ServiceClient, id string, member string) (r CreateResult) {
19 b := map[string]interface{}{"member": member}
20 _, r.Err = client.Post(createMemberURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
21 OkCodes: []int{200, 409, 403},
22 })
23 return
24}
25
26// List members returns list of members for specifed image id
27// More details: http://developer.openstack.org/api-ref-image-v2.html#listImageMembers-v2
28func List(client *gophercloud.ServiceClient, id string) pagination.Pager {
29 return pagination.NewPager(client, listMembersURL(client, id), func(r pagination.PageResult) pagination.Page {
30 return MemberPage{pagination.SinglePageBase(r)}
31 })
32}
33
34// Get image member details.
35// More details: http://developer.openstack.org/api-ref-image-v2.html#getImageMember-v2
36func Get(client *gophercloud.ServiceClient, imageID string, memberID string) (r DetailsResult) {
37 _, r.Err = client.Get(getMemberURL(client, imageID, memberID), &r.Body, &gophercloud.RequestOpts{OkCodes: []int{200}})
38 return
39}
40
41// Delete membership for given image.
42// Callee should be image owner
43// More details: http://developer.openstack.org/api-ref-image-v2.html#deleteImageMember-v2
44func Delete(client *gophercloud.ServiceClient, imageID string, memberID string) (r DeleteResult) {
45 _, r.Err = client.Delete(deleteMemberURL(client, imageID, memberID), &gophercloud.RequestOpts{OkCodes: []int{204, 403}})
46 return
47}
48
49// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
50type UpdateOptsBuilder interface {
51 ToImageMemberUpdateMap() (map[string]interface{}, error)
52}
53
54// UpdateOpts implements UpdateOptsBuilder
55type UpdateOpts struct {
56 Status string
57}
58
59// ToMemberUpdateMap formats an UpdateOpts structure into a request body.
60func (opts UpdateOpts) ToImageMemberUpdateMap() (map[string]interface{}, error) {
61 return map[string]interface{}{
62 "status": opts.Status,
63 }, nil
64}
65
66// Update function updates member
67// More details: http://developer.openstack.org/api-ref-image-v2.html#updateImageMember-v2
68func Update(client *gophercloud.ServiceClient, imageID string, memberID string, opts UpdateOptsBuilder) (r UpdateResult) {
69 b, err := opts.ToImageMemberUpdateMap()
70 if err != nil {
71 r.Err = err
72 return
73 }
74 _, r.Err = client.Put(updateMemberURL(client, imageID, memberID), b, &r.Body,
75 &gophercloud.RequestOpts{OkCodes: []int{200}})
76 return
77}