Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TF] Improve resource "cloudconnexa_dns_record" #42

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
name: Tests

env:
TF_ACC: "1"
CLOUDCONNEXA_TEST_ORGANIZATION: ${{ secrets.CLOUDCONNEXA_TEST_ORGANIZATION }}
CLOUDCONNEXA_CLIENT_ID: ${{ secrets.CLOUDCONNEXA_CLIENT_ID }}
CLOUDCONNEXA_CLIENT_SECRET: ${{ secrets.CLOUDCONNEXA_CLIENT_SECRET }}

on:
pull_request:
branches:
Expand Down Expand Up @@ -38,6 +45,7 @@ jobs:
# run acceptance tests in a matrix with Terraform core versions
test:
name: Matrix Test
environment: TestingEnv
needs: build
runs-on: ubuntu-latest
timeout-minutes: 15
Expand All @@ -64,10 +72,5 @@ jobs:
go mod download
- name: TF acceptance tests
timeout-minutes: 10
env:
TF_ACC: "1"
CLOUDCONNEXA_TEST_ORGANIZATION: "terraform-community"
CLOUDCONNEXA_CLIENT_ID: ${{ secrets.CVPN_CLIENT_ID }}
CLOUDCONNEXA_CLIENT_SECRET: ${{ secrets.CVPN_CLIENT_SECRET }}
run: |
go test -v -cover ./cloudconnexa
5 changes: 3 additions & 2 deletions cloudconnexa/resource_connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package cloudconnexa

import (
"fmt"
"github.com/openvpn/cloudconnexa-go-client/v2/cloudconnexa"
"testing"

"github.com/openvpn/cloudconnexa-go-client/v2/cloudconnexa"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
Expand Down Expand Up @@ -78,7 +79,7 @@ provider "cloudconnexa" {
}

resource "cloudconnexa_connector" "test" {
name = "%s"
name = "%[2]s"
vpn_region_id = "us-west-1"
network_item_type = "HOST"
network_item_id = "example_network_item_id"
Expand Down
13 changes: 9 additions & 4 deletions cloudconnexa/resource_dns_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,22 @@ func resourceDnsRecord() *schema.Resource {
Description: "The description for the UI. Defaults to `Managed by Terraform`.",
},
"ip_v4_addresses": {
Type: schema.TypeList,
Optional: true,
Type: schema.TypeList,
Optional: true,
AtLeastOneOf: []string{"ip_v4_addresses", "ip_v6_addresses"},
MinItems: 1,
Elem: &schema.Schema{

Type: schema.TypeString,
ValidateFunc: validation.IsIPv4Address,
},
Description: "The list of IPV4 addresses to which this record will resolve.",
},
"ip_v6_addresses": {
Type: schema.TypeList,
Optional: true,
Type: schema.TypeList,
Optional: true,
AtLeastOneOf: []string{"ip_v4_addresses", "ip_v6_addresses"},
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.IsIPv6Address,
Expand Down
63 changes: 61 additions & 2 deletions cloudconnexa/resource_dns_record_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cloudconnexa

import (
"errors"
"fmt"
"github.com/openvpn/cloudconnexa-go-client/v2/cloudconnexa"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -32,6 +34,36 @@ func TestAccCloudConnexaDnsRecord_basic(t *testing.T) {
})
}

func TestAccCloudConnexaDnsRecord_noIPs(t *testing.T) {
expectedErr, _ := regexp.Compile("one of `ip_v4_addresses,ip_v6_addresses` must be specified")
domainName := "test.cloudconnexa.com"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCloudConnexaDnsRecordConfigWithoutIPs(domainName),
ExpectError: expectedErr,
},
},
})
}

func TestAccCloudConnexaDnsRecord_IPsArrayIsEmpty(t *testing.T) {
expectedErr, _ := regexp.Compile("Attribute ip_v4_addresses requires 1 item minimum, but config has only 0")
domainName := "test.cloudconnexa.com"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCloudConnexaDnsRecordConfigIPv4Empty(domainName),
ExpectError: expectedErr,
},
},
})
}

func testAccCheckCloudConnexaDnsRecordDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*cloudconnexa.Client)

Expand All @@ -43,8 +75,8 @@ func testAccCheckCloudConnexaDnsRecordDestroy(s *terraform.State) error {
recordId := rs.Primary.ID
r, err := client.DnsRecords.GetDnsRecord(recordId)

if err != nil {
return err
if !errors.Is(err, cloudconnexa.ErrDnsRecordNotFound) {
return nil
}

if r != nil {
Expand All @@ -69,3 +101,30 @@ resource "cloudconnexa_dns_record" "test" {
}
`, testCloudID, domainName)
}

func testAccCloudConnexaDnsRecordConfigWithoutIPs(domainName string) string {
return fmt.Sprintf(`
provider "cloudconnexa" {
base_url = "https://%[1]s.api.openvpn.com"
}

resource "cloudconnexa_dns_record" "test" {
domain = "%[2]s"
description = "test description"
}
`, testCloudID, domainName)
}

func testAccCloudConnexaDnsRecordConfigIPv4Empty(domainName string) string {
return fmt.Sprintf(`
provider "cloudconnexa" {
base_url = "https://%[1]s.api.openvpn.com"
}

resource "cloudconnexa_dns_record" "test" {
domain = "%[2]s"
description = "test description"
ip_v4_addresses = []
}
`, testCloudID, domainName)
}
2 changes: 1 addition & 1 deletion cloudconnexa/resource_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ resource "cloudconnexa_network" "test" {
vpn_region_id = "fi-hel"
}
default_route {
subnet = "10.1.2.0/24"
subnet = "10.1.1.0/24"
type = "IP_V4"
}
}
Expand Down
15 changes: 8 additions & 7 deletions cloudconnexa/resource_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package cloudconnexa
import (
"errors"
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/openvpn/cloudconnexa-go-client/v2/cloudconnexa"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -81,30 +82,30 @@ func testAccCheckCloudConnexaServiceDestroy(state *terraform.State) error {
func testAccCloudConnexaServiceConfig(service cloudconnexa.IPService, networkName string) string {
return fmt.Sprintf(`
provider "cloudconnexa" {
base_url = "https://%s.api.openvpn.com"
base_url = "https://%[1]s.api.openvpn.com"
}

resource "cloudconnexa_network" "test" {
name = "%s"
name = "%[2]s"
description = "test"

default_connector {
name = "%s"
name = "%[3]s"
vpn_region_id = "fi-hel"
}
default_route {
value = "10.1.2.0/24"
subnet = "10.1.2.0/24"
type = "IP_V4"
}
}

resource "cloudconnexa_ip_service" "test" {
name = "%s"
name = "%[4]s"
type = "SERVICE_DESTINATION"
description = "test"
network_item_type = "NETWORK"
network_item_id = cloudconnexa_network.test.id
routes = ["test.ua" ]
routes = ["10.1.2.1/32" ]
config {
service_types = ["ANY"]
}
Expand Down
9 changes: 5 additions & 4 deletions cloudconnexa/resource_user_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/openvpn/cloudconnexa-go-client/v2/cloudconnexa"
"testing"

"github.com/openvpn/cloudconnexa-go-client/v2/cloudconnexa"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
Expand Down Expand Up @@ -96,11 +97,11 @@ func testAccCloudConnexaUserGroupConfig(userGroup cloudconnexa.UserGroup) string

return fmt.Sprintf(`
provider "cloudconnexa" {
base_url = "https://%s.api.openvpn.com"
base_url = "https://%[1]s.api.openvpn.com"
}
resource "cloudconnexa_user_group" "test" {
name = "%s"
vpn_region_ids = %s
name = "%[2]s"
vpn_region_ids = %[3]s

}
`, testCloudID, userGroup.Name, idsStr)
Expand Down
21 changes: 15 additions & 6 deletions cloudconnexa/resource_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package cloudconnexa
import (
"errors"
"fmt"
"github.com/openvpn/cloudconnexa-go-client/v2/cloudconnexa"
"testing"

"github.com/openvpn/cloudconnexa-go-client/v2/cloudconnexa"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
Expand Down Expand Up @@ -106,13 +107,21 @@ func testAccCloudConnexaUserImportStateIdFunc(n string) resource.ImportStateIdFu
func testAccCloudConnexaUserConfig(user cloudconnexa.User) string {
return fmt.Sprintf(`
provider "cloudconnexa" {
base_url = "https://%s.api.openvpn.com"
base_url = "https://%[1]s.api.openvpn.com"
}

resource "cloudconnexa_user_group" "test" {
name = "test-group"
vpn_region_ids = ["eu-central-1"]
connect_auth = "AUTH"
}

resource "cloudconnexa_user" "test" {
username = "%s"
email = "%s"
first_name = "%s"
last_name = "%s"
username = "%[2]s"
email = "%[3]s"
first_name = "%[4]s"
last_name = "%[5]s"
group_id = cloudconnexa_user_group.test.id
}
`, testCloudID, user.Username, user.Email, user.FirstName, user.LastName)
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ require (
github.com/gruntwork-io/terratest v0.47.0
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0
github.com/openvpn/cloudconnexa-go-client/v2 v2.0.13
github.com/openvpn/cloudconnexa-go-client/v2 v2.0.14
github.com/stretchr/testify v1.9.0
)

require (
cloud.google.com/go v0.112.0 // indirect
cloud.google.com/go/compute v1.24.0 // indirect
cloud.google.com/go/compute v1.24.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.6 // indirect
cloud.google.com/go/storage v1.36.0 // indirect
Expand Down Expand Up @@ -89,7 +89,7 @@ require (
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/time v0.6.0 // indirect
golang.org/x/tools v0.20.0 // indirect
google.golang.org/api v0.162.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,8 @@ github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zx
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
github.com/openvpn/cloudconnexa-go-client/v2 v2.0.13 h1:Bm47oT/O+CwXdwUu84NZZXETZCo/9Th3Cx+EGUXvyPU=
github.com/openvpn/cloudconnexa-go-client/v2 v2.0.13/go.mod h1:udq5IDkgXvMO6mQUEFsLHzEyGGAduhO0jJvlb9f4JkE=
github.com/openvpn/cloudconnexa-go-client/v2 v2.0.14 h1:rEQAt3MLLIe++dAMG64FpUgS1ZHUM/k77QtJ0GyfpZ0=
github.com/openvpn/cloudconnexa-go-client/v2 v2.0.14/go.mod h1:WJZU+oOwHUYoO7Q2tFnGavKGbhrAvo8OEgvvr9EEtCw=
github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4=
github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -797,8 +797,8 @@ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
Expand Down
Loading