github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/vpnaas/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 // CreateOptsBuilder allows extensions to add additional parameters to the 11 // Create request. 12 type CreateOptsBuilder interface { 13 ToServiceCreateMap() (map[string]any, error) 14 } 15 16 // CreateOpts contains all the values needed to create a new VPN service 17 type CreateOpts struct { 18 // TenantID specifies a tenant to own the VPN service. The caller must have 19 // an admin role in order to set this. Otherwise, this field is left unset 20 // and the caller will be the owner. 21 TenantID string `json:"tenant_id,omitempty"` 22 23 // SubnetID is the ID of the subnet. 24 SubnetID string `json:"subnet_id,omitempty"` 25 26 // RouterID is the ID of the router. 27 RouterID string `json:"router_id" required:"true"` 28 29 // Description is the human readable description of the service. 30 Description string `json:"description,omitempty"` 31 32 // AdminStateUp is the administrative state of the resource, which is up (true) or down (false). 33 AdminStateUp *bool `json:"admin_state_up"` 34 35 // Name is the human readable name of the service. 36 Name string `json:"name,omitempty"` 37 38 // The ID of the flavor. 39 FlavorID string `json:"flavor_id,omitempty"` 40 } 41 42 // ToServiceCreateMap casts a CreateOpts struct to a map. 43 func (opts CreateOpts) ToServiceCreateMap() (map[string]any, error) { 44 return gophercloud.BuildRequestBody(opts, "vpnservice") 45 } 46 47 // Create accepts a CreateOpts struct and uses the values to create a new 48 // VPN service. 49 func Create(ctx context.Context, c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 50 b, err := opts.ToServiceCreateMap() 51 if err != nil { 52 r.Err = err 53 return 54 } 55 resp, err := c.Post(ctx, rootURL(c), b, &r.Body, nil) 56 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 57 return 58 } 59 60 // Delete will permanently delete a particular VPN service based on its 61 // unique ID. 62 func Delete(ctx context.Context, c *gophercloud.ServiceClient, id string) (r DeleteResult) { 63 resp, err := c.Delete(ctx, resourceURL(c, id), nil) 64 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 65 return 66 } 67 68 // UpdateOptsBuilder allows extensions to add additional parameters to the 69 // Update request. 70 type UpdateOptsBuilder interface { 71 ToServiceUpdateMap() (map[string]any, error) 72 } 73 74 // UpdateOpts contains the values used when updating a VPN service 75 type UpdateOpts struct { 76 // Name is the human readable name of the service. 77 Name *string `json:"name,omitempty"` 78 79 // Description is the human readable description of the service. 80 Description *string `json:"description,omitempty"` 81 82 // AdminStateUp is the administrative state of the resource, which is up (true) or down (false). 83 AdminStateUp *bool `json:"admin_state_up,omitempty"` 84 } 85 86 // ToServiceUpdateMap casts aa UodateOpts struct to a map. 87 func (opts UpdateOpts) ToServiceUpdateMap() (map[string]any, error) { 88 return gophercloud.BuildRequestBody(opts, "vpnservice") 89 } 90 91 // Update allows VPN services to be updated. 92 func Update(ctx context.Context, c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 93 b, err := opts.ToServiceUpdateMap() 94 if err != nil { 95 r.Err = err 96 return 97 } 98 resp, err := c.Put(ctx, resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{ 99 OkCodes: []int{200}, 100 }) 101 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 102 return 103 } 104 105 // ListOptsBuilder allows extensions to add additional parameters to the 106 // List request. 107 type ListOptsBuilder interface { 108 ToServiceListQuery() (string, error) 109 } 110 111 // ListOpts allows the filtering and sorting of paginated collections through 112 // the API. Filtering is achieved by passing in struct field values that map to 113 // the VPN service attributes you want to see returned. 114 type ListOpts struct { 115 TenantID string `q:"tenant_id"` 116 Name string `q:"name"` 117 Description string `q:"description"` 118 AdminStateUp *bool `q:"admin_state_up"` 119 Status string `q:"status"` 120 SubnetID string `q:"subnet_id"` 121 RouterID string `q:"router_id"` 122 ProjectID string `q:"project_id"` 123 ExternalV6IP string `q:"external_v6_ip"` 124 ExternalV4IP string `q:"external_v4_ip"` 125 FlavorID string `q:"flavor_id"` 126 } 127 128 // ToServiceListQuery formats a ListOpts into a query string. 129 func (opts ListOpts) ToServiceListQuery() (string, error) { 130 q, err := gophercloud.BuildQueryString(opts) 131 return q.String(), err 132 } 133 134 // List returns a Pager which allows you to iterate over a collection of 135 // VPN services. It accepts a ListOpts struct, which allows you to filter 136 // and sort the returned collection for greater efficiency. 137 func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 138 url := rootURL(c) 139 if opts != nil { 140 query, err := opts.ToServiceListQuery() 141 if err != nil { 142 return pagination.Pager{Err: err} 143 } 144 url += query 145 } 146 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 147 return ServicePage{pagination.LinkedPageBase{PageResult: r}} 148 }) 149 } 150 151 // Get retrieves a particular VPN service based on its unique ID. 152 func Get(ctx context.Context, c *gophercloud.ServiceClient, id string) (r GetResult) { 153 resp, err := c.Get(ctx, resourceURL(c, id), &r.Body, nil) 154 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 155 return 156 }