github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/bgp/speakers/requests.go (about) 1 package speakers 2 3 import ( 4 "context" 5 6 "github.com/vnpaycloud-console/gophercloud/v2" 7 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 8 ) 9 10 // List the bgp speakers 11 func List(c *gophercloud.ServiceClient) pagination.Pager { 12 url := listURL(c) 13 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 14 return BGPSpeakerPage{pagination.SinglePageBase(r)} 15 }) 16 } 17 18 // Get retrieve the specific bgp speaker by its uuid 19 func Get(ctx context.Context, c *gophercloud.ServiceClient, id string) (r GetResult) { 20 resp, err := c.Get(ctx, getURL(c, id), &r.Body, nil) 21 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 22 return 23 } 24 25 // CreateOpts represents options used to create a BGP Speaker. 26 type CreateOpts struct { 27 Name string `json:"name"` 28 IPVersion int `json:"ip_version"` 29 AdvertiseFloatingIPHostRoutes bool `json:"advertise_floating_ip_host_routes"` 30 AdvertiseTenantNetworks bool `json:"advertise_tenant_networks"` 31 LocalAS string `json:"local_as"` 32 Networks []string `json:"networks,omitempty"` 33 } 34 35 // CreateOptsBuilder declare a function that build CreateOpts into a Create request body. 36 type CreateOptsBuilder interface { 37 ToSpeakerCreateMap() (map[string]any, error) 38 } 39 40 // ToSpeakerCreateMap builds a request body from CreateOpts. 41 func (opts CreateOpts) ToSpeakerCreateMap() (map[string]any, error) { 42 return gophercloud.BuildRequestBody(opts, jroot) 43 } 44 45 // Create accepts a CreateOpts and create a BGP Speaker. 46 func Create(ctx context.Context, c *gophercloud.ServiceClient, opts CreateOpts) (r CreateResult) { 47 b, err := opts.ToSpeakerCreateMap() 48 if err != nil { 49 r.Err = err 50 return 51 } 52 resp, err := c.Post(ctx, createURL(c), b, &r.Body, nil) 53 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 54 return 55 } 56 57 // Delete accepts a unique ID and deletes the bgp speaker associated with it. 58 func Delete(ctx context.Context, c *gophercloud.ServiceClient, speakerID string) (r DeleteResult) { 59 resp, err := c.Delete(ctx, deleteURL(c, speakerID), nil) 60 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 61 return 62 } 63 64 // UpdateOpts represents options used to update a BGP Speaker. 65 type UpdateOpts struct { 66 Name string `json:"name,omitempty"` 67 AdvertiseFloatingIPHostRoutes bool `json:"advertise_floating_ip_host_routes"` 68 AdvertiseTenantNetworks bool `json:"advertise_tenant_networks"` 69 } 70 71 // ToSpeakerUpdateMap build a request body from UpdateOpts 72 func (opts UpdateOpts) ToSpeakerUpdateMap() (map[string]any, error) { 73 return gophercloud.BuildRequestBody(opts, jroot) 74 } 75 76 // UpdateOptsBuilder allow the extensions to add additional parameters to the 77 // Update request. 78 type UpdateOptsBuilder interface { 79 ToSpeakerUpdateMap() (map[string]any, error) 80 } 81 82 // Update accepts a UpdateOpts and update the BGP Speaker. 83 func Update(ctx context.Context, c *gophercloud.ServiceClient, speakerID string, opts UpdateOptsBuilder) (r UpdateResult) { 84 b, err := opts.ToSpeakerUpdateMap() 85 if err != nil { 86 r.Err = err 87 return 88 } 89 resp, err := c.Put(ctx, updateURL(c, speakerID), b, &r.Body, &gophercloud.RequestOpts{ 90 OkCodes: []int{200}, 91 }) 92 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 93 return 94 } 95 96 // AddBGPPeerOpts represents options used to add a BGP Peer to a BGP Speaker 97 type AddBGPPeerOpts struct { 98 BGPPeerID string `json:"bgp_peer_id"` 99 } 100 101 // AddBGPPeerOptsBuilder declare a funtion that encode AddBGPPeerOpts into a request body 102 type AddBGPPeerOptsBuilder interface { 103 ToBGPSpeakerAddBGPPeerMap() (map[string]any, error) 104 } 105 106 // ToBGPSpeakerAddBGPPeerMap build a request body from AddBGPPeerOpts 107 func (opts AddBGPPeerOpts) ToBGPSpeakerAddBGPPeerMap() (map[string]any, error) { 108 return gophercloud.BuildRequestBody(opts, "") 109 } 110 111 // AddBGPPeer add the BGP peer to the speaker a.k.a. PUT /v2.0/bgp-speakers/{bgp-speaker-id}/add_bgp_peer 112 func AddBGPPeer(ctx context.Context, c *gophercloud.ServiceClient, bgpSpeakerID string, opts AddBGPPeerOptsBuilder) (r AddBGPPeerResult) { 113 b, err := opts.ToBGPSpeakerAddBGPPeerMap() 114 if err != nil { 115 r.Err = err 116 return 117 } 118 resp, err := c.Put(ctx, addBGPPeerURL(c, bgpSpeakerID), b, &r.Body, &gophercloud.RequestOpts{ 119 OkCodes: []int{200}, 120 }) 121 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 122 return 123 } 124 125 // RemoveBGPPeerOpts represents options used to remove a BGP Peer to a BGP Speaker 126 type RemoveBGPPeerOpts AddBGPPeerOpts 127 128 // RemoveBGPPeerOptsBuilder declare a funtion that encode RemoveBGPPeerOpts into a request body 129 type RemoveBGPPeerOptsBuilder interface { 130 ToBGPSpeakerRemoveBGPPeerMap() (map[string]any, error) 131 } 132 133 // ToBGPSpeakerRemoveBGPPeerMap build a request body from RemoveBGPPeerOpts 134 func (opts RemoveBGPPeerOpts) ToBGPSpeakerRemoveBGPPeerMap() (map[string]any, error) { 135 return gophercloud.BuildRequestBody(opts, "") 136 } 137 138 // RemoveBGPPeer remove the BGP peer from the speaker, a.k.a. PUT /v2.0/bgp-speakers/{bgp-speaker-id}/add_bgp_peer 139 func RemoveBGPPeer(ctx context.Context, c *gophercloud.ServiceClient, bgpSpeakerID string, opts RemoveBGPPeerOptsBuilder) (r RemoveBGPPeerResult) { 140 b, err := opts.ToBGPSpeakerRemoveBGPPeerMap() 141 if err != nil { 142 r.Err = err 143 return 144 } 145 resp, err := c.Put(ctx, removeBGPPeerURL(c, bgpSpeakerID), b, nil, &gophercloud.RequestOpts{ 146 OkCodes: []int{200}, 147 }) 148 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 149 return 150 } 151 152 // GetAdvertisedRoutes a.k.a. GET /v2.0/bgp-speakers/{bgp-speaker-id}/get_advertised_routes 153 func GetAdvertisedRoutes(c *gophercloud.ServiceClient, bgpSpeakerID string) pagination.Pager { 154 url := getAdvertisedRoutesURL(c, bgpSpeakerID) 155 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 156 return AdvertisedRoutePage{pagination.SinglePageBase(r)} 157 }) 158 } 159 160 // AddGatewayNetworkOptsBuilder declare a function that build AddGatewayNetworkOpts into a request body. 161 type AddGatewayNetworkOptsBuilder interface { 162 ToBGPSpeakerAddGatewayNetworkMap() (map[string]any, error) 163 } 164 165 // AddGatewayNetworkOpts represents the data that would be PUT to the endpoint 166 type AddGatewayNetworkOpts struct { 167 // The uuid of the network 168 NetworkID string `json:"network_id"` 169 } 170 171 // ToBGPSpeakerAddGatewayNetworkMap implements the function 172 func (opts AddGatewayNetworkOpts) ToBGPSpeakerAddGatewayNetworkMap() (map[string]any, error) { 173 return gophercloud.BuildRequestBody(opts, "") 174 } 175 176 // AddGatewayNetwork a.k.a. PUT /v2.0/bgp-speakers/{bgp-speaker-id}/add_gateway_network 177 func AddGatewayNetwork(ctx context.Context, c *gophercloud.ServiceClient, bgpSpeakerID string, opts AddGatewayNetworkOptsBuilder) (r AddGatewayNetworkResult) { 178 b, err := opts.ToBGPSpeakerAddGatewayNetworkMap() 179 if err != nil { 180 r.Err = err 181 return 182 } 183 resp, err := c.Put(ctx, addGatewayNetworkURL(c, bgpSpeakerID), b, &r.Body, &gophercloud.RequestOpts{ 184 OkCodes: []int{200}, 185 }) 186 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 187 return 188 } 189 190 // RemoveGatewayNetworkOptsBuilder declare a function that build RemoveGatewayNetworkOpts into a request body. 191 type RemoveGatewayNetworkOptsBuilder interface { 192 ToBGPSpeakerRemoveGatewayNetworkMap() (map[string]any, error) 193 } 194 195 // RemoveGatewayNetworkOpts represent the data that would be PUT to the endpoint 196 type RemoveGatewayNetworkOpts AddGatewayNetworkOpts 197 198 // ToBGPSpeakerRemoveGatewayNetworkMap implement the function 199 func (opts RemoveGatewayNetworkOpts) ToBGPSpeakerRemoveGatewayNetworkMap() (map[string]any, error) { 200 return gophercloud.BuildRequestBody(opts, "") 201 } 202 203 // RemoveGatewayNetwork a.k.a. PUT /v2.0/bgp-speakers/{bgp-speaker-id}/remove_gateway_network 204 func RemoveGatewayNetwork(ctx context.Context, c *gophercloud.ServiceClient, bgpSpeakerID string, opts RemoveGatewayNetworkOptsBuilder) (r RemoveGatewayNetworkResult) { 205 b, err := opts.ToBGPSpeakerRemoveGatewayNetworkMap() 206 if err != nil { 207 r.Err = err 208 return 209 } 210 resp, err := c.Put(ctx, removeGatewayNetworkURL(c, bgpSpeakerID), b, nil, &gophercloud.RequestOpts{ 211 OkCodes: []int{200}, 212 }) 213 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 214 return 215 }