github.com/gophercloud/gophercloud@v1.11.0/openstack/compute/v2/extensions/servergroups/requests.go (about)

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