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

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