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