github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/ecs/v1/cloudservers/requests.go (about) 1 package cloudservers 2 3 import ( 4 "encoding/base64" 5 6 "github.com/chnsz/golangsdk" 7 "github.com/chnsz/golangsdk/openstack/common/tags" 8 "github.com/chnsz/golangsdk/pagination" 9 ) 10 11 type CreateOpts struct { 12 ImageRef string `json:"imageRef" required:"true"` 13 14 FlavorRef string `json:"flavorRef" required:"true"` 15 16 Name string `json:"name" required:"true"` 17 18 UserData []byte `json:"-"` 19 20 // AdminPass sets the root user password. If not set, a randomly-generated 21 // password will be created and returned in the response. 22 AdminPass string `json:"adminPass,omitempty"` 23 24 KeyName string `json:"key_name,omitempty"` 25 26 VpcId string `json:"vpcid" required:"true"` 27 28 Nics []Nic `json:"nics" required:"true"` 29 30 PublicIp *PublicIp `json:"publicip,omitempty"` 31 32 Count int `json:"count,omitempty"` 33 34 IsAutoRename *bool `json:"isAutoRename,omitempty"` 35 36 RootVolume RootVolume `json:"root_volume" required:"true"` 37 38 DataVolumes []DataVolume `json:"data_volumes,omitempty"` 39 40 SecurityGroups []SecurityGroup `json:"security_groups,omitempty"` 41 42 AvailabilityZone string `json:"availability_zone,omitempty"` 43 44 ExtendParam *ServerExtendParam `json:"extendparam,omitempty"` 45 46 MetaData *MetaData `json:"metadata,omitempty"` 47 48 SchedulerHints *SchedulerHints `json:"os:scheduler_hints,omitempty"` 49 50 Tags []string `json:"tags,omitempty"` 51 52 ServerTags []tags.ResourceTag `json:"server_tags,omitempty"` 53 54 Description string `json:"description,omitempty"` 55 56 AutoTerminateTime string `json:"auto_terminate_time,omitempty"` 57 } 58 59 // CreateOptsBuilder allows extensions to add additional parameters to the 60 // Create request. 61 type CreateOptsBuilder interface { 62 ToServerCreateMap() (map[string]interface{}, error) 63 } 64 65 // ToServerCreateMap assembles a request body based on the contents of a 66 // CreateOpts. 67 func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) { 68 b, err := golangsdk.BuildRequestBody(opts, "") 69 if err != nil { 70 return nil, err 71 } 72 73 if opts.UserData != nil { 74 var userData string 75 if _, err := base64.StdEncoding.DecodeString(string(opts.UserData)); err != nil { 76 userData = base64.StdEncoding.EncodeToString(opts.UserData) 77 } else { 78 userData = string(opts.UserData) 79 } 80 b["user_data"] = &userData 81 } 82 83 return map[string]interface{}{"server": b}, nil 84 } 85 86 type Nic struct { 87 SubnetId string `json:"subnet_id" required:"true"` 88 IpAddress string `json:"ip_address,omitempty"` 89 90 // enable ipv6 or not 91 Ipv6Enable bool `json:"ipv6_enable,omitempty"` 92 // bandWidth id when ipv6 is enabled 93 BandWidth *Ipv6BandWidth `json:"ipv6_bandwidth,omitempty"` 94 } 95 96 type Ipv6BandWidth struct { 97 ID string `json:"id,omitempty"` 98 } 99 100 type PublicIp struct { 101 Id string `json:"id,omitempty"` 102 103 Eip *Eip `json:"eip,omitempty"` 104 105 DeleteOnTermination bool `json:"delete_on_termination,omitempty"` 106 } 107 108 type Eip struct { 109 IpType string `json:"iptype" required:"true"` 110 111 BandWidth *BandWidth `json:"bandwidth" required:"true"` 112 113 ExtendParam *EipExtendParam `json:"extendparam,omitempty"` 114 } 115 116 type BandWidth struct { 117 Size int `json:"size,omitempty"` 118 119 ShareType string `json:"sharetype" required:"true"` 120 121 ChargeMode string `json:"chargemode,omitempty"` 122 123 Id string `json:"id,omitempty"` 124 } 125 126 type EipExtendParam struct { 127 ChargingMode string `json:"chargingMode,omitempty"` 128 } 129 130 type RootVolume struct { 131 VolumeType string `json:"volumetype" required:"true"` 132 133 Size int `json:"size,omitempty"` 134 135 // The iops of evs volume. Only required when volume_type is `GPSSD2` or `ESSD2` 136 IOPS int `json:"iops,omitempty"` 137 // The throughput of evs volume. Only required when volume_type is `GPSSD2` 138 Throughput int `json:"throughput,omitempty"` 139 140 ExtendParam *VolumeExtendParam `json:"extendparam,omitempty"` 141 142 Metadata *VolumeMetadata `json:"metadata,omitempty"` 143 144 ClusterId string `json:"cluster_id,omitempty"` 145 // The cluster type is default to DSS 146 ClusterType string `json:"cluster_type,omitempty"` 147 } 148 149 type DataVolume struct { 150 VolumeType string `json:"volumetype" required:"true"` 151 152 Size int `json:"size" required:"true"` 153 154 MultiAttach *bool `json:"multiattach,omitempty"` 155 156 PassThrough *bool `json:"hw:passthrough,omitempty"` 157 158 // The iops of evs volume. Only required when volume_type is `GPSSD2` or `ESSD2` 159 IOPS int `json:"iops,omitempty"` 160 // The throughput of evs volume. Only required when volume_type is `GPSSD2` 161 Throughput int `json:"throughput,omitempty"` 162 163 Extendparam *VolumeExtendParam `json:"extendparam,omitempty"` 164 165 Metadata *VolumeMetadata `json:"metadata,omitempty"` 166 167 ClusterId string `json:"cluster_id,omitempty"` 168 // The cluster type is default to DSS 169 ClusterType string `json:"cluster_type,omitempty"` 170 } 171 172 type VolumeExtendParam struct { 173 SnapshotId string `json:"snapshotId,omitempty"` 174 } 175 176 type VolumeMetadata struct { 177 SystemEncrypted string `json:"__system__encrypted,omitempty"` 178 SystemCmkid string `json:"__system__cmkid,omitempty"` 179 } 180 181 type ServerExtendParam struct { 182 ChargingMode string `json:"chargingMode,omitempty"` 183 RegionID string `json:"regionID,omitempty"` 184 PeriodType string `json:"periodType,omitempty"` 185 PeriodNum int `json:"periodNum,omitempty"` 186 IsAutoRenew string `json:"isAutoRenew,omitempty"` 187 IsAutoPay string `json:"isAutoPay,omitempty"` 188 EnterpriseProjectId string `json:"enterprise_project_id,omitempty"` 189 SupportAutoRecovery string `json:"support_auto_recovery,omitempty"` 190 191 // Specifies whether to support the function of creating a disk and then ECS: true of false 192 DiskPrior string `json:"diskPrior,omitempty"` 193 194 // When creating a spot ECS, set the parameter value to "spot" 195 MarketType string `json:"marketType,omitempty"` 196 // Specifies the highest price per hour you accept for a spot ECS 197 SpotPrice string `json:"spotPrice,omitempty"` 198 // Specifies the service duration of the spot ECS in hours 199 SpotDurationHours int `json:"spot_duration_hours,omitempty"` 200 // Specifies the number of time periods in the service duration 201 SpotDurationCount int `json:"spot_duration_count,omitempty"` 202 // Specifies the spot ECS interruption policy, which can only be set to "immediate" currently 203 InterruptionPolicy string `json:"interruption_policy,omitempty"` 204 } 205 206 type MetaData struct { 207 OpSvcUserId string `json:"op_svc_userid,omitempty"` 208 AgencyName string `json:"agency_name,omitempty"` 209 AgentList string `json:"__support_agent_list,omitempty"` 210 } 211 212 type SecurityGroup struct { 213 ID string `json:"id" required:"true"` 214 } 215 216 type SchedulerHints struct { 217 Group string `json:"group,omitempty"` 218 FaultDomain string `json:"fault_domain,omitempty"` 219 220 // Specifies whether the ECS is created on a Dedicated Host (DeH) or in a shared pool. 221 Tenancy string `json:"tenancy,omitempty"` 222 223 // DedicatedHostID specifies a DeH ID. 224 DedicatedHostID string `json:"dedicated_host_id,omitempty"` 225 } 226 227 // Create requests a server to be provisioned to the user in the current tenant. 228 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) { 229 reqBody, err := opts.ToServerCreateMap() 230 if err != nil { 231 r.Err = err 232 return 233 } 234 235 _, r.Err = client.Post(createURL(client), reqBody, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}}) 236 return 237 } 238 239 // CreatePrePaid requests a server to be provisioned to the user in the current tenant. 240 func CreatePrePaid(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r OrderResult) { 241 reqBody, err := opts.ToServerCreateMap() 242 if err != nil { 243 r.Err = err 244 return 245 } 246 247 _, r.Err = client.Post(createURL(client), reqBody, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}}) 248 return 249 } 250 251 // Get retrieves a particular Server based on its unique ID. 252 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 253 _, r.Err = c.Get(getURL(c, id), &r.Body, &golangsdk.RequestOpts{ 254 OkCodes: []int{200, 203}, 255 }) 256 return 257 } 258 259 type DeleteOpts struct { 260 Servers []Server `json:"servers" required:"true"` 261 DeletePublicIP bool `json:"delete_publicip,omitempty"` 262 DeleteVolume bool `json:"delete_volume,omitempty"` 263 } 264 265 type Server struct { 266 Id string `json:"id" required:"true"` 267 } 268 269 // ToServerDeleteMap assembles a request body based on the contents of a 270 // DeleteOpts. 271 func (opts DeleteOpts) ToServerDeleteMap() (map[string]interface{}, error) { 272 return golangsdk.BuildRequestBody(opts, "") 273 } 274 275 // Delete requests a server to be deleted to the user in the current tenant. 276 func Delete(client *golangsdk.ServiceClient, opts DeleteOpts) (r JobResult) { 277 reqBody, err := opts.ToServerDeleteMap() 278 if err != nil { 279 r.Err = err 280 return 281 } 282 _, r.Err = client.Post(deleteURL(client), reqBody, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}}) 283 return 284 } 285 286 type DeleteOrderOpts struct { 287 ResourceIds []string `json:"resourceIds" required:"true"` 288 UnSubType int `json:"unSubType" required:"true"` 289 } 290 291 // ToServerDeleteOrderMap assembles a request body based on the contents of a 292 // DeleteOrderOpts. 293 func (opts DeleteOrderOpts) ToServerDeleteOrderMap() (map[string]interface{}, error) { 294 return golangsdk.BuildRequestBody(opts, "") 295 } 296 297 // DeleteOrder requests a server to be deleted to the user in the current tenant. 298 func DeleteOrder(client *golangsdk.ServiceClient, opts DeleteOrderOpts) (r DeleteOrderResult) { 299 reqBody, err := opts.ToServerDeleteOrderMap() 300 if err != nil { 301 r.Err = err 302 return 303 } 304 _, r.Err = client.Post(deleteOrderURL(client), reqBody, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}}) 305 return 306 } 307 308 // ListOptsBuilder allows extensions to add additional parameters to the 309 // List request. 310 type ListOptsBuilder interface { 311 ToServerListQuery() (string, error) 312 } 313 314 // ListOpts allows the filtering and sorting of paginated collections through 315 // the API. Filtering is achieved by passing in struct field values that map to 316 // the server attributes you want to see returned. Marker and Limit are used 317 // for pagination. 318 type ListOpts struct { 319 // Name of the server as a string; can be queried with regular expressions. 320 // Realize that ?name=bob returns both bob and bobb. If you need to match bob 321 // only, you can use a regular expression matching the syntax of the 322 // underlying database server implemented for Compute. 323 Name string `q:"name"` 324 325 // Flavor is the name of the flavor in URL format. 326 Flavor string `q:"flavor"` 327 328 // Status is the value of the status of the server so that you can filter on 329 // "ACTIVE" for example. 330 Status string `q:"status"` 331 332 // Specifies the ECS that is bound to an enterprise project. 333 EnterpriseProjectID string `q:"enterprise_project_id"` 334 335 // Indicates the filtering result for IPv4 addresses, which are fuzzy matched. 336 // These IP addresses are private IP addresses of the ECS. 337 IP string `q:"ip"` 338 339 // Specifies the maximum number of ECSs on one page. 340 // Each page contains 25 ECSs by default, and a maximum of 1000 ECSs are returned. 341 Limit int `q:"limit"` 342 343 // Specifies a page number. The default value is 1. 344 // The value must be greater than or equal to 0. If the value is 0, the first page is displayed. 345 Offset int `q:"offset"` 346 } 347 348 // ToServerListQuery formats a ListOpts into a query string. 349 func (opts ListOpts) ToServerListQuery() (string, error) { 350 q, err := golangsdk.BuildQueryString(opts) 351 return q.String(), err 352 } 353 354 // List makes a request against the API to list servers accessible to you. 355 func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 356 url := listDetailURL(client) 357 if opts != nil { 358 query, err := opts.ToServerListQuery() 359 if err != nil { 360 return pagination.Pager{Err: err} 361 } 362 url += query 363 } 364 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 365 return ServerPage{pagination.PageSizeBase{PageResult: r}} 366 }) 367 } 368 369 type ResizeOpts struct { 370 FlavorRef string `json:"flavorRef" required:"true"` 371 Mode string `json:"mode,omitempty"` 372 ExtendParam *ResizeExtendParam `json:"extendparam,omitempty"` 373 } 374 375 type ResizeExtendParam struct { 376 AutoPay string `json:"isAutoPay,omitempty"` 377 } 378 379 // ResizeOptsBuilder allows extensions to add additional parameters to the 380 // Resize request. 381 type ResizeOptsBuilder interface { 382 ToServerResizeMap() (map[string]interface{}, error) 383 } 384 385 // ToServerResizeMap assembles a request body based on the contents of a 386 // ResizeOpts. 387 func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) { 388 return golangsdk.BuildRequestBody(opts, "resize") 389 } 390 391 // Resize requests a server to be resizeed. 392 func Resize(client *golangsdk.ServiceClient, opts ResizeOptsBuilder, serverId string) (r JobResult) { 393 reqBody, err := opts.ToServerResizeMap() 394 if err != nil { 395 r.Err = err 396 return 397 } 398 399 _, r.Err = client.Post(resizeURL(client, serverId), reqBody, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}}) 400 return 401 } 402 403 // ChangeAdminPassword alters the administrator or root password for a specified 404 // server. 405 func ChangeAdminPassword(client *golangsdk.ServiceClient, id, newPassword string) (r PasswordResult) { 406 b := map[string]interface{}{ 407 "reset-password": map[string]string{ 408 "new_password": newPassword, 409 }, 410 } 411 _, r.Err = client.Put(passwordURL(client, id), b, nil, &golangsdk.RequestOpts{OkCodes: []int{204}}) 412 return 413 } 414 415 // UpdateOptsBuilder allows extensions to add additional attributes to the 416 // Update request. 417 type UpdateOptsBuilder interface { 418 ToServerUpdateMap() (map[string]interface{}, error) 419 } 420 421 // UpdateOpts specifies the base attributes that may be updated on an existing 422 // server. 423 type UpdateOpts struct { 424 Name string `json:"name,omitempty"` 425 Hostname string `json:"hostname,omitempty"` 426 UserData []byte `json:"-"` 427 Description *string `json:"description,omitempty"` 428 } 429 430 // ToServerUpdateMap formats an UpdateOpts structure into a request body. 431 func (opts UpdateOpts) ToServerUpdateMap() (map[string]interface{}, error) { 432 b, err := golangsdk.BuildRequestBody(opts, "") 433 if err != nil { 434 return nil, err 435 } 436 437 var userData string 438 if _, err := base64.StdEncoding.DecodeString(string(opts.UserData)); err != nil { 439 userData = base64.StdEncoding.EncodeToString(opts.UserData) 440 } else { 441 userData = string(opts.UserData) 442 } 443 b["user_data"] = &userData 444 445 return map[string]interface{}{"server": b}, nil 446 } 447 448 // Update requests that various attributes of the indicated server be changed. 449 func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 450 b, err := opts.ToServerUpdateMap() 451 if err != nil { 452 r.Err = err 453 return 454 } 455 _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &golangsdk.RequestOpts{ 456 OkCodes: []int{200}, 457 }) 458 return 459 } 460 461 // UpdateMetadata updates (or creates) all the metadata specified by opts for 462 // the given server ID. This operation does not affect already-existing metadata 463 // that is not specified by opts. 464 func UpdateMetadata(client *golangsdk.ServiceClient, id string, opts map[string]interface{}) (r UpdateMetadataResult) { 465 b := map[string]interface{}{"metadata": opts} 466 _, r.Err = client.Post(metadataURL(client, id), b, &r.Body, nil) 467 return 468 } 469 470 // DeleteMetadatItem will delete the key-value pair with the given key for the given server ID. 471 func DeleteMetadatItem(client *golangsdk.ServiceClient, id, key string) (r DeleteMetadatItemResult) { 472 _, r.Err = client.Delete(metadatItemURL(client, id, key), nil) 473 return 474 } 475 476 // update auto terminate time for the given server ID. 477 func UpdateAutoTerminateTime(client *golangsdk.ServiceClient, id, terminateTime string) (r UpdateResult) { 478 body := map[string]interface{}{ 479 "auto_terminate_time": terminateTime, 480 } 481 _, r.Err = client.Post(updateAutoTerminateTimeURL(client, id), body, nil, &golangsdk.RequestOpts{OkCodes: []int{204}}) 482 return 483 }