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 VPCs.
20func (c *Client) DescribeVpcs(ctx context.Context, params *DescribeVpcsInput, optFns ...func(*Options)) (*DescribeVpcsOutput, error) {
21	if params == nil {
22		params = &DescribeVpcsInput{}
23	}
24
25	result, metadata, err := c.invokeOperation(ctx, "DescribeVpcs", params, optFns, addOperationDescribeVpcsMiddlewares)
26	if err != nil {
27		return nil, err
28	}
29
30	out := result.(*DescribeVpcsOutput)
31	out.ResultMetadata = metadata
32	return out, nil
33}
34
35type DescribeVpcsInput struct {
36
37	// Checks whether you have the required permissions for the action, without
38	// actually making the request, and provides an error response. If you have the
39	// required permissions, the error response is DryRunOperation. Otherwise, it is
40	// UnauthorizedOperation.
41	DryRun bool
42
43	// One or more filters.
44	//
45	// * cidr - The primary IPv4 CIDR block of the VPC. The CIDR
46	// block you specify must exactly match the VPC's CIDR block for information to be
47	// returned for the VPC. Must contain the slash followed by one or two digits (for
48	// example, /28).
49	//
50	// * cidr-block-association.cidr-block - An IPv4 CIDR block
51	// associated with the VPC.
52	//
53	// * cidr-block-association.association-id - The
54	// association ID for an IPv4 CIDR block associated with the VPC.
55	//
56	// *
57	// cidr-block-association.state - The state of an IPv4 CIDR block associated with
58	// the VPC.
59	//
60	// * dhcp-options-id - The ID of a set of DHCP options.
61	//
62	// *
63	// ipv6-cidr-block-association.ipv6-cidr-block - An IPv6 CIDR block associated with
64	// the VPC.
65	//
66	// * ipv6-cidr-block-association.ipv6-pool - The ID of the IPv6 address
67	// pool from which the IPv6 CIDR block is allocated.
68	//
69	// *
70	// ipv6-cidr-block-association.association-id - The association ID for an IPv6 CIDR
71	// block associated with the VPC.
72	//
73	// * ipv6-cidr-block-association.state - The state
74	// of an IPv6 CIDR block associated with the VPC.
75	//
76	// * isDefault - Indicates whether
77	// the VPC is the default VPC.
78	//
79	// * owner-id - The ID of the AWS account that owns
80	// the VPC.
81	//
82	// * state - The state of the VPC (pending | available).
83	//
84	// * tag: - The
85	// key/value combination of a tag assigned to the resource. Use the tag key in the
86	// filter name and the tag value as the filter value. For example, to find all
87	// resources that have a tag with the key Owner and the value TeamA, specify
88	// tag:Owner for the filter name and TeamA for the filter value.
89	//
90	// * tag-key - The
91	// key of a tag assigned to the resource. Use this filter to find all resources
92	// assigned a tag with a specific key, regardless of the tag value.
93	//
94	// * vpc-id - The
95	// ID of the VPC.
96	Filters []types.Filter
97
98	// The maximum number of results to return with a single call. To retrieve the
99	// remaining results, make another call with the returned nextToken value.
100	MaxResults int32
101
102	// The token for the next page of results.
103	NextToken *string
104
105	// One or more VPC IDs. Default: Describes all your VPCs.
106	VpcIds []string
107}
108
109type DescribeVpcsOutput struct {
110
111	// The token to use to retrieve the next page of results. This value is null when
112	// there are no more results to return.
113	NextToken *string
114
115	// Information about one or more VPCs.
116	Vpcs []types.Vpc
117
118	// Metadata pertaining to the operation's result.
119	ResultMetadata middleware.Metadata
120}
121
122func addOperationDescribeVpcsMiddlewares(stack *middleware.Stack, options Options) (err error) {
123	err = stack.Serialize.Add(&awsEc2query_serializeOpDescribeVpcs{}, middleware.After)
124	if err != nil {
125		return err
126	}
127	err = stack.Deserialize.Add(&awsEc2query_deserializeOpDescribeVpcs{}, middleware.After)
128	if err != nil {
129		return err
130	}
131	if err = addSetLoggerMiddleware(stack, options); err != nil {
132		return err
133	}
134	if err = awsmiddleware.AddClientRequestIDMiddleware(stack); err != nil {
135		return err
136	}
137	if err = smithyhttp.AddComputeContentLengthMiddleware(stack); err != nil {
138		return err
139	}
140	if err = addResolveEndpointMiddleware(stack, options); err != nil {
141		return err
142	}
143	if err = v4.AddComputePayloadSHA256Middleware(stack); err != nil {
144		return err
145	}
146	if err = addRetryMiddlewares(stack, options); err != nil {
147		return err
148	}
149	if err = addHTTPSignerV4Middleware(stack, options); err != nil {
150		return err
151	}
152	if err = awsmiddleware.AddRawResponseToMetadata(stack); err != nil {
153		return err
154	}
155	if err = awsmiddleware.AddRecordResponseTiming(stack); err != nil {
156		return err
157	}
158	if err = addClientUserAgent(stack); err != nil {
159		return err
160	}
161	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
162		return err
163	}
164	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
165		return err
166	}
167	if err = stack.Initialize.Add(newServiceMetadataMiddleware_opDescribeVpcs(options.Region), middleware.Before); err != nil {
168		return err
169	}
170	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
171		return err
172	}
173	if err = addResponseErrorMiddleware(stack); err != nil {
174		return err
175	}
176	if err = addRequestResponseLogging(stack, options); err != nil {
177		return err
178	}
179	return nil
180}
181
182// DescribeVpcsAPIClient is a client that implements the DescribeVpcs operation.
183type DescribeVpcsAPIClient interface {
184	DescribeVpcs(context.Context, *DescribeVpcsInput, ...func(*Options)) (*DescribeVpcsOutput, error)
185}
186
187var _ DescribeVpcsAPIClient = (*Client)(nil)
188
189// DescribeVpcsPaginatorOptions is the paginator options for DescribeVpcs
190type DescribeVpcsPaginatorOptions struct {
191	// The maximum number of results to return with a single call. To retrieve the
192	// remaining results, make another call with the returned nextToken value.
193	Limit int32
194
195	// Set to true if pagination should stop if the service returns a pagination token
196	// that matches the most recent token provided to the service.
197	StopOnDuplicateToken bool
198}
199
200// DescribeVpcsPaginator is a paginator for DescribeVpcs
201type DescribeVpcsPaginator struct {
202	options   DescribeVpcsPaginatorOptions
203	client    DescribeVpcsAPIClient
204	params    *DescribeVpcsInput
205	nextToken *string
206	firstPage bool
207}
208
209// NewDescribeVpcsPaginator returns a new DescribeVpcsPaginator
210func NewDescribeVpcsPaginator(client DescribeVpcsAPIClient, params *DescribeVpcsInput, optFns ...func(*DescribeVpcsPaginatorOptions)) *DescribeVpcsPaginator {
211	options := DescribeVpcsPaginatorOptions{}
212	if params.MaxResults != 0 {
213		options.Limit = params.MaxResults
214	}
215
216	for _, fn := range optFns {
217		fn(&options)
218	}
219
220	if params == nil {
221		params = &DescribeVpcsInput{}
222	}
223
224	return &DescribeVpcsPaginator{
225		options:   options,
226		client:    client,
227		params:    params,
228		firstPage: true,
229	}
230}
231
232// HasMorePages returns a boolean indicating whether more pages are available
233func (p *DescribeVpcsPaginator) HasMorePages() bool {
234	return p.firstPage || p.nextToken != nil
235}
236
237// NextPage retrieves the next DescribeVpcs page.
238func (p *DescribeVpcsPaginator) NextPage(ctx context.Context, optFns ...func(*Options)) (*DescribeVpcsOutput, error) {
239	if !p.HasMorePages() {
240		return nil, fmt.Errorf("no more pages available")
241	}
242
243	params := *p.params
244	params.NextToken = p.nextToken
245
246	params.MaxResults = p.options.Limit
247
248	result, err := p.client.DescribeVpcs(ctx, &params, optFns...)
249	if err != nil {
250		return nil, err
251	}
252	p.firstPage = false
253
254	prevToken := p.nextToken
255	p.nextToken = result.NextToken
256
257	if p.options.StopOnDuplicateToken && prevToken != nil && p.nextToken != nil && *prevToken == *p.nextToken {
258		p.nextToken = nil
259	}
260
261	return result, nil
262}
263
264// VpcAvailableWaiterOptions are waiter options for VpcAvailableWaiter
265type VpcAvailableWaiterOptions struct {
266
267	// Set of options to modify how an operation is invoked. These apply to all
268	// operations invoked for this client. Use functional options on operation call to
269	// modify this list for per operation behavior.
270	APIOptions []func(*middleware.Stack) error
271
272	// MinDelay is the minimum amount of time to delay between retries. If unset,
273	// VpcAvailableWaiter will use default minimum delay of 15 seconds. Note that
274	// MinDelay must resolve to a value lesser than or equal to the MaxDelay.
275	MinDelay time.Duration
276
277	// MaxDelay is the maximum amount of time to delay between retries. If unset or set
278	// to zero, VpcAvailableWaiter will use default max delay of 120 seconds. Note that
279	// MaxDelay must resolve to value greater than or equal to the MinDelay.
280	MaxDelay time.Duration
281
282	// LogWaitAttempts is used to enable logging for waiter retry attempts
283	LogWaitAttempts bool
284
285	// Retryable is function that can be used to override the service defined
286	// waiter-behavior based on operation output, or returned error. This function is
287	// used by the waiter to decide if a state is retryable or a terminal state. By
288	// default service-modeled logic will populate this option. This option can thus be
289	// used to define a custom waiter state with fall-back to service-modeled waiter
290	// state mutators.The function returns an error in case of a failure state. In case
291	// of retry state, this function returns a bool value of true and nil error, while
292	// in case of success it returns a bool value of false and nil error.
293	Retryable func(context.Context, *DescribeVpcsInput, *DescribeVpcsOutput, error) (bool, error)
294}
295
296// VpcAvailableWaiter defines the waiters for VpcAvailable
297type VpcAvailableWaiter struct {
298	client DescribeVpcsAPIClient
299
300	options VpcAvailableWaiterOptions
301}
302
303// NewVpcAvailableWaiter constructs a VpcAvailableWaiter.
304func NewVpcAvailableWaiter(client DescribeVpcsAPIClient, optFns ...func(*VpcAvailableWaiterOptions)) *VpcAvailableWaiter {
305	options := VpcAvailableWaiterOptions{}
306	options.MinDelay = 15 * time.Second
307	options.MaxDelay = 120 * time.Second
308	options.Retryable = vpcAvailableStateRetryable
309
310	for _, fn := range optFns {
311		fn(&options)
312	}
313	return &VpcAvailableWaiter{
314		client:  client,
315		options: options,
316	}
317}
318
319// Wait calls the waiter function for VpcAvailable waiter. The maxWaitDur is the
320// maximum wait duration the waiter will wait. The maxWaitDur is required and must
321// be greater than zero.
322func (w *VpcAvailableWaiter) Wait(ctx context.Context, params *DescribeVpcsInput, maxWaitDur time.Duration, optFns ...func(*VpcAvailableWaiterOptions)) error {
323	if maxWaitDur <= 0 {
324		return fmt.Errorf("maximum wait time for waiter must be greater than zero")
325	}
326
327	options := w.options
328	for _, fn := range optFns {
329		fn(&options)
330	}
331
332	if options.MaxDelay <= 0 {
333		options.MaxDelay = 120 * time.Second
334	}
335
336	if options.MinDelay > options.MaxDelay {
337		return fmt.Errorf("minimum waiter delay %v must be lesser than or equal to maximum waiter delay of %v.", options.MinDelay, options.MaxDelay)
338	}
339
340	ctx, cancelFn := context.WithTimeout(ctx, maxWaitDur)
341	defer cancelFn()
342
343	logger := smithywaiter.Logger{}
344	remainingTime := maxWaitDur
345
346	var attempt int64
347	for {
348
349		attempt++
350		apiOptions := options.APIOptions
351		start := time.Now()
352
353		if options.LogWaitAttempts {
354			logger.Attempt = attempt
355			apiOptions = append([]func(*middleware.Stack) error{}, options.APIOptions...)
356			apiOptions = append(apiOptions, logger.AddLogger)
357		}
358
359		out, err := w.client.DescribeVpcs(ctx, params, func(o *Options) {
360			o.APIOptions = append(o.APIOptions, apiOptions...)
361		})
362
363		retryable, err := options.Retryable(ctx, params, out, err)
364		if err != nil {
365			return err
366		}
367		if !retryable {
368			return nil
369		}
370
371		remainingTime -= time.Since(start)
372		if remainingTime < options.MinDelay || remainingTime <= 0 {
373			break
374		}
375
376		// compute exponential backoff between waiter retries
377		delay, err := smithywaiter.ComputeDelay(
378			attempt, options.MinDelay, options.MaxDelay, remainingTime,
379		)
380		if err != nil {
381			return fmt.Errorf("error computing waiter delay, %w", err)
382		}
383
384		remainingTime -= delay
385		// sleep for the delay amount before invoking a request
386		if err := smithytime.SleepWithContext(ctx, delay); err != nil {
387			return fmt.Errorf("request cancelled while waiting, %w", err)
388		}
389	}
390	return fmt.Errorf("exceeded max wait time for VpcAvailable waiter")
391}
392
393func vpcAvailableStateRetryable(ctx context.Context, input *DescribeVpcsInput, output *DescribeVpcsOutput, err error) (bool, error) {
394
395	if err == nil {
396		pathValue, err := jmespath.Search("Vpcs[].State", output)
397		if err != nil {
398			return false, fmt.Errorf("error evaluating waiter state: %w", err)
399		}
400
401		expectedValue := "available"
402		var match = true
403		listOfValues, ok := pathValue.([]interface{})
404		if !ok {
405			return false, fmt.Errorf("waiter comparator expected list got %T", pathValue)
406		}
407
408		if len(listOfValues) == 0 {
409			match = false
410		}
411		for _, v := range listOfValues {
412			value, ok := v.(types.VpcState)
413			if !ok {
414				return false, fmt.Errorf("waiter comparator expected types.VpcState value, got %T", pathValue)
415			}
416
417			if string(value) != expectedValue {
418				match = false
419			}
420		}
421
422		if match {
423			return false, nil
424		}
425	}
426
427	return true, nil
428}
429
430func newServiceMetadataMiddleware_opDescribeVpcs(region string) *awsmiddleware.RegisterServiceMetadata {
431	return &awsmiddleware.RegisterServiceMetadata{
432		Region:        region,
433		ServiceID:     ServiceID,
434		SigningName:   "ec2",
435		OperationName: "DescribeVpcs",
436	}
437}
438