1// Code generated by smithy-go-codegen DO NOT EDIT.
2
3package ec2
4
5import (
6	"context"
7	"fmt"
8	awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
9	"github.com/aws/aws-sdk-go-v2/aws/signer/v4"
10	"github.com/aws/aws-sdk-go-v2/service/ec2/types"
11	"github.com/aws/smithy-go/middleware"
12	smithytime "github.com/aws/smithy-go/time"
13	smithyhttp "github.com/aws/smithy-go/transport/http"
14	smithywaiter "github.com/aws/smithy-go/waiter"
15	"github.com/jmespath/go-jmespath"
16	"time"
17)
18
19// Describes one or more of your VPN connections. For more information, see AWS
20// Site-to-Site VPN (https://docs.aws.amazon.com/vpn/latest/s2svpn/VPC_VPN.html) in
21// the AWS Site-to-Site VPN User Guide.
22func (c *Client) DescribeVpnConnections(ctx context.Context, params *DescribeVpnConnectionsInput, optFns ...func(*Options)) (*DescribeVpnConnectionsOutput, error) {
23	if params == nil {
24		params = &DescribeVpnConnectionsInput{}
25	}
26
27	result, metadata, err := c.invokeOperation(ctx, "DescribeVpnConnections", params, optFns, addOperationDescribeVpnConnectionsMiddlewares)
28	if err != nil {
29		return nil, err
30	}
31
32	out := result.(*DescribeVpnConnectionsOutput)
33	out.ResultMetadata = metadata
34	return out, nil
35}
36
37// Contains the parameters for DescribeVpnConnections.
38type DescribeVpnConnectionsInput struct {
39
40	// Checks whether you have the required permissions for the action, without
41	// actually making the request, and provides an error response. If you have the
42	// required permissions, the error response is DryRunOperation. Otherwise, it is
43	// UnauthorizedOperation.
44	DryRun bool
45
46	// One or more filters.
47	//
48	// * customer-gateway-configuration - The configuration
49	// information for the customer gateway.
50	//
51	// * customer-gateway-id - The ID of a
52	// customer gateway associated with the VPN connection.
53	//
54	// * state - The state of the
55	// VPN connection (pending | available | deleting | deleted).
56	//
57	// *
58	// option.static-routes-only - Indicates whether the connection has static routes
59	// only. Used for devices that do not support Border Gateway Protocol (BGP).
60	//
61	// *
62	// route.destination-cidr-block - The destination CIDR block. This corresponds to
63	// the subnet used in a customer data center.
64	//
65	// * bgp-asn - The BGP Autonomous
66	// System Number (ASN) associated with a BGP device.
67	//
68	// * tag: - The key/value
69	// combination of a tag assigned to the resource. Use the tag key in the filter
70	// name and the tag value as the filter value. For example, to find all resources
71	// that have a tag with the key Owner and the value TeamA, specify tag:Owner for
72	// the filter name and TeamA for the filter value.
73	//
74	// * tag-key - The key of a tag
75	// assigned to the resource. Use this filter to find all resources assigned a tag
76	// with a specific key, regardless of the tag value.
77	//
78	// * type - The type of VPN
79	// connection. Currently the only supported type is ipsec.1.
80	//
81	// * vpn-connection-id -
82	// The ID of the VPN connection.
83	//
84	// * vpn-gateway-id - The ID of a virtual private
85	// gateway associated with the VPN connection.
86	//
87	// * transit-gateway-id - The ID of a
88	// transit gateway associated with the VPN connection.
89	Filters []types.Filter
90
91	// One or more VPN connection IDs. Default: Describes your VPN connections.
92	VpnConnectionIds []string
93}
94
95// Contains the output of DescribeVpnConnections.
96type DescribeVpnConnectionsOutput struct {
97
98	// Information about one or more VPN connections.
99	VpnConnections []types.VpnConnection
100
101	// Metadata pertaining to the operation's result.
102	ResultMetadata middleware.Metadata
103}
104
105func addOperationDescribeVpnConnectionsMiddlewares(stack *middleware.Stack, options Options) (err error) {
106	err = stack.Serialize.Add(&awsEc2query_serializeOpDescribeVpnConnections{}, middleware.After)
107	if err != nil {
108		return err
109	}
110	err = stack.Deserialize.Add(&awsEc2query_deserializeOpDescribeVpnConnections{}, middleware.After)
111	if err != nil {
112		return err
113	}
114	if err = addSetLoggerMiddleware(stack, options); err != nil {
115		return err
116	}
117	if err = awsmiddleware.AddClientRequestIDMiddleware(stack); err != nil {
118		return err
119	}
120	if err = smithyhttp.AddComputeContentLengthMiddleware(stack); err != nil {
121		return err
122	}
123	if err = addResolveEndpointMiddleware(stack, options); err != nil {
124		return err
125	}
126	if err = v4.AddComputePayloadSHA256Middleware(stack); err != nil {
127		return err
128	}
129	if err = addRetryMiddlewares(stack, options); err != nil {
130		return err
131	}
132	if err = addHTTPSignerV4Middleware(stack, options); err != nil {
133		return err
134	}
135	if err = awsmiddleware.AddRawResponseToMetadata(stack); err != nil {
136		return err
137	}
138	if err = awsmiddleware.AddRecordResponseTiming(stack); err != nil {
139		return err
140	}
141	if err = addClientUserAgent(stack); err != nil {
142		return err
143	}
144	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
145		return err
146	}
147	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
148		return err
149	}
150	if err = stack.Initialize.Add(newServiceMetadataMiddleware_opDescribeVpnConnections(options.Region), middleware.Before); err != nil {
151		return err
152	}
153	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
154		return err
155	}
156	if err = addResponseErrorMiddleware(stack); err != nil {
157		return err
158	}
159	if err = addRequestResponseLogging(stack, options); err != nil {
160		return err
161	}
162	return nil
163}
164
165// DescribeVpnConnectionsAPIClient is a client that implements the
166// DescribeVpnConnections operation.
167type DescribeVpnConnectionsAPIClient interface {
168	DescribeVpnConnections(context.Context, *DescribeVpnConnectionsInput, ...func(*Options)) (*DescribeVpnConnectionsOutput, error)
169}
170
171var _ DescribeVpnConnectionsAPIClient = (*Client)(nil)
172
173// VpnConnectionAvailableWaiterOptions are waiter options for
174// VpnConnectionAvailableWaiter
175type VpnConnectionAvailableWaiterOptions struct {
176
177	// Set of options to modify how an operation is invoked. These apply to all
178	// operations invoked for this client. Use functional options on operation call to
179	// modify this list for per operation behavior.
180	APIOptions []func(*middleware.Stack) error
181
182	// MinDelay is the minimum amount of time to delay between retries. If unset,
183	// VpnConnectionAvailableWaiter will use default minimum delay of 15 seconds. Note
184	// that MinDelay must resolve to a value lesser than or equal to the MaxDelay.
185	MinDelay time.Duration
186
187	// MaxDelay is the maximum amount of time to delay between retries. If unset or set
188	// to zero, VpnConnectionAvailableWaiter will use default max delay of 120 seconds.
189	// Note that MaxDelay must resolve to value greater than or equal to the MinDelay.
190	MaxDelay time.Duration
191
192	// LogWaitAttempts is used to enable logging for waiter retry attempts
193	LogWaitAttempts bool
194
195	// Retryable is function that can be used to override the service defined
196	// waiter-behavior based on operation output, or returned error. This function is
197	// used by the waiter to decide if a state is retryable or a terminal state. By
198	// default service-modeled logic will populate this option. This option can thus be
199	// used to define a custom waiter state with fall-back to service-modeled waiter
200	// state mutators.The function returns an error in case of a failure state. In case
201	// of retry state, this function returns a bool value of true and nil error, while
202	// in case of success it returns a bool value of false and nil error.
203	Retryable func(context.Context, *DescribeVpnConnectionsInput, *DescribeVpnConnectionsOutput, error) (bool, error)
204}
205
206// VpnConnectionAvailableWaiter defines the waiters for VpnConnectionAvailable
207type VpnConnectionAvailableWaiter struct {
208	client DescribeVpnConnectionsAPIClient
209
210	options VpnConnectionAvailableWaiterOptions
211}
212
213// NewVpnConnectionAvailableWaiter constructs a VpnConnectionAvailableWaiter.
214func NewVpnConnectionAvailableWaiter(client DescribeVpnConnectionsAPIClient, optFns ...func(*VpnConnectionAvailableWaiterOptions)) *VpnConnectionAvailableWaiter {
215	options := VpnConnectionAvailableWaiterOptions{}
216	options.MinDelay = 15 * time.Second
217	options.MaxDelay = 120 * time.Second
218	options.Retryable = vpnConnectionAvailableStateRetryable
219
220	for _, fn := range optFns {
221		fn(&options)
222	}
223	return &VpnConnectionAvailableWaiter{
224		client:  client,
225		options: options,
226	}
227}
228
229// Wait calls the waiter function for VpnConnectionAvailable waiter. The maxWaitDur
230// is the maximum wait duration the waiter will wait. The maxWaitDur is required
231// and must be greater than zero.
232func (w *VpnConnectionAvailableWaiter) Wait(ctx context.Context, params *DescribeVpnConnectionsInput, maxWaitDur time.Duration, optFns ...func(*VpnConnectionAvailableWaiterOptions)) error {
233	if maxWaitDur <= 0 {
234		return fmt.Errorf("maximum wait time for waiter must be greater than zero")
235	}
236
237	options := w.options
238	for _, fn := range optFns {
239		fn(&options)
240	}
241
242	if options.MaxDelay <= 0 {
243		options.MaxDelay = 120 * time.Second
244	}
245
246	if options.MinDelay > options.MaxDelay {
247		return fmt.Errorf("minimum waiter delay %v must be lesser than or equal to maximum waiter delay of %v.", options.MinDelay, options.MaxDelay)
248	}
249
250	ctx, cancelFn := context.WithTimeout(ctx, maxWaitDur)
251	defer cancelFn()
252
253	logger := smithywaiter.Logger{}
254	remainingTime := maxWaitDur
255
256	var attempt int64
257	for {
258
259		attempt++
260		apiOptions := options.APIOptions
261		start := time.Now()
262
263		if options.LogWaitAttempts {
264			logger.Attempt = attempt
265			apiOptions = append([]func(*middleware.Stack) error{}, options.APIOptions...)
266			apiOptions = append(apiOptions, logger.AddLogger)
267		}
268
269		out, err := w.client.DescribeVpnConnections(ctx, params, func(o *Options) {
270			o.APIOptions = append(o.APIOptions, apiOptions...)
271		})
272
273		retryable, err := options.Retryable(ctx, params, out, err)
274		if err != nil {
275			return err
276		}
277		if !retryable {
278			return nil
279		}
280
281		remainingTime -= time.Since(start)
282		if remainingTime < options.MinDelay || remainingTime <= 0 {
283			break
284		}
285
286		// compute exponential backoff between waiter retries
287		delay, err := smithywaiter.ComputeDelay(
288			attempt, options.MinDelay, options.MaxDelay, remainingTime,
289		)
290		if err != nil {
291			return fmt.Errorf("error computing waiter delay, %w", err)
292		}
293
294		remainingTime -= delay
295		// sleep for the delay amount before invoking a request
296		if err := smithytime.SleepWithContext(ctx, delay); err != nil {
297			return fmt.Errorf("request cancelled while waiting, %w", err)
298		}
299	}
300	return fmt.Errorf("exceeded max wait time for VpnConnectionAvailable waiter")
301}
302
303func vpnConnectionAvailableStateRetryable(ctx context.Context, input *DescribeVpnConnectionsInput, output *DescribeVpnConnectionsOutput, err error) (bool, error) {
304
305	if err == nil {
306		pathValue, err := jmespath.Search("VpnConnections[].State", output)
307		if err != nil {
308			return false, fmt.Errorf("error evaluating waiter state: %w", err)
309		}
310
311		expectedValue := "available"
312		var match = true
313		listOfValues, ok := pathValue.([]interface{})
314		if !ok {
315			return false, fmt.Errorf("waiter comparator expected list got %T", pathValue)
316		}
317
318		if len(listOfValues) == 0 {
319			match = false
320		}
321		for _, v := range listOfValues {
322			value, ok := v.(types.VpnState)
323			if !ok {
324				return false, fmt.Errorf("waiter comparator expected types.VpnState value, got %T", pathValue)
325			}
326
327			if string(value) != expectedValue {
328				match = false
329			}
330		}
331
332		if match {
333			return false, nil
334		}
335	}
336
337	if err == nil {
338		pathValue, err := jmespath.Search("VpnConnections[].State", output)
339		if err != nil {
340			return false, fmt.Errorf("error evaluating waiter state: %w", err)
341		}
342
343		expectedValue := "deleting"
344		listOfValues, ok := pathValue.([]interface{})
345		if !ok {
346			return false, fmt.Errorf("waiter comparator expected list got %T", pathValue)
347		}
348
349		for _, v := range listOfValues {
350			value, ok := v.(types.VpnState)
351			if !ok {
352				return false, fmt.Errorf("waiter comparator expected types.VpnState value, got %T", pathValue)
353			}
354
355			if string(value) == expectedValue {
356				return false, fmt.Errorf("waiter state transitioned to Failure")
357			}
358		}
359	}
360
361	if err == nil {
362		pathValue, err := jmespath.Search("VpnConnections[].State", output)
363		if err != nil {
364			return false, fmt.Errorf("error evaluating waiter state: %w", err)
365		}
366
367		expectedValue := "deleted"
368		listOfValues, ok := pathValue.([]interface{})
369		if !ok {
370			return false, fmt.Errorf("waiter comparator expected list got %T", pathValue)
371		}
372
373		for _, v := range listOfValues {
374			value, ok := v.(types.VpnState)
375			if !ok {
376				return false, fmt.Errorf("waiter comparator expected types.VpnState value, got %T", pathValue)
377			}
378
379			if string(value) == expectedValue {
380				return false, fmt.Errorf("waiter state transitioned to Failure")
381			}
382		}
383	}
384
385	return true, nil
386}
387
388// VpnConnectionDeletedWaiterOptions are waiter options for
389// VpnConnectionDeletedWaiter
390type VpnConnectionDeletedWaiterOptions struct {
391
392	// Set of options to modify how an operation is invoked. These apply to all
393	// operations invoked for this client. Use functional options on operation call to
394	// modify this list for per operation behavior.
395	APIOptions []func(*middleware.Stack) error
396
397	// MinDelay is the minimum amount of time to delay between retries. If unset,
398	// VpnConnectionDeletedWaiter will use default minimum delay of 15 seconds. Note
399	// that MinDelay must resolve to a value lesser than or equal to the MaxDelay.
400	MinDelay time.Duration
401
402	// MaxDelay is the maximum amount of time to delay between retries. If unset or set
403	// to zero, VpnConnectionDeletedWaiter will use default max delay of 120 seconds.
404	// Note that MaxDelay must resolve to value greater than or equal to the MinDelay.
405	MaxDelay time.Duration
406
407	// LogWaitAttempts is used to enable logging for waiter retry attempts
408	LogWaitAttempts bool
409
410	// Retryable is function that can be used to override the service defined
411	// waiter-behavior based on operation output, or returned error. This function is
412	// used by the waiter to decide if a state is retryable or a terminal state. By
413	// default service-modeled logic will populate this option. This option can thus be
414	// used to define a custom waiter state with fall-back to service-modeled waiter
415	// state mutators.The function returns an error in case of a failure state. In case
416	// of retry state, this function returns a bool value of true and nil error, while
417	// in case of success it returns a bool value of false and nil error.
418	Retryable func(context.Context, *DescribeVpnConnectionsInput, *DescribeVpnConnectionsOutput, error) (bool, error)
419}
420
421// VpnConnectionDeletedWaiter defines the waiters for VpnConnectionDeleted
422type VpnConnectionDeletedWaiter struct {
423	client DescribeVpnConnectionsAPIClient
424
425	options VpnConnectionDeletedWaiterOptions
426}
427
428// NewVpnConnectionDeletedWaiter constructs a VpnConnectionDeletedWaiter.
429func NewVpnConnectionDeletedWaiter(client DescribeVpnConnectionsAPIClient, optFns ...func(*VpnConnectionDeletedWaiterOptions)) *VpnConnectionDeletedWaiter {
430	options := VpnConnectionDeletedWaiterOptions{}
431	options.MinDelay = 15 * time.Second
432	options.MaxDelay = 120 * time.Second
433	options.Retryable = vpnConnectionDeletedStateRetryable
434
435	for _, fn := range optFns {
436		fn(&options)
437	}
438	return &VpnConnectionDeletedWaiter{
439		client:  client,
440		options: options,
441	}
442}
443
444// Wait calls the waiter function for VpnConnectionDeleted waiter. The maxWaitDur
445// is the maximum wait duration the waiter will wait. The maxWaitDur is required
446// and must be greater than zero.
447func (w *VpnConnectionDeletedWaiter) Wait(ctx context.Context, params *DescribeVpnConnectionsInput, maxWaitDur time.Duration, optFns ...func(*VpnConnectionDeletedWaiterOptions)) error {
448	if maxWaitDur <= 0 {
449		return fmt.Errorf("maximum wait time for waiter must be greater than zero")
450	}
451
452	options := w.options
453	for _, fn := range optFns {
454		fn(&options)
455	}
456
457	if options.MaxDelay <= 0 {
458		options.MaxDelay = 120 * time.Second
459	}
460
461	if options.MinDelay > options.MaxDelay {
462		return fmt.Errorf("minimum waiter delay %v must be lesser than or equal to maximum waiter delay of %v.", options.MinDelay, options.MaxDelay)
463	}
464
465	ctx, cancelFn := context.WithTimeout(ctx, maxWaitDur)
466	defer cancelFn()
467
468	logger := smithywaiter.Logger{}
469	remainingTime := maxWaitDur
470
471	var attempt int64
472	for {
473
474		attempt++
475		apiOptions := options.APIOptions
476		start := time.Now()
477
478		if options.LogWaitAttempts {
479			logger.Attempt = attempt
480			apiOptions = append([]func(*middleware.Stack) error{}, options.APIOptions...)
481			apiOptions = append(apiOptions, logger.AddLogger)
482		}
483
484		out, err := w.client.DescribeVpnConnections(ctx, params, func(o *Options) {
485			o.APIOptions = append(o.APIOptions, apiOptions...)
486		})
487
488		retryable, err := options.Retryable(ctx, params, out, err)
489		if err != nil {
490			return err
491		}
492		if !retryable {
493			return nil
494		}
495
496		remainingTime -= time.Since(start)
497		if remainingTime < options.MinDelay || remainingTime <= 0 {
498			break
499		}
500
501		// compute exponential backoff between waiter retries
502		delay, err := smithywaiter.ComputeDelay(
503			attempt, options.MinDelay, options.MaxDelay, remainingTime,
504		)
505		if err != nil {
506			return fmt.Errorf("error computing waiter delay, %w", err)
507		}
508
509		remainingTime -= delay
510		// sleep for the delay amount before invoking a request
511		if err := smithytime.SleepWithContext(ctx, delay); err != nil {
512			return fmt.Errorf("request cancelled while waiting, %w", err)
513		}
514	}
515	return fmt.Errorf("exceeded max wait time for VpnConnectionDeleted waiter")
516}
517
518func vpnConnectionDeletedStateRetryable(ctx context.Context, input *DescribeVpnConnectionsInput, output *DescribeVpnConnectionsOutput, err error) (bool, error) {
519
520	if err == nil {
521		pathValue, err := jmespath.Search("VpnConnections[].State", output)
522		if err != nil {
523			return false, fmt.Errorf("error evaluating waiter state: %w", err)
524		}
525
526		expectedValue := "deleted"
527		var match = true
528		listOfValues, ok := pathValue.([]interface{})
529		if !ok {
530			return false, fmt.Errorf("waiter comparator expected list got %T", pathValue)
531		}
532
533		if len(listOfValues) == 0 {
534			match = false
535		}
536		for _, v := range listOfValues {
537			value, ok := v.(types.VpnState)
538			if !ok {
539				return false, fmt.Errorf("waiter comparator expected types.VpnState value, got %T", pathValue)
540			}
541
542			if string(value) != expectedValue {
543				match = false
544			}
545		}
546
547		if match {
548			return false, nil
549		}
550	}
551
552	if err == nil {
553		pathValue, err := jmespath.Search("VpnConnections[].State", output)
554		if err != nil {
555			return false, fmt.Errorf("error evaluating waiter state: %w", err)
556		}
557
558		expectedValue := "pending"
559		listOfValues, ok := pathValue.([]interface{})
560		if !ok {
561			return false, fmt.Errorf("waiter comparator expected list got %T", pathValue)
562		}
563
564		for _, v := range listOfValues {
565			value, ok := v.(types.VpnState)
566			if !ok {
567				return false, fmt.Errorf("waiter comparator expected types.VpnState value, got %T", pathValue)
568			}
569
570			if string(value) == expectedValue {
571				return false, fmt.Errorf("waiter state transitioned to Failure")
572			}
573		}
574	}
575
576	return true, nil
577}
578
579func newServiceMetadataMiddleware_opDescribeVpnConnections(region string) *awsmiddleware.RegisterServiceMetadata {
580	return &awsmiddleware.RegisterServiceMetadata{
581		Region:        region,
582		ServiceID:     ServiceID,
583		SigningName:   "ec2",
584		OperationName: "DescribeVpnConnections",
585	}
586}
587