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

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