Add missing error-checks from code review

Some error-checking was omitted.

Specifically, the cloudTrailSetLogging call in the Create function was
ignoring the return and cloudTrailGetLoggingStatus could crash on a
nil-dereference during the return.  Fixed both.

Fixed some needless casting in cloudTrailGetLoggingStatus.
Clarified error message in acceptance tests.
Removed needless option from example in docs.
This commit is contained in:
Paul Forman 2015-11-22 12:54:11 -07:00
parent 484887c0c5
commit 9cec40ea3c
3 changed files with 11 additions and 6 deletions

View File

@ -91,7 +91,10 @@ func resourceAwsCloudTrailCreate(d *schema.ResourceData, meta interface{}) error
// AWS CloudTrail sets newly-created trails to false.
if v, ok := d.GetOk("enable_logging"); ok && v.(bool) {
cloudTrailSetLogging(conn, v.(bool), d.Id())
err := cloudTrailSetLogging(conn, v.(bool), d.Id())
if err != nil {
return err
}
}
return resourceAwsCloudTrailRead(d, meta)
@ -125,7 +128,7 @@ func resourceAwsCloudTrailRead(d *schema.ResourceData, meta interface{}) error {
d.Set("include_global_service_events", trail.IncludeGlobalServiceEvents)
d.Set("sns_topic_name", trail.SnsTopicName)
logstatus, err := cloudTrailGetLoggingStatus(conn, *trail.Name)
logstatus, err := cloudTrailGetLoggingStatus(conn, trail.Name)
if err != nil {
return err
}
@ -191,11 +194,14 @@ func resourceAwsCloudTrailDelete(d *schema.ResourceData, meta interface{}) error
return err
}
func cloudTrailGetLoggingStatus(conn *cloudtrail.CloudTrail, id string) (bool, error) {
func cloudTrailGetLoggingStatus(conn *cloudtrail.CloudTrail, id *string) (bool, error) {
GetTrailStatusOpts := &cloudtrail.GetTrailStatusInput{
Name: aws.String(id),
Name: id,
}
resp, err := conn.GetTrailStatus(GetTrailStatusOpts)
if err != nil {
return false, fmt.Errorf("Error retrieving logging status of CloudTrail (%s): %s", id, err)
}
return *resp.IsLogging, err
}

View File

@ -115,7 +115,7 @@ func testAccCheckCloudTrailLoggingEnabled(n string, desired bool, trail *cloudtr
return err
}
if *resp.IsLogging != desired {
return fmt.Errorf("Logging status is incorrect")
return fmt.Errorf("Expected logging status %t, given %t", desired, *resp.IsLogging)
}
return nil

View File

@ -16,7 +16,6 @@ resource "aws_cloudtrail" "foobar" {
name = "tf-trail-foobar"
s3_bucket_name = "${aws_s3_bucket.foo.id}"
s3_key_prefix = "/prefix"
enable_logging = true
include_global_service_events = false
}