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

     1  package aws
     2  
     3  import (
     4  	"encoding/base64"
     5  	"encoding/json"
     6  	"reflect"
     7  	"regexp"
     8  )
     9  
    10  // Base64Encode encodes data if the input isn't already encoded using base64.StdEncoding.EncodeToString.
    11  // If the input is already base64 encoded, return the original input unchanged.
    12  func base64Encode(data []byte) string {
    13  	// Check whether the data is already Base64 encoded; don't double-encode
    14  	if isBase64Encoded(data) {
    15  		return string(data)
    16  	}
    17  	// data has not been encoded encode and return
    18  	return base64.StdEncoding.EncodeToString(data)
    19  }
    20  
    21  func isBase64Encoded(data []byte) bool {
    22  	_, err := base64.StdEncoding.DecodeString(string(data))
    23  	return err == nil
    24  }
    25  
    26  func looksLikeJsonString(s interface{}) bool {
    27  	return regexp.MustCompile(`^\s*{`).MatchString(s.(string))
    28  }
    29  
    30  func jsonBytesEqual(b1, b2 []byte) bool {
    31  	var o1 interface{}
    32  	if err := json.Unmarshal(b1, &o1); err != nil {
    33  		return false
    34  	}
    35  
    36  	var o2 interface{}
    37  	if err := json.Unmarshal(b2, &o2); err != nil {
    38  		return false
    39  	}
    40  
    41  	return reflect.DeepEqual(o1, o2)
    42  }