github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/layer3/portforwarding/requests.go (about) 1 package portforwarding 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6 ) 7 8 type ListOptsBuilder interface { 9 ToPortForwardingListQuery() (string, error) 10 } 11 12 // ListOpts allows the filtering and sorting of paginated collections through 13 // the API. Filtering is achieved by passing in struct field values that map to 14 // the port forwarding attributes you want to see returned. SortKey allows you to 15 // sort by a particular network attribute. SortDir sets the direction, and is 16 // either `asc' or `desc'. Marker and Limit are used for pagination. 17 type ListOpts struct { 18 ID string `q:"id"` 19 InternalPortID string `q:"internal_port_id"` 20 ExternalPort string `q:"external_port"` 21 InternalIPAddress string `q:"internal_ip_address"` 22 Protocol string `q:"protocol"` 23 InternalPort string `q:"internal_port"` 24 SortKey string `q:"sort_key"` 25 SortDir string `q:"sort_dir"` 26 Fields string `q:"fields"` 27 Limit int `q:"limit"` 28 Marker string `q:"marker"` 29 } 30 31 // ToPortForwardingListQuery formats a ListOpts into a query string. 32 func (opts ListOpts) ToPortForwardingListQuery() (string, error) { 33 q, err := gophercloud.BuildQueryString(opts) 34 return q.String(), err 35 } 36 37 // List returns a Pager which allows you to iterate over a collection of 38 // Port Forwarding resources. It accepts a ListOpts struct, which allows you to 39 // filter and sort the returned collection for greater efficiency. 40 func List(c *gophercloud.ServiceClient, opts ListOptsBuilder, id string) pagination.Pager { 41 url := portForwardingUrl(c, id) 42 if opts != nil { 43 query, err := opts.ToPortForwardingListQuery() 44 if err != nil { 45 return pagination.Pager{Err: err} 46 } 47 url += query 48 } 49 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 50 return PortForwardingPage{pagination.LinkedPageBase{PageResult: r}} 51 }) 52 } 53 54 // Get retrieves a particular port forwarding resource based on its unique ID. 55 func Get(c *gophercloud.ServiceClient, floatingIpId string, pfId string) (r GetResult) { 56 resp, err := c.Get(singlePortForwardingUrl(c, floatingIpId, pfId), &r.Body, nil) 57 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 58 return 59 } 60 61 // CreateOpts contains all the values needed to create a new port forwarding 62 // resource. All attributes are required. 63 type CreateOpts struct { 64 InternalPortID string `json:"internal_port_id"` 65 InternalIPAddress string `json:"internal_ip_address"` 66 InternalPort int `json:"internal_port"` 67 ExternalPort int `json:"external_port"` 68 Protocol string `json:"protocol"` 69 } 70 71 // CreateOptsBuilder allows extensions to add additional parameters to the 72 // Create request. 73 type CreateOptsBuilder interface { 74 ToPortForwardingCreateMap() (map[string]interface{}, error) 75 } 76 77 // ToPortForwardingCreateMap allows CreateOpts to satisfy the CreateOptsBuilder 78 // interface 79 func (opts CreateOpts) ToPortForwardingCreateMap() (map[string]interface{}, error) { 80 return gophercloud.BuildRequestBody(opts, "port_forwarding") 81 } 82 83 // Create accepts a CreateOpts struct and uses the values provided to create a 84 // new port forwarding for an existing floating IP. 85 func Create(c *gophercloud.ServiceClient, floatingIpId string, opts CreateOptsBuilder) (r CreateResult) { 86 b, err := opts.ToPortForwardingCreateMap() 87 if err != nil { 88 r.Err = err 89 return 90 } 91 resp, err := c.Post(portForwardingUrl(c, floatingIpId), b, &r.Body, nil) 92 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 93 return 94 } 95 96 // UpdateOpts contains the values used when updating a port forwarding resource. 97 type UpdateOpts struct { 98 InternalPortID string `json:"internal_port_id,omitempty"` 99 InternalIPAddress string `json:"internal_ip_address,omitempty"` 100 InternalPort int `json:"internal_port,omitempty"` 101 ExternalPort int `json:"external_port,omitempty"` 102 Protocol string `json:"protocol,omitempty"` 103 } 104 105 // ToPortForwardingUpdateMap allows UpdateOpts to satisfy the UpdateOptsBuilder 106 // interface 107 func (opts UpdateOpts) ToPortForwardingUpdateMap() (map[string]interface{}, error) { 108 b, err := gophercloud.BuildRequestBody(opts, "port_forwarding") 109 if err != nil { 110 return nil, err 111 } 112 113 return b, nil 114 } 115 116 // UpdateOptsBuilder allows extensions to add additional parameters to the 117 // Update request. 118 type UpdateOptsBuilder interface { 119 ToPortForwardingUpdateMap() (map[string]interface{}, error) 120 } 121 122 // Update allows port forwarding resources to be updated. 123 func Update(c *gophercloud.ServiceClient, fipID string, pfID string, opts UpdateOptsBuilder) (r UpdateResult) { 124 b, err := opts.ToPortForwardingUpdateMap() 125 if err != nil { 126 r.Err = err 127 return 128 } 129 resp, err := c.Put(singlePortForwardingUrl(c, fipID, pfID), b, &r.Body, &gophercloud.RequestOpts{ 130 OkCodes: []int{200}, 131 }) 132 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 133 return 134 } 135 136 // Delete will permanently delete a particular port forwarding for a given floating ID. 137 func Delete(c *gophercloud.ServiceClient, floatingIpId string, pfId string) (r DeleteResult) { 138 resp, err := c.Delete(singlePortForwardingUrl(c, floatingIpId, pfId), nil) 139 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 140 return 141 }