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

     1  package dns
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	r "github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func testCheckAttrStringArray(name, key string, value []string) r.TestCheckFunc {
    12  	return func(s *terraform.State) error {
    13  		ms := s.RootModule()
    14  		rs, ok := ms.Resources[name]
    15  		if !ok {
    16  			return fmt.Errorf("Not found: %s", name)
    17  		}
    18  
    19  		is := rs.Primary
    20  		if is == nil {
    21  			return fmt.Errorf("No primary instance: %s", name)
    22  		}
    23  
    24  		attrKey := fmt.Sprintf("%s.#", key)
    25  		count, ok := is.Attributes[attrKey]
    26  		if !ok {
    27  			return fmt.Errorf("Attributes not found for %s", attrKey)
    28  		}
    29  
    30  		got, _ := strconv.Atoi(count)
    31  		if got != len(value) {
    32  			return fmt.Errorf("Mismatch array count for %s: got %s, wanted %d", key, count, len(value))
    33  		}
    34  
    35  		for i, want := range value {
    36  			attrKey = fmt.Sprintf("%s.%d", key, i)
    37  			got, ok := is.Attributes[attrKey]
    38  			if !ok {
    39  				return fmt.Errorf("Missing array item for %s", attrKey)
    40  			}
    41  			if got != want {
    42  				return fmt.Errorf(
    43  					"Mismatched array item for %s: got %s, want %s",
    44  					attrKey,
    45  					got,
    46  					want)
    47  			}
    48  		}
    49  
    50  		return nil
    51  	}
    52  }