1// Code generated by smithy-go-codegen DO NOT EDIT.
2
3package sagemaker
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/sagemaker/types"
11	"github.com/aws/smithy-go/middleware"
12	smithyhttp "github.com/aws/smithy-go/transport/http"
13	"time"
14)
15
16// Lists training jobs. When StatusEquals and MaxResults are set at the same time,
17// the MaxResults number of training jobs are first retrieved ignoring the
18// StatusEquals parameter and then they are filtered by the StatusEquals parameter,
19// which is returned as a response. For example, if ListTrainingJobs is invoked
20// with the following parameters: { ... MaxResults: 100, StatusEquals: InProgress
21// ... } Then, 100 trainings jobs with any status including those other than
22// InProgress are selected first (sorted according the creation time, from the
23// latest to the oldest) and those with status InProgress are returned. You can
24// quickly test the API using the following AWS CLI code. aws sagemaker
25// list-training-jobs --max-results 100 --status-equals InProgress
26func (c *Client) ListTrainingJobs(ctx context.Context, params *ListTrainingJobsInput, optFns ...func(*Options)) (*ListTrainingJobsOutput, error) {
27	if params == nil {
28		params = &ListTrainingJobsInput{}
29	}
30
31	result, metadata, err := c.invokeOperation(ctx, "ListTrainingJobs", params, optFns, addOperationListTrainingJobsMiddlewares)
32	if err != nil {
33		return nil, err
34	}
35
36	out := result.(*ListTrainingJobsOutput)
37	out.ResultMetadata = metadata
38	return out, nil
39}
40
41type ListTrainingJobsInput struct {
42
43	// A filter that returns only training jobs created after the specified time
44	// (timestamp).
45	CreationTimeAfter *time.Time
46
47	// A filter that returns only training jobs created before the specified time
48	// (timestamp).
49	CreationTimeBefore *time.Time
50
51	// A filter that returns only training jobs modified after the specified time
52	// (timestamp).
53	LastModifiedTimeAfter *time.Time
54
55	// A filter that returns only training jobs modified before the specified time
56	// (timestamp).
57	LastModifiedTimeBefore *time.Time
58
59	// The maximum number of training jobs to return in the response.
60	MaxResults *int32
61
62	// A string in the training job name. This filter returns only training jobs whose
63	// name contains the specified string.
64	NameContains *string
65
66	// If the result of the previous ListTrainingJobs request was truncated, the
67	// response includes a NextToken. To retrieve the next set of training jobs, use
68	// the token in the next request.
69	NextToken *string
70
71	// The field to sort results by. The default is CreationTime.
72	SortBy types.SortBy
73
74	// The sort order for results. The default is Ascending.
75	SortOrder types.SortOrder
76
77	// A filter that retrieves only training jobs with a specific status.
78	StatusEquals types.TrainingJobStatus
79}
80
81type ListTrainingJobsOutput struct {
82
83	// An array of TrainingJobSummary objects, each listing a training job.
84	//
85	// This member is required.
86	TrainingJobSummaries []types.TrainingJobSummary
87
88	// If the response is truncated, Amazon SageMaker returns this token. To retrieve
89	// the next set of training jobs, use it in the subsequent request.
90	NextToken *string
91
92	// Metadata pertaining to the operation's result.
93	ResultMetadata middleware.Metadata
94}
95
96func addOperationListTrainingJobsMiddlewares(stack *middleware.Stack, options Options) (err error) {
97	err = stack.Serialize.Add(&awsAwsjson11_serializeOpListTrainingJobs{}, middleware.After)
98	if err != nil {
99		return err
100	}
101	err = stack.Deserialize.Add(&awsAwsjson11_deserializeOpListTrainingJobs{}, middleware.After)
102	if err != nil {
103		return err
104	}
105	if err = addSetLoggerMiddleware(stack, options); err != nil {
106		return err
107	}
108	if err = awsmiddleware.AddClientRequestIDMiddleware(stack); err != nil {
109		return err
110	}
111	if err = smithyhttp.AddComputeContentLengthMiddleware(stack); err != nil {
112		return err
113	}
114	if err = addResolveEndpointMiddleware(stack, options); err != nil {
115		return err
116	}
117	if err = v4.AddComputePayloadSHA256Middleware(stack); err != nil {
118		return err
119	}
120	if err = addRetryMiddlewares(stack, options); err != nil {
121		return err
122	}
123	if err = addHTTPSignerV4Middleware(stack, options); err != nil {
124		return err
125	}
126	if err = awsmiddleware.AddRawResponseToMetadata(stack); err != nil {
127		return err
128	}
129	if err = awsmiddleware.AddRecordResponseTiming(stack); err != nil {
130		return err
131	}
132	if err = addClientUserAgent(stack); err != nil {
133		return err
134	}
135	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
136		return err
137	}
138	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
139		return err
140	}
141	if err = stack.Initialize.Add(newServiceMetadataMiddleware_opListTrainingJobs(options.Region), middleware.Before); err != nil {
142		return err
143	}
144	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
145		return err
146	}
147	if err = addResponseErrorMiddleware(stack); err != nil {
148		return err
149	}
150	if err = addRequestResponseLogging(stack, options); err != nil {
151		return err
152	}
153	return nil
154}
155
156// ListTrainingJobsAPIClient is a client that implements the ListTrainingJobs
157// operation.
158type ListTrainingJobsAPIClient interface {
159	ListTrainingJobs(context.Context, *ListTrainingJobsInput, ...func(*Options)) (*ListTrainingJobsOutput, error)
160}
161
162var _ ListTrainingJobsAPIClient = (*Client)(nil)
163
164// ListTrainingJobsPaginatorOptions is the paginator options for ListTrainingJobs
165type ListTrainingJobsPaginatorOptions struct {
166	// The maximum number of training jobs to return in the response.
167	Limit int32
168
169	// Set to true if pagination should stop if the service returns a pagination token
170	// that matches the most recent token provided to the service.
171	StopOnDuplicateToken bool
172}
173
174// ListTrainingJobsPaginator is a paginator for ListTrainingJobs
175type ListTrainingJobsPaginator struct {
176	options   ListTrainingJobsPaginatorOptions
177	client    ListTrainingJobsAPIClient
178	params    *ListTrainingJobsInput
179	nextToken *string
180	firstPage bool
181}
182
183// NewListTrainingJobsPaginator returns a new ListTrainingJobsPaginator
184func NewListTrainingJobsPaginator(client ListTrainingJobsAPIClient, params *ListTrainingJobsInput, optFns ...func(*ListTrainingJobsPaginatorOptions)) *ListTrainingJobsPaginator {
185	if params == nil {
186		params = &ListTrainingJobsInput{}
187	}
188
189	options := ListTrainingJobsPaginatorOptions{}
190	if params.MaxResults != nil {
191		options.Limit = *params.MaxResults
192	}
193
194	for _, fn := range optFns {
195		fn(&options)
196	}
197
198	return &ListTrainingJobsPaginator{
199		options:   options,
200		client:    client,
201		params:    params,
202		firstPage: true,
203	}
204}
205
206// HasMorePages returns a boolean indicating whether more pages are available
207func (p *ListTrainingJobsPaginator) HasMorePages() bool {
208	return p.firstPage || p.nextToken != nil
209}
210
211// NextPage retrieves the next ListTrainingJobs page.
212func (p *ListTrainingJobsPaginator) NextPage(ctx context.Context, optFns ...func(*Options)) (*ListTrainingJobsOutput, error) {
213	if !p.HasMorePages() {
214		return nil, fmt.Errorf("no more pages available")
215	}
216
217	params := *p.params
218	params.NextToken = p.nextToken
219
220	var limit *int32
221	if p.options.Limit > 0 {
222		limit = &p.options.Limit
223	}
224	params.MaxResults = limit
225
226	result, err := p.client.ListTrainingJobs(ctx, &params, optFns...)
227	if err != nil {
228		return nil, err
229	}
230	p.firstPage = false
231
232	prevToken := p.nextToken
233	p.nextToken = result.NextToken
234
235	if p.options.StopOnDuplicateToken && prevToken != nil && p.nextToken != nil && *prevToken == *p.nextToken {
236		p.nextToken = nil
237	}
238
239	return result, nil
240}
241
242func newServiceMetadataMiddleware_opListTrainingJobs(region string) *awsmiddleware.RegisterServiceMetadata {
243	return &awsmiddleware.RegisterServiceMetadata{
244		Region:        region,
245		ServiceID:     ServiceID,
246		SigningName:   "sagemaker",
247		OperationName: "ListTrainingJobs",
248	}
249}
250