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

     1  package services
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // ListOptsBuilder allows extensions to add additional parameters to
     9  // the List request.
    10  type ListOptsBuilder interface {
    11  	ToServicesListQuery() (string, error)
    12  }
    13  
    14  // ListOpts represents options to list services.
    15  type ListOpts struct {
    16  	Binary string `q:"binary"`
    17  	Host   string `q:"host"`
    18  }
    19  
    20  // ToServicesListQuery formats a ListOpts into a query string.
    21  func (opts ListOpts) ToServicesListQuery() (string, error) {
    22  	q, err := gophercloud.BuildQueryString(opts)
    23  	return q.String(), err
    24  }
    25  
    26  // List makes a request against the API to list services.
    27  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    28  	url := listURL(client)
    29  	if opts != nil {
    30  		query, err := opts.ToServicesListQuery()
    31  		if err != nil {
    32  			return pagination.Pager{Err: err}
    33  		}
    34  		url += query
    35  	}
    36  
    37  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    38  		return ServicePage{pagination.SinglePageBase(r)}
    39  	})
    40  }
    41  
    42  type ServiceStatus string
    43  
    44  const (
    45  	// ServiceEnabled is used to mark a service as being enabled.
    46  	ServiceEnabled ServiceStatus = "enabled"
    47  
    48  	// ServiceDisabled is used to mark a service as being disabled.
    49  	ServiceDisabled ServiceStatus = "disabled"
    50  )
    51  
    52  // UpdateOpts specifies the base attributes that may be updated on a service.
    53  type UpdateOpts struct {
    54  	// Status represents the new service status. One of enabled or disabled.
    55  	Status ServiceStatus `json:"status,omitempty"`
    56  
    57  	// DisabledReason represents the reason for disabling a service.
    58  	DisabledReason string `json:"disabled_reason,omitempty"`
    59  
    60  	// ForcedDown is a manual override to tell nova that the service in question
    61  	// has been fenced manually by the operations team.
    62  	ForcedDown bool `json:"forced_down,omitempty"`
    63  }
    64  
    65  // ToServiceUpdateMap formats an UpdateOpts structure into a request body.
    66  func (opts UpdateOpts) ToServiceUpdateMap() (map[string]interface{}, error) {
    67  	return gophercloud.BuildRequestBody(opts, "")
    68  }
    69  
    70  // Update requests that various attributes of the indicated service be changed.
    71  func Update(client *gophercloud.ServiceClient, id string, opts UpdateOpts) (r UpdateResult) {
    72  	b, err := opts.ToServiceUpdateMap()
    73  	if err != nil {
    74  		r.Err = err
    75  		return
    76  	}
    77  	resp, err := client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
    78  		OkCodes: []int{200},
    79  	})
    80  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    81  	return
    82  }
    83  
    84  // Delete will delete the existing service with the provided ID.
    85  func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
    86  	resp, err := client.Delete(updateURL(client, id), &gophercloud.RequestOpts{
    87  		OkCodes: []int{204},
    88  	})
    89  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    90  	return
    91  }