github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v2/extensions/vpnaas/ipsecpolicies/requests.go (about)

     1  package ipsecpolicies
     2  
     3  import (
     4  	"github.com/opentelekomcloud/gophertelekomcloud"
     5  	"github.com/opentelekomcloud/gophertelekomcloud/pagination"
     6  )
     7  
     8  type TransformProtocol string
     9  type AuthAlgorithm string
    10  type EncapsulationMode string
    11  type EncryptionAlgorithm string
    12  type PFS string
    13  type Unit string
    14  
    15  const (
    16  	TransformProtocolESP       TransformProtocol   = "esp"
    17  	TransformProtocolAH        TransformProtocol   = "ah"
    18  	TransformProtocolAHESP     TransformProtocol   = "ah-esp"
    19  	AuthAlgorithmMD5           AuthAlgorithm       = "md5"
    20  	AuthAlgorithmSHA1          AuthAlgorithm       = "sha1"
    21  	AuthAlgorithmSHA256        AuthAlgorithm       = "sha2-256"
    22  	AuthAlgorithmSHA384        AuthAlgorithm       = "sha2-384"
    23  	AuthAlgorithmSHA512        AuthAlgorithm       = "sha2-512"
    24  	EncryptionAlgorithm3DES    EncryptionAlgorithm = "3des"
    25  	EncryptionAlgorithmAES128  EncryptionAlgorithm = "aes-128"
    26  	EncryptionAlgorithmAES256  EncryptionAlgorithm = "aes-256"
    27  	EncryptionAlgorithmAES192  EncryptionAlgorithm = "aes-192"
    28  	EncapsulationModeTunnel    EncapsulationMode   = "tunnel"
    29  	EncapsulationModeTransport EncapsulationMode   = "transport"
    30  	UnitSeconds                Unit                = "seconds"
    31  	UnitKilobytes              Unit                = "kilobytes"
    32  	PFSGroup2                  PFS                 = "group2"
    33  	PFSGroup5                  PFS                 = "group5"
    34  	PFSGroup14                 PFS                 = "group14"
    35  )
    36  
    37  // CreateOptsBuilder allows extensions to add additional parameters to the
    38  // Create request.
    39  type CreateOptsBuilder interface {
    40  	ToPolicyCreateMap() (map[string]interface{}, error)
    41  }
    42  
    43  // CreateOpts contains all the values needed to create a new IPSec policy
    44  type CreateOpts struct {
    45  	// TenantID specifies a tenant to own the IPSec policy. The caller must have
    46  	// an admin role in order to set this. Otherwise, this field is left unset
    47  	// and the caller will be the owner.
    48  	TenantID string `json:"tenant_id,omitempty"`
    49  
    50  	// Description is the human readable description of the policy.
    51  	Description string `json:"description,omitempty"`
    52  
    53  	// Name is the human readable name of the policy.
    54  	// Does not have to be unique.
    55  	Name string `json:"name,omitempty"`
    56  
    57  	// AuthAlgorithm is the authentication hash algorithm.
    58  	// Valid values are sha1, sha256, sha384, sha512.
    59  	// The default is sha1.
    60  	AuthAlgorithm AuthAlgorithm `json:"auth_algorithm,omitempty"`
    61  
    62  	// EncapsulationMode is the encapsulation mode.
    63  	// A valid value is tunnel or transport.
    64  	// Default is tunnel.
    65  	EncapsulationMode EncapsulationMode `json:"encapsulation_mode,omitempty"`
    66  
    67  	// EncryptionAlgorithm is the encryption algorithm.
    68  	// A valid value is 3des, aes-128, aes-192, aes-256, and so on.
    69  	// Default is aes-128.
    70  	EncryptionAlgorithm EncryptionAlgorithm `json:"encryption_algorithm,omitempty"`
    71  
    72  	// PFS is the Perfect forward secrecy mode.
    73  	// A valid value is Group2, Group5, Group14, and so on.
    74  	// Default is Group5.
    75  	PFS PFS `json:"pfs,omitempty"`
    76  
    77  	// TransformProtocol is the transform protocol.
    78  	// A valid value is ESP, AH, or AH- ESP.
    79  	// Default is ESP.
    80  	TransformProtocol TransformProtocol `json:"transform_protocol,omitempty"`
    81  
    82  	// Lifetime is the lifetime of the security association
    83  	Lifetime *LifetimeCreateOpts `json:"lifetime,omitempty"`
    84  }
    85  
    86  // The lifetime consists of a unit and integer value
    87  // You can omit either the unit or value portion of the lifetime
    88  type LifetimeCreateOpts struct {
    89  	// Units is the units for the lifetime of the security association
    90  	// Default unit is seconds
    91  	Units Unit `json:"units,omitempty"`
    92  
    93  	// The lifetime value.
    94  	// Must be a positive integer.
    95  	// Default value is 3600.
    96  	Value int `json:"value,omitempty"`
    97  }
    98  
    99  // ToPolicyCreateMap casts a CreateOpts struct to a map.
   100  func (opts CreateOpts) ToPolicyCreateMap() (map[string]interface{}, error) {
   101  	return golangsdk.BuildRequestBody(opts, "ipsecpolicy")
   102  }
   103  
   104  // Create accepts a CreateOpts struct and uses the values to create a new
   105  // IPSec policy
   106  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   107  	b, err := opts.ToPolicyCreateMap()
   108  	if err != nil {
   109  		r.Err = err
   110  		return
   111  	}
   112  	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
   113  	return
   114  }
   115  
   116  // Delete will permanently delete a particular IPSec policy based on its
   117  // unique ID.
   118  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   119  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   120  	return
   121  }
   122  
   123  // Get retrieves a particular IPSec policy based on its unique ID.
   124  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   125  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   126  	return
   127  }
   128  
   129  // ListOptsBuilder allows extensions to add additional parameters to the
   130  // List request.
   131  type ListOptsBuilder interface {
   132  	ToPolicyListQuery() (string, error)
   133  }
   134  
   135  // ListOpts allows the filtering of paginated collections through
   136  // the API. Filtering is achieved by passing in struct field values that map to
   137  // the IPSec policy attributes you want to see returned.
   138  type ListOpts struct {
   139  	TenantID            string `q:"tenant_id"`
   140  	Name                string `q:"name"`
   141  	Description         string `q:"description"`
   142  	ProjectID           string `q:"project_id"`
   143  	AuthAlgorithm       string `q:"auth_algorithm"`
   144  	EncapsulationMode   string `q:"encapsulation_mode"`
   145  	EncryptionAlgorithm string `q:"encryption_algorithm"`
   146  	PFS                 string `q:"pfs"`
   147  	TransformProtocol   string `q:"transform_protocol"`
   148  }
   149  
   150  // ToPolicyListQuery formats a ListOpts into a query string.
   151  func (opts ListOpts) ToPolicyListQuery() (string, error) {
   152  	q, err := golangsdk.BuildQueryString(opts)
   153  	if err != nil {
   154  		return "", err
   155  	}
   156  	return q.String(), err
   157  }
   158  
   159  // List returns a Pager which allows you to iterate over a collection of
   160  // IPSec policies. It accepts a ListOpts struct, which allows you to filter
   161  // the returned collection for greater efficiency.
   162  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   163  	url := rootURL(c)
   164  	if opts != nil {
   165  		query, err := opts.ToPolicyListQuery()
   166  		if err != nil {
   167  			return pagination.Pager{Err: err}
   168  		}
   169  		url += query
   170  	}
   171  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   172  		return PolicyPage{pagination.LinkedPageBase{PageResult: r}}
   173  	})
   174  }
   175  
   176  // UpdateOptsBuilder allows extensions to add additional parameters to the
   177  // Update request.
   178  type UpdateOptsBuilder interface {
   179  	ToPolicyUpdateMap() (map[string]interface{}, error)
   180  }
   181  
   182  type LifetimeUpdateOpts struct {
   183  	Units Unit `json:"units,omitempty"`
   184  	Value int  `json:"value,omitempty"`
   185  }
   186  
   187  // UpdateOpts contains the values used when updating an IPSec policy
   188  type UpdateOpts struct {
   189  	Description         *string             `json:"description,omitempty"`
   190  	Name                *string             `json:"name,omitempty"`
   191  	AuthAlgorithm       AuthAlgorithm       `json:"auth_algorithm,omitempty"`
   192  	EncapsulationMode   EncapsulationMode   `json:"encapsulation_mode,omitempty"`
   193  	EncryptionAlgorithm EncryptionAlgorithm `json:"encryption_algorithm,omitempty"`
   194  	PFS                 PFS                 `json:"pfs,omitempty"`
   195  	TransformProtocol   TransformProtocol   `json:"transform_protocol,omitempty"`
   196  	Lifetime            *LifetimeUpdateOpts `json:"lifetime,omitempty"`
   197  }
   198  
   199  // ToPolicyUpdateMap casts an UpdateOpts struct to a map.
   200  func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) {
   201  	return golangsdk.BuildRequestBody(opts, "ipsecpolicy")
   202  }
   203  
   204  // Update allows IPSec policies to be updated.
   205  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   206  	b, err := opts.ToPolicyUpdateMap()
   207  	if err != nil {
   208  		r.Err = err
   209  		return
   210  	}
   211  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   212  		OkCodes: []int{200},
   213  	})
   214  	return
   215  }