github.com/gophercloud/gophercloud@v1.11.0/openstack/container/v1/capsules/requests.go (about) 1 package capsules 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6 ) 7 8 // CreateOptsBuilder is the interface options structs have to satisfy in order 9 // to be used in the main Create operation in this package. Since many 10 // extensions decorate or modify the common logic, it is useful for them to 11 // satisfy a basic interface in order for them to be used. 12 type CreateOptsBuilder interface { 13 ToCapsuleCreateMap() (map[string]interface{}, error) 14 } 15 16 // ListOptsBuilder allows extensions to add additional parameters to the 17 // List request. 18 type ListOptsBuilder interface { 19 ToCapsuleListQuery() (string, error) 20 } 21 22 // Get requests details on a single capsule, by ID. 23 func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { 24 resp, err := client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{ 25 OkCodes: []int{200, 203}, 26 }) 27 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 28 return 29 } 30 31 // CreateOpts is the common options struct used in this package's Create 32 // operation. 33 type CreateOpts struct { 34 // A structure that contains either the template file or url. Call the 35 // associated methods to extract the information relevant to send in a create request. 36 TemplateOpts *Template `json:"-" required:"true"` 37 } 38 39 // ToCapsuleCreateMap assembles a request body based on the contents of 40 // a CreateOpts. 41 func (opts CreateOpts) ToCapsuleCreateMap() (map[string]interface{}, error) { 42 b, err := gophercloud.BuildRequestBody(opts, "") 43 if err != nil { 44 return nil, err 45 } 46 47 if err := opts.TemplateOpts.Parse(); err != nil { 48 return nil, err 49 } 50 b["template"] = string(opts.TemplateOpts.Bin) 51 52 return b, nil 53 } 54 55 // Create implements create capsule request. 56 func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 57 b, err := opts.ToCapsuleCreateMap() 58 if err != nil { 59 r.Err = err 60 return 61 } 62 resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{OkCodes: []int{202}}) 63 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 64 return 65 } 66 67 // ListOpts allows the filtering and sorting of paginated collections through 68 // the API. Filtering is achieved by passing in struct field values that map to 69 // the capsule attributes you want to see returned. Marker and Limit are used 70 // for pagination. 71 type ListOpts struct { 72 Marker string `q:"marker"` 73 Limit int `q:"limit"` 74 SortKey string `q:"sort_key"` 75 SortDir string `q:"sort_dir"` 76 AllProjects bool `q:"all_projects"` 77 } 78 79 // ToCapsuleListQuery formats a ListOpts into a query string. 80 func (opts ListOpts) ToCapsuleListQuery() (string, error) { 81 q, err := gophercloud.BuildQueryString(opts) 82 return q.String(), err 83 } 84 85 // List makes a request against the API to list capsules accessible to you. 86 func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 87 url := listURL(client) 88 if opts != nil { 89 query, err := opts.ToCapsuleListQuery() 90 if err != nil { 91 return pagination.Pager{Err: err} 92 } 93 url += query 94 } 95 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 96 return CapsulePage{pagination.LinkedPageBase{PageResult: r}} 97 }) 98 } 99 100 // Delete implements Capsule delete request. 101 func 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 }