1package containers 2 3import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6) 7 8// ContainerType represents the valid types of containers. 9type ContainerType string 10 11const ( 12 GenericContainer ContainerType = "generic" 13 RSAContainer ContainerType = "rsa" 14 CertificateContainer ContainerType = "certificate" 15) 16 17// ListOptsBuilder allows extensions to add additional parameters to 18// the List request 19type ListOptsBuilder interface { 20 ToContainerListQuery() (string, error) 21} 22 23// ListOpts provides options to filter the List results. 24type ListOpts struct { 25 // Limit is the amount of containers to retrieve. 26 Limit int `q:"limit"` 27 28 // Name is the name of the container 29 Name string `q:"name"` 30 31 // Offset is the index within the list to retrieve. 32 Offset int `q:"offset"` 33} 34 35// ToContainerListQuery formats a ListOpts into a query string. 36func (opts ListOpts) ToContainerListQuery() (string, error) { 37 q, err := gophercloud.BuildQueryString(opts) 38 return q.String(), err 39} 40 41// List retrieves a list of containers. 42func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 43 url := listURL(client) 44 if opts != nil { 45 query, err := opts.ToContainerListQuery() 46 if err != nil { 47 return pagination.Pager{Err: err} 48 } 49 url += query 50 } 51 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 52 return ContainerPage{pagination.LinkedPageBase{PageResult: r}} 53 }) 54} 55 56// Get retrieves details of a container. 57func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { 58 resp, err := client.Get(getURL(client, id), &r.Body, nil) 59 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 60 return 61} 62 63// CreateOptsBuilder allows extensions to add additional parameters to 64// the Create request. 65type CreateOptsBuilder interface { 66 ToContainerCreateMap() (map[string]interface{}, error) 67} 68 69// CreateOpts provides options used to create a container. 70type CreateOpts struct { 71 // Type represents the type of container. 72 Type ContainerType `json:"type" required:"true"` 73 74 // Name is the name of the container. 75 Name string `json:"name"` 76 77 // SecretRefs is a list of secret refs for the container. 78 SecretRefs []SecretRef `json:"secret_refs,omitempty"` 79} 80 81// ToContainerCreateMap formats a CreateOpts into a create request. 82func (opts CreateOpts) ToContainerCreateMap() (map[string]interface{}, error) { 83 return gophercloud.BuildRequestBody(opts, "") 84} 85 86// Create creates a new container. 87func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 88 b, err := opts.ToContainerCreateMap() 89 if err != nil { 90 r.Err = err 91 return 92 } 93 resp, err := client.Post(createURL(client), &b, &r.Body, &gophercloud.RequestOpts{ 94 OkCodes: []int{201}, 95 }) 96 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 97 return 98} 99 100// Delete deletes a container. 101func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { 102 resp, err := client.Delete(deleteURL(client, id), nil) 103 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 104 return 105} 106 107// ListConsumersOptsBuilder allows extensions to add additional parameters to 108// the ListConsumers request 109type ListConsumersOptsBuilder interface { 110 ToContainerListConsumersQuery() (string, error) 111} 112 113// ListConsumersOpts provides options to filter the List results. 114type ListConsumersOpts struct { 115 // Limit is the amount of consumers to retrieve. 116 Limit int `q:"limit"` 117 118 // Offset is the index within the list to retrieve. 119 Offset int `q:"offset"` 120} 121 122// ToContainerListConsumersQuery formats a ListConsumersOpts into a query 123// string. 124func (opts ListOpts) ToContainerListConsumersQuery() (string, error) { 125 q, err := gophercloud.BuildQueryString(opts) 126 return q.String(), err 127} 128 129// ListConsumers retrieves a list of consumers from a container. 130func ListConsumers(client *gophercloud.ServiceClient, containerID string, opts ListConsumersOptsBuilder) pagination.Pager { 131 url := listConsumersURL(client, containerID) 132 if opts != nil { 133 query, err := opts.ToContainerListConsumersQuery() 134 if err != nil { 135 return pagination.Pager{Err: err} 136 } 137 url += query 138 } 139 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 140 return ConsumerPage{pagination.LinkedPageBase{PageResult: r}} 141 }) 142} 143 144// CreateConsumerOptsBuilder allows extensions to add additional parameters to 145// the Create request. 146type CreateConsumerOptsBuilder interface { 147 ToContainerConsumerCreateMap() (map[string]interface{}, error) 148} 149 150// CreateConsumerOpts provides options used to create a container. 151type CreateConsumerOpts struct { 152 // Name is the name of the consumer. 153 Name string `json:"name"` 154 155 // URL is the URL to the consumer resource. 156 URL string `json:"URL"` 157} 158 159// ToContainerConsumerCreateMap formats a CreateConsumerOpts into a create 160// request. 161func (opts CreateConsumerOpts) ToContainerConsumerCreateMap() (map[string]interface{}, error) { 162 return gophercloud.BuildRequestBody(opts, "") 163} 164 165// CreateConsumer creates a new consumer. 166func CreateConsumer(client *gophercloud.ServiceClient, containerID string, opts CreateConsumerOptsBuilder) (r CreateConsumerResult) { 167 b, err := opts.ToContainerConsumerCreateMap() 168 if err != nil { 169 r.Err = err 170 return 171 } 172 resp, err := client.Post(createConsumerURL(client, containerID), &b, &r.Body, &gophercloud.RequestOpts{ 173 OkCodes: []int{200}, 174 }) 175 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 176 return 177} 178 179// DeleteConsumerOptsBuilder allows extensions to add additional parameters to 180// the Delete request. 181type DeleteConsumerOptsBuilder interface { 182 ToContainerConsumerDeleteMap() (map[string]interface{}, error) 183} 184 185// DeleteConsumerOpts represents options used for deleting a consumer. 186type DeleteConsumerOpts struct { 187 // Name is the name of the consumer. 188 Name string `json:"name"` 189 190 // URL is the URL to the consumer resource. 191 URL string `json:"URL"` 192} 193 194// ToContainerConsumerDeleteMap formats a DeleteConsumerOpts into a create 195// request. 196func (opts DeleteConsumerOpts) ToContainerConsumerDeleteMap() (map[string]interface{}, error) { 197 return gophercloud.BuildRequestBody(opts, "") 198} 199 200// DeleteConsumer deletes a consumer. 201func DeleteConsumer(client *gophercloud.ServiceClient, containerID string, opts DeleteConsumerOptsBuilder) (r DeleteConsumerResult) { 202 url := deleteConsumerURL(client, containerID) 203 204 b, err := opts.ToContainerConsumerDeleteMap() 205 if err != nil { 206 r.Err = err 207 return 208 } 209 210 resp, err := client.Request("DELETE", url, &gophercloud.RequestOpts{ 211 JSONBody: b, 212 JSONResponse: &r.Body, 213 OkCodes: []int{200}, 214 }) 215 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 216 return 217} 218 219// SecretRefBuilder allows extensions to add additional parameters to the 220// Create request. 221type SecretRefBuilder interface { 222 ToContainerSecretRefMap() (map[string]interface{}, error) 223} 224 225// ToContainerSecretRefMap formats a SecretRefBuilder into a create 226// request. 227func (opts SecretRef) ToContainerSecretRefMap() (map[string]interface{}, error) { 228 return gophercloud.BuildRequestBody(opts, "") 229} 230 231// CreateSecret creates a new consumer. 232func CreateSecretRef(client *gophercloud.ServiceClient, containerID string, opts SecretRefBuilder) (r CreateSecretRefResult) { 233 b, err := opts.ToContainerSecretRefMap() 234 if err != nil { 235 r.Err = err 236 return 237 } 238 resp, err := client.Post(createSecretRefURL(client, containerID), &b, &r.Body, &gophercloud.RequestOpts{ 239 OkCodes: []int{201}, 240 }) 241 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 242 return 243} 244 245// DeleteSecret deletes a consumer. 246func DeleteSecretRef(client *gophercloud.ServiceClient, containerID string, opts SecretRefBuilder) (r DeleteSecretRefResult) { 247 url := deleteSecretRefURL(client, containerID) 248 249 b, err := opts.ToContainerSecretRefMap() 250 if err != nil { 251 r.Err = err 252 return 253 } 254 255 resp, err := client.Request("DELETE", url, &gophercloud.RequestOpts{ 256 JSONBody: b, 257 OkCodes: []int{204}, 258 }) 259 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 260 return 261} 262