diff --git a/appstream/provider.go b/appstream/provider.go index 7ea1f60..9daeb88 100644 --- a/appstream/provider.go +++ b/appstream/provider.go @@ -64,6 +64,7 @@ func Provider() terraform.ResourceProvider { "appstream_stack": resourceAppstreamStack(), "appstream_image_builder": resourceAppstreamImageBuilder(), "appstream_fleet": resourceAppstreamFleet(), + "appstream_usage_report_subscription": resourceAppstreamUsageReportSubscription(), }, } provider.ConfigureFunc = func(d *schema.ResourceData) (interface{}, error) { diff --git a/appstream/resource_usage_report_subscription.go b/appstream/resource_usage_report_subscription.go new file mode 100644 index 0000000..87609af --- /dev/null +++ b/appstream/resource_usage_report_subscription.go @@ -0,0 +1,93 @@ +package appstream + +import ( + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/appstream" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "log" +) + + +func resourceAppstreamUsageReportSubscription() *schema.Resource { + return &schema.Resource { + Create: resourceAppstreamUsageReportSubscriptionCreate, + Read: resourceAppstreamUsageReportSubscriptionRead, + Delete: resourceAppstreamUsageReportSubscriptionDelete, + Importer: &schema.ResourceImporter { + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "s3_bucket_name": { + Type: schema.TypeString, + Computed: true, + }, + "schedule": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func resourceAppstreamUsageReportSubscriptionCreate(d *schema.ResourceData, meta interface{}) error { + + svc := meta.(*AWSClient).appstreamconn + + CreateUsageReportSubscriptionInputOpts := &appstream.CreateUsageReportSubscriptionInput{} + + log.Printf("[DEBUG] Run configuration: %s", CreateUsageReportSubscriptionInputOpts) + + resp, err := svc.CreateUsageReportSubscription(CreateUsageReportSubscriptionInputOpts) + + if err != nil { + log.Printf("[ERROR] Error creating Appstream Usage Report: %s", err) + return err + } + + log.Printf("[DEBUG] Usage Report created %s", resp) + + d.SetId(*resp.S3BucketName) + d.Set("s3_bucket_name", resp.S3BucketName) + d.Set("schedule", resp.Schedule) + + return nil +} + +func resourceAppstreamUsageReportSubscriptionRead(d *schema.ResourceData, meta interface{}) error { + + svc := meta.(*AWSClient).appstreamconn + + resp, err := svc.DescribeUsageReportSubscriptions(&appstream.DescribeUsageReportSubscriptionsInput{}) + if err != nil { + log.Printf("[ERROR] Error describing Appstream Usage Reports Subscription: %s", err) + return err + } + for _, v := range resp.UsageReportSubscriptions { + + if aws.StringValue(v.S3BucketName) == d.Get("s3_bucket_name") { + d.Set("s3_bucket_name", v.S3BucketName) + d.Set("schedule", v.Schedule) + + return nil + } + } + + d.SetId("") + return nil + +} + +func resourceAppstreamUsageReportSubscriptionDelete(d *schema.ResourceData, meta interface{}) error { + svc := meta.(*AWSClient).appstreamconn + + del, err := svc.DeleteUsageReportSubscription(&appstream.DeleteUsageReportSubscriptionInput{}) + + if err != nil { + log.Printf("[ERROR] Error deleting Appstream Usage Report Subscription: %s", err) + return err + } + log.Printf("[DEBUG] %s", del) + + return nil +} diff --git a/build.groovy b/build.groovy index 9853ae5..680b7a4 100644 --- a/build.groovy +++ b/build.groovy @@ -19,7 +19,7 @@ node ("master") { "GOPATH=${env.HOME}/go", "GOBIN=${env.HOME}/go/bin", ]){ - sh "go build -o ${env.HOME}/.terraform.d/plugins/terraform-provider-appstream_1.0.9" + sh "go build -o ${env.HOME}/.terraform.d/plugins/terraform-provider-appstream_1.0.10" } } } \ No newline at end of file diff --git a/examples/appstream.tf b/examples/appstream.tf index 42aebed..2a13c64 100644 --- a/examples/appstream.tf +++ b/examples/appstream.tf @@ -1,9 +1,9 @@ provider "appstream" { - version = "1.0.9" - assume_role { - role_arn = var.assume_role_arn - } - region = var.region_primary + version = "1.0.10" +// assume_role { +// role_arn = var.assume_role_arn +// } +// region = var.region_primary } @@ -63,3 +63,6 @@ resource "appstream_fleet" "test-fleet" { } state = "RUNNING" } + +resource "appstream_usage_report_subscription" "usage_report" { +} \ No newline at end of file