-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
d/aws_cloudwatch_event_source: New Data Source (#19219)
* d/aws_cloudwatch_event_source: New Data Source * changelog * Remove unnused variable * Register datasource at provider * Tidy up after rebase. * Add documentation for `name` attribute. * Use the value of 'EVENT_BRIDGE_PARTNER_EVENT_SOURCE_NAME' as the name prefix. * Fix importlint errors. * Fix awsproviderlint error 'AT003: acceptance test function name should include underscore'. Co-authored-by: Kit Ewbank <Kit_Ewbank@hotmail.com>
- Loading branch information
Showing
5 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-data-source | ||
aws_cloudwatch_event_source | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_basic(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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) |