github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/openstack/util.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  // BuildRequest takes an opts struct and builds a request body for
    12  // Gophercloud to execute
    13  func BuildRequest(opts interface{}, parent string) (map[string]interface{}, error) {
    14  	b, err := gophercloud.BuildRequestBody(opts, "")
    15  	if err != nil {
    16  		return nil, err
    17  	}
    18  
    19  	if b["value_specs"] != nil {
    20  		for k, v := range b["value_specs"].(map[string]interface{}) {
    21  			b[k] = v
    22  		}
    23  		delete(b, "value_specs")
    24  	}
    25  
    26  	return map[string]interface{}{parent: b}, nil
    27  }
    28  
    29  // CheckDeleted checks the error to see if it's a 404 (Not Found) and, if so,
    30  // sets the resource ID to the empty string instead of throwing an error.
    31  func CheckDeleted(d *schema.ResourceData, err error, msg string) error {
    32  	if _, ok := err.(gophercloud.ErrDefault404); ok {
    33  		d.SetId("")
    34  		return nil
    35  	}
    36  
    37  	return fmt.Errorf("%s: %s", msg, err)
    38  }
    39  
    40  // GetRegion returns the region from either d.Get("region") or OS_REGION_NAME
    41  func GetRegion(d *schema.ResourceData) string {
    42  	if v, ok := d.GetOk("region"); ok {
    43  		return v.(string)
    44  	}
    45  
    46  	if v := os.Getenv("OS_REGION_NAME"); v != "" {
    47  		return v
    48  	}
    49  
    50  	return ""
    51  }
    52  
    53  // MapValueSpecs converts ResourceData into a map
    54  func MapValueSpecs(d *schema.ResourceData) map[string]string {
    55  	m := make(map[string]string)
    56  	for key, val := range d.Get("value_specs").(map[string]interface{}) {
    57  		m[key] = val.(string)
    58  	}
    59  	return m
    60  }