github.com/IBM-Cloud/terraform@v0.6.4-0.20170726051544-8872b87621df/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  		gotCount, _ := strconv.Atoi(count)
    31  		if gotCount != len(value) {
    32  			return fmt.Errorf("Mismatch array count for %s: got %s, wanted %d", key, count, len(value))
    33  		}
    34  
    35  	Next:
    36  		for i := 0; i < gotCount; i++ {
    37  			attrKey = fmt.Sprintf("%s.%d", key, i)
    38  			got, ok := is.Attributes[attrKey]
    39  			if !ok {
    40  				return fmt.Errorf("Missing array item for %s", attrKey)
    41  			}
    42  			for _, want := range value {
    43  				if got == want {
    44  					continue Next
    45  				}
    46  			}
    47  			return fmt.Errorf(
    48  				"Unexpected array item for %s: got %s",
    49  				attrKey,
    50  				got)
    51  		}
    52  
    53  		return nil
    54  	}
    55  }