-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
Add resource aws_cloudwatch_event_bus #10256
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"log" | ||
|
||
"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, | ||
}, | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsCloudWatchEventBusCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).cloudwatcheventsconn | ||
|
||
eventBusName := d.Get("name").(string) | ||
params := &cloudwatchevents.CreateEventBusInput{ | ||
Name: aws.String(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 isAWSErr(err, cloudwatchevents.ErrCodeResourceNotFoundException, "") { | ||
log.Printf("[WARN] CloudWatch Event Bus (%s) not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
if err != nil { | ||
return fmt.Errorf("error reading CloudWatch Event Bus: %s", err) | ||
} | ||
|
||
log.Printf("[DEBUG] Found CloudWatch Event bus: %#v", *output) | ||
|
||
d.Set("arn", output.Arn) | ||
d.Set("name", output.Name) | ||
|
||
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 isAWSErr(err, cloudwatchevents.ErrCodeResourceNotFoundException, "") { | ||
log.Printf("[WARN] CloudWatch Event Bus (%s) not found", d.Id()) | ||
return nil | ||
} | ||
if err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add the "resource not found" checking you now have in |
||
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"log" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like an event bus can't be deleted if it has associated rules or targets so we'll need to add Dependencies: []string{...}, here. |
||
Dependencies: []string{ | ||
"aws_cloudwatch_event_rule", | ||
"aws_cloudwatch_event_target", | ||
}, | ||
}) | ||
} | ||
|
||
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 := &cloudwatchevents.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(&cloudwatchevents.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) { | ||
var eventBusOutput cloudwatchevents.DescribeEventBusOutput | ||
busName := acctest.RandomWithPrefix("tf-acc-test") | ||
busNameModified := acctest.RandomWithPrefix("tf-acc-test") | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSCloudWatchEventBusDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSCloudWatchEventBusConfig(busName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCloudWatchEventBusExists("aws_cloudwatch_event_bus.foo", &eventBusOutput), | ||
resource.TestCheckResourceAttr("aws_cloudwatch_event_bus.foo", "name", busName), | ||
), | ||
}, | ||
{ | ||
Config: testAccAWSCloudWatchEventBusConfig(busNameModified), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCloudWatchEventBusExists("aws_cloudwatch_event_bus.foo", &eventBusOutput), | ||
resource.TestCheckResourceAttr("aws_cloudwatch_event_bus.foo", "name", busNameModified), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSCloudWatchEventBus_disappears(t *testing.T) { | ||
var eventBusOutput cloudwatchevents.DescribeEventBusOutput | ||
busName := acctest.RandomWithPrefix("tf-acc-test") | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSCloudWatchEventBusDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSCloudWatchEventBusConfig(busName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCloudWatchEventBusExists("aws_cloudwatch_event_bus.foo", &eventBusOutput), | ||
testAccCheckCloudWatchEventBusDisappears(&eventBusOutput), | ||
), | ||
ExpectNonEmptyPlan: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
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 := cloudwatchevents.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, v *cloudwatchevents.DescribeEventBusOutput) 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 := cloudwatchevents.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") | ||
} | ||
|
||
*v = *resp | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckCloudWatchEventBusDisappears(v *cloudwatchevents.DescribeEventBusOutput) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).cloudwatcheventsconn | ||
opts := &cloudwatchevents.DeleteEventBusInput{ | ||
Name: v.Name, | ||
} | ||
_, err := conn.DeleteEventBus(opts) | ||
return err | ||
} | ||
} | ||
|
||
func testAccAWSCloudWatchEventBusConfig(name string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_cloudwatch_event_bus" "foo" { | ||
name = "%s" | ||
} | ||
`, name) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to handle the case where the resource has been deleted outside Terraform by checking for a NotFound error, maybe:
other errors can be wrapped:
This can be tested by adding a
_disappears
test case, for example:https://github.com/terraform-providers/terraform-provider-aws/blob/4875ae5fef0990f4a7e3768c81fcc732b90e0f13/aws/resource_aws_cloudwatch_log_stream_test.go#L33-L52