github.com/danrjohnson/terraform@v0.7.0-rc2.0.20160627135212-d0fc1fa086ff/builtin/providers/azurerm/tags.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func tagsSchema() *schema.Schema {
    11  	return &schema.Schema{
    12  		Type:         schema.TypeMap,
    13  		Optional:     true,
    14  		ValidateFunc: validateAzureRMTags,
    15  	}
    16  }
    17  
    18  func tagValueToString(v interface{}) (string, error) {
    19  	switch value := v.(type) {
    20  	case string:
    21  		return value, nil
    22  	case int:
    23  		return fmt.Sprintf("%d", value), nil
    24  	default:
    25  		return "", fmt.Errorf("unknown tag type %T in tag value", value)
    26  	}
    27  }
    28  
    29  func validateAzureRMTags(v interface{}, k string) (ws []string, es []error) {
    30  	tagsMap := v.(map[string]interface{})
    31  
    32  	if len(tagsMap) > 15 {
    33  		es = append(es, errors.New("a maximum of 15 tags can be applied to each ARM resource"))
    34  	}
    35  
    36  	for k, v := range tagsMap {
    37  		if len(k) > 512 {
    38  			es = append(es, fmt.Errorf("the maximum length for a tag key is 512 characters: %q is %d characters", k, len(k)))
    39  		}
    40  
    41  		value, err := tagValueToString(v)
    42  		if err != nil {
    43  			es = append(es, err)
    44  		} else if len(value) > 256 {
    45  			es = append(es, fmt.Errorf("the maximum length for a tag value is 256 characters: the value for %q is %d characters", k, len(value)))
    46  		}
    47  	}
    48  
    49  	return
    50  }
    51  
    52  func expandTags(tagsMap map[string]interface{}) *map[string]*string {
    53  	output := make(map[string]*string, len(tagsMap))
    54  
    55  	for i, v := range tagsMap {
    56  		//Validate should have ignored this error already
    57  		value, _ := tagValueToString(v)
    58  		output[i] = &value
    59  	}
    60  
    61  	return &output
    62  }
    63  
    64  func flattenAndSetTags(d *schema.ResourceData, tagsMap *map[string]*string) {
    65  	if tagsMap == nil {
    66  		return
    67  	}
    68  
    69  	output := make(map[string]interface{}, len(*tagsMap))
    70  
    71  	for i, v := range *tagsMap {
    72  		output[i] = *v
    73  	}
    74  
    75  	d.Set("tags", output)
    76  }