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

     1  package siteconnections
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     8  )
     9  
    10  // CreateOptsBuilder allows extensions to add additional parameters to the
    11  // Create request.
    12  type CreateOptsBuilder interface {
    13  	ToConnectionCreateMap() (map[string]any, error)
    14  }
    15  type Action string
    16  type Initiator string
    17  
    18  const (
    19  	ActionHold             Action    = "hold"
    20  	ActionClear            Action    = "clear"
    21  	ActionRestart          Action    = "restart"
    22  	ActionDisabled         Action    = "disabled"
    23  	ActionRestartByPeer    Action    = "restart-by-peer"
    24  	InitiatorBiDirectional Initiator = "bi-directional"
    25  	InitiatorResponseOnly  Initiator = "response-only"
    26  )
    27  
    28  // DPDCreateOpts contains all the values needed to create a valid configuration for Dead Peer detection protocols
    29  type DPDCreateOpts struct {
    30  	// The dead peer detection (DPD) action.
    31  	// A valid value is clear, hold, restart, disabled, or restart-by-peer.
    32  	// Default value is hold.
    33  	Action Action `json:"action,omitempty"`
    34  
    35  	// The dead peer detection (DPD) timeout in seconds.
    36  	// A valid value is a positive integer that is greater than the DPD interval value.
    37  	// Default is 120.
    38  	Timeout int `json:"timeout,omitempty"`
    39  
    40  	// The dead peer detection (DPD) interval, in seconds.
    41  	// A valid value is a positive integer.
    42  	// Default is 30.
    43  	Interval int `json:"interval,omitempty"`
    44  }
    45  
    46  // CreateOpts contains all the values needed to create a new IPSec site connection
    47  type CreateOpts struct {
    48  	// The ID of the IKE policy
    49  	IKEPolicyID string `json:"ikepolicy_id"`
    50  
    51  	// The ID of the VPN Service
    52  	VPNServiceID string `json:"vpnservice_id"`
    53  
    54  	// The ID for the endpoint group that contains private subnets for the local side of the connection.
    55  	// You must specify this parameter with the peer_ep_group_id parameter unless
    56  	// in backward- compatible mode where peer_cidrs is provided with a subnet_id for the VPN service.
    57  	LocalEPGroupID string `json:"local_ep_group_id,omitempty"`
    58  
    59  	// The ID of the IPsec policy.
    60  	IPSecPolicyID string `json:"ipsecpolicy_id"`
    61  
    62  	// The peer router identity for authentication.
    63  	// A valid value is an IPv4 address, IPv6 address, e-mail address, key ID, or FQDN.
    64  	// Typically, this value matches the peer_address value.
    65  	PeerID string `json:"peer_id"`
    66  
    67  	// The ID of the project
    68  	TenantID string `json:"tenant_id,omitempty"`
    69  
    70  	// The ID for the endpoint group that contains private CIDRs in the form < net_address > / < prefix >
    71  	// for the peer side of the connection.
    72  	// You must specify this parameter with the local_ep_group_id parameter unless in backward-compatible mode
    73  	// where peer_cidrs is provided with a subnet_id for the VPN service.
    74  	PeerEPGroupID string `json:"peer_ep_group_id,omitempty"`
    75  
    76  	// An ID to be used instead of the external IP address for a virtual router used in traffic between instances on different networks in east-west traffic.
    77  	// Most often, local ID would be domain name, email address, etc.
    78  	// If this is not configured then the external IP address will be used as the ID.
    79  	LocalID string `json:"local_id,omitempty"`
    80  
    81  	// The human readable name of the connection.
    82  	// Does not have to be unique.
    83  	// Default is an empty string
    84  	Name string `json:"name,omitempty"`
    85  
    86  	// The human readable description of the connection.
    87  	// Does not have to be unique.
    88  	// Default is an empty string
    89  	Description string `json:"description,omitempty"`
    90  
    91  	// The peer gateway public IPv4 or IPv6 address or FQDN.
    92  	PeerAddress string `json:"peer_address"`
    93  
    94  	// The pre-shared key.
    95  	// A valid value is any string.
    96  	PSK string `json:"psk"`
    97  
    98  	// Indicates whether this VPN can only respond to connections or both respond to and initiate connections.
    99  	// A valid value is response-only or bi-directional. Default is bi-directional.
   100  	Initiator Initiator `json:"initiator,omitempty"`
   101  
   102  	// Unique list of valid peer private CIDRs in the form < net_address > / < prefix > .
   103  	PeerCIDRs []string `json:"peer_cidrs,omitempty"`
   104  
   105  	// The administrative state of the resource, which is up (true) or down (false).
   106  	// Default is false
   107  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   108  
   109  	// A dictionary with dead peer detection (DPD) protocol controls.
   110  	DPD *DPDCreateOpts `json:"dpd,omitempty"`
   111  
   112  	// The maximum transmission unit (MTU) value to address fragmentation.
   113  	// Minimum value is 68 for IPv4, and 1280 for IPv6.
   114  	MTU int `json:"mtu,omitempty"`
   115  }
   116  
   117  // ToConnectionCreateMap casts a CreateOpts struct to a map.
   118  func (opts CreateOpts) ToConnectionCreateMap() (map[string]any, error) {
   119  	return gophercloud.BuildRequestBody(opts, "ipsec_site_connection")
   120  }
   121  
   122  // Create accepts a CreateOpts struct and uses the values to create a new
   123  // IPSec site connection.
   124  func Create(ctx context.Context, c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   125  	b, err := opts.ToConnectionCreateMap()
   126  	if err != nil {
   127  		r.Err = err
   128  		return
   129  	}
   130  	resp, err := c.Post(ctx, rootURL(c), b, &r.Body, nil)
   131  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   132  	return
   133  }
   134  
   135  // Delete will permanently delete a particular IPSec site connection based on its
   136  // unique ID.
   137  func Delete(ctx context.Context, c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   138  	resp, err := c.Delete(ctx, resourceURL(c, id), nil)
   139  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   140  	return
   141  }
   142  
   143  // Get retrieves a particular IPSec site connection based on its unique ID.
   144  func Get(ctx context.Context, c *gophercloud.ServiceClient, id string) (r GetResult) {
   145  	resp, err := c.Get(ctx, resourceURL(c, id), &r.Body, nil)
   146  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   147  	return
   148  }
   149  
   150  // ListOptsBuilder allows extensions to add additional parameters to the
   151  // List request.
   152  type ListOptsBuilder interface {
   153  	ToConnectionListQuery() (string, error)
   154  }
   155  
   156  // ListOpts allows the filtering and sorting of paginated collections through
   157  // the API. Filtering is achieved by passing in struct field values that map to
   158  // the IPSec site connection attributes you want to see returned.
   159  type ListOpts struct {
   160  	IKEPolicyID    string    `q:"ikepolicy_id"`
   161  	VPNServiceID   string    `q:"vpnservice_id"`
   162  	LocalEPGroupID string    `q:"local_ep_group_id"`
   163  	IPSecPolicyID  string    `q:"ipsecpolicy_id"`
   164  	PeerID         string    `q:"peer_id"`
   165  	TenantID       string    `q:"tenant_id"`
   166  	ProjectID      string    `q:"project_id"`
   167  	PeerEPGroupID  string    `q:"peer_ep_group_id"`
   168  	LocalID        string    `q:"local_id"`
   169  	Name           string    `q:"name"`
   170  	Description    string    `q:"description"`
   171  	PeerAddress    string    `q:"peer_address"`
   172  	PSK            string    `q:"psk"`
   173  	Initiator      Initiator `q:"initiator"`
   174  	AdminStateUp   *bool     `q:"admin_state_up"`
   175  	MTU            int       `q:"mtu"`
   176  }
   177  
   178  // ToConnectionListQuery formats a ListOpts into a query string.
   179  func (opts ListOpts) ToConnectionListQuery() (string, error) {
   180  	q, err := gophercloud.BuildQueryString(opts)
   181  	return q.String(), err
   182  }
   183  
   184  // List returns a Pager which allows you to iterate over a collection of
   185  // IPSec site connections. It accepts a ListOpts struct, which allows you to filter
   186  // and sort the returned collection for greater efficiency.
   187  func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   188  	url := rootURL(c)
   189  	if opts != nil {
   190  		query, err := opts.ToConnectionListQuery()
   191  		if err != nil {
   192  			return pagination.Pager{Err: err}
   193  		}
   194  		url += query
   195  	}
   196  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   197  		return ConnectionPage{pagination.LinkedPageBase{PageResult: r}}
   198  	})
   199  }
   200  
   201  // UpdateOptsBuilder allows extensions to add additional parameters to the
   202  // Update request.
   203  type UpdateOptsBuilder interface {
   204  	ToConnectionUpdateMap() (map[string]any, error)
   205  }
   206  
   207  // UpdateOpts contains the values used when updating the DPD of an IPSec site connection
   208  type DPDUpdateOpts struct {
   209  	Action   Action `json:"action,omitempty"`
   210  	Timeout  int    `json:"timeout,omitempty"`
   211  	Interval int    `json:"interval,omitempty"`
   212  }
   213  
   214  // UpdateOpts contains the values used when updating an IPSec site connection
   215  type UpdateOpts struct {
   216  	Description    *string        `json:"description,omitempty"`
   217  	Name           *string        `json:"name,omitempty"`
   218  	LocalID        string         `json:"local_id,omitempty"`
   219  	PeerAddress    string         `json:"peer_address,omitempty"`
   220  	PeerID         string         `json:"peer_id,omitempty"`
   221  	PeerCIDRs      []string       `json:"peer_cidrs,omitempty"`
   222  	LocalEPGroupID string         `json:"local_ep_group_id,omitempty"`
   223  	PeerEPGroupID  string         `json:"peer_ep_group_id,omitempty"`
   224  	MTU            int            `json:"mtu,omitempty"`
   225  	Initiator      Initiator      `json:"initiator,omitempty"`
   226  	PSK            string         `json:"psk,omitempty"`
   227  	DPD            *DPDUpdateOpts `json:"dpd,omitempty"`
   228  	AdminStateUp   *bool          `json:"admin_state_up,omitempty"`
   229  }
   230  
   231  // ToConnectionUpdateMap casts an UpdateOpts struct to a map.
   232  func (opts UpdateOpts) ToConnectionUpdateMap() (map[string]any, error) {
   233  	return gophercloud.BuildRequestBody(opts, "ipsec_site_connection")
   234  }
   235  
   236  // Update allows IPSec site connections to be updated.
   237  func Update(ctx context.Context, c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   238  	b, err := opts.ToConnectionUpdateMap()
   239  	if err != nil {
   240  		r.Err = err
   241  		return
   242  	}
   243  	resp, err := c.Put(ctx, resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   244  		OkCodes: []int{200},
   245  	})
   246  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   247  	return
   248  }