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