github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/openstack/util.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"os"
     7  	"sort"
     8  	"strings"
     9  
    10  	"github.com/Unknwon/com"
    11  	"github.com/gophercloud/gophercloud"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  // BuildRequest takes an opts struct and builds a request body for
    16  // Gophercloud to execute
    17  func BuildRequest(opts interface{}, parent string) (map[string]interface{}, error) {
    18  	b, err := gophercloud.BuildRequestBody(opts, "")
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  
    23  	b = AddValueSpecs(b)
    24  
    25  	return map[string]interface{}{parent: b}, nil
    26  }
    27  
    28  // CheckDeleted checks the error to see if it's a 404 (Not Found) and, if so,
    29  // sets the resource ID to the empty string instead of throwing an error.
    30  func CheckDeleted(d *schema.ResourceData, err error, msg string) error {
    31  	if _, ok := err.(gophercloud.ErrDefault404); ok {
    32  		d.SetId("")
    33  		return nil
    34  	}
    35  
    36  	return fmt.Errorf("%s: %s", msg, err)
    37  }
    38  
    39  // GetRegion returns the region from either d.Get("region") or OS_REGION_NAME
    40  func GetRegion(d *schema.ResourceData) string {
    41  	if v, ok := d.GetOk("region"); ok {
    42  		return v.(string)
    43  	}
    44  
    45  	if v := os.Getenv("OS_REGION_NAME"); v != "" {
    46  		return v
    47  	}
    48  
    49  	return ""
    50  }
    51  
    52  // AddValueSpecs expands the 'value_specs' object and removes 'value_specs'
    53  // from the reqeust body.
    54  func AddValueSpecs(body map[string]interface{}) map[string]interface{} {
    55  	if body["value_specs"] != nil {
    56  		for k, v := range body["value_specs"].(map[string]interface{}) {
    57  			body[k] = v
    58  		}
    59  		delete(body, "value_specs")
    60  	}
    61  
    62  	return body
    63  }
    64  
    65  // MapValueSpecs converts ResourceData into a map
    66  func MapValueSpecs(d *schema.ResourceData) map[string]string {
    67  	m := make(map[string]string)
    68  	for key, val := range d.Get("value_specs").(map[string]interface{}) {
    69  		m[key] = val.(string)
    70  	}
    71  	return m
    72  }
    73  
    74  // List of headers that need to be redacted
    75  var REDACT_HEADERS = []string{"x-auth-token", "x-auth-key", "x-service-token",
    76  	"x-storage-token", "x-account-meta-temp-url-key", "x-account-meta-temp-url-key-2",
    77  	"x-container-meta-temp-url-key", "x-container-meta-temp-url-key-2", "set-cookie",
    78  	"x-subject-token"}
    79  
    80  // RedactHeaders processes a headers object, returning a redacted list
    81  func RedactHeaders(headers http.Header) (processedHeaders []string) {
    82  	for name, header := range headers {
    83  		for _, v := range header {
    84  			if com.IsSliceContainsStr(REDACT_HEADERS, name) {
    85  				processedHeaders = append(processedHeaders, fmt.Sprintf("%v: %v", name, "***"))
    86  			} else {
    87  				processedHeaders = append(processedHeaders, fmt.Sprintf("%v: %v", name, v))
    88  			}
    89  		}
    90  	}
    91  	return
    92  }
    93  
    94  // FormatHeaders processes a headers object plus a deliminator, returning a string
    95  func FormatHeaders(headers http.Header, seperator string) string {
    96  	redactedHeaders := RedactHeaders(headers)
    97  	sort.Strings(redactedHeaders)
    98  
    99  	return strings.Join(redactedHeaders, seperator)
   100  }