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