Skip to content

Commit

Permalink
Update Terraform SDK to V2 (#1139)
Browse files Browse the repository at this point in the history
Use v2.7.1 version of terraform-plugin-sdk.

Cleanup imports.

Change `validation.SingleIP()` -> `validation.IsIPAddress`,
`validation.ValidateRFC3339TimeString` -> `validation.IsRFC3339Time`.

Replace `Removed` Schema property with `Deprecated`.

Provide context into CustomizeDiff functions.

Use functions with the new signatures to configure the Provider with
Terraform SDK V2.

Add context into resourceImagesImageV2UpdateComputedAttributes.

Fix gofmt in pathorcontents package.

Fix networking_port_v2 datasource issue: "profile: StateFunc is
extraneous, value should just be changed before setting on computed-only
field" by removing custom StateFunc.

Fix containerinfra_cluster_v1 resource issuze: "kubeconfig: TypeMap with
Elem *Resource not supported, use TypeList/TypeSet with Elem *Resource
or TypeMap with Elem *Schema" by changing type to Type.Schema and
updating flattenContainerInfraV1Kubeconfig function.

Change the deprecated "Providers" field of resource.Test to
"ProviderFactories".

Use CreateContext, ReadContext, UpdateContext, DeleteContext instead of
deprecated resource functions. Use diag.Diagnostics and Context for
those functions.

Disable `TestAccComputeV2SecGroup_self`,
`TestAccComputeV2SecGroup_lowerCaseCIDR`, `TestAccNetworkingV2Subnet_allocationPool`
tests since SDK V2 currently does not support indexes into TypeSet.

Disable `TestAccComputeV2FloatingIPAssociate_attachToFirstNetwork` test
since SDK V2 fails with collection error.

Add `image_name`, `power_state` attributes to `compute_instance_v2` datasource
since we try to set them just like in resource.

Convert blockstorage quotaset volume types map to map of strings before
calling Set.

Update `TestAccBlockStorageQuotasetV3_basic` volume types quotas.

Disabled flacky `TestAccNetworkingV2Trunk_trunkUpdateSubports` test since
it can fail with `PortInUse` error.
  • Loading branch information
ozerovandrei committed Oct 2, 2021
1 parent 3548c50 commit cec35ae
Show file tree
Hide file tree
Showing 447 changed files with 5,830 additions and 5,273 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.15
require (
github.com/gophercloud/gophercloud v0.17.1-0.20210517213536-0be823b69be8
github.com/gophercloud/utils v0.0.0-20210216074907-f6de111f2eae
github.com/hashicorp/terraform-plugin-sdk v1.17.2
github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.1
github.com/mitchellh/go-homedir v1.1.0
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
Expand Down
177 changes: 65 additions & 112 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"github.com/hashicorp/terraform-plugin-sdk/plugin"
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
"github.com/terraform-provider-openstack/terraform-provider-openstack/openstack"
)

Expand Down
3 changes: 1 addition & 2 deletions openstack/blockstorage_extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"fmt"

"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/schedulerhints"

"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/gophercloud/utils/terraform/hashcode"
)

