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

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