github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/layer3/floatingips/requests.go (about) 1 package floatingips 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6 ) 7 8 // ListOptsBuilder allows extensions to add additional parameters to the 9 // List request. 10 type ListOptsBuilder interface { 11 ToFloatingIPListQuery() (string, error) 12 } 13 14 // ListOpts allows the filtering and sorting of paginated collections through 15 // the API. Filtering is achieved by passing in struct field values that map to 16 // the floating IP attributes you want to see returned. SortKey allows you to 17 // sort by a particular network attribute. SortDir sets the direction, and is 18 // either `asc' or `desc'. Marker and Limit are used for pagination. 19 type ListOpts struct { 20 ID string `q:"id"` 21 Description string `q:"description"` 22 FloatingNetworkID string `q:"floating_network_id"` 23 PortID string `q:"port_id"` 24 FixedIP string `q:"fixed_ip_address"` 25 FloatingIP string `q:"floating_ip_address"` 26 TenantID string `q:"tenant_id"` 27 ProjectID string `q:"project_id"` 28 Limit int `q:"limit"` 29 Marker string `q:"marker"` 30 SortKey string `q:"sort_key"` 31 SortDir string `q:"sort_dir"` 32 RouterID string `q:"router_id"` 33 Status string `q:"status"` 34 Tags string `q:"tags"` 35 TagsAny string `q:"tags-any"` 36 NotTags string `q:"not-tags"` 37 NotTagsAny string `q:"not-tags-any"` 38 } 39 40 // ToNetworkListQuery formats a ListOpts into a query string. 41 func (opts ListOpts) ToFloatingIPListQuery() (string, error) { 42 q, err := gophercloud.BuildQueryString(opts) 43 return q.String(), err 44 } 45 46 // List returns a Pager which allows you to iterate over a collection of 47 // floating IP resources. It accepts a ListOpts struct, which allows you to 48 // filter and sort the returned collection for greater efficiency. 49 func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 50 url := rootURL(c) 51 if opts != nil { 52 query, err := opts.ToFloatingIPListQuery() 53 if err != nil { 54 return pagination.Pager{Err: err} 55 } 56 url += query 57 } 58 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 59 return FloatingIPPage{pagination.LinkedPageBase{PageResult: r}} 60 }) 61 } 62 63 // CreateOptsBuilder allows extensions to add additional parameters to the 64 // Create request. 65 type CreateOptsBuilder interface { 66 ToFloatingIPCreateMap() (map[string]interface{}, error) 67 } 68 69 // CreateOpts contains all the values needed to create a new floating IP 70 // resource. The only required fields are FloatingNetworkID and PortID which 71 // refer to the external network and internal port respectively. 72 type CreateOpts struct { 73 Description string `json:"description,omitempty"` 74 FloatingNetworkID string `json:"floating_network_id" required:"true"` 75 FloatingIP string `json:"floating_ip_address,omitempty"` 76 PortID string `json:"port_id,omitempty"` 77 FixedIP string `json:"fixed_ip_address,omitempty"` 78 SubnetID string `json:"subnet_id,omitempty"` 79 TenantID string `json:"tenant_id,omitempty"` 80 ProjectID string `json:"project_id,omitempty"` 81 } 82 83 // ToFloatingIPCreateMap allows CreateOpts to satisfy the CreateOptsBuilder 84 // interface 85 func (opts CreateOpts) ToFloatingIPCreateMap() (map[string]interface{}, error) { 86 return gophercloud.BuildRequestBody(opts, "floatingip") 87 } 88 89 // Create accepts a CreateOpts struct and uses the values provided to create a 90 // new floating IP resource. You can create floating IPs on external networks 91 // only. If you provide a FloatingNetworkID which refers to a network that is 92 // not external (i.e. its `router:external' attribute is False), the operation 93 // will fail and return a 400 error. 94 // 95 // If you do not specify a FloatingIP address value, the operation will 96 // automatically allocate an available address for the new resource. If you do 97 // choose to specify one, it must fall within the subnet range for the external 98 // network - otherwise the operation returns a 400 error. If the FloatingIP 99 // address is already in use, the operation returns a 409 error code. 100 // 101 // You can associate the new resource with an internal port by using the PortID 102 // field. If you specify a PortID that is not valid, the operation will fail and 103 // return 404 error code. 104 // 105 // You must also configure an IP address for the port associated with the PortID 106 // you have provided - this is what the FixedIP refers to: an IP fixed to a 107 // port. Because a port might be associated with multiple IP addresses, you can 108 // use the FixedIP field to associate a particular IP address rather than have 109 // the API assume for you. If you specify an IP address that is not valid, the 110 // operation will fail and return a 400 error code. If the PortID and FixedIP 111 // are already associated with another resource, the operation will fail and 112 // returns a 409 error code. 113 func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 114 b, err := opts.ToFloatingIPCreateMap() 115 if err != nil { 116 r.Err = err 117 return 118 } 119 resp, err := c.Post(rootURL(c), b, &r.Body, nil) 120 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 121 return 122 } 123 124 // Get retrieves a particular floating IP resource based on its unique ID. 125 func Get(c *gophercloud.ServiceClient, id string) (r GetResult) { 126 resp, err := c.Get(resourceURL(c, id), &r.Body, nil) 127 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 128 return 129 } 130 131 // UpdateOptsBuilder allows extensions to add additional parameters to the 132 // Update request. 133 type UpdateOptsBuilder interface { 134 ToFloatingIPUpdateMap() (map[string]interface{}, error) 135 } 136 137 // UpdateOpts contains the values used when updating a floating IP resource. The 138 // only value that can be updated is which internal port the floating IP is 139 // linked to. To associate the floating IP with a new internal port, provide its 140 // ID. To disassociate the floating IP from all ports, provide an empty string. 141 type UpdateOpts struct { 142 Description *string `json:"description,omitempty"` 143 PortID *string `json:"port_id,omitempty"` 144 FixedIP string `json:"fixed_ip_address,omitempty"` 145 } 146 147 // ToFloatingIPUpdateMap allows UpdateOpts to satisfy the UpdateOptsBuilder 148 // interface 149 func (opts UpdateOpts) ToFloatingIPUpdateMap() (map[string]interface{}, error) { 150 b, err := gophercloud.BuildRequestBody(opts, "floatingip") 151 if err != nil { 152 return nil, err 153 } 154 155 if m := b["floatingip"].(map[string]interface{}); m["port_id"] == "" { 156 m["port_id"] = nil 157 } 158 159 return b, nil 160 } 161 162 // Update allows floating IP resources to be updated. Currently, the only way to 163 // "update" a floating IP is to associate it with a new internal port, or 164 // disassociated it from all ports. See UpdateOpts for instructions of how to 165 // do this. 166 func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 167 b, err := opts.ToFloatingIPUpdateMap() 168 if err != nil { 169 r.Err = err 170 return 171 } 172 resp, err := c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{ 173 OkCodes: []int{200}, 174 }) 175 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 176 return 177 } 178 179 // Delete will permanently delete a particular floating IP resource. Please 180 // ensure this is what you want - you can also disassociate the IP from existing 181 // internal ports. 182 func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) { 183 resp, err := c.Delete(resourceURL(c, id), nil) 184 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 185 return 186 }