func expandBlockStorageExtensionsSchedulerHints(v schedulerhints.SchedulerHints) map[string]interface{} {
Expand Down
3 changes: 2 additions & 1 deletion openstack/blockstorage_extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package openstack
import (
"testing"

"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/schedulerhints"
"github.com/stretchr/testify/assert"

"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/schedulerhints"
)

func blockStorageExtensionsSchedulerHints() schedulerhints.SchedulerHints {
Expand Down
44 changes: 34 additions & 10 deletions openstack/blockstorage_quotaset.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,43 @@ import (
"strconv"
)

// blockStorageVolumeTypeQuotaConversion converts all values of the map to int.
func blockStorageVolumeTypeQuotaConversion(vtq map[string]interface{}) (map[string]interface{}, error) {
newVTQ := make(map[string]interface{})
for oldKey, oldVal := range vtq {
tmp, ok := oldVal.(string)
// blockStorageQuotasetVolTypeQuotaToInt converts block storage vol type quota from map of strings to map of integers.
func blockStorageQuotasetVolTypeQuotaToInt(raw map[string]interface{}) (map[string]interface{}, error) {
res := make(map[string]interface{}, len(raw))

for k, v := range raw {
strVal, ok := v.(string)
if !ok {
return nil, fmt.Errorf("Error asserting type for %+v", oldVal)
return nil, fmt.Errorf("%v is not a string", v)
}
newVal, err := strconv.Atoi(tmp)

intVal, err := strconv.Atoi(strVal)
if err != nil {
return nil, fmt.Errorf("Error converting to string for %s", tmp)
return nil, fmt.Errorf("%s can't be converted to int", strVal)
}

res[k] = intVal
}

return res, nil
}

// blockStorageQuotasetVolTypeQuotaToStr converts block storage vol type quota from map of interfaces to map of strings.
func blockStorageQuotasetVolTypeQuotaToStr(raw map[string]interface{}) (map[string]string, error) {
res := make(map[string]string, len(raw))

for k, v := range raw {
switch value := v.(type) {
case int:
res[k] = strconv.Itoa(value)
case float32, float64:
res[k] = fmt.Sprintf("%.0f", value)
case string:
res[k] = value
default:
return nil, fmt.Errorf("got unknown type for quota volume type %s value: %+v", k, v)
}
newVTQ[oldKey] = newVal
}
return newVTQ, nil

return res, nil
}
6 changes: 3 additions & 3 deletions openstack/blockstorage_quotaset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestBlockStorageVolumeTypeQuotaConversion(t *testing.T) {
"bar": 43,
}

actual, err := blockStorageVolumeTypeQuotaConversion(raw)
actual, err := blockStorageQuotasetVolTypeQuotaToInt(raw)

if err != nil {
t.Fatal(err)
Expand All @@ -33,7 +33,7 @@ func TestBlockStorageVolumeTypeQuotaConversion_err(t *testing.T) {
"bar": 200,
}

_, err := blockStorageVolumeTypeQuotaConversion(raw)
_, err := blockStorageQuotasetVolTypeQuotaToInt(raw)

if err == nil {
t.Fatal("Expected error in asserting string")
Expand All @@ -46,7 +46,7 @@ func TestBlockStorageVolumeTypeQuotaConversion_err2(t *testing.T) {
"bar": "bar",
}

_, err := blockStorageVolumeTypeQuotaConversion(raw)
_, err := blockStorageQuotasetVolTypeQuotaToInt(raw)

if err == nil {
t.Fatal("Expected error in converting to int")
Expand Down
3 changes: 2 additions & 1 deletion openstack/blockstorage_volume_attach_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package openstack
import (
"testing"

"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/volumeactions"
"github.com/stretchr/testify/assert"

"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/volumeactions"
)

func TestExpandBlockStorageV2AttachMode(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion openstack/blockstorage_volume_attach_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package openstack
import (
"testing"

"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/volumeactions"
"github.com/stretchr/testify/assert"

"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/volumeactions"
)

func TestExpandBlockStorageV3AttachMode(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions openstack/blockstorage_volume_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"bytes"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v1/volumes"

"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/gophercloud/utils/terraform/hashcode"
)

func flattenBlockStorageVolumeV1Attachments(v []map[string]interface{}) []map[string]interface{} {
Expand Down
4 changes: 2 additions & 2 deletions openstack/blockstorage_volume_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"testing"
"time"

"github.com/gophercloud/gophercloud/openstack/blockstorage/v1/volumes"

"github.com/stretchr/testify/assert"

"github.com/gophercloud/gophercloud/openstack/blockstorage/v1/volumes"
)

func blockStorageVolumeV1VolumeFixture() *volumes.Volume {
Expand Down
6 changes: 3 additions & 3 deletions openstack/blockstorage_volume_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"bytes"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes"

"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/gophercloud/utils/terraform/hashcode"
)

func flattenBlockStorageVolumeV2Attachments(v []volumes.Attachment) []map[string]interface{} {
Expand Down
4 changes: 2 additions & 2 deletions openstack/blockstorage_volume_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"testing"
"time"

"github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes"

"github.com/stretchr/testify/assert"

"github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes"
)

func blockStorageVolumeV2VolumeFixture() volumes.Volume {
Expand Down
6 changes: 3 additions & 3 deletions openstack/blockstorage_volume_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"bytes"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"

"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/gophercloud/utils/terraform/hashcode"
)

func flattenBlockStorageVolumeV3Attachments(v []volumes.Attachment) []map[string]interface{} {
Expand Down
4 changes: 2 additions & 2 deletions openstack/blockstorage_volume_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"testing"
"time"

"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"

"github.com/stretchr/testify/assert"

"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"
)

func blockStorageVolumeV3VolumeFixture() volumes.Volume {
Expand Down
4 changes: 1 addition & 3 deletions openstack/compute_flavor_v2.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package openstack

import (
"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
)
import "github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"

func expandComputeFlavorV2ExtraSpecs(raw map[string]interface{}) flavors.ExtraSpecsOpts {
extraSpecs := make(flavors.ExtraSpecsOpts, len(raw))
Expand Down
2 changes: 1 addition & 1 deletion openstack/compute_floatingip_associate_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
Expand Down
3 changes: 2 additions & 1 deletion openstack/compute_instance_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import (
"log"
"os"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/tenantnetworks"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

const (
Expand Down
4 changes: 2 additions & 2 deletions openstack/compute_interface_attach_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"log"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/attachinterfaces"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func computeInterfaceAttachV2AttachFunc(
Expand Down
4 changes: 1 addition & 3 deletions openstack/compute_keypair_v2.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package openstack

import (
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"
)
import "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"

// ComputeKeyPairV2CreateOpts is a custom KeyPair struct to include the ValueSpecs field.
type ComputeKeyPairV2CreateOpts struct {
Expand Down
7 changes: 4 additions & 3 deletions openstack/compute_secgroup_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"log"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/secgroups"
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/gophercloud/utils/terraform/hashcode"
)

func computeSecGroupV2RulesCheckForErrors(d *schema.ResourceData) error {
Expand Down
3 changes: 2 additions & 1 deletion openstack/compute_servergroup_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package openstack
import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups"
th "github.com/gophercloud/gophercloud/testhelper"
thclient "github.com/gophercloud/gophercloud/testhelper/client"
"github.com/stretchr/testify/assert"
)

func TestComputeServerGroupV2CreateOpts(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions openstack/compute_volume_attach_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"log"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/volumeattach"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func computeVolumeAttachV2ParseID(id string) (string, string, error) {
Expand Down
4 changes: 1 addition & 3 deletions openstack/compute_volume_attach_v2_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package openstack

import (
"testing"
)
import "testing"

func TestComputeVolumeAttachV2ParseID(t *testing.T) {
id := "foo/bar"
Expand Down
28 changes: 12 additions & 16 deletions openstack/containerinfra_shared_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import (
"os"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"gopkg.in/yaml.v2"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/containerinfra/v1/certificates"
"github.com/gophercloud/gophercloud/openstack/containerinfra/v1/clusters"
"github.com/gophercloud/gophercloud/openstack/containerinfra/v1/clustertemplates"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"

yaml "gopkg.in/yaml.v2"
)

const (
Expand Down Expand Up @@ -170,13 +169,10 @@ type kubernetesConfigUserData struct {
ClientCertificateData string `yaml:"client-certificate-data"`
}

func flattenContainerInfraV1Kubeconfig(d *schema.ResourceData, containerInfraClient *gophercloud.ServiceClient) (map[string]interface{}, error) {
var kubeconfig map[string]interface{}
name := d.Get("name").(string)
host := d.Get("api_address").(string)

if d.Get("kubeconfig.client_certificate").(string) != "" {
return d.Get("kubeconfig").(map[string]interface{}), nil
func flattenContainerInfraV1Kubeconfig(d *schema.ResourceData, containerInfraClient *gophercloud.ServiceClient) (map[string]string, error) {
clientSert, ok := d.Get("kubeconfig.client_certificate").(string)
if ok && clientSert != "" {
return d.Get("kubeconfig").(map[string]string), nil
}

certificateAuthority, err := certificates.Get(containerInfraClient, d.Id()).Extract()
Expand Down Expand Up @@ -228,20 +224,20 @@ func flattenContainerInfraV1Kubeconfig(d *schema.ResourceData, containerInfraCli
return nil, fmt.Errorf("Error requesting client certificate: %s", err)
}

name := d.Get("name").(string)
host := d.Get("api_address").(string)
rawKubeconfig, err := renderKubeconfig(name, host, []byte(certificateAuthority.PEM), []byte(clientCertificate.PEM), pemClientKey)
if err != nil {
return nil, fmt.Errorf("Error rendering kubeconfig: %s", err)
}

kubeconfig = map[string]interface{}{
return map[string]string{
"raw_config": string(rawKubeconfig),
"host": host,
"cluster_ca_certificate": certificateAuthority.PEM,
"client_certificate": clientCertificate.PEM,
"client_key": string(pemClientKey),
}

return kubeconfig, nil
}, nil
}

func renderKubeconfig(name string, host string, clusterCaCertificate []byte, clientCertificate []byte, clientKey []byte) ([]byte, error) {
Expand Down

0 comments on commit cec35ae

Please sign in to comment.