github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/openstack/util.go (about)

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