github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/compute/v2/servergroups/requests.go (about) 1 package servergroups 2 3 import ( 4 "context" 5 6 "github.com/vnpaycloud-console/gophercloud/v2" 7 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 8 ) 9 10 type ListOptsBuilder interface { 11 ToServerListQuery() (string, error) 12 } 13 14 type ListOpts struct { 15 // AllProjects is a bool to show all projects. 16 AllProjects bool `q:"all_projects"` 17 18 // Requests a page size of items. 19 Limit int `q:"limit"` 20 21 // Used in conjunction with limit to return a slice of items. 22 Offset int `q:"offset"` 23 } 24 25 // ToServerListQuery formats a ListOpts into a query string. 26 func (opts ListOpts) ToServerListQuery() (string, error) { 27 q, err := gophercloud.BuildQueryString(opts) 28 return q.String(), err 29 } 30 31 // List returns a Pager that allows you to iterate over a collection of 32 // ServerGroups. 33 func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 34 url := listURL(client) 35 if opts != nil { 36 query, err := opts.ToServerListQuery() 37 if err != nil { 38 return pagination.Pager{Err: err} 39 } 40 url += query 41 } 42 43 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 44 return ServerGroupPage{pagination.SinglePageBase(r)} 45 }) 46 } 47 48 // CreateOptsBuilder allows extensions to add additional parameters to the 49 // Create request. 50 type CreateOptsBuilder interface { 51 ToServerGroupCreateMap() (map[string]any, error) 52 } 53 54 // CreateOpts specifies Server Group creation parameters. 55 type CreateOpts struct { 56 // Name is the name of the server group. 57 Name string `json:"name" required:"true"` 58 59 // Policies are the server group policies. 60 Policies []string `json:"policies,omitempty"` 61 62 // Policy specifies the name of a policy. 63 // Requires microversion 2.64 or later. 64 Policy string `json:"policy,omitempty"` 65 66 // Rules specifies the set of rules. 67 // Requires microversion 2.64 or later. 68 Rules *Rules `json:"rules,omitempty"` 69 } 70 71 // ToServerGroupCreateMap constructs a request body from CreateOpts. 72 func (opts CreateOpts) ToServerGroupCreateMap() (map[string]any, error) { 73 return gophercloud.BuildRequestBody(opts, "server_group") 74 } 75 76 // Create requests the creation of a new Server Group. 77 func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 78 b, err := opts.ToServerGroupCreateMap() 79 if err != nil { 80 r.Err = err 81 return 82 } 83 resp, err := client.Post(ctx, createURL(client), b, &r.Body, &gophercloud.RequestOpts{ 84 OkCodes: []int{200}, 85 }) 86 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 87 return 88 } 89 90 // Get returns data about a previously created ServerGroup. 91 func Get(ctx context.Context, client *gophercloud.ServiceClient, id string) (r GetResult) { 92 resp, err := client.Get(ctx, getURL(client, id), &r.Body, nil) 93 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 94 return 95 } 96 97 // Delete requests the deletion of a previously allocated ServerGroup. 98 func Delete(ctx context.Context, client *gophercloud.ServiceClient, id string) (r DeleteResult) { 99 resp, err := client.Delete(ctx, deleteURL(client, id), nil) 100 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 101 return 102 }