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	smithyhttp "github.com/aws/smithy-go/transport/http"
13)
14
15// Describes the specified Dedicated Hosts or all your Dedicated Hosts. The results
16// describe only the Dedicated Hosts in the Region you're currently using. All
17// listed instances consume capacity on your Dedicated Host. Dedicated Hosts that
18// have recently been released are listed with the state released.
19func (c *Client) DescribeHosts(ctx context.Context, params *DescribeHostsInput, optFns ...func(*Options)) (*DescribeHostsOutput, error) {
20	if params == nil {
21		params = &DescribeHostsInput{}
22	}
23
24	result, metadata, err := c.invokeOperation(ctx, "DescribeHosts", params, optFns, addOperationDescribeHostsMiddlewares)
25	if err != nil {
26		return nil, err
27	}
28
29	out := result.(*DescribeHostsOutput)
30	out.ResultMetadata = metadata
31	return out, nil
32}
33
34type DescribeHostsInput struct {
35
36	// The filters.
37	//
38	// * auto-placement - Whether auto-placement is enabled or disabled
39	// (on | off).
40	//
41	// * availability-zone - The Availability Zone of the host.
42	//
43	// *
44	// client-token - The idempotency token that you provided when you allocated the
45	// host.
46	//
47	// * host-reservation-id - The ID of the reservation assigned to this
48	// host.
49	//
50	// * instance-type - The instance type size that the Dedicated Host is
51	// configured to support.
52	//
53	// * state - The allocation state of the Dedicated Host
54	// (available | under-assessment | permanent-failure | released |
55	// released-permanent-failure).
56	//
57	// * tag-key - The key of a tag assigned to the
58	// resource. Use this filter to find all resources assigned a tag with a specific
59	// key, regardless of the tag value.
60	Filter []types.Filter
61
62	// The IDs of the Dedicated Hosts. The IDs are used for targeted instance launches.
63	HostIds []string
64
65	// The maximum number of results to return for the request in a single page. The
66	// remaining results can be seen by sending another request with the returned
67	// nextToken value. This value can be between 5 and 500. If maxResults is given a
68	// larger value than 500, you receive an error. You cannot specify this parameter
69	// and the host IDs parameter in the same request.
70	MaxResults int32
71
72	// The token to use to retrieve the next page of results.
73	NextToken *string
74}
75
76type DescribeHostsOutput struct {
77
78	// Information about the Dedicated Hosts.
79	Hosts []types.Host
80
81	// The token to use to retrieve the next page of results. This value is null when
82	// there are no more results to return.
83	NextToken *string
84
85	// Metadata pertaining to the operation's result.
86	ResultMetadata middleware.Metadata
87}
88
89func addOperationDescribeHostsMiddlewares(stack *middleware.Stack, options Options) (err error) {
90	err = stack.Serialize.Add(&awsEc2query_serializeOpDescribeHosts{}, middleware.After)
91	if err != nil {
92		return err
93	}
94	err = stack.Deserialize.Add(&awsEc2query_deserializeOpDescribeHosts{}, middleware.After)
95	if err != nil {
96		return err
97	}
98	if err = addSetLoggerMiddleware(stack, options); err != nil {
99		return err
100	}
101	if err = awsmiddleware.AddClientRequestIDMiddleware(stack); err != nil {
102		return err
103	}
104	if err = smithyhttp.AddComputeContentLengthMiddleware(stack); err != nil {
105		return err
106	}
107	if err = addResolveEndpointMiddleware(stack, options); err != nil {
108		return err
109	}
110	if err = v4.AddComputePayloadSHA256Middleware(stack); err != nil {
111		return err
112	}
113	if err = addRetryMiddlewares(stack, options); err != nil {
114		return err
115	}
116	if err = addHTTPSignerV4Middleware(stack, options); err != nil {
117		return err
118	}
119	if err = awsmiddleware.AddRawResponseToMetadata(stack); err != nil {
120		return err
121	}
122	if err = awsmiddleware.AddRecordResponseTiming(stack); err != nil {
123		return err
124	}
125	if err = addClientUserAgent(stack); err != nil {
126		return err
127	}
128	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
129		return err
130	}
131	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
132		return err
133	}
134	if err = stack.Initialize.Add(newServiceMetadataMiddleware_opDescribeHosts(options.Region), middleware.Before); err != nil {
135		return err
136	}
137	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
138		return err
139	}
140	if err = addResponseErrorMiddleware(stack); err != nil {
141		return err
142	}
143	if err = addRequestResponseLogging(stack, options); err != nil {
144		return err
145	}
146	return nil
147}
148
149// DescribeHostsAPIClient is a client that implements the DescribeHosts operation.
150type DescribeHostsAPIClient interface {
151	DescribeHosts(context.Context, *DescribeHostsInput, ...func(*Options)) (*DescribeHostsOutput, error)
152}
153
154var _ DescribeHostsAPIClient = (*Client)(nil)
155
156// DescribeHostsPaginatorOptions is the paginator options for DescribeHosts
157type DescribeHostsPaginatorOptions struct {
158	// The maximum number of results to return for the request in a single page. The
159	// remaining results can be seen by sending another request with the returned
160	// nextToken value. This value can be between 5 and 500. If maxResults is given a
161	// larger value than 500, you receive an error. You cannot specify this parameter
162	// and the host IDs parameter in the same request.
163	Limit int32
164
165	// Set to true if pagination should stop if the service returns a pagination token
166	// that matches the most recent token provided to the service.
167	StopOnDuplicateToken bool
168}
169
170// DescribeHostsPaginator is a paginator for DescribeHosts
171type DescribeHostsPaginator struct {
172	options   DescribeHostsPaginatorOptions
173	client    DescribeHostsAPIClient
174	params    *DescribeHostsInput
175	nextToken *string
176	firstPage bool
177}
178
179// NewDescribeHostsPaginator returns a new DescribeHostsPaginator
180func NewDescribeHostsPaginator(client DescribeHostsAPIClient, params *DescribeHostsInput, optFns ...func(*DescribeHostsPaginatorOptions)) *DescribeHostsPaginator {
181	options := DescribeHostsPaginatorOptions{}
182	if params.MaxResults != 0 {
183		options.Limit = params.MaxResults
184	}
185
186	for _, fn := range optFns {
187		fn(&options)
188	}
189
190	if params == nil {
191		params = &DescribeHostsInput{}
192	}
193
194	return &DescribeHostsPaginator{
195		options:   options,
196		client:    client,
197		params:    params,
198		firstPage: true,
199	}
200}
201
202// HasMorePages returns a boolean indicating whether more pages are available
203func (p *DescribeHostsPaginator) HasMorePages() bool {
204	return p.firstPage || p.nextToken != nil
205}
206
207// NextPage retrieves the next DescribeHosts page.
208func (p *DescribeHostsPaginator) NextPage(ctx context.Context, optFns ...func(*Options)) (*DescribeHostsOutput, error) {
209	if !p.HasMorePages() {
210		return nil, fmt.Errorf("no more pages available")
211	}
212
213	params := *p.params
214	params.NextToken = p.nextToken
215
216	params.MaxResults = p.options.Limit
217
218	result, err := p.client.DescribeHosts(ctx, &params, optFns...)
219	if err != nil {
220		return nil, err
221	}
222	p.firstPage = false
223
224	prevToken := p.nextToken
225	p.nextToken = result.NextToken
226
227	if p.options.StopOnDuplicateToken && prevToken != nil && p.nextToken != nil && *prevToken == *p.nextToken {
228		p.nextToken = nil
229	}
230
231	return result, nil
232}
233
234func newServiceMetadataMiddleware_opDescribeHosts(region string) *awsmiddleware.RegisterServiceMetadata {
235	return &awsmiddleware.RegisterServiceMetadata{
236		Region:        region,
237		ServiceID:     ServiceID,
238		SigningName:   "ec2",
239		OperationName: "DescribeHosts",
240	}
241}
242