github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/diff_aws_policy_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/helper/resource"
     7  	"github.com/hashicorp/terraform/terraform"
     8  	"github.com/jen20/awspolicyequivalence"
     9  )
    10  
    11  func testAccCheckAwsPolicyMatch(resource, attr, expectedPolicy string) resource.TestCheckFunc {
    12  	return func(s *terraform.State) error {
    13  		rs, ok := s.RootModule().Resources[resource]
    14  		if !ok {
    15  			return fmt.Errorf("Not found: %s", resource)
    16  		}
    17  
    18  		if rs.Primary.ID == "" {
    19  			return fmt.Errorf("No ID is set")
    20  		}
    21  
    22  		given, ok := rs.Primary.Attributes[attr]
    23  		if !ok {
    24  			return fmt.Errorf("Attribute %q not found for %q", attr, resource)
    25  		}
    26  
    27  		areEquivalent, err := awspolicy.PoliciesAreEquivalent(given, expectedPolicy)
    28  		if err != nil {
    29  			return fmt.Errorf("Comparing AWS Policies failed: %s", err)
    30  		}
    31  
    32  		if !areEquivalent {
    33  			return fmt.Errorf("AWS policies differ.\nGiven: %s\nExpected: %s", given, expectedPolicy)
    34  		}
    35  
    36  		return nil
    37  	}
    38  }