1// Code generated by smithy-go-codegen DO NOT EDIT. 2 3package guardduty 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/guardduty/types" 11 "github.com/aws/smithy-go/middleware" 12 smithyhttp "github.com/aws/smithy-go/transport/http" 13) 14 15// Lists Amazon GuardDuty usage statistics over the last 30 days for the specified 16// detector ID. For newly enabled detectors or data sources the cost returned will 17// include only the usage so far under 30 days, this may differ from the cost 18// metrics in the console, which projects usage over 30 days to provide a monthly 19// cost estimate. For more information see Understanding How Usage Costs are 20// Calculated 21// (https://docs.aws.amazon.com/guardduty/latest/ug/monitoring_costs.html#usage-calculations). 22func (c *Client) GetUsageStatistics(ctx context.Context, params *GetUsageStatisticsInput, optFns ...func(*Options)) (*GetUsageStatisticsOutput, error) { 23 if params == nil { 24 params = &GetUsageStatisticsInput{} 25 } 26 27 result, metadata, err := c.invokeOperation(ctx, "GetUsageStatistics", params, optFns, addOperationGetUsageStatisticsMiddlewares) 28 if err != nil { 29 return nil, err 30 } 31 32 out := result.(*GetUsageStatisticsOutput) 33 out.ResultMetadata = metadata 34 return out, nil 35} 36 37type GetUsageStatisticsInput struct { 38 39 // The ID of the detector that specifies the GuardDuty service whose usage 40 // statistics you want to retrieve. 41 // 42 // This member is required. 43 DetectorId *string 44 45 // Represents the criteria used for querying usage. 46 // 47 // This member is required. 48 UsageCriteria *types.UsageCriteria 49 50 // The type of usage statistics to retrieve. 51 // 52 // This member is required. 53 UsageStatisticType types.UsageStatisticType 54 55 // The maximum number of results to return in the response. 56 MaxResults int32 57 58 // A token to use for paginating results that are returned in the response. Set the 59 // value of this parameter to null for the first request to a list action. For 60 // subsequent calls, use the NextToken value returned from the previous request to 61 // continue listing results after the first page. 62 NextToken *string 63 64 // The currency unit you would like to view your usage statistics in. Current valid 65 // values are USD. 66 Unit *string 67} 68 69type GetUsageStatisticsOutput struct { 70 71 // The pagination parameter to be used on the next list operation to retrieve more 72 // items. 73 NextToken *string 74 75 // The usage statistics object. If a UsageStatisticType was provided, the objects 76 // representing other types will be null. 77 UsageStatistics *types.UsageStatistics 78 79 // Metadata pertaining to the operation's result. 80 ResultMetadata middleware.Metadata 81} 82 83func addOperationGetUsageStatisticsMiddlewares(stack *middleware.Stack, options Options) (err error) { 84 err = stack.Serialize.Add(&awsRestjson1_serializeOpGetUsageStatistics{}, middleware.After) 85 if err != nil { 86 return err 87 } 88 err = stack.Deserialize.Add(&awsRestjson1_deserializeOpGetUsageStatistics{}, middleware.After) 89 if err != nil { 90 return err 91 } 92 if err = addSetLoggerMiddleware(stack, options); err != nil { 93 return err 94 } 95 if err = awsmiddleware.AddClientRequestIDMiddleware(stack); err != nil { 96 return err 97 } 98 if err = smithyhttp.AddComputeContentLengthMiddleware(stack); err != nil { 99 return err 100 } 101 if err = addResolveEndpointMiddleware(stack, options); err != nil { 102 return err 103 } 104 if err = v4.AddComputePayloadSHA256Middleware(stack); err != nil { 105 return err 106 } 107 if err = addRetryMiddlewares(stack, options); err != nil { 108 return err 109 } 110 if err = addHTTPSignerV4Middleware(stack, options); err != nil { 111 return err 112 } 113 if err = awsmiddleware.AddRawResponseToMetadata(stack); err != nil { 114 return err 115 } 116 if err = awsmiddleware.AddRecordResponseTiming(stack); err != nil { 117 return err 118 } 119 if err = addClientUserAgent(stack); err != nil { 120 return err 121 } 122 if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil { 123 return err 124 } 125 if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil { 126 return err 127 } 128 if err = addOpGetUsageStatisticsValidationMiddleware(stack); err != nil { 129 return err 130 } 131 if err = stack.Initialize.Add(newServiceMetadataMiddleware_opGetUsageStatistics(options.Region), middleware.Before); err != nil { 132 return err 133 } 134 if err = addRequestIDRetrieverMiddleware(stack); err != nil { 135 return err 136 } 137 if err = addResponseErrorMiddleware(stack); err != nil { 138 return err 139 } 140 if err = addRequestResponseLogging(stack, options); err != nil { 141 return err 142 } 143 return nil 144} 145 146// GetUsageStatisticsAPIClient is a client that implements the GetUsageStatistics 147// operation. 148type GetUsageStatisticsAPIClient interface { 149 GetUsageStatistics(context.Context, *GetUsageStatisticsInput, ...func(*Options)) (*GetUsageStatisticsOutput, error) 150} 151 152var _ GetUsageStatisticsAPIClient = (*Client)(nil) 153 154// GetUsageStatisticsPaginatorOptions is the paginator options for 155// GetUsageStatistics 156type GetUsageStatisticsPaginatorOptions struct { 157 // The maximum number of results to return in the response. 158 Limit int32 159 160 // Set to true if pagination should stop if the service returns a pagination token 161 // that matches the most recent token provided to the service. 162 StopOnDuplicateToken bool 163} 164 165// GetUsageStatisticsPaginator is a paginator for GetUsageStatistics 166type GetUsageStatisticsPaginator struct { 167 options GetUsageStatisticsPaginatorOptions 168 client GetUsageStatisticsAPIClient 169 params *GetUsageStatisticsInput 170 nextToken *string 171 firstPage bool 172} 173 174// NewGetUsageStatisticsPaginator returns a new GetUsageStatisticsPaginator 175func NewGetUsageStatisticsPaginator(client GetUsageStatisticsAPIClient, params *GetUsageStatisticsInput, optFns ...func(*GetUsageStatisticsPaginatorOptions)) *GetUsageStatisticsPaginator { 176 options := GetUsageStatisticsPaginatorOptions{} 177 if params.MaxResults != 0 { 178 options.Limit = params.MaxResults 179 } 180 181 for _, fn := range optFns { 182 fn(&options) 183 } 184 185 if params == nil { 186 params = &GetUsageStatisticsInput{} 187 } 188 189 return &GetUsageStatisticsPaginator{ 190 options: options, 191 client: client, 192 params: params, 193 firstPage: true, 194 } 195} 196 197// HasMorePages returns a boolean indicating whether more pages are available 198func (p *GetUsageStatisticsPaginator) HasMorePages() bool { 199 return p.firstPage || p.nextToken != nil 200} 201 202// NextPage retrieves the next GetUsageStatistics page. 203func (p *GetUsageStatisticsPaginator) NextPage(ctx context.Context, optFns ...func(*Options)) (*GetUsageStatisticsOutput, error) { 204 if !p.HasMorePages() { 205 return nil, fmt.Errorf("no more pages available") 206 } 207 208 params := *p.params 209 params.NextToken = p.nextToken 210 211 params.MaxResults = p.options.Limit 212 213 result, err := p.client.GetUsageStatistics(ctx, ¶ms, optFns...) 214 if err != nil { 215 return nil, err 216 } 217 p.firstPage = false 218 219 prevToken := p.nextToken 220 p.nextToken = result.NextToken 221 222 if p.options.StopOnDuplicateToken && prevToken != nil && p.nextToken != nil && *prevToken == *p.nextToken { 223 p.nextToken = nil 224 } 225 226 return result, nil 227} 228 229func newServiceMetadataMiddleware_opGetUsageStatistics(region string) *awsmiddleware.RegisterServiceMetadata { 230 return &awsmiddleware.RegisterServiceMetadata{ 231 Region: region, 232 ServiceID: ServiceID, 233 SigningName: "guardduty", 234 OperationName: "GetUsageStatistics", 235 } 236} 237