github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/bgpvpns/requests.go (about)

     1  package bgpvpns
     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 List
    11  // request.
    12  type ListOptsBuilder interface {
    13  	ToBGPVPNListQuery() (string, error)
    14  }
    15  
    16  // ListOpts allows the filtering and sorting of paginated collections through the API.
    17  type ListOpts struct {
    18  	Fields    []string `q:"fields"`
    19  	ProjectID string   `q:"project_id"`
    20  	Networks  []string `q:"networks"`
    21  	Routers   []string `q:"routers"`
    22  	Ports     []string `q:"ports"`
    23  	Limit     int      `q:"limit"`
    24  	Marker    string   `q:"marker"`
    25  }
    26  
    27  // ToBGPVPNListQuery formats a ListOpts into a query string.
    28  func (opts ListOpts) ToBGPVPNListQuery() (string, error) {
    29  	q, err := gophercloud.BuildQueryString(opts)
    30  	if err != nil {
    31  		return "", err
    32  	}
    33  	return q.String(), nil
    34  }
    35  
    36  // List the BGP VPNs
    37  func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    38  	url := listURL(c)
    39  	query, err := opts.ToBGPVPNListQuery()
    40  	if err != nil {
    41  		return pagination.Pager{Err: err}
    42  	}
    43  	url += query
    44  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    45  		p := BGPVPNPage{pagination.MarkerPageBase{PageResult: r}}
    46  		p.MarkerPageBase.Owner = p
    47  		return p
    48  	})
    49  }
    50  
    51  // Get retrieve the specific BGP VPN by its uuid
    52  func Get(ctx context.Context, c *gophercloud.ServiceClient, id string) (r GetResult) {
    53  	resp, err := c.Get(ctx, getURL(c, id), &r.Body, nil)
    54  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    55  	return
    56  }
    57  
    58  // CreateOptsBuilder allows extensions to add additional parameters to the
    59  // Create request.
    60  type CreateOptsBuilder interface {
    61  	ToBGPVPNCreateMap() (map[string]any, error)
    62  }
    63  
    64  // CreateOpts represents options used to create a BGP VPN.
    65  type CreateOpts struct {
    66  	Name                string   `json:"name,omitempty"`
    67  	RouteDistinguishers []string `json:"route_distinguishers,omitempty"`
    68  	RouteTargets        []string `json:"route_targets,omitempty"`
    69  	ImportTargets       []string `json:"import_targets,omitempty"`
    70  	ExportTargets       []string `json:"export_targets,omitempty"`
    71  	LocalPref           int      `json:"local_pref,omitempty"`
    72  	VNI                 int      `json:"vni,omitempty"`
    73  	TenantID            string   `json:"tenant_id,omitempty"`
    74  	ProjectID           string   `json:"project_id,omitempty"`
    75  	Type                string   `json:"type,omitempty"`
    76  }
    77  
    78  // ToBGPVPNCreateMap builds a request body from CreateOpts.
    79  func (opts CreateOpts) ToBGPVPNCreateMap() (map[string]any, error) {
    80  	return gophercloud.BuildRequestBody(opts, "bgpvpn")
    81  }
    82  
    83  // Create a BGP VPN
    84  func Create(ctx context.Context, c *gophercloud.ServiceClient, opts CreateOpts) (r CreateResult) {
    85  	b, err := opts.ToBGPVPNCreateMap()
    86  	if err != nil {
    87  		r.Err = err
    88  		return
    89  	}
    90  	resp, err := c.Post(ctx, createURL(c), b, &r.Body, nil)
    91  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    92  	return
    93  }
    94  
    95  // Delete accepts a unique ID and deletes the BGP VPN associated with it.
    96  func Delete(ctx context.Context, c *gophercloud.ServiceClient, id string) (r DeleteResult) {
    97  	resp, err := c.Delete(ctx, deleteURL(c, id), nil)
    98  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    99  	return
   100  }
   101  
   102  // UpdateOptsBuilder allows extensions to add additional parameters to the
   103  // Update request.
   104  type UpdateOptsBuilder interface {
   105  	ToBGPVPNUpdateMap() (map[string]any, error)
   106  }
   107  
   108  // UpdateOpts represents options used to update a BGP VPN.
   109  type UpdateOpts struct {
   110  	Name                *string   `json:"name,omitempty"`
   111  	RouteDistinguishers *[]string `json:"route_distinguishers,omitempty"`
   112  	RouteTargets        *[]string `json:"route_targets,omitempty"`
   113  	ImportTargets       *[]string `json:"import_targets,omitempty"`
   114  	ExportTargets       *[]string `json:"export_targets,omitempty"`
   115  	LocalPref           *int      `json:"local_pref,omitempty"`
   116  }
   117  
   118  // ToBGPVPNUpdateMap builds a request body from UpdateOpts.
   119  func (opts UpdateOpts) ToBGPVPNUpdateMap() (map[string]any, error) {
   120  	return gophercloud.BuildRequestBody(opts, "bgpvpn")
   121  }
   122  
   123  // Update accept a BGP VPN ID and an UpdateOpts and update the BGP VPN
   124  func Update(ctx context.Context, c *gophercloud.ServiceClient, id string, opts UpdateOpts) (r UpdateResult) {
   125  	b, err := opts.ToBGPVPNUpdateMap()
   126  	if err != nil {
   127  		r.Err = err
   128  		return
   129  	}
   130  	resp, err := c.Put(ctx, updateURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   131  		OkCodes: []int{200},
   132  	})
   133  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   134  	return
   135  }
   136  
   137  // ListNetworkAssociationsOptsBuilder allows extensions to add additional
   138  // parameters to the ListNetworkAssociations request.
   139  type ListNetworkAssociationsOptsBuilder interface {
   140  	ToNetworkAssociationsListQuery() (string, error)
   141  }
   142  
   143  // ListNetworkAssociationsOpts allows the filtering and sorting of paginated
   144  // collections through the API.
   145  type ListNetworkAssociationsOpts struct {
   146  	Fields []string `q:"fields"`
   147  	Limit  int      `q:"limit"`
   148  	Marker string   `q:"marker"`
   149  }
   150  
   151  // ToNetworkAssociationsListQuery formats a ListNetworkAssociationsOpts into a
   152  // query string.
   153  func (opts ListNetworkAssociationsOpts) ToNetworkAssociationsListQuery() (string, error) {
   154  	q, err := gophercloud.BuildQueryString(opts)
   155  	if err != nil {
   156  		return "", err
   157  	}
   158  	return q.String(), nil
   159  }
   160  
   161  // ListNetworkAssociations pages over the network associations of a specified
   162  // BGP VPN.
   163  func ListNetworkAssociations(c *gophercloud.ServiceClient, id string, opts ListNetworkAssociationsOptsBuilder) pagination.Pager {
   164  	url := listNetworkAssociationsURL(c, id)
   165  	query, err := opts.ToNetworkAssociationsListQuery()
   166  	if err != nil {
   167  		return pagination.Pager{Err: err}
   168  	}
   169  	url += query
   170  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   171  		p := NetworkAssociationPage{pagination.MarkerPageBase{PageResult: r}}
   172  		p.MarkerPageBase.Owner = p
   173  		return p
   174  	})
   175  }
   176  
   177  // CreateNetworkAssociationOptsBuilder allows extensions to add additional
   178  // parameters to the CreateNetworkAssociation request.
   179  type CreateNetworkAssociationOptsBuilder interface {
   180  	ToNetworkAssociationCreateMap() (map[string]interface{}, error)
   181  }
   182  
   183  // CreateNetworkAssociationOpts represents options used to create a BGP VPN
   184  // network association.
   185  type CreateNetworkAssociationOpts struct {
   186  	NetworkID string `json:"network_id" required:"true"`
   187  	TenantID  string `json:"tenant_id,omitempty"`
   188  	ProjectID string `json:"project_id,omitempty"`
   189  }
   190  
   191  // ToNetworkAssociationCreateMap builds a request body from
   192  // CreateNetworkAssociationOpts.
   193  func (opts CreateNetworkAssociationOpts) ToNetworkAssociationCreateMap() (map[string]interface{}, error) {
   194  	return gophercloud.BuildRequestBody(opts, "network_association")
   195  }
   196  
   197  // CreateNetworkAssociation creates a new network association for a specified
   198  // BGP VPN.
   199  func CreateNetworkAssociation(ctx context.Context, client *gophercloud.ServiceClient, id string, opts CreateNetworkAssociationOptsBuilder) (r CreateNetworkAssociationResult) {
   200  	b, err := opts.ToNetworkAssociationCreateMap()
   201  	if err != nil {
   202  		r.Err = err
   203  		return
   204  	}
   205  	resp, err := client.Post(ctx, createNetworkAssociationURL(client, id), b, &r.Body, nil)
   206  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   207  	return
   208  }
   209  
   210  // GetNetworkAssociation retrieves a specific network association by BGP VPN id
   211  // and network association id.
   212  func GetNetworkAssociation(ctx context.Context, c *gophercloud.ServiceClient, bgpVpnID string, id string) (r GetNetworkAssociationResult) {
   213  	resp, err := c.Get(ctx, getNetworkAssociationURL(c, bgpVpnID, id), &r.Body, nil)
   214  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   215  	return
   216  }
   217  
   218  // DeleteNetworkAssociation deletes a specific network association by BGP VPN id
   219  // and network association id.
   220  func DeleteNetworkAssociation(ctx context.Context, c *gophercloud.ServiceClient, bgpVpnID string, id string) (r DeleteNetworkAssociationResult) {
   221  	resp, err := c.Delete(ctx, deleteNetworkAssociationURL(c, bgpVpnID, id), nil)
   222  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   223  	return
   224  }
   225  
   226  // ListRouterAssociationsOptsBuilder allows extensions to add additional
   227  // parameters to the ListRouterAssociations request.
   228  type ListRouterAssociationsOptsBuilder interface {
   229  	ToRouterAssociationsListQuery() (string, error)
   230  }
   231  
   232  // ListRouterAssociationsOpts allows the filtering and sorting of paginated
   233  // collections through the API.
   234  type ListRouterAssociationsOpts struct {
   235  	Fields []string `q:"fields"`
   236  	Limit  int      `q:"limit"`
   237  	Marker string   `q:"marker"`
   238  }
   239  
   240  // ToRouterAssociationsListQuery formats a ListRouterAssociationsOpts into a
   241  // query string.
   242  func (opts ListRouterAssociationsOpts) ToRouterAssociationsListQuery() (string, error) {
   243  	q, err := gophercloud.BuildQueryString(opts)
   244  	if err != nil {
   245  		return "", err
   246  	}
   247  	return q.String(), nil
   248  }
   249  
   250  // ListRouterAssociations pages over the router associations of a specified
   251  // BGP VPN.
   252  func ListRouterAssociations(c *gophercloud.ServiceClient, id string, opts ListRouterAssociationsOptsBuilder) pagination.Pager {
   253  	url := listRouterAssociationsURL(c, id)
   254  	query, err := opts.ToRouterAssociationsListQuery()
   255  	if err != nil {
   256  		return pagination.Pager{Err: err}
   257  	}
   258  	url += query
   259  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   260  		p := RouterAssociationPage{pagination.MarkerPageBase{PageResult: r}}
   261  		p.MarkerPageBase.Owner = p
   262  		return p
   263  	})
   264  }
   265  
   266  // CreateRouterAssociationOptsBuilder allows extensions to add additional
   267  // parameters to the CreateRouterAssociation request.
   268  type CreateRouterAssociationOptsBuilder interface {
   269  	ToRouterAssociationCreateMap() (map[string]interface{}, error)
   270  }
   271  
   272  // CreateRouterAssociationOpts represents options used to create a BGP VPN
   273  // router association.
   274  type CreateRouterAssociationOpts struct {
   275  	RouterID             string `json:"router_id" required:"true"`
   276  	AdvertiseExtraRoutes *bool  `json:"advertise_extra_routes,omitempty"`
   277  	TenantID             string `json:"tenant_id,omitempty"`
   278  	ProjectID            string `json:"project_id,omitempty"`
   279  }
   280  
   281  // ToRouterAssociationCreateMap builds a request body from
   282  // CreateRouterAssociationOpts.
   283  func (opts CreateRouterAssociationOpts) ToRouterAssociationCreateMap() (map[string]interface{}, error) {
   284  	return gophercloud.BuildRequestBody(opts, "router_association")
   285  }
   286  
   287  // CreateRouterAssociation creates a new router association for a specified
   288  // BGP VPN.
   289  func CreateRouterAssociation(ctx context.Context, client *gophercloud.ServiceClient, id string, opts CreateRouterAssociationOptsBuilder) (r CreateRouterAssociationResult) {
   290  	b, err := opts.ToRouterAssociationCreateMap()
   291  	if err != nil {
   292  		r.Err = err
   293  		return
   294  	}
   295  	resp, err := client.Post(ctx, createRouterAssociationURL(client, id), b, &r.Body, nil)
   296  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   297  	return
   298  }
   299  
   300  // GetRouterAssociation retrieves a specific router association by BGP VPN id
   301  // and router association id.
   302  func GetRouterAssociation(ctx context.Context, c *gophercloud.ServiceClient, bgpVpnID string, id string) (r GetRouterAssociationResult) {
   303  	resp, err := c.Get(ctx, getRouterAssociationURL(c, bgpVpnID, id), &r.Body, nil)
   304  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   305  	return
   306  }
   307  
   308  // DeleteRouterAssociation deletes a specific router association by BGP VPN id
   309  // and router association id.
   310  func DeleteRouterAssociation(ctx context.Context, c *gophercloud.ServiceClient, bgpVpnID string, id string) (r DeleteRouterAssociationResult) {
   311  	resp, err := c.Delete(ctx, deleteRouterAssociationURL(c, bgpVpnID, id), nil)
   312  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   313  	return
   314  }
   315  
   316  // UpdateRouterAssociationOptsBuilder allows extensions to add additional
   317  // parameters to the UpdateRouterAssociation request.
   318  type UpdateRouterAssociationOptsBuilder interface {
   319  	ToRouterAssociationUpdateMap() (map[string]interface{}, error)
   320  }
   321  
   322  // UpdateRouterAssociationOpts represents options used to update a BGP VPN
   323  // router association.
   324  type UpdateRouterAssociationOpts struct {
   325  	AdvertiseExtraRoutes *bool `json:"advertise_extra_routes,omitempty"`
   326  }
   327  
   328  // ToRouterAssociationUpdateMap builds a request body from
   329  // UpdateRouterAssociationOpts.
   330  func (opts UpdateRouterAssociationOpts) ToRouterAssociationUpdateMap() (map[string]interface{}, error) {
   331  	return gophercloud.BuildRequestBody(opts, "router_association")
   332  }
   333  
   334  // UpdateRouterAssociation updates a router association for a specified BGP VPN.
   335  func UpdateRouterAssociation(ctx context.Context, client *gophercloud.ServiceClient, bgpVpnID string, id string, opts UpdateRouterAssociationOptsBuilder) (r UpdateRouterAssociationResult) {
   336  	b, err := opts.ToRouterAssociationUpdateMap()
   337  	if err != nil {
   338  		r.Err = err
   339  		return
   340  	}
   341  	resp, err := client.Put(ctx, updateRouterAssociationURL(client, bgpVpnID, id), b, &r.Body, &gophercloud.RequestOpts{
   342  		OkCodes: []int{200},
   343  	})
   344  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   345  	return
   346  }
   347  
   348  // ListPortAssociationsOptsBuilder allows extensions to add additional
   349  // parameters to the ListPortAssociations request.
   350  type ListPortAssociationsOptsBuilder interface {
   351  	ToPortAssociationsListQuery() (string, error)
   352  }
   353  
   354  // ListPortAssociationsOpts allows the filtering and sorting of paginated
   355  // collections through the API.
   356  type ListPortAssociationsOpts struct {
   357  	Fields []string `q:"fields"`
   358  	Limit  int      `q:"limit"`
   359  	Marker string   `q:"marker"`
   360  }
   361  
   362  // ToPortAssociationsListQuery formats a ListPortAssociationsOpts into a
   363  // query string.
   364  func (opts ListPortAssociationsOpts) ToPortAssociationsListQuery() (string, error) {
   365  	q, err := gophercloud.BuildQueryString(opts)
   366  	if err != nil {
   367  		return "", err
   368  	}
   369  	return q.String(), nil
   370  }
   371  
   372  // ListPortAssociations pages over the port associations of a specified
   373  // BGP VPN.
   374  func ListPortAssociations(c *gophercloud.ServiceClient, id string, opts ListPortAssociationsOptsBuilder) pagination.Pager {
   375  	url := listPortAssociationsURL(c, id)
   376  	query, err := opts.ToPortAssociationsListQuery()
   377  	if err != nil {
   378  		return pagination.Pager{Err: err}
   379  	}
   380  	url += query
   381  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   382  		p := PortAssociationPage{pagination.MarkerPageBase{PageResult: r}}
   383  		p.MarkerPageBase.Owner = p
   384  		return p
   385  	})
   386  }
   387  
   388  // PortRoutes represents the routes to be advertised by a BGP VPN port
   389  type PortRoutes struct {
   390  	Type      string `json:"type" required:"true"`
   391  	Prefix    string `json:"prefix,omitempty"`
   392  	BGPVPNID  string `json:"bgpvpn_id,omitempty"`
   393  	LocalPref *int   `json:"local_pref,omitempty"`
   394  }
   395  
   396  // CreatePortAssociationOptsBuilder allows extensions to add additional
   397  // parameters to the CreatePortAssociation request.
   398  type CreatePortAssociationOptsBuilder interface {
   399  	ToPortAssociationCreateMap() (map[string]interface{}, error)
   400  }
   401  
   402  // CreatePortAssociationOpts represents options used to create a BGP VPN
   403  // port association.
   404  type CreatePortAssociationOpts struct {
   405  	PortID            string       `json:"port_id" required:"true"`
   406  	Routes            []PortRoutes `json:"routes,omitempty"`
   407  	AdvertiseFixedIPs *bool        `json:"advertise_fixed_ips,omitempty"`
   408  	TenantID          string       `json:"tenant_id,omitempty"`
   409  	ProjectID         string       `json:"project_id,omitempty"`
   410  }
   411  
   412  // ToPortAssociationCreateMap builds a request body from
   413  // CreatePortAssociationOpts.
   414  func (opts CreatePortAssociationOpts) ToPortAssociationCreateMap() (map[string]interface{}, error) {
   415  	return gophercloud.BuildRequestBody(opts, "port_association")
   416  }
   417  
   418  // CreatePortAssociation creates a new port association for a specified
   419  // BGP VPN.
   420  func CreatePortAssociation(ctx context.Context, client *gophercloud.ServiceClient, id string, opts CreatePortAssociationOptsBuilder) (r CreatePortAssociationResult) {
   421  	b, err := opts.ToPortAssociationCreateMap()
   422  	if err != nil {
   423  		r.Err = err
   424  		return
   425  	}
   426  	resp, err := client.Post(ctx, createPortAssociationURL(client, id), b, &r.Body, nil)
   427  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   428  	return
   429  }
   430  
   431  // GetPortAssociation retrieves a specific port association by BGP VPN id
   432  // and port association id.
   433  func GetPortAssociation(ctx context.Context, c *gophercloud.ServiceClient, bgpVpnID string, id string) (r GetPortAssociationResult) {
   434  	resp, err := c.Get(ctx, getPortAssociationURL(c, bgpVpnID, id), &r.Body, nil)
   435  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   436  	return
   437  }
   438  
   439  // DeletePortAssociation deletes a specific port association by BGP VPN id
   440  // and port association id.
   441  func DeletePortAssociation(ctx context.Context, c *gophercloud.ServiceClient, bgpVpnID string, id string) (r DeletePortAssociationResult) {
   442  	resp, err := c.Delete(ctx, deletePortAssociationURL(c, bgpVpnID, id), nil)
   443  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   444  	return
   445  }
   446  
   447  // UpdatePortAssociationOptsBuilder allows extensions to add additional
   448  // parameters to the UpdatePortAssociation request.
   449  type UpdatePortAssociationOptsBuilder interface {
   450  	ToPortAssociationUpdateMap() (map[string]interface{}, error)
   451  }
   452  
   453  // UpdatePortAssociationOpts represents options used to update a BGP VPN
   454  // port association.
   455  type UpdatePortAssociationOpts struct {
   456  	Routes            *[]PortRoutes `json:"routes,omitempty"`
   457  	AdvertiseFixedIPs *bool         `json:"advertise_fixed_ips,omitempty"`
   458  }
   459  
   460  // ToPortAssociationUpdateMap builds a request body from
   461  // UpdatePortAssociationOpts.
   462  func (opts UpdatePortAssociationOpts) ToPortAssociationUpdateMap() (map[string]interface{}, error) {
   463  	return gophercloud.BuildRequestBody(opts, "port_association")
   464  }
   465  
   466  // UpdatePortAssociation updates a port association for a specified BGP VPN.
   467  func UpdatePortAssociation(ctx context.Context, client *gophercloud.ServiceClient, bgpVpnID string, id string, opts UpdatePortAssociationOptsBuilder) (r UpdatePortAssociationResult) {
   468  	b, err := opts.ToPortAssociationUpdateMap()
   469  	if err != nil {
   470  		r.Err = err
   471  		return
   472  	}
   473  	resp, err := client.Put(ctx, updatePortAssociationURL(client, bgpVpnID, id), b, &r.Body, &gophercloud.RequestOpts{
   474  		OkCodes: []int{200},
   475  	})
   476  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   477  	return
   478  }