Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d/aws_cloudwatch_event_source: New Data Source #19219

Merged
merged 9 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
3 changes: 3 additions & 0 deletions .changelog/19219.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_cloudwatch_event_source
```
72 changes: 72 additions & 0 deletions aws/data_source_aws_cloudwatch_event_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
events "github.com/aws/aws-sdk-go/service/cloudwatchevents"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceAwsCloudWatchEventSource() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsCloudWatchEventSourceRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"name_prefix": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"created_by": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceAwsCloudWatchEventSourceRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatcheventsconn

input := &events.ListEventSourcesInput{}
if v, ok := d.GetOk("name_prefix"); ok {
input.NamePrefix = aws.String(v.(string))
}

log.Printf("[DEBUG] Listing cloudwatch Event sources: %s", input)

resp, err := conn.ListEventSources(input)
if err != nil {
return fmt.Errorf("error listing cloudwatch event sources: %w", err)
}

if resp == nil || len(resp.EventSources) == 0 {
return fmt.Errorf("no matching partner event source")
}
if len(resp.EventSources) > 1 {
return fmt.Errorf("multiple event sources matched; use additional constraints to reduce matches to a single event source")
}

es := resp.EventSources[0]

d.SetId(aws.StringValue(es.Name))
d.Set("arn", es.Arn)
d.Set("created_by", es.CreatedBy)
d.Set("name", es.Name)
d.Set("state", es.State)

return nil
}
51 changes: 51 additions & 0 deletions aws/data_source_aws_cloudwatch_event_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package aws

import (
"fmt"
"os"
"strings"
"testing"

"github.com/aws/aws-sdk-go/service/cloudwatchevents"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceAwsCloudWatchEventSource(t *testing.T) {
key := "EVENT_BRIDGE_PARTNER_EVENT_SOURCE_NAME"
busName := os.Getenv(key)
if busName == "" {
t.Skipf("Environment variable %s is not set", key)
}

parts := strings.Split(busName, "/")
if len(parts) < 2 {
t.Errorf("unable to parse partner event bus name %s", busName)
}
createdBy := parts[0] + "/" + parts[1]

dataSourceName := "data.aws_cloudwatch_event_source.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, cloudwatchevents.EndpointsID),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAwsDataSourcePartnerEventSourceConfig(busName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "name", busName),
resource.TestCheckResourceAttr(dataSourceName, "created_by", createdBy),
resource.TestCheckResourceAttrSet(dataSourceName, "arn"),
),
},
},
})
}

func testAccAwsDataSourcePartnerEventSourceConfig(namePrefix string) string {
return fmt.Sprintf(`
data "aws_cloudwatch_event_source" "test" {
name_prefix = "%s"
}
`, namePrefix)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func Provider() *schema.Provider {
"aws_cloudfront_origin_request_policy": dataSourceAwsCloudFrontOriginRequestPolicy(),
"aws_cloudhsm_v2_cluster": dataSourceCloudHsmV2Cluster(),
"aws_cloudtrail_service_account": dataSourceAwsCloudTrailServiceAccount(),
"aws_cloudwatch_event_source": dataSourceAwsCloudWatchEventSource(),
"aws_cloudwatch_log_group": dataSourceAwsCloudwatchLogGroup(),
"aws_codeartifact_authorization_token": dataSourceAwsCodeArtifactAuthorizationToken(),
"aws_codeartifact_repository_endpoint": dataSourceAwsCodeArtifactRepositoryEndpoint(),
Expand Down
36 changes: 36 additions & 0 deletions website/docs/d/cloudwatch_event_source.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
subcategory: "CloudWatch"
layout: "aws"
page_title: "AWS: aws_cloudwatch_event_source"
description: |-
Get information on an EventBridge (Cloudwatch) Event Source.
---

# Data Source: aws_cloudwatch_event_source

Use this data source to get information about an EventBridge Partner Event Source. This data source will only return one partner event source. An error will be returned if multiple sources match the same name prefix.

~> **Note:** EventBridge was formerly known as CloudWatch Events. The functionality is identical.

## Example Usage

```terraform
data "aws_cloudwatch_event_source" "examplepartner" {
name_prefix = "aws.partner/examplepartner.com"
}
```

## Argument Reference

The following arguments are supported:

* `name_prefix` - (Optional) Specifying this limits the results to only those partner event sources with names that start with the specified prefix

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `arn` - The ARN of the partner event source
* `created_by` - The name of the SaaS partner that created the event source
* `name` - The name of the event source
* `state` - The state of the event source (`ACTIVE` or `PENDING`)