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:
parent
484887c0c5
commit
9cec40ea3c
|
@ -91,7 +91,10 @@ func resourceAwsCloudTrailCreate(d *schema.ResourceData, meta interface{}) error
|
||||||
|
|
||||||
// AWS CloudTrail sets newly-created trails to false.
|
// AWS CloudTrail sets newly-created trails to false.
|
||||||
if v, ok := d.GetOk("enable_logging"); ok && v.(bool) {
|
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)
|
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("include_global_service_events", trail.IncludeGlobalServiceEvents)
|
||||||
d.Set("sns_topic_name", trail.SnsTopicName)
|
d.Set("sns_topic_name", trail.SnsTopicName)
|
||||||
|
|
||||||
logstatus, err := cloudTrailGetLoggingStatus(conn, *trail.Name)
|
logstatus, err := cloudTrailGetLoggingStatus(conn, trail.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -191,11 +194,14 @@ func resourceAwsCloudTrailDelete(d *schema.ResourceData, meta interface{}) error
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func cloudTrailGetLoggingStatus(conn *cloudtrail.CloudTrail, id string) (bool, error) {
|
func cloudTrailGetLoggingStatus(conn *cloudtrail.CloudTrail, id *string) (bool, error) {
|
||||||
GetTrailStatusOpts := &cloudtrail.GetTrailStatusInput{
|
GetTrailStatusOpts := &cloudtrail.GetTrailStatusInput{
|
||||||
Name: aws.String(id),
|
Name: id,
|
||||||
}
|
}
|
||||||
resp, err := conn.GetTrailStatus(GetTrailStatusOpts)
|
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
|
return *resp.IsLogging, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -115,7 +115,7 @@ func testAccCheckCloudTrailLoggingEnabled(n string, desired bool, trail *cloudtr
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if *resp.IsLogging != desired {
|
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
|
return nil
|
||||||
|
|
|
@ -16,7 +16,6 @@ resource "aws_cloudtrail" "foobar" {
|
||||||
name = "tf-trail-foobar"
|
name = "tf-trail-foobar"
|
||||||
s3_bucket_name = "${aws_s3_bucket.foo.id}"
|
s3_bucket_name = "${aws_s3_bucket.foo.id}"
|
||||||
s3_key_prefix = "/prefix"
|
s3_key_prefix = "/prefix"
|
||||||
enable_logging = true
|
|
||||||
include_global_service_events = false
|
include_global_service_events = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue