2015-02-10 04:27:39 +01:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-12-04 15:24:07 +01:00
|
|
|
"os"
|
2015-02-10 04:27:39 +01:00
|
|
|
|
2016-09-04 04:51:22 +02:00
|
|
|
"github.com/gophercloud/gophercloud"
|
2015-02-10 04:27:39 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
2016-12-04 15:24:07 +01:00
|
|
|
// BuildRequest takes an opts struct and builds a request body for
|
|
|
|
// Gophercloud to execute
|
|
|
|
func BuildRequest(opts interface{}, parent string) (map[string]interface{}, error) {
|
|
|
|
b, err := gophercloud.BuildRequestBody(opts, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if b["value_specs"] != nil {
|
|
|
|
for k, v := range b["value_specs"].(map[string]interface{}) {
|
|
|
|
b[k] = v
|
|
|
|
}
|
|
|
|
delete(b, "value_specs")
|
|
|
|
}
|
|
|
|
|
|
|
|
return map[string]interface{}{parent: b}, nil
|
|
|
|
}
|
|
|
|
|
2015-02-10 04:27:39 +01:00
|
|
|
// CheckDeleted checks the error to see if it's a 404 (Not Found) and, if so,
|
|
|
|
// sets the resource ID to the empty string instead of throwing an error.
|
2015-02-13 01:25:45 +01:00
|
|
|
func CheckDeleted(d *schema.ResourceData, err error, msg string) error {
|
2016-09-04 06:57:25 +02:00
|
|
|
if _, ok := err.(gophercloud.ErrDefault404); ok {
|
2015-02-10 04:27:39 +01:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-04 06:57:25 +02:00
|
|
|
|
2015-02-13 01:25:45 +01:00
|
|
|
return fmt.Errorf("%s: %s", msg, err)
|
2015-02-10 04:27:39 +01:00
|
|
|
}
|
2016-10-03 20:12:28 +02:00
|
|
|
|
2016-12-04 15:24:07 +01:00
|
|
|
// GetRegion returns the region from either d.Get("region") or OS_REGION_NAME
|
|
|
|
func GetRegion(d *schema.ResourceData) string {
|
|
|
|
if v, ok := d.GetOk("region"); ok {
|
|
|
|
return v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := os.Getenv("OS_REGION_NAME"); v != "" {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-10-05 18:21:54 +02:00
|
|
|
// MapValueSpecs converts ResourceData into a map
|
|
|
|
func MapValueSpecs(d *schema.ResourceData) map[string]string {
|
|
|
|
m := make(map[string]string)
|
|
|
|
for key, val := range d.Get("value_specs").(map[string]interface{}) {
|
|
|
|
m[key] = val.(string)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|