-
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.
Add resource aws_cloudwatch_event_bus
- Loading branch information
1 parent
9da6ca2
commit d47b9a4
Showing
13 changed files
with
640 additions
and
37 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
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,118 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/structure" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/cloudwatchevents" | ||
) | ||
|
||
func resourceAwsCloudWatchEventBus() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsCloudWatchEventBusCreate, | ||
Read: resourceAwsCloudWatchEventBusRead, | ||
Delete: resourceAwsCloudWatchEventBusDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validateCloudWatchEventBusName, | ||
}, | ||
"event_source_name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
ValidateFunc: validateCloudWatchEventSourceName, | ||
}, | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"policy": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
StateFunc: func(v interface{}) string { | ||
json, _ := structure.NormalizeJsonString(v.(string)) | ||
return json | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsCloudWatchEventBusCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).cloudwatcheventsconn | ||
|
||
eventBusName := d.Get("name").(string) | ||
params := &cloudwatchevents.CreateEventBusInput{ | ||
Name: aws.String(eventBusName), | ||
} | ||
|
||
if v, ok := d.GetOk("event_source_name"); ok { | ||
if eventBusName != v.(string) { | ||
return fmt.Errorf("Event bus name %v must match event source name %v", eventBusName, v) | ||
} | ||
params.EventSourceName = aws.String(v.(string)) | ||
} else if strings.HasPrefix(eventBusName, "aws.") { | ||
return fmt.Errorf("EventBus name starting with 'aws.' is not valid: %v", eventBusName) | ||
} | ||
|
||
log.Printf("[DEBUG] Creating CloudWatch Event Bus: %v", params) | ||
|
||
_, err := conn.CreateEventBus(params) | ||
if err != nil { | ||
return fmt.Errorf("Creating CloudWatch Event Bus %v failed: %v", eventBusName, err) | ||
} | ||
|
||
d.SetId(eventBusName) | ||
|
||
log.Printf("[INFO] CloudWatch Event Bus %v created", d.Id()) | ||
|
||
return resourceAwsCloudWatchEventBusRead(d, meta) | ||
} | ||
|
||
func resourceAwsCloudWatchEventBusRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).cloudwatcheventsconn | ||
log.Printf("[DEBUG] Reading CloudWatch Event Bus: %v", d.Id()) | ||
|
||
input := &cloudwatchevents.DescribeEventBusInput{ | ||
Name: aws.String(d.Id()), | ||
} | ||
|
||
output, err := conn.DescribeEventBus(input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Found CloudWatch Event bus: %#v", *output) | ||
|
||
d.Set("arn", output.Arn) | ||
d.Set("name", output.Name) | ||
d.Set("policy", output.Policy) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsCloudWatchEventBusDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).cloudwatcheventsconn | ||
log.Printf("[INFO] Deleting CloudWatch Event Bus: %v", d.Id()) | ||
_, err := conn.DeleteEventBus(&cloudwatchevents.DeleteEventBusInput{ | ||
Name: aws.String(d.Id()), | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Error deleting CloudWatch Event Bus %v: %v", d.Id(), err) | ||
} | ||
log.Printf("[INFO] CloudWatch Event Bus %v deleted", d.Id()) | ||
|
||
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,184 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
events "github.com/aws/aws-sdk-go/service/cloudwatchevents" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
func init() { | ||
resource.AddTestSweepers("aws_cloudwatch_event_bus", &resource.Sweeper{ | ||
Name: "aws_cloudwatch_event_bus", | ||
F: testSweepCloudWatchEventBuses, | ||
}) | ||
} | ||
|
||
func testSweepCloudWatchEventBuses(region string) error { | ||
client, err := sharedClientForRegion(region) | ||
if err != nil { | ||
return fmt.Errorf("Error getting client: %s", err) | ||
} | ||
conn := client.(*AWSClient).cloudwatcheventsconn | ||
|
||
input := &events.ListEventBusesInput{} | ||
|
||
for { | ||
output, err := conn.ListEventBuses(input) | ||
if err != nil { | ||
if testSweepSkipSweepError(err) { | ||
log.Printf("[WARN] Skipping CloudWatch Event Bus sweep for %s: %s", region, err) | ||
return nil | ||
} | ||
return fmt.Errorf("Error retrieving CloudWatch Event Buses: %s", err) | ||
} | ||
|
||
if len(output.EventBuses) == 0 { | ||
log.Print("[DEBUG] No CloudWatch Event Buses to sweep") | ||
return nil | ||
} | ||
|
||
for _, eventBus := range output.EventBuses { | ||
name := aws.StringValue(eventBus.Name) | ||
if name == "default" { | ||
continue | ||
} | ||
|
||
log.Printf("[INFO] Deleting CloudWatch Event Bus %s", name) | ||
_, err := conn.DeleteEventBus(&events.DeleteEventBusInput{ | ||
Name: aws.String(name), | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Error deleting CloudWatch Event Bus %s: %s", name, err) | ||
} | ||
} | ||
|
||
if output.NextToken == nil { | ||
break | ||
} | ||
input.NextToken = output.NextToken | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func TestAccAWSCloudWatchEventBus_basic(t *testing.T) { | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSCloudWatchEventBusDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSCloudWatchEventBusConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCloudWatchEventBusExists("aws_cloudwatch_event_bus.foo"), | ||
resource.TestCheckResourceAttr("aws_cloudwatch_event_bus.foo", "name", "tf-acc-cw-event-bus"), | ||
), | ||
}, | ||
{ | ||
Config: testAccAWSCloudWatchEventBusConfigModified, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCloudWatchEventBusExists("aws_cloudwatch_event_bus.foo"), | ||
resource.TestCheckResourceAttr("aws_cloudwatch_event_bus.foo", "name", "tf-acc-cw-event-bus-mod"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSCloudWatchEventBus_withRule(t *testing.T) { | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSCloudWatchEventBusDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSCloudWatchEventBusConfigRule, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCloudWatchEventBusExists("aws_cloudwatch_event_bus.foo"), | ||
resource.TestCheckResourceAttr("aws_cloudwatch_event_bus.foo", "name", "tf-acc-cw-event-bus-with-rule"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAWSCloudWatchEventBusDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).cloudwatcheventsconn | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_cloudwatch_event_bus" { | ||
continue | ||
} | ||
|
||
params := events.DescribeEventBusInput{ | ||
Name: aws.String(rs.Primary.ID), | ||
} | ||
|
||
resp, err := conn.DescribeEventBus(¶ms) | ||
|
||
if err == nil { | ||
return fmt.Errorf("CloudWatch Event Bus %q still exists: %s", | ||
rs.Primary.ID, resp) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCheckCloudWatchEventBusExists(n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
conn := testAccProvider.Meta().(*AWSClient).cloudwatcheventsconn | ||
params := events.DescribeEventBusInput{ | ||
Name: aws.String(rs.Primary.ID), | ||
} | ||
resp, err := conn.DescribeEventBus(¶ms) | ||
if err != nil { | ||
return err | ||
} | ||
if resp == nil { | ||
return fmt.Errorf("Event Bus not found") | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
var testAccAWSCloudWatchEventBusConfig = ` | ||
resource "aws_cloudwatch_event_bus" "foo" { | ||
name = "tf-acc-cw-event-bus" | ||
} | ||
` | ||
|
||
var testAccAWSCloudWatchEventBusConfigModified = ` | ||
resource "aws_cloudwatch_event_bus" "foo" { | ||
name = "tf-acc-cw-event-bus-mod" | ||
} | ||
` | ||
|
||
var testAccAWSCloudWatchEventBusConfigRule = ` | ||
resource "aws_cloudwatch_event_bus" "foo" { | ||
name = "tf-acc-cw-event-bus-with-rule" | ||
} | ||
resource "aws_cloudwatch_event_rule" "foo" { | ||
name = "tf-acc-cw-event-rule-for-bus" | ||
event_bus_name = aws_cloudwatch_event_bus.foo.name | ||
event_pattern = <<PATTERN | ||
{ | ||
"detail-type": [ | ||
"foo" | ||
] | ||
} | ||
PATTERN | ||
} | ||
` |
Oops, something went wrong.