github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/endpoints/requests.go (about)

     1  package endpoints
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  type CreateOptsBuilder interface {
     9  	ToEndpointCreateMap() (map[string]interface{}, error)
    10  }
    11  
    12  // CreateOpts contains the subset of Endpoint attributes that should be used
    13  // to create an Endpoint.
    14  type CreateOpts struct {
    15  	// Availability is the interface type of the Endpoint (admin, internal,
    16  	// or public), referenced by the gophercloud.Availability type.
    17  	Availability gophercloud.Availability `json:"interface" required:"true"`
    18  
    19  	// Name is the name of the Endpoint.
    20  	Name string `json:"name" required:"true"`
    21  
    22  	// Region is the region the Endpoint is located in.
    23  	// This field can be omitted or left as a blank string.
    24  	Region string `json:"region,omitempty"`
    25  
    26  	// URL is the url of the Endpoint.
    27  	URL string `json:"url" required:"true"`
    28  
    29  	// ServiceID is the ID of the service the Endpoint refers to.
    30  	ServiceID string `json:"service_id" required:"true"`
    31  }
    32  
    33  // ToEndpointCreateMap builds a request body from the Endpoint Create options.
    34  func (opts CreateOpts) ToEndpointCreateMap() (map[string]interface{}, error) {
    35  	return gophercloud.BuildRequestBody(opts, "endpoint")
    36  }
    37  
    38  // Create inserts a new Endpoint into the service catalog.
    39  func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    40  	b, err := opts.ToEndpointCreateMap()
    41  	if err != nil {
    42  		r.Err = err
    43  		return
    44  	}
    45  	resp, err := client.Post(listURL(client), &b, &r.Body, nil)
    46  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    47  	return
    48  }
    49  
    50  // ListOptsBuilder allows extensions to add parameters to the List request.
    51  type ListOptsBuilder interface {
    52  	ToEndpointListParams() (string, error)
    53  }
    54  
    55  // ListOpts allows finer control over the endpoints returned by a List call.
    56  // All fields are optional.
    57  type ListOpts struct {
    58  	// Availability is the interface type of the Endpoint (admin, internal,
    59  	// or public), referenced by the gophercloud.Availability type.
    60  	Availability gophercloud.Availability `q:"interface"`
    61  
    62  	// ServiceID is the ID of the service the Endpoint refers to.
    63  	ServiceID string `q:"service_id"`
    64  
    65  	// RegionID is the ID of the region the Endpoint refers to.
    66  	RegionID string `q:"region_id"`
    67  }
    68  
    69  // ToEndpointListParams builds a list request from the List options.
    70  func (opts ListOpts) ToEndpointListParams() (string, error) {
    71  	q, err := gophercloud.BuildQueryString(opts)
    72  	return q.String(), err
    73  }
    74  
    75  // List enumerates endpoints in a paginated collection, optionally filtered
    76  // by ListOpts criteria.
    77  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    78  	u := listURL(client)
    79  	if opts != nil {
    80  		q, err := gophercloud.BuildQueryString(opts)
    81  		if err != nil {
    82  			return pagination.Pager{Err: err}
    83  		}
    84  		u += q.String()
    85  	}
    86  	return pagination.NewPager(client, u, func(r pagination.PageResult) pagination.Page {
    87  		return EndpointPage{pagination.LinkedPageBase{PageResult: r}}
    88  	})
    89  }
    90  
    91  // UpdateOptsBuilder allows extensions to add parameters to the Update request.
    92  type UpdateOptsBuilder interface {
    93  	ToEndpointUpdateMap() (map[string]interface{}, error)
    94  }
    95  
    96  // UpdateOpts contains the subset of Endpoint attributes that should be used to
    97  // update an Endpoint.
    98  type UpdateOpts struct {
    99  	// Availability is the interface type of the Endpoint (admin, internal,
   100  	// or public), referenced by the gophercloud.Availability type.
   101  	Availability gophercloud.Availability `json:"interface,omitempty"`
   102  
   103  	// Name is the name of the Endpoint.
   104  	Name string `json:"name,omitempty"`
   105  
   106  	// Region is the region the Endpoint is located in.
   107  	// This field can be omitted or left as a blank string.
   108  	Region string `json:"region,omitempty"`
   109  
   110  	// URL is the url of the Endpoint.
   111  	URL string `json:"url,omitempty"`
   112  
   113  	// ServiceID is the ID of the service the Endpoint refers to.
   114  	ServiceID string `json:"service_id,omitempty"`
   115  }
   116  
   117  // ToEndpointUpdateMap builds an update request body from the Update options.
   118  func (opts UpdateOpts) ToEndpointUpdateMap() (map[string]interface{}, error) {
   119  	return gophercloud.BuildRequestBody(opts, "endpoint")
   120  }
   121  
   122  // Update changes an existing endpoint with new data.
   123  func Update(client *gophercloud.ServiceClient, endpointID string, opts UpdateOptsBuilder) (r UpdateResult) {
   124  	b, err := opts.ToEndpointUpdateMap()
   125  	if err != nil {
   126  		r.Err = err
   127  		return
   128  	}
   129  	resp, err := client.Patch(endpointURL(client, endpointID), &b, &r.Body, nil)
   130  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   131  	return
   132  }
   133  
   134  // Delete removes an endpoint from the service catalog.
   135  func Delete(client *gophercloud.ServiceClient, endpointID string) (r DeleteResult) {
   136  	resp, err := client.Delete(endpointURL(client, endpointID), nil)
   137  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   138  	return
   139  }