github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/compute/v2/services/requests.go (about)

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