Skip to content

Commit

Permalink
AWS Marketplace metering
Browse files Browse the repository at this point in the history
  • Loading branch information
wardviaene committed Jun 13, 2024
1 parent 910a97a commit d5c2374
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
28 changes: 28 additions & 0 deletions pkg/awsmarketplace/meter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package awsmarketplace

import (
"fmt"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/marketplacemetering"
)

func reportUsage(sess *session.Session, productCode string) {
svc := marketplacemetering.New(sess)

for {
_, err := svc.MeterUsage(&marketplacemetering.MeterUsageInput{
ProductCode: aws.String(productCode),
Timestamp: aws.Time(time.Now()),
UsageDimension: aws.String("HourlyUsage"),
UsageQuantity: aws.Int64(1),
})

if err != nil {
fmt.Printf("MeterUsage error: %s\n", err)
}
time.Sleep(1 * time.Hour)
}
}
8 changes: 5 additions & 3 deletions pkg/awsmarketplace/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ func Register(awsRegion string) error {
return fmt.Errorf("couldn't initialize S3: %s", err)
}

productCode := os.Getenv("PROD_CODE")

// Create a MarketplaceMetering client from just a session.
svc := marketplacemetering.New(sess)

out, err := svc.RegisterUsage(&marketplacemetering.RegisterUsageInput{
ProductCode: aws.String(os.Getenv("PROD_CODE")),
_, err = svc.RegisterUsage(&marketplacemetering.RegisterUsageInput{
ProductCode: aws.String(productCode),
PublicKeyVersion: aws.Int64(1),
})

if err != nil {
return fmt.Errorf("RegisterUsage error: %s", err)
}

fmt.Printf("Response from RegisterUsage API call: %s\n", aws.StringValue(out.Signature))
go reportUsage(sess, productCode)

return nil
}
3 changes: 2 additions & 1 deletion terraform/iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ resource "aws_iam_role_policy" "roxprox-task-role" {
{
"Effect": "Allow",
"Action": [
"aws-marketplace:RegisterUsage"
"aws-marketplace:RegisterUsage",
"aws-marketplace:MeterUsage"
],
"Resource": "*"
}
Expand Down

0 comments on commit d5c2374

Please sign in to comment.