diff --git a/pkg/awsmarketplace/meter.go b/pkg/awsmarketplace/meter.go new file mode 100644 index 0000000..419b41c --- /dev/null +++ b/pkg/awsmarketplace/meter.go @@ -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) + } +} diff --git a/pkg/awsmarketplace/register.go b/pkg/awsmarketplace/register.go index ae3358d..388e673 100644 --- a/pkg/awsmarketplace/register.go +++ b/pkg/awsmarketplace/register.go @@ -15,11 +15,13 @@ 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), }) @@ -27,7 +29,7 @@ func Register(awsRegion string) error { 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 } diff --git a/terraform/iam.tf b/terraform/iam.tf index 1790ef4..a9321fc 100644 --- a/terraform/iam.tf +++ b/terraform/iam.tf @@ -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": "*" }