Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/aws/aws-sdk-go-v2/config v1.28.0
github.com/aws/aws-sdk-go-v2/service/licensemanager v1.29.2
github.com/aws/aws-sdk-go-v2/service/marketplacemetering v1.25.2
github.com/aws/smithy-go v1.22.0
github.com/bradleyjkemp/cupaloy/v2 v2.8.0
github.com/cloudquery/cloudquery-api-go v1.13.1
github.com/cloudquery/plugin-pb-go v1.25.0
Expand Down Expand Up @@ -60,7 +61,6 @@ require (
github.com/aws/aws-sdk-go-v2/service/sso v1.24.2 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.2 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.32.2 // indirect
github.com/aws/smithy-go v1.22.0 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
Expand Down
18 changes: 16 additions & 2 deletions premium/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
awsConfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/marketplacemetering"
"github.com/aws/aws-sdk-go-v2/service/marketplacemetering/types"
"github.com/aws/smithy-go"
cqapi "github.com/cloudquery/cloudquery-api-go"
"github.com/cloudquery/cloudquery-api-go/auth"
"github.com/cloudquery/cloudquery-api-go/config"
Expand Down Expand Up @@ -274,7 +275,8 @@ func NewUsageClient(meta plugin.Meta, ops ...UsageClientOptions) (UsageClient, e
}

func (u *BatchUpdater) setupAWSMarketplace() error {
cfg, err := awsConfig.LoadDefaultConfig(context.TODO())
ctx := context.TODO()
cfg, err := awsConfig.LoadDefaultConfig(ctx)
if err != nil {
return fmt.Errorf("failed to load AWS config: %w", err)
}
Expand All @@ -288,7 +290,19 @@ func (u *BatchUpdater) setupAWSMarketplace() error {

u.minTimeBetweenFlushes = 1 * time.Minute
u.backgroundUpdater()
return nil

_, err = u.awsMarketplaceClient.MeterUsage(ctx, &marketplacemetering.MeterUsageInput{
ProductCode: aws.String(awsMarketplaceProductCode()),
Timestamp: aws.Time(time.Now()),
UsageDimension: aws.String("rows"),
UsageQuantity: aws.Int32(int32(0)),
DryRun: aws.Bool(true),
})
var apiErr smithy.APIError
if errors.As(err, &apiErr) && apiErr.ErrorCode() == "DryRunOperation" {
return nil
}
return fmt.Errorf("failed dry run invocation with error: %w", err)
}

func isAWSMarketplace() bool {
Expand Down
21 changes: 19 additions & 2 deletions premium/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/marketplacemetering"
"github.com/aws/aws-sdk-go-v2/service/marketplacemetering/types"
"github.com/aws/smithy-go"
cqapi "github.com/cloudquery/cloudquery-api-go"
"github.com/cloudquery/cloudquery-api-go/auth"
"github.com/cloudquery/cloudquery-api-go/config"
Expand Down Expand Up @@ -366,6 +367,15 @@ func TestUsageService_AWSMarketplaceDone(t *testing.T) {
m := mocks.NewMockAWSMarketplaceClientInterface(ctrl)

out := marketplacemetering.MeterUsageOutput{}
inTest := meteringInput{
marketplacemetering.MeterUsageInput{
ProductCode: aws.String("2a8bdkarwqrp0tmo4errl65s7"),
UsageDimension: aws.String("rows"),
UsageQuantity: aws.Int32(int32(0)),
DryRun: aws.Bool(true)},
}
errTest := smithy.GenericAPIError{Code: "DryRunOperation", Message: "No errors detected in dry run"}

in := meteringInput{
MeterUsageInput: marketplacemetering.MeterUsageInput{
ProductCode: aws.String("2a8bdkarwqrp0tmo4errl65s7"),
Expand All @@ -391,7 +401,12 @@ func TestUsageService_AWSMarketplaceDone(t *testing.T) {
},
}
assert.NoError(t, faker.FakeObject(&out))
m.EXPECT().MeterUsage(gomock.Any(), in).Return(&out, nil)

gomock.InOrder(
m.EXPECT().MeterUsage(gomock.Any(), inTest).Return(&out, &errTest),
m.EXPECT().MeterUsage(gomock.Any(), in).Return(&out, nil),
)

t.Setenv("CQ_AWS_MARKETPLACE_CONTAINER", "true")
usageClient := newClient(t, nil, WithBatchLimit(50), WithAWSMarketplaceClient(m))

Expand Down Expand Up @@ -909,7 +924,9 @@ func (mi meteringInput) Matches(x any) bool {
if aws.ToInt32(testInput.UsageQuantity) != aws.ToInt32(mi.UsageQuantity) {
return false
}

if aws.ToBool(testInput.DryRun) != aws.ToBool(mi.DryRun) {
return false
}
return true
}

Expand Down
Loading