github.com/gophercloud/gophercloud@v1.11.0/openstack/clustering/v1/profiles/requests.go (about) 1 package profiles 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6 ) 7 8 // CreateOptsBuilder allows extensions to add additional parameters to the 9 // Create request. 10 type CreateOptsBuilder interface { 11 ToProfileCreateMap() (map[string]interface{}, error) 12 } 13 14 // CreateOpts represents options used for creating a profile. 15 type CreateOpts struct { 16 Name string `json:"name" required:"true"` 17 Metadata map[string]interface{} `json:"metadata,omitempty"` 18 Spec Spec `json:"spec" required:"true"` 19 } 20 21 // ToProfileCreateMap constructs a request body from CreateOpts. 22 func (opts CreateOpts) ToProfileCreateMap() (map[string]interface{}, error) { 23 return gophercloud.BuildRequestBody(opts, "profile") 24 } 25 26 // Create requests the creation of a new profile on the server. 27 func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 28 b, err := opts.ToProfileCreateMap() 29 if err != nil { 30 r.Err = err 31 return 32 } 33 resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ 34 OkCodes: []int{200, 201}, 35 }) 36 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 37 return 38 } 39 40 // Get retrieves detail of a single profile. 41 func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { 42 resp, err := client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{ 43 OkCodes: []int{200}, 44 }) 45 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 46 return 47 } 48 49 // ListOptsBuilder allows extensions to add additional parameters to the 50 // List request. 51 type ListOptsBuilder interface { 52 ToProfileListQuery() (string, error) 53 } 54 55 // ListOpts represents options used to list profiles. 56 type ListOpts struct { 57 GlobalProject *bool `q:"global_project"` 58 Limit int `q:"limit"` 59 Marker string `q:"marker"` 60 Name string `q:"name"` 61 Sort string `q:"sort"` 62 Type string `q:"type"` 63 } 64 65 // ToProfileListQuery formats a ListOpts into a query string. 66 func (opts ListOpts) ToProfileListQuery() (string, error) { 67 q, err := gophercloud.BuildQueryString(opts) 68 return q.String(), err 69 } 70 71 // List instructs OpenStack to provide a list of profiles. 72 func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 73 url := listURL(client) 74 if opts != nil { 75 query, err := opts.ToProfileListQuery() 76 if err != nil { 77 return pagination.Pager{Err: err} 78 } 79 url += query 80 } 81 82 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 83 return ProfilePage{pagination.LinkedPageBase{PageResult: r}} 84 }) 85 } 86 87 // UpdateOptsBuilder allows extensions to add additional parameters to the 88 // Update request. 89 type UpdateOptsBuilder interface { 90 ToProfileUpdateMap() (map[string]interface{}, error) 91 } 92 93 // UpdateOpts represents options used to update a profile. 94 type UpdateOpts struct { 95 Metadata map[string]interface{} `json:"metadata,omitempty"` 96 Name string `json:"name,omitempty"` 97 } 98 99 // ToProfileUpdateMap constructs a request body from UpdateOpts. 100 func (opts UpdateOpts) ToProfileUpdateMap() (map[string]interface{}, error) { 101 return gophercloud.BuildRequestBody(opts, "profile") 102 } 103 104 // Update updates a profile. 105 func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 106 b, err := opts.ToProfileUpdateMap() 107 if err != nil { 108 r.Err = err 109 return r 110 } 111 resp, err := client.Patch(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ 112 OkCodes: []int{200}, 113 }) 114 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 115 return 116 } 117 118 // Delete deletes the specified profile via profile id. 119 func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { 120 resp, err := client.Delete(deleteURL(client, id), nil) 121 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 122 return 123 } 124 125 // ValidateOptsBuilder allows extensions to add additional parameters to the 126 // Validate request. 127 type ValidateOptsBuilder interface { 128 ToProfileValidateMap() (map[string]interface{}, error) 129 } 130 131 // ValidateOpts params 132 type ValidateOpts struct { 133 Spec Spec `json:"spec" required:"true"` 134 } 135 136 // ToProfileValidateMap formats a CreateOpts into a body map. 137 func (opts ValidateOpts) ToProfileValidateMap() (map[string]interface{}, error) { 138 return gophercloud.BuildRequestBody(opts, "profile") 139 } 140 141 // Validate profile. 142 func Validate(client *gophercloud.ServiceClient, opts ValidateOpts) (r ValidateResult) { 143 b, err := opts.ToProfileValidateMap() 144 if err != nil { 145 r.Err = err 146 return 147 } 148 resp, err := client.Post(validateURL(client), b, &r.Body, &gophercloud.RequestOpts{ 149 OkCodes: []int{200}, 150 }) 151 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 152 return 153 }