github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/compute/v2/aggregates/requests.go (about) 1 package aggregates 2 3 import ( 4 "context" 5 "strconv" 6 7 "github.com/vnpaycloud-console/gophercloud/v2" 8 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 9 ) 10 11 // List makes a request against the API to list aggregates. 12 func List(client *gophercloud.ServiceClient) pagination.Pager { 13 return pagination.NewPager(client, aggregatesListURL(client), func(r pagination.PageResult) pagination.Page { 14 return AggregatesPage{pagination.SinglePageBase(r)} 15 }) 16 } 17 18 type CreateOpts struct { 19 // The name of the host aggregate. 20 Name string `json:"name" required:"true"` 21 22 // The availability zone of the host aggregate. 23 // You should use a custom availability zone rather than 24 // the default returned by the os-availability-zone API. 25 // The availability zone must not include ‘:’ in its name. 26 AvailabilityZone string `json:"availability_zone,omitempty"` 27 } 28 29 func (opts CreateOpts) ToAggregatesCreateMap() (map[string]any, error) { 30 return gophercloud.BuildRequestBody(opts, "aggregate") 31 } 32 33 // Create makes a request against the API to create an aggregate. 34 func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOpts) (r CreateResult) { 35 b, err := opts.ToAggregatesCreateMap() 36 if err != nil { 37 r.Err = err 38 return 39 } 40 resp, err := client.Post(ctx, aggregatesCreateURL(client), b, &r.Body, &gophercloud.RequestOpts{ 41 OkCodes: []int{200}, 42 }) 43 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 44 return 45 } 46 47 // Delete makes a request against the API to delete an aggregate. 48 func Delete(ctx context.Context, client *gophercloud.ServiceClient, aggregateID int) (r DeleteResult) { 49 v := strconv.Itoa(aggregateID) 50 resp, err := client.Delete(ctx, aggregatesDeleteURL(client, v), &gophercloud.RequestOpts{ 51 OkCodes: []int{200}, 52 }) 53 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 54 return 55 } 56 57 // Get makes a request against the API to get details for a specific aggregate. 58 func Get(ctx context.Context, client *gophercloud.ServiceClient, aggregateID int) (r GetResult) { 59 v := strconv.Itoa(aggregateID) 60 resp, err := client.Get(ctx, aggregatesGetURL(client, v), &r.Body, &gophercloud.RequestOpts{ 61 OkCodes: []int{200}, 62 }) 63 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 64 return 65 } 66 67 type UpdateOpts struct { 68 // The name of the host aggregate. 69 Name string `json:"name,omitempty"` 70 71 // The availability zone of the host aggregate. 72 // You should use a custom availability zone rather than 73 // the default returned by the os-availability-zone API. 74 // The availability zone must not include ‘:’ in its name. 75 AvailabilityZone string `json:"availability_zone,omitempty"` 76 } 77 78 func (opts UpdateOpts) ToAggregatesUpdateMap() (map[string]any, error) { 79 return gophercloud.BuildRequestBody(opts, "aggregate") 80 } 81 82 // Update makes a request against the API to update a specific aggregate. 83 func Update(ctx context.Context, client *gophercloud.ServiceClient, aggregateID int, opts UpdateOpts) (r UpdateResult) { 84 v := strconv.Itoa(aggregateID) 85 86 b, err := opts.ToAggregatesUpdateMap() 87 if err != nil { 88 r.Err = err 89 return 90 } 91 resp, err := client.Put(ctx, aggregatesUpdateURL(client, v), b, &r.Body, &gophercloud.RequestOpts{ 92 OkCodes: []int{200}, 93 }) 94 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 95 return 96 } 97 98 type AddHostOpts struct { 99 // The name of the host. 100 Host string `json:"host" required:"true"` 101 } 102 103 func (opts AddHostOpts) ToAggregatesAddHostMap() (map[string]any, error) { 104 return gophercloud.BuildRequestBody(opts, "add_host") 105 } 106 107 // AddHost makes a request against the API to add host to a specific aggregate. 108 func AddHost(ctx context.Context, client *gophercloud.ServiceClient, aggregateID int, opts AddHostOpts) (r ActionResult) { 109 v := strconv.Itoa(aggregateID) 110 111 b, err := opts.ToAggregatesAddHostMap() 112 if err != nil { 113 r.Err = err 114 return 115 } 116 resp, err := client.Post(ctx, aggregatesAddHostURL(client, v), b, &r.Body, &gophercloud.RequestOpts{ 117 OkCodes: []int{200}, 118 }) 119 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 120 return 121 } 122 123 type RemoveHostOpts struct { 124 // The name of the host. 125 Host string `json:"host" required:"true"` 126 } 127 128 func (opts RemoveHostOpts) ToAggregatesRemoveHostMap() (map[string]any, error) { 129 return gophercloud.BuildRequestBody(opts, "remove_host") 130 } 131 132 // RemoveHost makes a request against the API to remove host from a specific aggregate. 133 func RemoveHost(ctx context.Context, client *gophercloud.ServiceClient, aggregateID int, opts RemoveHostOpts) (r ActionResult) { 134 v := strconv.Itoa(aggregateID) 135 136 b, err := opts.ToAggregatesRemoveHostMap() 137 if err != nil { 138 r.Err = err 139 return 140 } 141 resp, err := client.Post(ctx, aggregatesRemoveHostURL(client, v), b, &r.Body, &gophercloud.RequestOpts{ 142 OkCodes: []int{200}, 143 }) 144 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 145 return 146 } 147 148 type SetMetadataOpts struct { 149 Metadata map[string]any `json:"metadata" required:"true"` 150 } 151 152 func (opts SetMetadataOpts) ToSetMetadataMap() (map[string]any, error) { 153 return gophercloud.BuildRequestBody(opts, "set_metadata") 154 } 155 156 // SetMetadata makes a request against the API to set metadata to a specific aggregate. 157 func SetMetadata(ctx context.Context, client *gophercloud.ServiceClient, aggregateID int, opts SetMetadataOpts) (r ActionResult) { 158 v := strconv.Itoa(aggregateID) 159 160 b, err := opts.ToSetMetadataMap() 161 if err != nil { 162 r.Err = err 163 return 164 } 165 resp, err := client.Post(ctx, aggregatesSetMetadataURL(client, v), b, &r.Body, &gophercloud.RequestOpts{ 166 OkCodes: []int{200}, 167 }) 168 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 169 return 170 }