Skip to content

Commit

Permalink
Provider: Fix flavor detection (#551)
Browse files Browse the repository at this point in the history
* Provider: Fix flavor detection

It appears that Terraform v0.12 won't use DefaultFunc when the field is
set to computed. While this might be a bug, we only have a few
occurrences of this in the provider, so I'm opting to work around this
instead of reporting it upstream and possibly have a legacy measure put
in place.

This commit will have the flavors still use the correct environment
variables. Detection is just deferred to outside the schema.

* Provider: fixing typo in error message
  • Loading branch information
jtopjian committed Dec 21, 2018
1 parent 6f8ead1 commit a58ba91
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 30 deletions.
49 changes: 34 additions & 15 deletions openstack/resource_openstack_compute_instance_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,16 @@ func resourceComputeInstanceV2() *schema.Resource {
Computed: true,
},
"flavor_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: false,
Computed: true,
DefaultFunc: schema.EnvDefaultFunc("OS_FLAVOR_ID", nil),
Type: schema.TypeString,
Optional: true,
ForceNew: false,
Computed: true,
},
"flavor_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: false,
Computed: true,
DefaultFunc: schema.EnvDefaultFunc("OS_FLAVOR_NAME", nil),
Type: schema.TypeString,
Optional: true,
ForceNew: false,
Computed: true,
},
"floating_ip": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -394,6 +392,9 @@ func resourceComputeInstanceV2Create(d *schema.ResourceData, meta interface{}) e
return err
}

// Determines the Flavor ID using the following rules:
// If a flavor_id was specified, use it.
// If a flavor_name was specified, lookup the flavor ID, report if error.
flavorId, err := getFlavorID(computeClient, d)
if err != nil {
return err
Expand Down Expand Up @@ -1124,15 +1125,33 @@ func setImageInformation(computeClient *gophercloud.ServiceClient, server *serve
return nil
}

func getFlavorID(client *gophercloud.ServiceClient, d *schema.ResourceData) (string, error) {
flavorId := d.Get("flavor_id").(string)

if flavorId != "" {
func getFlavorID(computeClient *gophercloud.ServiceClient, d *schema.ResourceData) (string, error) {
if flavorId := d.Get("flavor_id").(string); flavorId != "" {
return flavorId, nil
} else {
// Try the OS_FLAVOR_ID environment variable
if v := os.Getenv("OS_FLAVOR_ID"); v != "" {
return v, nil
}
}

flavorName := d.Get("flavor_name").(string)
return flavors.IDFromName(client, flavorName)
if flavorName == "" {
// Try the OS_FLAVOR_NAME environment variable
if v := os.Getenv("OS_FLAVOR_NAME"); v != "" {
flavorName = v
}
}

if flavorName != "" {
flavorId, err := flavors.IDFromName(computeClient, flavorName)
if err != nil {
return "", err
}
return flavorId, nil
}

return "", fmt.Errorf("Neither a flavor_id or flavor_name could be determined.")
}

func resourceComputeSchedulerHintsHash(v interface{}) int {
Expand Down
6 changes: 3 additions & 3 deletions openstack/resource_openstack_compute_instance_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ var testAccComputeV2Instance_basic = fmt.Sprintf(`
resource "openstack_compute_instance_v2" "instance_1" {
name = "instance_1"
security_groups = ["default"]
metadata {
metadata = {
foo = "bar"
}
network {
Expand Down Expand Up @@ -1070,7 +1070,7 @@ var testAccComputeV2Instance_metadataRemove_1 = fmt.Sprintf(`
resource "openstack_compute_instance_v2" "instance_1" {
name = "instance_1"
security_groups = ["default"]
metadata {
metadata = {
foo = "bar"
abc = "def"
}
Expand All @@ -1084,7 +1084,7 @@ var testAccComputeV2Instance_metadataRemove_2 = fmt.Sprintf(`
resource "openstack_compute_instance_v2" "instance_1" {
name = "instance_1"
security_groups = ["default"]
metadata {
metadata = {
foo = "bar"
ghi = "jkl"
}
Expand Down
61 changes: 49 additions & 12 deletions openstack/resource_openstack_containerinfra_cluster_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package openstack
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -100,18 +101,16 @@ func resourceContainerInfraClusterV1() *schema.Resource {
Computed: true,
},
"flavor": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
DefaultFunc: schema.EnvDefaultFunc("OS_MAGNUM_FLAVOR", nil),
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"master_flavor": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
DefaultFunc: schema.EnvDefaultFunc("OS_MAGNUM_MASTER_FLAVOR", nil),
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"keypair": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -169,13 +168,26 @@ func resourceContainerInfraClusterV1Create(d *schema.ResourceData, meta interfac
return err
}

// Determine the flavors to use.
// First check if it was set in the config.
// If not, try using the appropriate environment variable.
flavor, err := containerInfraClusterV1Flavor(d)
if err != nil {
return fmt.Errorf("Unable to determine openstack_containerinfra_cluster_v1 flavor")
}

masterFlavor, err := containerInfraClusterV1Flavor(d)
if err != nil {
return fmt.Errorf("Unable to determine openstack_containerinfra_cluster_v1 master_flavor")
}

createOpts := clusters.CreateOpts{
ClusterTemplateID: d.Get("cluster_template_id").(string),
DiscoveryURL: d.Get("discovery_url").(string),
FlavorID: d.Get("flavor").(string),
FlavorID: flavor,
Keypair: d.Get("keypair").(string),
Labels: labels,
MasterFlavorID: d.Get("master_flavor").(string),
MasterFlavorID: masterFlavor,
Name: d.Get("name").(string),
}

Expand Down Expand Up @@ -376,3 +388,28 @@ func ContainerInfraClusterV1StateRefreshFunc(client *gophercloud.ServiceClient,
return c, c.Status, nil
}
}

func containerInfraClusterV1Flavor(d *schema.ResourceData) (string, error) {
if flavor := d.Get("flavor").(string); flavor != "" {
return flavor, nil
}
// Try the OS_MAGNUM_FLAVOR environment variable
if v := os.Getenv("OS_MAGNUM_FLAVOR"); v != "" {
return v, nil
}

return "", nil
}

func containerInfraClusterV1MasterFlavor(d *schema.ResourceData) (string, error) {
if flavor := d.Get("master_flavor").(string); flavor != "" {
return flavor, nil
}

// Try the OS_MAGNUM_FLAVOR environment variable
if v := os.Getenv("OS_MAGNUM_MASTER_FLAVOR"); v != "" {
return v, nil
}

return "", nil
}

0 comments on commit a58ba91

Please sign in to comment.