feat: add DNS record resource and datasources (#25)
* add DNS record * revamp tests * lint * cleanup * feat dns test * chore: add DNS Record tests * linting * f
This commit is contained in:
committed by
GitHub
parent
325d7b7f20
commit
e7164c0460
@@ -1,4 +1,4 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"github.com/hashicorp/go-version"
|
||||
)
|
||||
|
||||
func preCheckMinVersion(t *testing.T, min *version.Version) {
|
||||
func PreCheckMinVersion(t *testing.T, min *version.Version) {
|
||||
v, err := version.NewVersion(testClient.Version())
|
||||
if err != nil {
|
||||
t.Fatalf("error parsing version: %s", err)
|
||||
@@ -16,7 +16,7 @@ func preCheckMinVersion(t *testing.T, min *version.Version) {
|
||||
}
|
||||
}
|
||||
|
||||
func preCheckVersionConstraint(t *testing.T, cs string) {
|
||||
func PreCheckVersionConstraint(t *testing.T, cs string) {
|
||||
v, err := version.NewVersion(testClient.Version())
|
||||
if err != nil {
|
||||
t.Fatalf("Error parsing version: %s", err)
|
||||
@@ -1,7 +1,8 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
"testing"
|
||||
@@ -9,11 +10,7 @@ import (
|
||||
|
||||
func TestAccDataAccount_default(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -25,15 +22,10 @@ func TestAccDataAccount_default(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAccDataAccount_mac(t *testing.T) {
|
||||
mac, unallocateMac := allocateTestMac(t)
|
||||
mac, unallocateMac := pt.AllocateTestMac(t)
|
||||
defer unallocateMac()
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
// TODO: CheckDestroy: ,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccDataAccountConfig(mac, mac),
|
||||
@@ -1,18 +1,12 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAccDataAPGroup_default(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
// TODO: CheckDestroy: ,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccDataAPGroupConfig_default,
|
||||
@@ -1,12 +1,11 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"testing"
|
||||
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/hashicorp/go-version"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAccDataNetwork_byName(t *testing.T) {
|
||||
@@ -15,15 +14,10 @@ func TestAccDataNetwork_byName(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("error parsing version: %s", err)
|
||||
}
|
||||
if v.LessThan(provider.ControllerV7) {
|
||||
if v.LessThan(base.ControllerV7) {
|
||||
defaultName = "LAN"
|
||||
}
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -42,15 +36,11 @@ func TestAccDataNetwork_byID(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("error parsing version: %s", err)
|
||||
}
|
||||
if v.LessThan(provider.ControllerV7) {
|
||||
if v.LessThan(base.ControllerV7) {
|
||||
defaultName = "LAN"
|
||||
}
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
25
internal/provider/acctest/data_port_profile_test.go
Normal file
25
internal/provider/acctest/data_port_profile_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccDataPortProfile_default(t *testing.T) {
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
VersionConstraint: "< 7.4",
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccDataPortProfileConfig_default,
|
||||
Check: resource.ComposeTestCheckFunc(),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const testAccDataPortProfileConfig_default = `
|
||||
data "unifi_port_profile" "default" {
|
||||
}
|
||||
`
|
||||
26
internal/provider/acctest/data_user_group_test.go
Normal file
26
internal/provider/acctest/data_user_group_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccDataUserGroup_default(t *testing.T) {
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccDataUserGroupConfig_default,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const testAccDataUserGroupConfig_default = `
|
||||
data "unifi_user_group" "default" {
|
||||
}
|
||||
`
|
||||
@@ -1,4 +1,4 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -7,18 +7,17 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccDataUser_default(t *testing.T) {
|
||||
mac, unallocateTestMac := allocateTestMac(t)
|
||||
mac, unallocateTestMac := pt.AllocateTestMac(t)
|
||||
defer unallocateTestMac()
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
PreCheck: func() {
|
||||
//preCheck(t)
|
||||
|
||||
_, err := testClient.CreateUser(context.Background(), "default", &unifi.User{
|
||||
MAC: mac,
|
||||
Name: name,
|
||||
@@ -28,8 +27,6 @@ func TestAccDataUser_default(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
},
|
||||
//PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccDataUserConfig_default(mac),
|
||||
106
internal/provider/acctest/datasource_dns_record_test.go
Normal file
106
internal/provider/acctest/datasource_dns_record_test.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
)
|
||||
|
||||
const testDnsRecordDataSourceName = "data.unifi_dns_record.test"
|
||||
|
||||
func TestDNSRecordDataSource_basic(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
record string
|
||||
recordType string
|
||||
filterByName bool
|
||||
}{
|
||||
{
|
||||
name: "filter by name",
|
||||
record: "192.168.1.100",
|
||||
recordType: "A",
|
||||
filterByName: true,
|
||||
},
|
||||
{
|
||||
name: "filter by record",
|
||||
record: "192.168.1.200",
|
||||
recordType: "A",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
recordName := pt.RandHostname()
|
||||
r := dnsRecordTestCase{
|
||||
recordName: recordName,
|
||||
record: tc.record,
|
||||
recordType: tc.recordType,
|
||||
}
|
||||
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
MinVersion: base.ControllerVersionDnsRecords,
|
||||
|
||||
Steps: Steps{
|
||||
{
|
||||
Config: pt.ComposeConfig(testAccDnsRecordConfig(r), testAccDnsRecordDataSourceConfig(r, tc.filterByName)),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttr(testDnsRecordDataSourceName, "name", recordName),
|
||||
resource.TestCheckResourceAttr(testDnsRecordDataSourceName, "record", tc.record),
|
||||
resource.TestCheckResourceAttr(testDnsRecordDataSourceName, "type", tc.recordType),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
dnsDataSourceFilterErrorRegex = regexp.MustCompile(`[filter.name,filter.record]`)
|
||||
)
|
||||
|
||||
func TestDNSRecordDataSource_failWithoutFilter(t *testing.T) {
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
MinVersion: base.ControllerVersionDnsRecords,
|
||||
|
||||
Steps: Steps{
|
||||
{
|
||||
Config: testAccDnsRecordDataSourceWithoutFilter(),
|
||||
ExpectError: dnsDataSourceFilterErrorRegex,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccDnsRecordDataSourceConfig(tc dnsRecordTestCase, filterByName bool) string {
|
||||
filter := ""
|
||||
if filterByName {
|
||||
filter = "name = \"" + tc.recordName + "\""
|
||||
} else {
|
||||
filter = "record = \"" + tc.record + "\""
|
||||
}
|
||||
|
||||
return fmt.Sprintf(`
|
||||
data "unifi_dns_record" "test" {
|
||||
filter {
|
||||
%s
|
||||
}
|
||||
depends_on = [unifi_dns_record.test]
|
||||
}`, filter)
|
||||
}
|
||||
|
||||
func testAccDnsRecordDataSourceWithoutFilter() string {
|
||||
return `
|
||||
data "unifi_dns_record" "test" {
|
||||
filter {
|
||||
|
||||
}
|
||||
}`
|
||||
}
|
||||
88
internal/provider/acctest/datasource_dns_records_test.go
Normal file
88
internal/provider/acctest/datasource_dns_records_test.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
)
|
||||
|
||||
const testDnsRecordsDataSourceName = "data.unifi_dns_records.test"
|
||||
|
||||
func TestDNSRecordsDataSource_basic(t *testing.T) {
|
||||
records := []*dnsRecordTestCase{
|
||||
{
|
||||
name: "test1",
|
||||
record: "192.168.1.100",
|
||||
recordType: "A",
|
||||
},
|
||||
{
|
||||
name: "test2",
|
||||
record: "192.168.1.200",
|
||||
recordType: "A",
|
||||
},
|
||||
{
|
||||
name: "mail",
|
||||
record: "mail.example.com",
|
||||
recordType: "MX",
|
||||
priority: intPtr(10),
|
||||
},
|
||||
}
|
||||
|
||||
var configs []string
|
||||
var dependencies []string
|
||||
for _, record := range records {
|
||||
record.recordName = pt.RandHostname()
|
||||
resourceName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
|
||||
configs = append(configs, testAccDnsRecordConfigWithResourceName(resourceName, *record))
|
||||
dependencies = append(dependencies, fmt.Sprintf("unifi_dns_record.%s", resourceName))
|
||||
}
|
||||
configs = append(configs, testAccDnsRecordsDataSourceConfig(dependencies))
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
MinVersion: base.ControllerVersionDnsRecords,
|
||||
Steps: Steps{
|
||||
{
|
||||
Config: pt.ComposeConfig(configs...),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttr(testDnsRecordsDataSourceName, "result.#", "3"),
|
||||
resource.TestCheckResourceAttrSet(testDnsRecordsDataSourceName, "result.0.name"),
|
||||
resource.TestCheckResourceAttrSet(testDnsRecordsDataSourceName, "result.0.record"),
|
||||
resource.TestCheckResourceAttrSet(testDnsRecordsDataSourceName, "result.0.type"),
|
||||
resource.TestCheckResourceAttrSet(testDnsRecordsDataSourceName, "result.1.name"),
|
||||
resource.TestCheckResourceAttrSet(testDnsRecordsDataSourceName, "result.1.record"),
|
||||
resource.TestCheckResourceAttrSet(testDnsRecordsDataSourceName, "result.1.type"),
|
||||
resource.TestCheckResourceAttrSet(testDnsRecordsDataSourceName, "result.2.name"),
|
||||
resource.TestCheckResourceAttrSet(testDnsRecordsDataSourceName, "result.2.record"),
|
||||
resource.TestCheckResourceAttrSet(testDnsRecordsDataSourceName, "result.2.type"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestDNSRecordsDataSource_noRecords(t *testing.T) {
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
MinVersion: base.ControllerVersionDnsRecords,
|
||||
Steps: Steps{
|
||||
{
|
||||
Config: testAccDnsRecordsDataSourceConfig(nil),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttr(testDnsRecordsDataSourceName, "result.#", "0"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccDnsRecordsDataSourceConfig(deps []string) string {
|
||||
return `
|
||||
data "unifi_dns_records" "test" {
|
||||
depends_on = [
|
||||
` + strings.Join(deps, ",") + `
|
||||
]
|
||||
}`
|
||||
}
|
||||
94
internal/provider/acctest/provider_test.go
Normal file
94
internal/provider/acctest/provider_test.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"github.com/hashicorp/go-version"
|
||||
"github.com/hashicorp/terraform-plugin-framework/providerserver"
|
||||
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
|
||||
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
|
||||
"github.com/hashicorp/terraform-plugin-mux/tf5to6server"
|
||||
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type providersMap map[string]func() (tfprotov6.ProviderServer, error)
|
||||
|
||||
var (
|
||||
providers = createProviders()
|
||||
testClient unifi.Client
|
||||
)
|
||||
|
||||
type Steps []resource.TestStep
|
||||
|
||||
type AcceptanceTestCase struct {
|
||||
CheckDestroy resource.TestCheckFunc
|
||||
VersionConstraint string
|
||||
MinVersion *version.Version
|
||||
PreCheck func()
|
||||
Steps Steps
|
||||
}
|
||||
|
||||
func AcceptanceTest(t *testing.T, testCase AcceptanceTestCase) {
|
||||
t.Helper()
|
||||
if len(testCase.Steps) == 0 {
|
||||
t.Fatal("missing test steps")
|
||||
}
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
pt.PreCheck(t)
|
||||
if testCase.VersionConstraint != "" {
|
||||
PreCheckVersionConstraint(t, testCase.VersionConstraint)
|
||||
}
|
||||
if testCase.MinVersion != nil {
|
||||
PreCheckMinVersion(t, testCase.MinVersion)
|
||||
}
|
||||
if testCase.PreCheck != nil {
|
||||
testCase.PreCheck()
|
||||
}
|
||||
},
|
||||
ProtoV6ProviderFactories: providers,
|
||||
CheckDestroy: testCase.CheckDestroy,
|
||||
Steps: testCase.Steps,
|
||||
})
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
providers = createProviders()
|
||||
os.Exit(pt.Run(m, func(env *pt.TestEnvironment) {
|
||||
testClient = env.Client
|
||||
}))
|
||||
}
|
||||
|
||||
func createProviders() providersMap {
|
||||
ctx := context.Background()
|
||||
// Init mux servers
|
||||
return map[string]func() (tfprotov6.ProviderServer, error){
|
||||
"unifi": func() (tfprotov6.ProviderServer, error) {
|
||||
return tf6muxserver.NewMuxServer(ctx,
|
||||
providerserver.NewProtocol6(provider.NewV2("acctestv2")()),
|
||||
func() tfprotov6.ProviderServer {
|
||||
sdkV2Provider, err := tf5to6server.UpgradeServer(
|
||||
ctx,
|
||||
func() tfprotov5.ProviderServer {
|
||||
return schema.NewGRPCProviderServer(
|
||||
provider.New("acctestv1")(),
|
||||
)
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to create test providers: %w", err))
|
||||
}
|
||||
|
||||
return sdkV2Provider
|
||||
},
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,17 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
|
||||
"testing"
|
||||
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccAccount_basic(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -22,17 +21,15 @@ func TestAccAccount_basic(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_account.test", "name", name),
|
||||
),
|
||||
},
|
||||
importStep("unifi_account.test"),
|
||||
pt.ImportStep("unifi_account.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccAccount_mac(t *testing.T) {
|
||||
mac, unallocateMac := allocateTestMac(t)
|
||||
mac, unallocateMac := pt.AllocateTestMac(t)
|
||||
defer unallocateMac()
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -43,7 +40,7 @@ func TestAccAccount_mac(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_account.test", "password", mac),
|
||||
),
|
||||
},
|
||||
importStep("unifi_account.test"),
|
||||
pt.ImportStep("unifi_account.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
mapset "github.com/deckarep/golang-set/v2"
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
"github.com/hashicorp/terraform-plugin-testing/terraform"
|
||||
@@ -22,6 +23,7 @@ var (
|
||||
)
|
||||
|
||||
func allocateDevice(t *testing.T) (*unifi.Device, func()) {
|
||||
pt.MarkAccTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
deviceInit.Do(func() {
|
||||
@@ -175,10 +177,8 @@ func preCheckDeviceExists(t *testing.T, site, mac string) {
|
||||
}
|
||||
|
||||
func TestAccDevice_empty(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
CheckDestroy: testAccCheckDeviceDestroy,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
CheckDestroy: testAccCheckDeviceDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccDeviceConfigEmpty(),
|
||||
@@ -198,13 +198,11 @@ func TestAccDevice_switch_basic(t *testing.T) {
|
||||
|
||||
importStateVerifyIgnore := []string{"allow_adoption", "forget_on_destroy", "name"}
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
preCheckDeviceExists(t, site, device.MAC)
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
CheckDestroy: testAccCheckDeviceDestroy,
|
||||
CheckDestroy: testAccCheckDeviceDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccDeviceConfig(device.MAC),
|
||||
@@ -253,14 +251,12 @@ func TestAccDevice_switch_portOverrides(t *testing.T) {
|
||||
device, unallocateDevice := allocateDevice(t)
|
||||
defer unallocateDevice()
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
VersionConstraint: "< 7.4",
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
preCheckDeviceExists(t, site, device.MAC)
|
||||
preCheckVersionConstraint(t, "< 7.4")
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
CheckDestroy: testAccCheckDeviceDestroy,
|
||||
CheckDestroy: testAccCheckDeviceDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccDeviceConfig_withPortOverrides(device.MAC),
|
||||
318
internal/provider/acctest/resource_dns_record_test.go
Normal file
318
internal/provider/acctest/resource_dns_record_test.go
Normal file
@@ -0,0 +1,318 @@
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"github.com/hashicorp/terraform-plugin-testing/plancheck"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
"github.com/hashicorp/terraform-plugin-testing/terraform"
|
||||
)
|
||||
|
||||
const testDnsRecordResourceName = "unifi_dns_record.test"
|
||||
|
||||
type dnsRecordTestCase struct {
|
||||
name string
|
||||
recordName string
|
||||
record string
|
||||
recordType string
|
||||
ttl *int
|
||||
enabled *bool
|
||||
priority *int
|
||||
port *int
|
||||
weight *int
|
||||
}
|
||||
|
||||
func TestDNSRecord_basic(t *testing.T) {
|
||||
t.Parallel()
|
||||
testCases := []dnsRecordTestCase{
|
||||
{
|
||||
name: "A record",
|
||||
recordName: "test.com",
|
||||
record: "192.168.0.128",
|
||||
recordType: "A",
|
||||
},
|
||||
{
|
||||
name: "AAAA record",
|
||||
recordName: "ipv6.test.com",
|
||||
record: "2001:db8::1",
|
||||
recordType: "AAAA",
|
||||
},
|
||||
{
|
||||
name: "CNAME record",
|
||||
recordName: "alias.test.com",
|
||||
record: "target.test.com",
|
||||
recordType: "CNAME",
|
||||
},
|
||||
{
|
||||
name: "NS record",
|
||||
recordName: "ns.test.com",
|
||||
record: "127.0.0.1",
|
||||
recordType: "NS",
|
||||
},
|
||||
{
|
||||
name: "MX record with priority",
|
||||
recordName: "mail.test.com",
|
||||
record: "mx.test.com",
|
||||
recordType: "MX",
|
||||
priority: intPtr(10),
|
||||
},
|
||||
{
|
||||
name: "disabled A record",
|
||||
recordName: "disabled.test.com",
|
||||
record: "192.168.1.100",
|
||||
recordType: "A",
|
||||
enabled: boolPtr(false),
|
||||
},
|
||||
{
|
||||
name: "A record with TTL",
|
||||
recordName: "ttl.test.com",
|
||||
record: "192.168.1.100",
|
||||
recordType: "A",
|
||||
ttl: intPtr(3600),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
steps := []resource.TestStep{
|
||||
{
|
||||
Config: testAccDnsRecordConfig(tc),
|
||||
Check: testAccDnsRecordCheckAttrs(tc),
|
||||
},
|
||||
pt.ImportStep(testDnsRecordResourceName),
|
||||
}
|
||||
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
MinVersion: base.ControllerVersionDnsRecords,
|
||||
Steps: steps,
|
||||
CheckDestroy: testAccCheckDNSRecordDestroy,
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNSRecord_SRV(t *testing.T) {
|
||||
t.Parallel()
|
||||
testCases := []dnsRecordTestCase{
|
||||
{
|
||||
name: "SRV record with all fields",
|
||||
recordName: "_sip._tcp.test.com",
|
||||
record: "sip.test.com",
|
||||
recordType: "SRV",
|
||||
port: intPtr(5060),
|
||||
priority: intPtr(10),
|
||||
weight: intPtr(20),
|
||||
},
|
||||
{
|
||||
name: "SRV record with minimal fields",
|
||||
recordName: "_ldap._tcp.test.com",
|
||||
record: "ldap.test.com",
|
||||
recordType: "SRV",
|
||||
port: intPtr(389),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
MinVersion: base.ControllerVersionDnsRecords,
|
||||
CheckDestroy: testAccCheckDNSRecordDestroy,
|
||||
Steps: Steps{
|
||||
{
|
||||
Config: testAccDnsRecordConfig(tc),
|
||||
Check: testAccDnsRecordCheckAttrs(tc),
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNSRecord_Update(t *testing.T) {
|
||||
initial := dnsRecordTestCase{
|
||||
name: "initial",
|
||||
recordName: "update.test.com",
|
||||
record: "192.168.1.100",
|
||||
recordType: "A",
|
||||
ttl: intPtr(3600),
|
||||
}
|
||||
|
||||
updated := dnsRecordTestCase{
|
||||
name: "updated",
|
||||
recordName: "update.test.com",
|
||||
record: "192.168.1.200",
|
||||
recordType: "A",
|
||||
ttl: intPtr(7200),
|
||||
}
|
||||
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
MinVersion: base.ControllerVersionDnsRecords,
|
||||
CheckDestroy: testAccCheckDNSRecordDestroy,
|
||||
Steps: Steps{
|
||||
{
|
||||
Config: testAccDnsRecordConfig(initial),
|
||||
Check: testAccDnsRecordCheckAttrs(initial),
|
||||
},
|
||||
{
|
||||
Config: testAccDnsRecordConfig(updated),
|
||||
Check: testAccDnsRecordCheckAttrs(updated),
|
||||
ConfigPlanChecks: pt.CheckResourceAction(testDnsRecordResourceName, plancheck.ResourceActionUpdate),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestDNSRecord_MissingAttributes(t *testing.T) {
|
||||
t.Parallel()
|
||||
testCases := map[string]func() string{
|
||||
"name": testAccDnsRecordConfigMissingName,
|
||||
"record": testAccDnsRecordConfigMissingRecord,
|
||||
"type": testAccDnsRecordConfigMissingType,
|
||||
}
|
||||
for k, v := range testCases {
|
||||
t.Run(fmt.Sprintf("missing %s", k), func(t *testing.T) {
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
MinVersion: base.ControllerVersionDnsRecords,
|
||||
Steps: Steps{
|
||||
{
|
||||
Config: v(),
|
||||
ExpectError: pt.MissingArgumentErrorRegex(k),
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func testAccCheckDNSRecordDestroy(s *terraform.State) error {
|
||||
for _, rs := range s.RootModule().Resources {
|
||||
if rs.Type != "unifi_dns_record" {
|
||||
continue
|
||||
}
|
||||
|
||||
_, err := testClient.GetDNSRecord(context.Background(), "default", rs.Primary.ID)
|
||||
if err == nil {
|
||||
return fmt.Errorf("DNS Record %s still exists", rs.Primary.ID)
|
||||
}
|
||||
// If we get a 404 error, that means the resource was deleted
|
||||
if strings.Contains(err.Error(), "404") || strings.Contains(err.Error(), "not found") {
|
||||
continue
|
||||
}
|
||||
// For any other error, return it
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func testAccDnsRecordConfig(tc dnsRecordTestCase) string {
|
||||
return testAccDnsRecordConfigWithResourceName("test", tc)
|
||||
}
|
||||
|
||||
func testAccDnsRecordConfigMissingName() string {
|
||||
return `
|
||||
resource "unifi_dns_record" "test" {
|
||||
record = "127.0.0.1"
|
||||
type = "A"
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
func testAccDnsRecordConfigMissingRecord() string {
|
||||
return `
|
||||
resource "unifi_dns_record" "test" {
|
||||
name = "test.com"
|
||||
type = "A"
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
func testAccDnsRecordConfigMissingType() string {
|
||||
return `
|
||||
resource "unifi_dns_record" "test" {
|
||||
name = "test.com"
|
||||
record = "127.0.0.1"
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
func testAccDnsRecordConfigWithResourceName(resourceName string, tc dnsRecordTestCase) string {
|
||||
var attrs string
|
||||
|
||||
if tc.ttl != nil {
|
||||
attrs += fmt.Sprintf("\tttl = %d\n", *tc.ttl)
|
||||
}
|
||||
if tc.enabled != nil {
|
||||
attrs += fmt.Sprintf("\tenabled = %t\n", *tc.enabled)
|
||||
}
|
||||
if tc.priority != nil {
|
||||
attrs += fmt.Sprintf("\tpriority = %d\n", *tc.priority)
|
||||
}
|
||||
if tc.port != nil {
|
||||
attrs += fmt.Sprintf("\tport = %d\n", *tc.port)
|
||||
}
|
||||
if tc.weight != nil {
|
||||
attrs += fmt.Sprintf("\tweight = %d\n", *tc.weight)
|
||||
}
|
||||
|
||||
return fmt.Sprintf(`
|
||||
resource "unifi_dns_record" "%s" {
|
||||
name = "%s"
|
||||
record = "%s"
|
||||
type = "%s"
|
||||
%s}
|
||||
`, resourceName, tc.recordName, tc.record, tc.recordType, attrs)
|
||||
}
|
||||
|
||||
func testAccDnsRecordCheckAttrs(tc dnsRecordTestCase) resource.TestCheckFunc {
|
||||
// expected default values
|
||||
var (
|
||||
ttl = 0
|
||||
enabled = true
|
||||
priority = 0
|
||||
port = 0
|
||||
weight = 0
|
||||
)
|
||||
|
||||
if tc.ttl != nil {
|
||||
ttl = *tc.ttl
|
||||
}
|
||||
if tc.enabled != nil {
|
||||
enabled = *tc.enabled
|
||||
}
|
||||
if tc.priority != nil {
|
||||
priority = *tc.priority
|
||||
}
|
||||
if tc.port != nil {
|
||||
port = *tc.port
|
||||
}
|
||||
if tc.weight != nil {
|
||||
weight = *tc.weight
|
||||
}
|
||||
|
||||
checks := []resource.TestCheckFunc{
|
||||
resource.TestCheckResourceAttr(testDnsRecordResourceName, "name", tc.recordName),
|
||||
resource.TestCheckResourceAttr(testDnsRecordResourceName, "record", tc.record),
|
||||
resource.TestCheckResourceAttr(testDnsRecordResourceName, "type", tc.recordType),
|
||||
resource.TestCheckResourceAttr(testDnsRecordResourceName, "ttl", strconv.Itoa(ttl)),
|
||||
resource.TestCheckResourceAttr(testDnsRecordResourceName, "enabled", strconv.FormatBool(enabled)),
|
||||
resource.TestCheckResourceAttr(testDnsRecordResourceName, "priority", strconv.Itoa(priority)),
|
||||
resource.TestCheckResourceAttr(testDnsRecordResourceName, "port", strconv.Itoa(port)),
|
||||
resource.TestCheckResourceAttr(testDnsRecordResourceName, "weight", strconv.Itoa(weight)),
|
||||
}
|
||||
return resource.ComposeTestCheckFunc(checks...)
|
||||
}
|
||||
|
||||
func intPtr(i int) *int {
|
||||
return &i
|
||||
}
|
||||
|
||||
func boolPtr(b bool) *bool {
|
||||
return &b
|
||||
}
|
||||
@@ -1,15 +1,14 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccDynamicDNS_dyndns(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -18,7 +17,7 @@ func TestAccDynamicDNS_dyndns(t *testing.T) {
|
||||
// // testCheckFirewallGroupExists(t, "name"),
|
||||
// ),
|
||||
},
|
||||
importStep("unifi_dynamic_dns.test"),
|
||||
pt.ImportStep("unifi_dynamic_dns.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -7,14 +7,13 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccFirewallGroup_port_group(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -23,20 +22,18 @@ func TestAccFirewallGroup_port_group(t *testing.T) {
|
||||
// // testCheckFirewallGroupExists(t, "name"),
|
||||
// ),
|
||||
},
|
||||
importStep("unifi_firewall_group.test"),
|
||||
pt.ImportStep("unifi_firewall_group.test"),
|
||||
{
|
||||
Config: testAccFirewallGroupConfig(name, "port-group", []string{"80", "443"}),
|
||||
},
|
||||
importStep("unifi_firewall_group.test"),
|
||||
pt.ImportStep("unifi_firewall_group.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccFirewallGroup_address_group(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -45,23 +42,21 @@ func TestAccFirewallGroup_address_group(t *testing.T) {
|
||||
// // testCheckFirewallGroupExists(t, "name"),
|
||||
// ),
|
||||
},
|
||||
importStep("unifi_firewall_group.test"),
|
||||
pt.ImportStep("unifi_firewall_group.test"),
|
||||
{
|
||||
Config: testAccFirewallGroupConfig(name, "address-group", []string{"10.0.0.1", "10.0.0.2"}),
|
||||
},
|
||||
importStep("unifi_firewall_group.test"),
|
||||
pt.ImportStep("unifi_firewall_group.test"),
|
||||
{
|
||||
Config: testAccFirewallGroupConfig(name, "address-group", []string{"10.0.0.0/24"}),
|
||||
},
|
||||
importStep("unifi_firewall_group.test"),
|
||||
pt.ImportStep("unifi_firewall_group.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccFirewallGroup_same_name(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -1,10 +1,11 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
)
|
||||
@@ -12,9 +13,7 @@ import (
|
||||
func TestAccFirewallRule_basic(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -24,14 +23,14 @@ func TestAccFirewallRule_basic(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_firewall_rule.test", "enabled", "true"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_firewall_rule.test"),
|
||||
pt.ImportStep("unifi_firewall_rule.test"),
|
||||
{
|
||||
Config: testAccFirewallRuleConfig(name, false),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttr("unifi_firewall_rule.test", "enabled", "false"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_firewall_rule.test"),
|
||||
pt.ImportStep("unifi_firewall_rule.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -39,9 +38,7 @@ func TestAccFirewallRule_basic(t *testing.T) {
|
||||
func TestAccFirewallRule_port(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccFirewallRuleConfigWithPort(name),
|
||||
@@ -50,7 +47,7 @@ func TestAccFirewallRule_port(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_firewall_rule.test", "dst_port", "53"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_firewall_rule.test"),
|
||||
pt.ImportStep("unifi_firewall_rule.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -58,14 +55,12 @@ func TestAccFirewallRule_port(t *testing.T) {
|
||||
func TestAccFirewallRule_icmp(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccFirewallRuleConfigWithICMP(name),
|
||||
},
|
||||
importStep("unifi_firewall_rule.test"),
|
||||
pt.ImportStep("unifi_firewall_rule.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -73,9 +68,7 @@ func TestAccFirewallRule_icmp(t *testing.T) {
|
||||
func TestAccFirewallRule_multiple_address_groups(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -89,9 +82,7 @@ func TestAccFirewallRule_multiple_address_groups(t *testing.T) {
|
||||
func TestAccFirewallRule_multiple_port_groups(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -105,9 +96,7 @@ func TestAccFirewallRule_multiple_port_groups(t *testing.T) {
|
||||
func TestAccFirewallRule_address_and_port_group(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -116,7 +105,7 @@ func TestAccFirewallRule_address_and_port_group(t *testing.T) {
|
||||
// // testCheckFirewallGroupExists(t, "name"),
|
||||
// ),
|
||||
},
|
||||
importStep("unifi_firewall_rule.test"),
|
||||
pt.ImportStep("unifi_firewall_rule.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -124,15 +113,13 @@ func TestAccFirewallRule_address_and_port_group(t *testing.T) {
|
||||
func TestAccFirewallRule_IPv6_basic(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccFirewallRuleConfigIPv6(name),
|
||||
},
|
||||
importStep("unifi_firewall_rule.test"),
|
||||
pt.ImportStep("unifi_firewall_rule.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -140,14 +127,12 @@ func TestAccFirewallRule_IPv6_basic(t *testing.T) {
|
||||
func TestAccFirewallRule_IPv6_dst_port(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccFirewallRuleConfigIPv6WithPort(name),
|
||||
},
|
||||
importStep("unifi_firewall_rule.test"),
|
||||
pt.ImportStep("unifi_firewall_rule.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"net"
|
||||
"regexp"
|
||||
"strconv"
|
||||
@@ -15,12 +15,10 @@ import (
|
||||
|
||||
func TestAccNetwork_basic(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet1, vlan1 := getTestVLAN(t)
|
||||
subnet2, vlan2 := getTestVLAN(t)
|
||||
subnet1, vlan1 := pt.GetTestVLAN(t)
|
||||
subnet2, vlan2 := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -31,7 +29,7 @@ func TestAccNetwork_basic(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "igmp_snooping", "true"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_network.test"),
|
||||
pt.ImportStep("unifi_network.test"),
|
||||
{
|
||||
Config: testAccNetworkConfig(name, subnet2, vlan2, false, nil),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
@@ -39,12 +37,12 @@ func TestAccNetwork_basic(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "igmp_snooping", "false"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_network.test"),
|
||||
pt.ImportStep("unifi_network.test"),
|
||||
// re-test import here with default site, but full ID string
|
||||
{
|
||||
ResourceName: "unifi_network.test",
|
||||
ImportState: true,
|
||||
ImportStateIdFunc: siteAndIDImportStateIDFunc("unifi_network.test"),
|
||||
ImportStateIdFunc: pt.SiteAndIDImportStateIDFunc("unifi_network.test"),
|
||||
ImportStateVerify: true,
|
||||
},
|
||||
},
|
||||
@@ -53,11 +51,9 @@ func TestAccNetwork_basic(t *testing.T) {
|
||||
|
||||
func TestAccNetwork_weird_cidr(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -66,18 +62,16 @@ func TestAccNetwork_weird_cidr(t *testing.T) {
|
||||
// TODO: ...
|
||||
),
|
||||
},
|
||||
importStep("unifi_network.test"),
|
||||
pt.ImportStep("unifi_network.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccNetwork_dhcp_dns(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -86,7 +80,7 @@ func TestAccNetwork_dhcp_dns(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "dhcp_dns.0", "192.168.1.101"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_network.test"),
|
||||
pt.ImportStep("unifi_network.test"),
|
||||
{
|
||||
Config: testAccNetworkConfig(name, subnet, vlan, true, []string{"192.168.1.101", "192.168.1.102"}),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
@@ -94,7 +88,7 @@ func TestAccNetwork_dhcp_dns(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "dhcp_dns.1", "192.168.1.102"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_network.test"),
|
||||
pt.ImportStep("unifi_network.test"),
|
||||
{
|
||||
Config: testAccNetworkConfig(name, subnet, vlan, true, nil),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
@@ -113,11 +107,9 @@ func TestAccNetwork_dhcp_dns(t *testing.T) {
|
||||
|
||||
func TestAccNetwork_dhcp_boot(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -126,7 +118,7 @@ func TestAccNetwork_dhcp_boot(t *testing.T) {
|
||||
// TODO: ...
|
||||
),
|
||||
},
|
||||
importStep("unifi_network.test"),
|
||||
pt.ImportStep("unifi_network.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -135,13 +127,11 @@ func TestAccNetwork_v6(t *testing.T) {
|
||||
t.Skip("FIXME")
|
||||
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet1, vlan1 := getTestVLAN(t)
|
||||
subnet2, vlan2 := getTestVLAN(t)
|
||||
subnet3, vlan3 := getTestVLAN(t)
|
||||
subnet1, vlan1 := pt.GetTestVLAN(t)
|
||||
subnet2, vlan2 := pt.GetTestVLAN(t)
|
||||
subnet3, vlan3 := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -152,7 +142,7 @@ func TestAccNetwork_v6(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "ipv6_static_subnet", "fd6a:37be:e362::1/64"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_network.test"),
|
||||
pt.ImportStep("unifi_network.test"),
|
||||
{
|
||||
Config: testAccNetworkConfigV6(name, subnet2, vlan2, "static", "fd6a:37be:e363::1/64"),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
@@ -160,7 +150,7 @@ func TestAccNetwork_v6(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "ipv6_static_subnet", "fd6a:37be:e363::1/64"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_network.test"),
|
||||
pt.ImportStep("unifi_network.test"),
|
||||
{
|
||||
Config: testAccNetworkConfigDhcpV6(
|
||||
name,
|
||||
@@ -195,9 +185,7 @@ func TestAccNetwork_v6(t *testing.T) {
|
||||
func TestAccNetwork_wan(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -214,7 +202,7 @@ func TestAccNetwork_wan(t *testing.T) {
|
||||
resource.TestCheckOutput("wan_dns2", "4.4.4.4"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_network.wan_test"),
|
||||
pt.ImportStep("unifi_network.wan_test"),
|
||||
// remove qos
|
||||
{
|
||||
Config: testWanNetworkConfig(name, "WAN", "pppoe", "192.168.1.1", 0, "username", "password", "8.8.8.8", "4.4.4.4"),
|
||||
@@ -230,7 +218,7 @@ func TestAccNetwork_wan(t *testing.T) {
|
||||
resource.TestCheckOutput("wan_dns2", "4.4.4.4"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_network.wan_test"),
|
||||
pt.ImportStep("unifi_network.wan_test"),
|
||||
{
|
||||
Config: testWanNetworkConfig(name, "WAN", "pppoe", "192.168.1.1", 1, "username", "password", "8.8.8.8", "4.4.4.4"),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
@@ -245,7 +233,7 @@ func TestAccNetwork_wan(t *testing.T) {
|
||||
resource.TestCheckOutput("wan_dns2", "4.4.4.4"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_network.wan_test"),
|
||||
pt.ImportStep("unifi_network.wan_test"),
|
||||
{
|
||||
Config: testWanV6NetworkConfig(name, "dhcpv6", 47),
|
||||
ExpectError: regexp.MustCompile(regexp.QuoteMeta("expected wan_dhcp_v6_pd_size to be in the range (48 - 64)")),
|
||||
@@ -267,12 +255,10 @@ func TestAccNetwork_wan(t *testing.T) {
|
||||
|
||||
func TestAccNetwork_differentSite(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet1, vlan1 := getTestVLAN(t)
|
||||
subnet2, vlan2 := getTestVLAN(t)
|
||||
subnet1, vlan1 := pt.GetTestVLAN(t)
|
||||
subnet2, vlan2 := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -284,7 +270,7 @@ func TestAccNetwork_differentSite(t *testing.T) {
|
||||
{
|
||||
ResourceName: "unifi_network.test",
|
||||
ImportState: true,
|
||||
ImportStateIdFunc: siteAndIDImportStateIDFunc("unifi_network.test"),
|
||||
ImportStateIdFunc: pt.SiteAndIDImportStateIDFunc("unifi_network.test"),
|
||||
ImportStateVerify: true,
|
||||
},
|
||||
{
|
||||
@@ -296,7 +282,7 @@ func TestAccNetwork_differentSite(t *testing.T) {
|
||||
{
|
||||
ResourceName: "unifi_network.test",
|
||||
ImportState: true,
|
||||
ImportStateIdFunc: siteAndIDImportStateIDFunc("unifi_network.test"),
|
||||
ImportStateIdFunc: pt.SiteAndIDImportStateIDFunc("unifi_network.test"),
|
||||
ImportStateVerify: true,
|
||||
},
|
||||
},
|
||||
@@ -305,13 +291,11 @@ func TestAccNetwork_differentSite(t *testing.T) {
|
||||
|
||||
func TestAccNetwork_importByName(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet1, vlan1 := getTestVLAN(t)
|
||||
subnet2, vlan2 := getTestVLAN(t)
|
||||
subnet3, vlan3 := getTestVLAN(t)
|
||||
subnet1, vlan1 := pt.GetTestVLAN(t)
|
||||
subnet2, vlan2 := pt.GetTestVLAN(t)
|
||||
subnet3, vlan3 := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
// Apply and import network by name.
|
||||
{
|
||||
@@ -352,13 +336,9 @@ func TestAccNetwork_importByName(t *testing.T) {
|
||||
|
||||
func TestAccNetwork_dhcpRelay(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -367,27 +347,23 @@ func TestAccNetwork_dhcpRelay(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "dhcp_relay_enabled", "true"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_network.test"),
|
||||
pt.ImportStep("unifi_network.test"),
|
||||
{
|
||||
Config: testAccNetworkConfigDHCPRelay(name, subnet, vlan, false),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "dhcp_relay_enabled", "false"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_network.test"),
|
||||
pt.ImportStep("unifi_network.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccNetwork_vlanOnly(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
_, vlan := getTestVLAN(t)
|
||||
_, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -399,7 +375,7 @@ func TestAccNetwork_vlanOnly(t *testing.T) {
|
||||
{
|
||||
ResourceName: "unifi_network.test",
|
||||
ImportState: true,
|
||||
ImportStateIdFunc: siteAndIDImportStateIDFunc("unifi_network.test"),
|
||||
ImportStateIdFunc: pt.SiteAndIDImportStateIDFunc("unifi_network.test"),
|
||||
ImportStateVerify: true,
|
||||
},
|
||||
},
|
||||
@@ -408,14 +384,10 @@ func TestAccNetwork_vlanOnly(t *testing.T) {
|
||||
|
||||
func TestAccNetwork_mdns(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
preCheckMinVersion(t, provider.ControllerV7)
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
VersionConstraint: "> 7.0",
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -424,14 +396,14 @@ func TestAccNetwork_mdns(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "multicast_dns", "true"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_network.test"),
|
||||
pt.ImportStep("unifi_network.test"),
|
||||
{
|
||||
Config: testAccNetworkConfigMDNS(name, subnet, vlan, false),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "multicast_dns", "false"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_network.test"),
|
||||
pt.ImportStep("unifi_network.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
|
||||
"testing"
|
||||
|
||||
@@ -12,9 +13,7 @@ func TestAccPortForward_basic(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
name2 := acctest.RandomWithPrefix("tfacc")
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -24,7 +23,7 @@ func TestAccPortForward_basic(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_port_forward.test", "dst_port", "22"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_port_forward.test"),
|
||||
pt.ImportStep("unifi_port_forward.test"),
|
||||
{
|
||||
Config: testAccPortForwardConfig("22", false, "10.1.1.2", "8022", name),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
@@ -32,23 +31,21 @@ func TestAccPortForward_basic(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_port_forward.test", "fwd_ip", "10.1.1.2"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_port_forward.test"),
|
||||
pt.ImportStep("unifi_port_forward.test"),
|
||||
{
|
||||
Config: testAccPortForwardConfig("22", false, "10.1.1.1", "22", name2),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttr("unifi_port_forward.test", "name", name2),
|
||||
),
|
||||
},
|
||||
importStep("unifi_port_forward.test"),
|
||||
pt.ImportStep("unifi_port_forward.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccPortForward_src_ip(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -58,16 +55,14 @@ func TestAccPortForward_src_ip(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_port_forward.test", "dst_port", "22"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_port_forward.test"),
|
||||
pt.ImportStep("unifi_port_forward.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccPortForward_src_cidr(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -77,7 +72,7 @@ func TestAccPortForward_src_cidr(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_port_forward.test", "dst_port", "22"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_port_forward.test"),
|
||||
pt.ImportStep("unifi_port_forward.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
|
||||
"testing"
|
||||
|
||||
@@ -10,12 +11,8 @@ import (
|
||||
|
||||
func TestAccPortProfile_basic(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
preCheckVersionConstraint(t, "< 7.4")
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
VersionConstraint: "< 7.4",
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -25,7 +22,7 @@ func TestAccPortProfile_basic(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_port_profile.test", "name", name),
|
||||
),
|
||||
},
|
||||
importStep("unifi_port_profile.test"),
|
||||
pt.ImportStep("unifi_port_profile.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
|
||||
"testing"
|
||||
|
||||
@@ -10,9 +11,7 @@ import (
|
||||
|
||||
func TestAccRadiusProfile_basic(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -21,16 +20,14 @@ func TestAccRadiusProfile_basic(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_radius_profile.test", "name", name),
|
||||
),
|
||||
},
|
||||
importStep("unifi_radius_profile.test"),
|
||||
pt.ImportStep("unifi_radius_profile.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccRadiusProfile_servers(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -39,15 +36,13 @@ func TestAccRadiusProfile_servers(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_radius_profile.test", "name", name),
|
||||
),
|
||||
},
|
||||
importStep("unifi_radius_profile.test"),
|
||||
pt.ImportStep("unifi_radius_profile.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccRadiusProfile_importByName(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
// Apply and import network by name.
|
||||
{
|
||||
@@ -1,6 +1,7 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
@@ -12,19 +13,19 @@ var settingMgmtLock = sync.Mutex{}
|
||||
func TestAccSettingMgmt_basic(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
pt.PreCheck(t)
|
||||
settingMgmtLock.Lock()
|
||||
t.Cleanup(func() {
|
||||
settingMgmtLock.Unlock()
|
||||
})
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
ProtoV6ProviderFactories: providers,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccSettingMgmtConfig_basic(),
|
||||
Check: resource.ComposeTestCheckFunc(),
|
||||
},
|
||||
importStep("unifi_setting_mgmt.test"),
|
||||
pt.ImportStep("unifi_setting_mgmt.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -32,13 +33,13 @@ func TestAccSettingMgmt_basic(t *testing.T) {
|
||||
func TestAccSettingMgmt_site(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
pt.PreCheck(t)
|
||||
settingMgmtLock.Lock()
|
||||
t.Cleanup(func() {
|
||||
settingMgmtLock.Unlock()
|
||||
})
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
ProtoV6ProviderFactories: providers,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccSettingMgmtConfig_site(),
|
||||
@@ -47,7 +48,7 @@ func TestAccSettingMgmt_site(t *testing.T) {
|
||||
{
|
||||
ResourceName: "unifi_setting_mgmt.test",
|
||||
ImportState: true,
|
||||
ImportStateIdFunc: siteAndIDImportStateIDFunc("unifi_setting_mgmt.test"),
|
||||
ImportStateIdFunc: pt.SiteAndIDImportStateIDFunc("unifi_setting_mgmt.test"),
|
||||
ImportStateVerify: true,
|
||||
},
|
||||
},
|
||||
@@ -57,13 +58,13 @@ func TestAccSettingMgmt_site(t *testing.T) {
|
||||
func TestAccSettingMgmt_sshKeys(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
pt.PreCheck(t)
|
||||
settingMgmtLock.Lock()
|
||||
t.Cleanup(func() {
|
||||
settingMgmtLock.Unlock()
|
||||
})
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
ProtoV6ProviderFactories: providers,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccSettingMgmtConfig_sshKeys(),
|
||||
@@ -72,7 +73,7 @@ func TestAccSettingMgmt_sshKeys(t *testing.T) {
|
||||
{
|
||||
ResourceName: "unifi_setting_mgmt.test",
|
||||
ImportState: true,
|
||||
ImportStateIdFunc: siteAndIDImportStateIDFunc("unifi_setting_mgmt.test"),
|
||||
ImportStateIdFunc: pt.SiteAndIDImportStateIDFunc("unifi_setting_mgmt.test"),
|
||||
ImportStateVerify: true,
|
||||
},
|
||||
},
|
||||
@@ -1,6 +1,7 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
@@ -12,19 +13,19 @@ var settingRadiusLock = sync.Mutex{}
|
||||
func TestAccSettingRadius_basic(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
pt.PreCheck(t)
|
||||
settingRadiusLock.Lock()
|
||||
t.Cleanup(func() {
|
||||
settingRadiusLock.Unlock()
|
||||
})
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
ProtoV6ProviderFactories: providers,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccSettingRadiusConfig_basic(),
|
||||
Check: resource.ComposeTestCheckFunc(),
|
||||
},
|
||||
importStep("unifi_setting_radius.test"),
|
||||
pt.ImportStep("unifi_setting_radius.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -32,13 +33,13 @@ func TestAccSettingRadius_basic(t *testing.T) {
|
||||
func TestAccSettingRadius_site(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
pt.PreCheck(t)
|
||||
settingRadiusLock.Lock()
|
||||
t.Cleanup(func() {
|
||||
settingRadiusLock.Unlock()
|
||||
})
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
ProtoV6ProviderFactories: providers,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccSettingRadiusConfig_site(),
|
||||
@@ -47,7 +48,7 @@ func TestAccSettingRadius_site(t *testing.T) {
|
||||
{
|
||||
ResourceName: "unifi_setting_radius.test",
|
||||
ImportState: true,
|
||||
ImportStateIdFunc: siteAndIDImportStateIDFunc("unifi_setting_radius.test"),
|
||||
ImportStateIdFunc: pt.SiteAndIDImportStateIDFunc("unifi_setting_radius.test"),
|
||||
ImportStateVerify: true,
|
||||
},
|
||||
},
|
||||
@@ -57,13 +58,13 @@ func TestAccSettingRadius_site(t *testing.T) {
|
||||
func TestAccSettingRadius_full(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
pt.PreCheck(t)
|
||||
settingRadiusLock.Lock()
|
||||
t.Cleanup(func() {
|
||||
settingRadiusLock.Unlock()
|
||||
})
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
ProtoV6ProviderFactories: providers,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccSettingRadiusConfig_full(),
|
||||
@@ -72,7 +73,7 @@ func TestAccSettingRadius_full(t *testing.T) {
|
||||
{
|
||||
ResourceName: "unifi_setting_radius.test",
|
||||
ImportState: true,
|
||||
ImportStateIdFunc: siteAndIDImportStateIDFunc("unifi_setting_radius.test"),
|
||||
ImportStateIdFunc: pt.SiteAndIDImportStateIDFunc("unifi_setting_radius.test"),
|
||||
ImportStateVerify: true,
|
||||
},
|
||||
},
|
||||
@@ -82,19 +83,19 @@ func TestAccSettingRadius_full(t *testing.T) {
|
||||
func TestAccSettingRadius_vlan(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
pt.PreCheck(t)
|
||||
settingRadiusLock.Lock()
|
||||
t.Cleanup(func() {
|
||||
settingRadiusLock.Unlock()
|
||||
})
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
ProtoV6ProviderFactories: providers,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccSettingRadiusConfig_vlan(),
|
||||
Check: resource.ComposeTestCheckFunc(),
|
||||
},
|
||||
importStep("unifi_setting_radius.test"),
|
||||
pt.ImportStep("unifi_setting_radius.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"regexp"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -15,30 +16,30 @@ var settingUsgLock = sync.Mutex{}
|
||||
func TestAccSettingUsg_mdns_v6(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
preCheckVersionConstraint(t, "< 7")
|
||||
pt.PreCheck(t)
|
||||
PreCheckVersionConstraint(t, "< 7")
|
||||
settingUsgLock.Lock()
|
||||
t.Cleanup(func() {
|
||||
settingUsgLock.Unlock()
|
||||
})
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
ProtoV6ProviderFactories: providers,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccSettingUsgConfig_mdns(true),
|
||||
Check: resource.ComposeTestCheckFunc(),
|
||||
},
|
||||
importStep("unifi_setting_usg.test"),
|
||||
pt.ImportStep("unifi_setting_usg.test"),
|
||||
{
|
||||
Config: testAccSettingUsgConfig_mdns(false),
|
||||
Check: resource.ComposeTestCheckFunc(),
|
||||
},
|
||||
importStep("unifi_setting_usg.test"),
|
||||
pt.ImportStep("unifi_setting_usg.test"),
|
||||
{
|
||||
Config: testAccSettingUsgConfig_mdns(true),
|
||||
Check: resource.ComposeTestCheckFunc(),
|
||||
},
|
||||
importStep("unifi_setting_usg.test"),
|
||||
pt.ImportStep("unifi_setting_usg.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -46,14 +47,14 @@ func TestAccSettingUsg_mdns_v6(t *testing.T) {
|
||||
func TestAccSettingUsg_mdns_v7(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
preCheckVersionConstraint(t, ">= 7")
|
||||
pt.PreCheck(t)
|
||||
PreCheckVersionConstraint(t, ">= 7")
|
||||
settingUsgLock.Lock()
|
||||
t.Cleanup(func() {
|
||||
settingUsgLock.Unlock()
|
||||
})
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
ProtoV6ProviderFactories: providers,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccSettingUsgConfig_mdns(true),
|
||||
@@ -66,19 +67,19 @@ func TestAccSettingUsg_mdns_v7(t *testing.T) {
|
||||
func TestAccSettingUsg_dhcpRelay(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
pt.PreCheck(t)
|
||||
settingUsgLock.Lock()
|
||||
t.Cleanup(func() {
|
||||
settingUsgLock.Unlock()
|
||||
})
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
ProtoV6ProviderFactories: providers,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccSettingUsgConfig_dhcpRelay(),
|
||||
Check: resource.ComposeTestCheckFunc(),
|
||||
},
|
||||
importStep("unifi_setting_usg.test"),
|
||||
pt.ImportStep("unifi_setting_usg.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -86,13 +87,13 @@ func TestAccSettingUsg_dhcpRelay(t *testing.T) {
|
||||
func TestAccSettingUsg_site(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
pt.PreCheck(t)
|
||||
settingUsgLock.Lock()
|
||||
t.Cleanup(func() {
|
||||
settingUsgLock.Unlock()
|
||||
})
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
ProtoV6ProviderFactories: providers,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccSettingUsgConfig_site(),
|
||||
@@ -101,7 +102,7 @@ func TestAccSettingUsg_site(t *testing.T) {
|
||||
{
|
||||
ResourceName: "unifi_setting_usg.test",
|
||||
ImportState: true,
|
||||
ImportStateIdFunc: siteAndIDImportStateIDFunc("unifi_setting_usg.test"),
|
||||
ImportStateIdFunc: pt.SiteAndIDImportStateIDFunc("unifi_setting_usg.test"),
|
||||
ImportStateVerify: true,
|
||||
},
|
||||
},
|
||||
@@ -1,8 +1,9 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -13,9 +14,7 @@ import (
|
||||
func TestAccSite_basic(t *testing.T) {
|
||||
var siteName string
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// FIXME causes flaky tests. See: https://github.com/paultyng/terraform-provider-unifi/issues/480
|
||||
//CheckDestroy: testAccCheckSiteResourceDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
@@ -31,14 +30,14 @@ func TestAccSite_basic(t *testing.T) {
|
||||
},
|
||||
),
|
||||
},
|
||||
importStep("unifi_site.test"),
|
||||
pt.ImportStep("unifi_site.test"),
|
||||
{
|
||||
Config: testAccSiteConfig("tfacc-desc2"),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttr("unifi_site.test", "description", "tfacc-desc2"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_site.test"),
|
||||
pt.ImportStep("unifi_site.test"),
|
||||
|
||||
// test importing from name, not id
|
||||
{
|
||||
@@ -1,7 +1,8 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"net"
|
||||
"strconv"
|
||||
"testing"
|
||||
@@ -19,9 +20,7 @@ func TestAccStaticRoute_nextHop(t *testing.T) {
|
||||
distance := 1
|
||||
nextHop := net.IPv4(172, 16, 0, 1).To4()
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -34,7 +33,7 @@ func TestAccStaticRoute_nextHop(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_static_route.test", "next_hop", nextHop.String()),
|
||||
),
|
||||
},
|
||||
importStep("unifi_static_route.test"),
|
||||
pt.ImportStep("unifi_static_route.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -48,9 +47,7 @@ func TestAccStaticRoute_nextHop_ipv6(t *testing.T) {
|
||||
distance := 1
|
||||
nextHop := net.IP{0xfd, 0x6a, 0x37, 0xbe, 0xe3, 0x62, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1}
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -63,7 +60,7 @@ func TestAccStaticRoute_nextHop_ipv6(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_static_route.test", "next_hop", nextHop.String()),
|
||||
),
|
||||
},
|
||||
importStep("unifi_static_route.test"),
|
||||
pt.ImportStep("unifi_static_route.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -76,9 +73,7 @@ func TestAccStaticRoute_blackhole(t *testing.T) {
|
||||
}
|
||||
distance := 1
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -90,7 +85,7 @@ func TestAccStaticRoute_blackhole(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_static_route.test", "distance", strconv.Itoa(distance)),
|
||||
),
|
||||
},
|
||||
importStep("unifi_static_route.test"),
|
||||
pt.ImportStep("unifi_static_route.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -103,9 +98,7 @@ func TestAccStaticRoute_blackhole_ipv6(t *testing.T) {
|
||||
}
|
||||
distance := 1
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -117,7 +110,7 @@ func TestAccStaticRoute_blackhole_ipv6(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_static_route.test", "distance", strconv.Itoa(distance)),
|
||||
),
|
||||
},
|
||||
importStep("unifi_static_route.test"),
|
||||
pt.ImportStep("unifi_static_route.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -131,9 +124,7 @@ func TestAccStaticRoute_interface(t *testing.T) {
|
||||
distance := 1
|
||||
networkInterface := "WAN2"
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -146,7 +137,7 @@ func TestAccStaticRoute_interface(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_static_route.test", "interface", networkInterface),
|
||||
),
|
||||
},
|
||||
importStep("unifi_static_route.test"),
|
||||
pt.ImportStep("unifi_static_route.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -160,9 +151,7 @@ func TestAccStaticRoute_interface_ipv6(t *testing.T) {
|
||||
distance := 1
|
||||
networkInterface := "WAN2"
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -175,7 +164,7 @@ func TestAccStaticRoute_interface_ipv6(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_static_route.test", "interface", networkInterface),
|
||||
),
|
||||
},
|
||||
importStep("unifi_static_route.test"),
|
||||
pt.ImportStep("unifi_static_route.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
|
||||
"testing"
|
||||
|
||||
@@ -10,9 +11,7 @@ import (
|
||||
|
||||
func TestAccUserGroup_basic(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -24,11 +23,11 @@ func TestAccUserGroup_basic(t *testing.T) {
|
||||
{
|
||||
Config: testAccUserGroupConfig_qos(name),
|
||||
},
|
||||
importStep("unifi_user_group.test"),
|
||||
pt.ImportStep("unifi_user_group.test"),
|
||||
{
|
||||
Config: testAccUserGroupConfig(name),
|
||||
},
|
||||
importStep("unifi_user_group.test"),
|
||||
pt.ImportStep("unifi_user_group.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
|
||||
"net"
|
||||
"regexp"
|
||||
@@ -17,16 +18,14 @@ import (
|
||||
)
|
||||
|
||||
func userImportStep(name string) resource.TestStep {
|
||||
return importStep(name, "allow_existing", "skip_forget_on_destroy")
|
||||
return pt.ImportStep(name, "allow_existing", "skip_forget_on_destroy")
|
||||
}
|
||||
|
||||
func TestAccUser_basic(t *testing.T) {
|
||||
mac, unallocateTestMac := allocateTestMac(t)
|
||||
mac, unallocateTestMac := pt.AllocateTestMac(t)
|
||||
defer unallocateTestMac()
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -56,19 +55,17 @@ func TestAccUser_basic(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAccUser_fixed_ip(t *testing.T) {
|
||||
mac, unallocateTestMac := allocateTestMac(t)
|
||||
mac, unallocateTestMac := pt.AllocateTestMac(t)
|
||||
defer unallocateTestMac()
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
ip, err := cidr.Host(subnet, 1)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -103,13 +100,11 @@ func TestAccUser_fixed_ip(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAccUser_blocking(t *testing.T) {
|
||||
mac, unallocateTestMac := allocateTestMac(t)
|
||||
mac, unallocateTestMac := pt.AllocateTestMac(t)
|
||||
defer unallocateTestMac()
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -141,14 +136,12 @@ func TestAccUser_blocking(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAccUser_existing_mac_allow(t *testing.T) {
|
||||
mac, unallocateTestMac := allocateTestMac(t)
|
||||
mac, unallocateTestMac := pt.AllocateTestMac(t)
|
||||
defer unallocateTestMac()
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
|
||||
_, err := testClient.CreateUser(context.Background(), "default", &unifi.User{
|
||||
MAC: mac,
|
||||
Name: name,
|
||||
@@ -158,10 +151,7 @@ func TestAccUser_existing_mac_allow(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
CheckDestroy: func(*terraform.State) error {
|
||||
// TODO: CheckDestroy: ,
|
||||
|
||||
return testClient.DeleteUserByMAC(context.Background(), "default", mac)
|
||||
},
|
||||
Steps: []resource.TestStep{
|
||||
@@ -178,7 +168,7 @@ func TestAccUser_existing_mac_allow(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAccUser_existing_mac_deny(t *testing.T) {
|
||||
mac, unallocateTestMac := allocateTestMac(t)
|
||||
mac, unallocateTestMac := pt.AllocateTestMac(t)
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
|
||||
_, err := testClient.CreateUser(context.Background(), "default", &unifi.User{
|
||||
@@ -199,9 +189,7 @@ func TestAccUser_existing_mac_deny(t *testing.T) {
|
||||
unallocateTestMac()
|
||||
}()
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccUserConfig_existing(mac, name, "tfacc note", false, false),
|
||||
@@ -212,13 +200,12 @@ func TestAccUser_existing_mac_deny(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAccUser_fingerprint(t *testing.T) {
|
||||
mac, unallocateTestMac := allocateTestMac(t)
|
||||
mac, unallocateTestMac := pt.AllocateTestMac(t)
|
||||
defer unallocateTestMac()
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
ProviderFactories: providerFactories,
|
||||
CheckDestroy: testCheckUserDestroy,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
CheckDestroy: testCheckUserDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccUserConfig_fingerprint(mac, name, 123),
|
||||
@@ -246,23 +233,19 @@ func TestAccUser_fingerprint(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAccUser_localdns(t *testing.T) {
|
||||
mac, unallocateTestMac := allocateTestMac(t)
|
||||
mac, unallocateTestMac := pt.AllocateTestMac(t)
|
||||
defer unallocateTestMac()
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
ip, err := cidr.Host(subnet, 1)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
preCheckVersionConstraint(t, ">= 7.2.91")
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
VersionConstraint: ">= 7.2.91",
|
||||
CheckDestroy: testCheckUserDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
@@ -1,28 +1,21 @@
|
||||
package v1
|
||||
package acctest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
pt "github.com/filipowm/terraform-provider-unifi/internal/provider/testing"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
"github.com/hashicorp/terraform-plugin-testing/terraform"
|
||||
)
|
||||
|
||||
func TestAccWLAN_wpapsk(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
CheckDestroy: func(*terraform.State) error {
|
||||
// TODO: actual CheckDestroy
|
||||
|
||||
return nil
|
||||
},
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccWLANConfig_wpapsk(name, subnet, vlan, "disabled"),
|
||||
@@ -30,23 +23,16 @@ func TestAccWLAN_wpapsk(t *testing.T) {
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccWLAN_open(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
CheckDestroy: func(*terraform.State) error {
|
||||
// TODO: actual CheckDestroy
|
||||
|
||||
return nil
|
||||
},
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccWLANConfig_open(name, subnet, vlan),
|
||||
@@ -54,37 +40,30 @@ func TestAccWLAN_open(t *testing.T) {
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
{
|
||||
Config: testAccWLANConfig_open_mac_filter(name, subnet, vlan),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
{
|
||||
Config: testAccWLANConfig_open(name, subnet, vlan),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccWLAN_change_security_and_pmf(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
CheckDestroy: func(*terraform.State) error {
|
||||
// TODO: actual CheckDestroy
|
||||
|
||||
return nil
|
||||
},
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccWLANConfig_wpapsk(name, subnet, vlan, "disabled"),
|
||||
@@ -92,51 +71,44 @@ func TestAccWLAN_change_security_and_pmf(t *testing.T) {
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
{
|
||||
Config: testAccWLANConfig_open(name, subnet, vlan),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
{
|
||||
Config: testAccWLANConfig_wpapsk(name, subnet, vlan, "optional"),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
{
|
||||
Config: testAccWLANConfig_wpapsk(name, subnet, vlan, "required"),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
{
|
||||
Config: testAccWLANConfig_wpapsk(name, subnet, vlan, "disabled"),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccWLAN_schedule(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
CheckDestroy: func(*terraform.State) error {
|
||||
// TODO: actual CheckDestroy
|
||||
|
||||
return nil
|
||||
},
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccWLANConfig_schedule(name, subnet, vlan),
|
||||
@@ -144,7 +116,7 @@ func TestAccWLAN_schedule(t *testing.T) {
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
// remove schedule
|
||||
{
|
||||
Config: testAccWLANConfig_open(name, subnet, vlan),
|
||||
@@ -152,23 +124,16 @@ func TestAccWLAN_schedule(t *testing.T) {
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccWLAN_wpaeap(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
CheckDestroy: func(*terraform.State) error {
|
||||
// TODO: actual CheckDestroy
|
||||
|
||||
return nil
|
||||
},
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccWLANConfig_wpaeap(name, subnet, vlan),
|
||||
@@ -176,23 +141,16 @@ func TestAccWLAN_wpaeap(t *testing.T) {
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccWLAN_wlan_band(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
CheckDestroy: func(*terraform.State) error {
|
||||
// TODO: actual CheckDestroy
|
||||
|
||||
return nil
|
||||
},
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccWLANConfig_wlan_band(name, subnet, vlan),
|
||||
@@ -200,23 +158,16 @@ func TestAccWLAN_wlan_band(t *testing.T) {
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccWLAN_no2ghz_oui(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
CheckDestroy: func(*terraform.State) error {
|
||||
// TODO: actual CheckDestroy
|
||||
|
||||
return nil
|
||||
},
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccWLANConfig_no2ghz_oui(name, subnet, vlan),
|
||||
@@ -224,23 +175,16 @@ func TestAccWLAN_no2ghz_oui(t *testing.T) {
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccWLAN_proxy_arp(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
CheckDestroy: func(*terraform.State) error {
|
||||
// TODO: actual CheckDestroy
|
||||
|
||||
return nil
|
||||
},
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccWLANConfig_proxy_arp(name, subnet, vlan, true),
|
||||
@@ -248,23 +192,16 @@ func TestAccWLAN_proxy_arp(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_wlan.test", "proxy_arp", "true"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccWLAN_bss_transition(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
CheckDestroy: func(*terraform.State) error {
|
||||
// TODO: actual CheckDestroy
|
||||
|
||||
return nil
|
||||
},
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccWLANConfig_bss_transition(name, subnet, vlan, false),
|
||||
@@ -272,23 +209,16 @@ func TestAccWLAN_bss_transition(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_wlan.test", "bss_transition", "false"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccWLAN_uapsd(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
CheckDestroy: func(*terraform.State) error {
|
||||
// TODO: actual CheckDestroy
|
||||
|
||||
return nil
|
||||
},
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccWLANConfig_uapsd(name, subnet, vlan),
|
||||
@@ -296,23 +226,16 @@ func TestAccWLAN_uapsd(t *testing.T) {
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccWLAN_fast_roaming_enabled(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
CheckDestroy: func(*terraform.State) error {
|
||||
// TODO: actual CheckDestroy
|
||||
|
||||
return nil
|
||||
},
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccWLANConfig_fast_roaming_enabled(name, subnet, vlan, true),
|
||||
@@ -320,26 +243,17 @@ func TestAccWLAN_fast_roaming_enabled(t *testing.T) {
|
||||
resource.TestCheckResourceAttr("unifi_wlan.test", "fast_roaming_enabled", "true"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccWLAN_wpa3(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
preCheckMinVersion(t, provider.ControllerVersionWPA3)
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
CheckDestroy: func(*terraform.State) error {
|
||||
// TODO: actual CheckDestroy
|
||||
|
||||
return nil
|
||||
},
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
MinVersion: base.ControllerVersionWPA3,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccWLANConfig_wpa3(name, subnet, vlan, false, "required"),
|
||||
@@ -347,37 +261,30 @@ func TestAccWLAN_wpa3(t *testing.T) {
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
{
|
||||
Config: testAccWLANConfig_wpa3(name, subnet, vlan, true, "optional"),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
{
|
||||
Config: testAccWLANConfig_wpa3(name, subnet, vlan, false, "required"),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccWLAN_minimum_data_rate(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
subnet, vlan := getTestVLAN(t)
|
||||
subnet, vlan := pt.GetTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
CheckDestroy: func(*terraform.State) error {
|
||||
// TODO: actual CheckDestroy
|
||||
|
||||
return nil
|
||||
},
|
||||
AcceptanceTest(t, AcceptanceTestCase{
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccWLANConfig_minimum_data_rate(name, subnet, vlan, 5500, 18000),
|
||||
@@ -385,35 +292,35 @@ func TestAccWLAN_minimum_data_rate(t *testing.T) {
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
{
|
||||
Config: testAccWLANConfig_minimum_data_rate(name, subnet, vlan, 1000, 18000),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
{
|
||||
Config: testAccWLANConfig_minimum_data_rate(name, subnet, vlan, 0, 0),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
{
|
||||
Config: testAccWLANConfig_minimum_data_rate(name, subnet, vlan, 6000, 9000),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
{
|
||||
Config: testAccWLANConfig_minimum_data_rate(name, subnet, vlan, 18000, 6000),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
pt.ImportStep("unifi_wlan.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -1,18 +1,17 @@
|
||||
package v1
|
||||
package apgroup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
)
|
||||
|
||||
func dataAPGroup() *schema.Resource {
|
||||
func DataAPGroup() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_ap_group` data source can be used to retrieve the ID for an AP group by name.",
|
||||
|
||||
ReadContext: dataAPGroupRead,
|
||||
ReadContext: DataAPGroupRead,
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"id": {
|
||||
@@ -35,8 +34,8 @@ func dataAPGroup() *schema.Resource {
|
||||
}
|
||||
}
|
||||
|
||||
func dataAPGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
func DataAPGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*base.Client)
|
||||
|
||||
name := d.Get("name").(string)
|
||||
site := d.Get("site").(string)
|
||||
45
internal/provider/base/base.go
Normal file
45
internal/provider/base/base.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
)
|
||||
|
||||
type BaseData interface {
|
||||
SetClient(client *Client)
|
||||
}
|
||||
|
||||
func ConfigureDatasource(base BaseData, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
||||
if req.ProviderData == nil {
|
||||
return
|
||||
}
|
||||
|
||||
cfg, ok := req.ProviderData.(*Client)
|
||||
if !ok {
|
||||
resp.Diagnostics.AddError("Unexpected Datasource Configure Type", fmt.Sprintf("Expected provider.Client, got: %T", req.ProviderData))
|
||||
return
|
||||
}
|
||||
if cfg == nil {
|
||||
resp.Diagnostics.AddError("Empty configuration", "provider.Client is nil")
|
||||
return
|
||||
}
|
||||
base.SetClient(cfg)
|
||||
}
|
||||
|
||||
func ConfigureResource(base BaseData, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
|
||||
if req.ProviderData == nil {
|
||||
return
|
||||
}
|
||||
|
||||
cfg, ok := req.ProviderData.(*Client)
|
||||
if !ok {
|
||||
resp.Diagnostics.AddError("Unexpected Resource Configure Type", fmt.Sprintf("Expected provider.Client, got: %T", req.ProviderData))
|
||||
return
|
||||
}
|
||||
if cfg == nil {
|
||||
resp.Diagnostics.AddError("Empty configuration", "provider.Client is nil")
|
||||
return
|
||||
}
|
||||
base.SetClient(cfg)
|
||||
}
|
||||
@@ -1,13 +1,16 @@
|
||||
package provider
|
||||
package base
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
"github.com/hashicorp/go-version"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func IsServerErrorContains(err error, messageContains string) bool {
|
||||
@@ -76,3 +79,21 @@ type Client struct {
|
||||
Site string
|
||||
Version *version.Version
|
||||
}
|
||||
|
||||
func CreateHttpTransport(insecure bool) http.RoundTripper {
|
||||
return &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).DialContext,
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: insecure,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
package provider
|
||||
package base
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/go-version"
|
||||
)
|
||||
|
||||
@@ -13,7 +12,10 @@ func asVersion(versionString string) *version.Version {
|
||||
var (
|
||||
ControllerV6 = asVersion("6.0.0")
|
||||
ControllerV7 = asVersion("7.0.0")
|
||||
ControllerV9 = asVersion("9.0.0")
|
||||
ControllerVersionApiKeyAuth = asVersion("9.0.108")
|
||||
// https://community.ui.com/releases/UniFi-Network-Application-8-2-93/fce86dc6-897a-4944-9c53-1eec7e37e738
|
||||
ControllerVersionDnsRecords = asVersion("8.2.93")
|
||||
|
||||
// https://community.ui.com/releases/UniFi-Network-Controller-6-1-61/62f1ad38-1ac5-430c-94b0-becbb8f71d7d
|
||||
ControllerVersionWPA3 = asVersion("6.1.61")
|
||||
@@ -27,6 +29,10 @@ func (c *Client) IsControllerV7() bool {
|
||||
return c.Version.GreaterThanOrEqual(ControllerV7)
|
||||
}
|
||||
|
||||
func (c *Client) IsControllerV9() bool {
|
||||
return c.Version.GreaterThanOrEqual(ControllerV9)
|
||||
}
|
||||
|
||||
func (c *Client) SupportsApiKeyAuthentication() bool {
|
||||
return c.Version.GreaterThanOrEqual(ControllerVersionApiKeyAuth)
|
||||
}
|
||||
@@ -35,6 +41,10 @@ func (c *Client) SupportsWPA3() bool {
|
||||
return c.Version.GreaterThanOrEqual(ControllerVersionWPA3)
|
||||
}
|
||||
|
||||
func (c *Client) SupportsDnsRecords() bool {
|
||||
return c.Version.GreaterThanOrEqual(ControllerVersionDnsRecords)
|
||||
}
|
||||
|
||||
func CheckMinimumControllerVersion(versionString string) error {
|
||||
v, err := version.NewVersion(versionString)
|
||||
if err != nil {
|
||||
@@ -1,10 +1,11 @@
|
||||
package v1
|
||||
package device
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -16,7 +17,7 @@ import (
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
|
||||
)
|
||||
|
||||
func resourceDevice() *schema.Resource {
|
||||
func ResourceDevice() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_device` manages a device of the network.\n\n" +
|
||||
"Devices are adopted by the controller, so it is not possible for this resource to be created through " +
|
||||
@@ -50,8 +51,8 @@ func resourceDevice() *schema.Resource {
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
ForceNew: true,
|
||||
DiffSuppressFunc: macDiffSuppressFunc,
|
||||
ValidateFunc: validation.StringMatch(macAddressRegexp, "Mac address is invalid"),
|
||||
DiffSuppressFunc: utils.MacDiffSuppressFunc,
|
||||
ValidateFunc: validation.StringMatch(utils.MacAddressRegexp, "Mac address is invalid"),
|
||||
},
|
||||
"name": {
|
||||
Description: "The name of the device.",
|
||||
@@ -139,7 +140,7 @@ func resourceDevice() *schema.Resource {
|
||||
}
|
||||
|
||||
func resourceDeviceImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
id := d.Id()
|
||||
site := d.Get("site").(string)
|
||||
if site == "" {
|
||||
@@ -152,9 +153,9 @@ func resourceDeviceImport(ctx context.Context, d *schema.ResourceData, meta inte
|
||||
id = importParts[1]
|
||||
}
|
||||
|
||||
if macAddressRegexp.MatchString(id) {
|
||||
if utils.MacAddressRegexp.MatchString(id) {
|
||||
// look up id by mac
|
||||
mac := cleanMAC(id)
|
||||
mac := utils.CleanMAC(id)
|
||||
device, err := c.GetDeviceByMAC(ctx, site, mac)
|
||||
|
||||
if err != nil {
|
||||
@@ -175,7 +176,7 @@ func resourceDeviceImport(ctx context.Context, d *schema.ResourceData, meta inte
|
||||
}
|
||||
|
||||
func resourceDeviceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
site := d.Get("site").(string)
|
||||
if site == "" {
|
||||
@@ -187,7 +188,7 @@ func resourceDeviceCreate(ctx context.Context, d *schema.ResourceData, meta inte
|
||||
return diag.Errorf("no MAC address specified, please import the device using terraform import")
|
||||
}
|
||||
|
||||
mac = cleanMAC(mac)
|
||||
mac = utils.CleanMAC(mac)
|
||||
device, err := c.GetDeviceByMAC(ctx, site, mac)
|
||||
|
||||
if device == nil {
|
||||
@@ -218,7 +219,7 @@ func resourceDeviceCreate(ctx context.Context, d *schema.ResourceData, meta inte
|
||||
}
|
||||
|
||||
func resourceDeviceUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
site := d.Get("site").(string)
|
||||
if site == "" {
|
||||
@@ -247,7 +248,7 @@ func resourceDeviceUpdate(ctx context.Context, d *schema.ResourceData, meta inte
|
||||
}
|
||||
|
||||
func resourceDeviceDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
if !d.Get("forget_on_destroy").(bool) {
|
||||
return nil
|
||||
@@ -274,7 +275,7 @@ func resourceDeviceDelete(ctx context.Context, d *schema.ResourceData, meta inte
|
||||
}
|
||||
|
||||
func resourceDeviceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -389,7 +390,7 @@ func fromPortOverride(po unifi.DevicePortOverrides) (map[string]interface{}, err
|
||||
}
|
||||
|
||||
func waitForDeviceState(ctx context.Context, d *schema.ResourceData, meta interface{}, targetState unifi.DeviceState, pendingStates []unifi.DeviceState, timeout time.Duration) (*unifi.Device, error) {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
site := d.Get("site").(string)
|
||||
mac := d.Get("mac").(string)
|
||||
131
internal/provider/dns/datasource_dns_record.go
Normal file
131
internal/provider/dns/datasource_dns_record.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package dns
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
"github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator"
|
||||
"github.com/hashicorp/terraform-plugin-framework-validators/objectvalidator"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/path"
|
||||
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
||||
)
|
||||
|
||||
var (
|
||||
_ datasource.DataSource = &dnsRecordDatasource{}
|
||||
_ datasource.DataSourceWithConfigure = &dnsRecordDatasource{}
|
||||
_ base.BaseData = &dnsRecordDatasource{}
|
||||
_ datasource.DataSourceWithConfigValidators = &dnsRecordDatasource{}
|
||||
)
|
||||
|
||||
type dnsRecordDatasource struct {
|
||||
client *base.Client
|
||||
}
|
||||
|
||||
func NewDnsRecordDatasource() datasource.DataSource {
|
||||
return &dnsRecordDatasource{}
|
||||
}
|
||||
|
||||
func (d *dnsRecordDatasource) ConfigValidators(_ context.Context) []datasource.ConfigValidator {
|
||||
return []datasource.ConfigValidator{
|
||||
datasourcevalidator.ExactlyOneOf(
|
||||
path.MatchRoot("filter").AtName("name"),
|
||||
path.MatchRoot("filter").AtName("record"),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
func (d *dnsRecordDatasource) SetClient(client *base.Client) {
|
||||
d.client = client
|
||||
}
|
||||
|
||||
func (d *dnsRecordDatasource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
||||
base.ConfigureDatasource(d, req, resp)
|
||||
}
|
||||
|
||||
func (d *dnsRecordDatasource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
resp.TypeName = fmt.Sprintf("%s_%s", req.ProviderTypeName, resourceName)
|
||||
}
|
||||
|
||||
func (d *dnsRecordDatasource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
Description: "Retrieves information about a specific DNS record.",
|
||||
Attributes: dnsRecordDatasourceAttributes,
|
||||
Blocks: map[string]schema.Block{
|
||||
"filter": schema.SingleNestedBlock{
|
||||
Description: "Filter to apply to the DNS record.",
|
||||
Validators: []validator.Object{
|
||||
objectvalidator.IsRequired(),
|
||||
},
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"name": schema.StringAttribute{
|
||||
Description: "DNS record name.",
|
||||
Optional: true,
|
||||
},
|
||||
"record": schema.StringAttribute{
|
||||
Description: "DNS record content.",
|
||||
Optional: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (d *dnsRecordDatasource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
if !d.client.SupportsDnsRecords() {
|
||||
resp.Diagnostics.AddError("DNS Records are not supported", fmt.Sprintf("The Unifi controller in version %q does not support DNS records. Required controller version: %q", d.client.Version, base.ControllerVersionDnsRecords))
|
||||
}
|
||||
var state dnsRecordDatasourceModel
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
if state.Filter == nil {
|
||||
// TODO remove after testing validation
|
||||
resp.Diagnostics.AddError("Filter is required", "Filter is required. Validation should prevent this from happening.")
|
||||
return
|
||||
}
|
||||
list, err := d.client.ListDNSRecord(ctx, d.client.Site)
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError("Failed to list DNS records", err.Error())
|
||||
return
|
||||
}
|
||||
if len(list) == 0 {
|
||||
resp.Diagnostics.AddError("DNS record not found", "No DNS record found")
|
||||
return
|
||||
}
|
||||
var nameFilter, recordFilter string
|
||||
if utils.IsStringValueNotEmpty(state.Filter.Name) {
|
||||
nameFilter = state.Filter.Name.ValueString()
|
||||
}
|
||||
if utils.IsStringValueNotEmpty(state.Filter.Record) {
|
||||
recordFilter = state.Filter.Record.ValueString()
|
||||
}
|
||||
if nameFilter != "" && recordFilter != "" {
|
||||
// TODO remove after testing validation
|
||||
resp.Diagnostics.AddError("Filter is invalid", "Only one of 'name' or 'record' can be specified. Validation should prevent this from happening.")
|
||||
return
|
||||
}
|
||||
var found *unifi.DNSRecord
|
||||
for _, record := range list {
|
||||
if nameFilter != "" && record.Key == nameFilter {
|
||||
found = &record
|
||||
break
|
||||
}
|
||||
if recordFilter != "" && record.Value == recordFilter {
|
||||
found = &record
|
||||
break
|
||||
}
|
||||
}
|
||||
if found == nil {
|
||||
resp.Diagnostics.AddError("DNS record not found", "No DNS record found")
|
||||
return
|
||||
}
|
||||
(&state.dnsRecordModel).merge(found)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
|
||||
}
|
||||
81
internal/provider/dns/datasource_dns_records.go
Normal file
81
internal/provider/dns/datasource_dns_records.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package dns
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
var (
|
||||
_ datasource.DataSource = &dnsRecordsDatasource{}
|
||||
_ datasource.DataSourceWithConfigure = &dnsRecordsDatasource{}
|
||||
_ base.BaseData = &dnsRecordsDatasource{}
|
||||
)
|
||||
|
||||
type dnsRecordsDatasource struct {
|
||||
client *base.Client
|
||||
}
|
||||
|
||||
func NewDnsRecordsDatasource() datasource.DataSource {
|
||||
return &dnsRecordsDatasource{}
|
||||
}
|
||||
|
||||
func (d *dnsRecordsDatasource) SetClient(client *base.Client) {
|
||||
d.client = client
|
||||
}
|
||||
|
||||
func (d *dnsRecordsDatasource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
||||
base.ConfigureDatasource(d, req, resp)
|
||||
}
|
||||
|
||||
func (d *dnsRecordsDatasource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
resp.TypeName = fmt.Sprintf("%s_%ss", req.ProviderTypeName, resourceName)
|
||||
}
|
||||
|
||||
func (d *dnsRecordsDatasource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
Description: "Retrieves information about a all DNS records.",
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"result": schema.ListNestedAttribute{
|
||||
Description: "The list of DNS records.",
|
||||
Computed: true,
|
||||
NestedObject: schema.NestedAttributeObject{
|
||||
Attributes: dnsRecordDatasourceAttributes,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (d *dnsRecordsDatasource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
var state dnsRecordsDatasourceModel
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
records, err := d.client.ListDNSRecord(ctx, d.client.Site)
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError("Failed to list DNS records", err.Error())
|
||||
return
|
||||
}
|
||||
for _, record := range records {
|
||||
state.Records = append(state.Records, &dnsRecordModel{
|
||||
ID: types.StringValue(record.ID),
|
||||
SiteID: types.StringValue(record.SiteID),
|
||||
Name: types.StringValue(record.Key),
|
||||
Record: types.StringValue(record.Value),
|
||||
Enabled: types.BoolValue(record.Enabled),
|
||||
Port: types.Int32Value(int32(record.Port)),
|
||||
Priority: types.Int32Value(int32(record.Priority)),
|
||||
Type: types.StringValue(record.RecordType),
|
||||
TTL: types.Int32Value(int32(record.Ttl)),
|
||||
Weight: types.Int32Value(int32(record.Weight)),
|
||||
})
|
||||
}
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
|
||||
}
|
||||
102
internal/provider/dns/dns_record_model.go
Normal file
102
internal/provider/dns/dns_record_model.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package dns
|
||||
|
||||
import (
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
const resourceName = "dns_record"
|
||||
|
||||
type dnsRecordModel struct {
|
||||
ID types.String `tfsdk:"id"`
|
||||
SiteID types.String `tfsdk:"site_id"`
|
||||
Name types.String `tfsdk:"name"`
|
||||
Record types.String `tfsdk:"record"`
|
||||
Enabled types.Bool `tfsdk:"enabled"`
|
||||
Port types.Int32 `tfsdk:"port"`
|
||||
Priority types.Int32 `tfsdk:"priority"`
|
||||
Type types.String `tfsdk:"type"`
|
||||
TTL types.Int32 `tfsdk:"ttl"`
|
||||
Weight types.Int32 `tfsdk:"weight"`
|
||||
}
|
||||
|
||||
type dnsRecordDatasourceModel struct {
|
||||
dnsRecordModel
|
||||
Filter *dnsRecordFilterModel `tfsdk:"filter"`
|
||||
}
|
||||
|
||||
type dnsRecordsDatasourceModel struct {
|
||||
Records []*dnsRecordModel `tfsdk:"result"`
|
||||
}
|
||||
|
||||
var dnsRecordDatasourceAttributes = map[string]schema.Attribute{
|
||||
"id": utils.ID(),
|
||||
"site_id": utils.ID("The site ID where the DNS record is located."),
|
||||
"name": schema.StringAttribute{
|
||||
Description: "DNS record name.",
|
||||
Computed: true,
|
||||
},
|
||||
"record": schema.StringAttribute{
|
||||
Description: "DNS record content.",
|
||||
Computed: true,
|
||||
},
|
||||
"enabled": schema.BoolAttribute{
|
||||
Description: "Whether the DNS record is enabled.",
|
||||
Computed: true,
|
||||
},
|
||||
"port": schema.Int32Attribute{
|
||||
Description: "The port of the DNS record.",
|
||||
Computed: true,
|
||||
},
|
||||
"priority": schema.Int32Attribute{
|
||||
Description: "Priority of the DNS records. Present only for MX and SRV records; unused by other record types.",
|
||||
Computed: true,
|
||||
},
|
||||
"type": schema.StringAttribute{
|
||||
Description: "The type of the DNS record.",
|
||||
Computed: true,
|
||||
},
|
||||
"ttl": schema.Int32Attribute{
|
||||
Description: "Time To Live (TTL) of the DNS record in seconds. Setting to 0 means 'automatic'.",
|
||||
Computed: true,
|
||||
},
|
||||
"weight": schema.Int32Attribute{
|
||||
Description: "A numeric value indicating the relative weight of the record.",
|
||||
Computed: true,
|
||||
},
|
||||
}
|
||||
|
||||
type dnsRecordFilterModel struct {
|
||||
Name types.String `tfsdk:"name"`
|
||||
Record types.String `tfsdk:"record"`
|
||||
}
|
||||
|
||||
func (d *dnsRecordModel) asUnifiModel() *unifi.DNSRecord {
|
||||
return &unifi.DNSRecord{
|
||||
ID: d.ID.ValueString(),
|
||||
SiteID: d.SiteID.ValueString(),
|
||||
Key: d.Name.ValueString(),
|
||||
Value: d.Record.ValueString(),
|
||||
Enabled: d.Enabled.ValueBool(),
|
||||
Port: int(d.Port.ValueInt32()),
|
||||
Priority: int(d.Priority.ValueInt32()),
|
||||
RecordType: d.Type.ValueString(),
|
||||
Ttl: int(d.TTL.ValueInt32()),
|
||||
Weight: int(d.Weight.ValueInt32()),
|
||||
}
|
||||
}
|
||||
|
||||
func (d *dnsRecordModel) merge(other *unifi.DNSRecord) {
|
||||
d.ID = types.StringValue(other.ID)
|
||||
d.SiteID = types.StringValue(other.SiteID)
|
||||
d.Name = types.StringValue(other.Key)
|
||||
d.Record = types.StringValue(other.Value)
|
||||
d.Enabled = types.BoolValue(other.Enabled)
|
||||
d.Port = types.Int32Value(int32(other.Port))
|
||||
d.Priority = types.Int32Value(int32(other.Priority))
|
||||
d.Type = types.StringValue(other.RecordType)
|
||||
d.TTL = types.Int32Value(int32(other.Ttl))
|
||||
d.Weight = types.Int32Value(int32(other.Weight))
|
||||
}
|
||||
236
internal/provider/dns/resource_dns_record.go
Normal file
236
internal/provider/dns/resource_dns_record.go
Normal file
@@ -0,0 +1,236 @@
|
||||
package dns
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
"github.com/hashicorp/terraform-plugin-framework-validators/int32validator"
|
||||
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
|
||||
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
var (
|
||||
_ resource.Resource = &dnsRecordResource{}
|
||||
_ resource.ResourceWithConfigure = &dnsRecordResource{}
|
||||
_ resource.ResourceWithImportState = &dnsRecordResource{}
|
||||
_ base.BaseData = &dnsRecordResource{}
|
||||
)
|
||||
|
||||
type dnsRecordResource struct {
|
||||
client *base.Client
|
||||
}
|
||||
|
||||
func (d *dnsRecordResource) SetClient(client *base.Client) {
|
||||
d.client = client
|
||||
}
|
||||
|
||||
func NewDnsRecordResource() resource.Resource {
|
||||
return &dnsRecordResource{}
|
||||
}
|
||||
|
||||
func (d *dnsRecordResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
|
||||
base.ConfigureResource(d, req, resp)
|
||||
}
|
||||
|
||||
func (d *dnsRecordResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
resp.TypeName = fmt.Sprintf("%s_%s", req.ProviderTypeName, resourceName)
|
||||
}
|
||||
|
||||
func (d *dnsRecordResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
MarkdownDescription: "Manages a DNS record in the Unifi controller.",
|
||||
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"id": utils.ID(),
|
||||
"site_id": utils.ID("The site ID where the DNS record is located."),
|
||||
"name": schema.StringAttribute{
|
||||
MarkdownDescription: "DNS record name.",
|
||||
Required: true,
|
||||
},
|
||||
"record": schema.StringAttribute{
|
||||
MarkdownDescription: "DNS record content.",
|
||||
Required: true,
|
||||
},
|
||||
"enabled": schema.BoolAttribute{
|
||||
MarkdownDescription: "Whether the DNS record is enabled.",
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
Default: booldefault.StaticBool(true),
|
||||
},
|
||||
"port": schema.Int32Attribute{
|
||||
MarkdownDescription: "The port of the DNS record.",
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
Validators: []validator.Int32{
|
||||
int32validator.Between(1, 65535),
|
||||
},
|
||||
},
|
||||
"priority": schema.Int32Attribute{
|
||||
MarkdownDescription: "Required for MX and SRV records; unused by other record types. Records with lower priorities are preferred",
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
Validators: []validator.Int32{
|
||||
int32validator.AtLeast(1),
|
||||
},
|
||||
},
|
||||
"type": schema.StringAttribute{
|
||||
MarkdownDescription: "The type of the DNS record.",
|
||||
Required: true,
|
||||
Validators: []validator.String{
|
||||
stringvalidator.OneOf("A", "AAAA", "CNAME", "MX", "NS", "PTR", "SOA", "SRV", "TXT"),
|
||||
},
|
||||
},
|
||||
"ttl": schema.Int32Attribute{
|
||||
MarkdownDescription: "Time To Live (TTL) of the DNS record in seconds. Setting to 0 means 'automatic'.",
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"weight": schema.Int32Attribute{
|
||||
MarkdownDescription: "A numeric value indicating the relative weight of the record.",
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (d *dnsRecordResource) checkSupportsDnsRecords(diag *diag.Diagnostics) {
|
||||
if !d.client.SupportsDnsRecords() {
|
||||
diag.AddError("DNS Records are not supported", fmt.Sprintf("The Unifi controller in version %q does not support DNS records. Required controller version: %q", d.client.Version, base.ControllerVersionDnsRecords))
|
||||
}
|
||||
}
|
||||
|
||||
func (d *dnsRecordResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
d.checkSupportsDnsRecords(&resp.Diagnostics)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
var plan dnsRecordModel
|
||||
diags := req.Plan.Get(ctx, &plan)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
body := plan.asUnifiModel()
|
||||
|
||||
res, err := d.client.CreateDNSRecord(ctx, d.client.Site, body)
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError("Error creating DNS record", err.Error())
|
||||
return
|
||||
}
|
||||
plan.merge(res)
|
||||
|
||||
resp.State.Set(ctx, plan)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
}
|
||||
|
||||
func (d *dnsRecordResource) read(ctx context.Context, state *dnsRecordModel, diag *diag.Diagnostics) {
|
||||
res, err := d.client.GetDNSRecord(ctx, d.client.Site, state.ID.ValueString())
|
||||
if err != nil {
|
||||
diag.AddError("Error reading DNS record", err.Error())
|
||||
return
|
||||
}
|
||||
state.merge(res)
|
||||
}
|
||||
|
||||
func (d *dnsRecordResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
||||
d.checkSupportsDnsRecords(&resp.Diagnostics)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
var state dnsRecordModel
|
||||
diags := req.State.Get(ctx, &state)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
d.read(ctx, &state, &resp.Diagnostics)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
diags = resp.State.Set(ctx, state)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
}
|
||||
|
||||
func (d *dnsRecordResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
d.checkSupportsDnsRecords(&resp.Diagnostics)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
var plan, state dnsRecordModel
|
||||
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
body := plan.asUnifiModel()
|
||||
|
||||
res, err := d.client.UpdateDNSRecord(ctx, d.client.Site, body)
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError("Error updating DNS record", err.Error())
|
||||
return
|
||||
}
|
||||
state.merge(res)
|
||||
diags := resp.State.Set(ctx, state)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
}
|
||||
|
||||
func (d *dnsRecordResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
||||
d.checkSupportsDnsRecords(&resp.Diagnostics)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
var state dnsRecordModel
|
||||
diags := req.State.Get(ctx, &state)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
err := d.client.DeleteDNSRecord(ctx, d.client.Site, state.ID.ValueString())
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError("Error deleting DNS record", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (d *dnsRecordResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
||||
d.checkSupportsDnsRecords(&resp.Diagnostics)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
id := req.ID
|
||||
if id == "" {
|
||||
resp.Diagnostics.AddError("Invalid import ID", "The ID must be set")
|
||||
return
|
||||
}
|
||||
|
||||
state := dnsRecordModel{
|
||||
ID: types.StringValue(id),
|
||||
}
|
||||
d.read(ctx, &state, &resp.Diagnostics)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
d.read(ctx, &state, &resp.Diagnostics)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
diags := resp.State.Set(ctx, state)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
}
|
||||
@@ -1,16 +1,17 @@
|
||||
package v1
|
||||
package dns
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
)
|
||||
|
||||
func resourceDynamicDNS() *schema.Resource {
|
||||
func ResourceDynamicDNS() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_dynamic_dns` manages dynamic DNS settings for different providers.",
|
||||
|
||||
@@ -19,7 +20,7 @@ func resourceDynamicDNS() *schema.Resource {
|
||||
UpdateContext: resourceDynamicDNSUpdate,
|
||||
DeleteContext: resourceDynamicDNSDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
StateContext: importSiteAndID,
|
||||
StateContext: utils.ImportSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
@@ -76,7 +77,7 @@ func resourceDynamicDNS() *schema.Resource {
|
||||
}
|
||||
|
||||
func resourceDynamicDNSCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceDynamicDNSGetResourceData(d)
|
||||
if err != nil {
|
||||
@@ -127,7 +128,7 @@ func resourceDynamicDNSSetResourceData(resp *unifi.DynamicDNS, d *schema.Resourc
|
||||
}
|
||||
|
||||
func resourceDynamicDNSRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -149,7 +150,7 @@ func resourceDynamicDNSRead(ctx context.Context, d *schema.ResourceData, meta in
|
||||
}
|
||||
|
||||
func resourceDynamicDNSUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceDynamicDNSGetResourceData(d)
|
||||
if err != nil {
|
||||
@@ -173,7 +174,7 @@ func resourceDynamicDNSUpdate(ctx context.Context, d *schema.ResourceData, meta
|
||||
}
|
||||
|
||||
func resourceDynamicDNSDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package v1
|
||||
package firewall
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
|
||||
)
|
||||
|
||||
func resourceFirewallGroup() *schema.Resource {
|
||||
func ResourceFirewallGroup() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_firewall_group` manages groups of addresses or ports for use in firewall rules (`unifi_firewall_rule`).",
|
||||
|
||||
@@ -21,7 +21,7 @@ func resourceFirewallGroup() *schema.Resource {
|
||||
UpdateContext: resourceFirewallGroupUpdate,
|
||||
DeleteContext: resourceFirewallGroupDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
StateContext: importSiteAndID,
|
||||
StateContext: utils.ImportSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
@@ -59,7 +59,7 @@ func resourceFirewallGroup() *schema.Resource {
|
||||
}
|
||||
|
||||
func resourceFirewallGroupCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceFirewallGroupGetResourceData(d)
|
||||
if err != nil {
|
||||
@@ -73,7 +73,7 @@ func resourceFirewallGroupCreate(ctx context.Context, d *schema.ResourceData, me
|
||||
|
||||
resp, err := c.CreateFirewallGroup(ctx, site, req)
|
||||
if err != nil {
|
||||
if provider.IsServerErrorContains(err, "api.err.FirewallGroupExisted") {
|
||||
if base.IsServerErrorContains(err, "api.err.FirewallGroupExisted") {
|
||||
return diag.Errorf("firewall groups must have unique names: %s", err)
|
||||
}
|
||||
return diag.FromErr(err)
|
||||
@@ -107,7 +107,7 @@ func resourceFirewallGroupSetResourceData(resp *unifi.FirewallGroup, d *schema.R
|
||||
}
|
||||
|
||||
func resourceFirewallGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -129,7 +129,7 @@ func resourceFirewallGroupRead(ctx context.Context, d *schema.ResourceData, meta
|
||||
}
|
||||
|
||||
func resourceFirewallGroupUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceFirewallGroupGetResourceData(d)
|
||||
if err != nil {
|
||||
@@ -153,7 +153,7 @@ func resourceFirewallGroupUpdate(ctx context.Context, d *schema.ResourceData, me
|
||||
}
|
||||
|
||||
func resourceFirewallGroupDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package v1
|
||||
package firewall
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
"regexp"
|
||||
|
||||
@@ -17,7 +17,7 @@ var firewallRuleProtocolRegexp = regexp.MustCompile("^$|all|([0-9]|[1-9][0-9]|1[
|
||||
var firewallRuleProtocolV6Regexp = regexp.MustCompile("^$|([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])|ah|all|dccp|eigrp|esp|gre|icmpv6|ipcomp|ipv6|ipv6-frag|ipv6-icmp|ipv6-nonxt|ipv6-opts|ipv6-route|isis|l2tp|manet|mobility-header|mpls-in-ip|ospf|pim|rsvp|sctp|shim6|tcp|tcp_udp|udp|vrrp")
|
||||
var firewallRuleICMPv6TypenameRegexp = regexp.MustCompile("^$|address-unreachable|bad-header|beyond-scope|communication-prohibited|destination-unreachable|echo-reply|echo-request|failed-policy|neighbor-advertisement|neighbor-solicitation|no-route|packet-too-big|parameter-problem|port-unreachable|redirect|reject-route|router-advertisement|router-solicitation|time-exceeded|ttl-zero-during-reassembly|ttl-zero-during-transit|unknown-header-type|unknown-option")
|
||||
|
||||
func resourceFirewallRule() *schema.Resource {
|
||||
func ResourceFirewallRule() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_firewall_rule` manages an individual firewall rule on the gateway.",
|
||||
|
||||
@@ -26,7 +26,7 @@ func resourceFirewallRule() *schema.Resource {
|
||||
UpdateContext: resourceFirewallRuleUpdate,
|
||||
DeleteContext: resourceFirewallRuleDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
StateContext: importSiteAndID,
|
||||
StateContext: utils.ImportSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
@@ -131,7 +131,7 @@ func resourceFirewallRule() *schema.Resource {
|
||||
Description: "The source port of the firewall rule.",
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
ValidateFunc: validatePortRange,
|
||||
ValidateFunc: utils.ValidatePortRange,
|
||||
},
|
||||
"src_mac": {
|
||||
Description: "The source MAC address of the firewall rule.",
|
||||
@@ -172,7 +172,7 @@ func resourceFirewallRule() *schema.Resource {
|
||||
Description: "The destination port of the firewall rule.",
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
ValidateFunc: validatePortRange,
|
||||
ValidateFunc: utils.ValidatePortRange,
|
||||
},
|
||||
|
||||
// advanced
|
||||
@@ -212,7 +212,7 @@ func resourceFirewallRule() *schema.Resource {
|
||||
}
|
||||
|
||||
func resourceFirewallRuleCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceFirewallRuleGetResourceData(d)
|
||||
if err != nil {
|
||||
@@ -226,7 +226,7 @@ func resourceFirewallRuleCreate(ctx context.Context, d *schema.ResourceData, met
|
||||
|
||||
resp, err := c.CreateFirewallRule(ctx, site, req)
|
||||
if err != nil {
|
||||
if provider.IsServerErrorContains(err, "api.err.FirewallGroupTypeExists") {
|
||||
if base.IsServerErrorContains(err, "api.err.FirewallGroupTypeExists") {
|
||||
return diag.Errorf("firewall rule groups must be of different group types (ie. a port group and address group): %s", err)
|
||||
}
|
||||
|
||||
@@ -320,7 +320,7 @@ func resourceFirewallRuleSetResourceData(resp *unifi.FirewallRule, d *schema.Res
|
||||
}
|
||||
|
||||
func resourceFirewallRuleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -342,7 +342,7 @@ func resourceFirewallRuleRead(ctx context.Context, d *schema.ResourceData, meta
|
||||
}
|
||||
|
||||
func resourceFirewallRuleUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceFirewallRuleGetResourceData(d)
|
||||
if err != nil {
|
||||
@@ -366,7 +366,7 @@ func resourceFirewallRuleUpdate(ctx context.Context, d *schema.ResourceData, met
|
||||
}
|
||||
|
||||
func resourceFirewallRuleDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package v1
|
||||
package network
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
)
|
||||
|
||||
func dataNetwork() *schema.Resource {
|
||||
func DataNetwork() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_network` data source can be used to retrieve settings for a network by name or ID.",
|
||||
|
||||
@@ -129,7 +129,7 @@ func dataNetwork() *schema.Resource {
|
||||
Computed: true,
|
||||
},
|
||||
"dhcp_v6_start": {
|
||||
Description: "Start address of the DHCPv6 range. Used in static DHCPv6 configuration.",
|
||||
Description: "start address of the DHCPv6 range. Used in static DHCPv6 configuration.",
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
@@ -169,7 +169,7 @@ func dataNetwork() *schema.Resource {
|
||||
Computed: true,
|
||||
},
|
||||
"ipv6_pd_start": {
|
||||
Description: "Start address of the DHCPv6 range. Used if `ipv6_interface_type` is set to `pd`.",
|
||||
Description: "start address of the DHCPv6 range. Used if `ipv6_interface_type` is set to `pd`.",
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
@@ -281,7 +281,7 @@ func dataNetwork() *schema.Resource {
|
||||
}
|
||||
|
||||
func dataNetworkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
name := d.Get("name").(string)
|
||||
site := d.Get("site").(string)
|
||||
@@ -1,14 +1,13 @@
|
||||
package v1
|
||||
package network
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
)
|
||||
|
||||
func dataPortProfile() *schema.Resource {
|
||||
func DataPortProfile() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_port_profile` data source can be used to retrieve the ID for a port profile by name.",
|
||||
|
||||
@@ -37,7 +36,7 @@ func dataPortProfile() *schema.Resource {
|
||||
}
|
||||
|
||||
func dataPortProfileRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
name := d.Get("name").(string)
|
||||
site := d.Get("site").(string)
|
||||
@@ -1,10 +1,10 @@
|
||||
package v1
|
||||
package network
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -44,7 +44,7 @@ var (
|
||||
validateIpV6RAPriority = validation.StringMatch(ipV6RAPriorityRegexp, "invalid IPv6 RA priority")
|
||||
)
|
||||
|
||||
func resourceNetwork() *schema.Resource {
|
||||
func ResourceNetwork() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_network` manages WAN/LAN/VLAN networks.",
|
||||
|
||||
@@ -91,7 +91,7 @@ func resourceNetwork() *schema.Resource {
|
||||
Description: "The subnet of the network. Must be a valid CIDR address.",
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
DiffSuppressFunc: cidrDiffSuppress,
|
||||
DiffSuppressFunc: utils.CidrDiffSuppress,
|
||||
ValidateFunc: utils.CidrValidate,
|
||||
},
|
||||
"network_group": {
|
||||
@@ -189,7 +189,7 @@ func resourceNetwork() *schema.Resource {
|
||||
Default: 86400,
|
||||
},
|
||||
"dhcp_v6_start": {
|
||||
Description: "Start address of the DHCPv6 range. Used in static DHCPv6 configuration.",
|
||||
Description: "start address of the DHCPv6 range. Used in static DHCPv6 configuration.",
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
ValidateFunc: validation.IsIPv6Address,
|
||||
@@ -240,7 +240,7 @@ func resourceNetwork() *schema.Resource {
|
||||
Optional: true,
|
||||
},
|
||||
"ipv6_pd_start": {
|
||||
Description: "Start address of the DHCPv6 range. Used if `ipv6_interface_type` is set to `pd`.",
|
||||
Description: "start address of the DHCPv6 range. Used if `ipv6_interface_type` is set to `pd`.",
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
ValidateFunc: validation.IsIPv6Address,
|
||||
@@ -384,7 +384,7 @@ func resourceNetwork() *schema.Resource {
|
||||
}
|
||||
|
||||
func resourceNetworkCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceNetworkGetResourceData(d, meta)
|
||||
if err != nil {
|
||||
@@ -626,7 +626,7 @@ func resourceNetworkSetResourceData(resp *unifi.Network, d *schema.ResourceData,
|
||||
}
|
||||
|
||||
func resourceNetworkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -648,7 +648,7 @@ func resourceNetworkRead(ctx context.Context, d *schema.ResourceData, meta inter
|
||||
}
|
||||
|
||||
func resourceNetworkUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceNetworkGetResourceData(d, meta)
|
||||
if err != nil {
|
||||
@@ -671,7 +671,7 @@ func resourceNetworkUpdate(ctx context.Context, d *schema.ResourceData, meta int
|
||||
}
|
||||
|
||||
func resourceNetworkDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
site := d.Get("site").(string)
|
||||
if site == "" {
|
||||
@@ -687,7 +687,7 @@ func resourceNetworkDelete(ctx context.Context, d *schema.ResourceData, meta int
|
||||
}
|
||||
|
||||
func importNetwork(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
id := d.Id()
|
||||
site := d.Get("site").(string)
|
||||
if site == "" {
|
||||
@@ -1,9 +1,9 @@
|
||||
package v1
|
||||
package network
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
|
||||
)
|
||||
|
||||
func resourcePortProfile() *schema.Resource {
|
||||
func ResourcePortProfile() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_port_profile` manages a port profile for use on network switches.",
|
||||
|
||||
@@ -21,7 +21,7 @@ func resourcePortProfile() *schema.Resource {
|
||||
UpdateContext: resourcePortProfileUpdate,
|
||||
DeleteContext: resourcePortProfileDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
StateContext: importSiteAndID,
|
||||
StateContext: utils.ImportSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
@@ -258,7 +258,7 @@ func resourcePortProfile() *schema.Resource {
|
||||
}
|
||||
|
||||
func resourcePortProfileCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourcePortProfileGetResourceData(d)
|
||||
if err != nil {
|
||||
@@ -371,7 +371,7 @@ func resourcePortProfileSetResourceData(resp *unifi.PortProfile, d *schema.Resou
|
||||
}
|
||||
|
||||
func resourcePortProfileRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -392,7 +392,7 @@ func resourcePortProfileRead(ctx context.Context, d *schema.ResourceData, meta i
|
||||
}
|
||||
|
||||
func resourcePortProfileUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourcePortProfileGetResourceData(d)
|
||||
if err != nil {
|
||||
@@ -416,7 +416,7 @@ func resourcePortProfileUpdate(ctx context.Context, d *schema.ResourceData, meta
|
||||
}
|
||||
|
||||
func resourcePortProfileDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package v1
|
||||
package network
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
@@ -18,7 +18,7 @@ var (
|
||||
wlanValidMinimumDataRate5g = []int{6000, 9000, 12000, 18000, 24000, 36000, 48000, 54000}
|
||||
)
|
||||
|
||||
func resourceWLAN() *schema.Resource {
|
||||
func ResourceWLAN() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_wlan` manages a WiFi network / SSID.",
|
||||
|
||||
@@ -27,7 +27,7 @@ func resourceWLAN() *schema.Resource {
|
||||
UpdateContext: resourceWLANUpdate,
|
||||
DeleteContext: resourceWLANDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
StateContext: importSiteAndID,
|
||||
StateContext: utils.ImportSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
@@ -110,8 +110,8 @@ func resourceWLAN() *schema.Resource {
|
||||
Optional: true,
|
||||
Elem: &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
ValidateFunc: validation.StringMatch(macAddressRegexp, "Mac address is invalid"),
|
||||
DiffSuppressFunc: macDiffSuppressFunc,
|
||||
ValidateFunc: validation.StringMatch(utils.MacAddressRegexp, "Mac address is invalid"),
|
||||
DiffSuppressFunc: utils.MacDiffSuppressFunc,
|
||||
},
|
||||
},
|
||||
"mac_filter_policy": {
|
||||
@@ -128,7 +128,7 @@ func resourceWLAN() *schema.Resource {
|
||||
Optional: true,
|
||||
},
|
||||
"schedule": {
|
||||
Description: "Start and stop schedules for the WLAN",
|
||||
Description: "start and stop schedules for the WLAN",
|
||||
Type: schema.TypeList,
|
||||
Optional: true,
|
||||
Elem: &schema.Resource{
|
||||
@@ -140,13 +140,13 @@ func resourceWLAN() *schema.Resource {
|
||||
ValidateFunc: validation.StringInSlice([]string{"sun", "mon", "tue", "wed", "thu", "fri", "sat", "sun"}, false),
|
||||
},
|
||||
"start_hour": {
|
||||
Description: "Start hour for the block (0-23).",
|
||||
Description: "start hour for the block (0-23).",
|
||||
Type: schema.TypeInt,
|
||||
Required: true,
|
||||
ValidateFunc: validation.IntBetween(0, 23),
|
||||
},
|
||||
"start_minute": {
|
||||
Description: "Start minute for the block (0-59).",
|
||||
Description: "start minute for the block (0-59).",
|
||||
Type: schema.TypeInt,
|
||||
Optional: true,
|
||||
Default: 0,
|
||||
@@ -245,7 +245,7 @@ func resourceWLAN() *schema.Resource {
|
||||
}
|
||||
|
||||
func resourceWLANGetResourceData(d *schema.ResourceData, meta interface{}) (*unifi.WLAN, error) {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
security := d.Get("security").(string)
|
||||
passphrase := d.Get("passphrase").(string)
|
||||
@@ -267,7 +267,7 @@ func resourceWLANGetResourceData(d *schema.ResourceData, meta interface{}) (*uni
|
||||
}
|
||||
if !c.SupportsWPA3() {
|
||||
if wpa3 || wpa3Transition {
|
||||
return nil, fmt.Errorf("WPA 3 support is not available on controller version %q, you must be on %q or higher", c.Version, provider.ControllerVersionWPA3)
|
||||
return nil, fmt.Errorf("WPA 3 support is not available on controller version %q, you must be on %q or higher", c.Version, base.ControllerVersionWPA3)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -355,7 +355,7 @@ func resourceWLANGetResourceData(d *schema.ResourceData, meta interface{}) (*uni
|
||||
}
|
||||
|
||||
func resourceWLANCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceWLANGetResourceData(d, meta)
|
||||
if err != nil {
|
||||
@@ -443,7 +443,7 @@ func resourceWLANSetResourceData(resp *unifi.WLAN, d *schema.ResourceData, meta
|
||||
}
|
||||
|
||||
func resourceWLANRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
site := d.Get("site").(string)
|
||||
@@ -464,7 +464,7 @@ func resourceWLANRead(ctx context.Context, d *schema.ResourceData, meta interfac
|
||||
}
|
||||
|
||||
func resourceWLANUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceWLANGetResourceData(d, meta)
|
||||
if err != nil {
|
||||
@@ -487,7 +487,7 @@ func resourceWLANUpdate(ctx context.Context, d *schema.ResourceData, meta interf
|
||||
}
|
||||
|
||||
func resourceWLANDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
site := d.Get("site").(string)
|
||||
@@ -1,5 +1,27 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/apgroup"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/device"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/dns"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/firewall"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/network"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/radius"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/routing"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/settings"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/site"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/user"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
ProviderUsernameDescription = "Local user name for the Unifi controller API. Can be specified with the `UNIFI_USERNAME` environment variable."
|
||||
ProviderPasswordDescription = "Password for the user accessing the API. Can be specified with the `UNIFI_PASSWORD` environment variable."
|
||||
@@ -11,3 +33,135 @@ const (
|
||||
"if you are using your local API without setting up a signed certificate. Can be specified with the " +
|
||||
"`UNIFI_INSECURE` environment variable."
|
||||
)
|
||||
|
||||
func init() {
|
||||
schema.DescriptionKind = schema.StringMarkdown
|
||||
|
||||
schema.SchemaDescriptionBuilder = func(s *schema.Schema) string {
|
||||
desc := s.Description
|
||||
if s.Default != nil {
|
||||
desc += fmt.Sprintf(" Defaults to `%v`.", s.Default)
|
||||
}
|
||||
if s.Deprecated != "" {
|
||||
desc += " " + s.Deprecated
|
||||
}
|
||||
return strings.TrimSpace(desc)
|
||||
}
|
||||
}
|
||||
|
||||
func New(version string) func() *schema.Provider {
|
||||
return func() *schema.Provider {
|
||||
p := &schema.Provider{
|
||||
Schema: map[string]*schema.Schema{
|
||||
"username": {
|
||||
Description: ProviderUsernameDescription,
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("UNIFI_USERNAME", ""),
|
||||
},
|
||||
"password": {
|
||||
Description: ProviderPasswordDescription,
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Sensitive: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("UNIFI_PASSWORD", ""),
|
||||
},
|
||||
"api_key": {
|
||||
Description: ProviderAPIKeyDescription,
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Sensitive: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("UNIFI_API_KEY", ""),
|
||||
},
|
||||
"api_url": {
|
||||
Description: ProviderAPIURLDescription,
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("UNIFI_API", ""),
|
||||
},
|
||||
"site": {
|
||||
Description: ProviderSiteDescription,
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("UNIFI_SITE", "default"),
|
||||
},
|
||||
"allow_insecure": {
|
||||
Description: ProviderAllowInsecureDescription,
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("UNIFI_INSECURE", false),
|
||||
},
|
||||
},
|
||||
DataSourcesMap: map[string]*schema.Resource{
|
||||
"unifi_ap_group": apgroup.DataAPGroup(),
|
||||
"unifi_network": network.DataNetwork(),
|
||||
"unifi_port_profile": network.DataPortProfile(),
|
||||
"unifi_radius_profile": radius.DataRADIUSProfile(),
|
||||
"unifi_user_group": user.DataUserGroup(),
|
||||
"unifi_user": user.DataUser(),
|
||||
"unifi_account": radius.DataAccount(),
|
||||
},
|
||||
ResourcesMap: map[string]*schema.Resource{
|
||||
// TODO: "unifi_ap_group"
|
||||
"unifi_device": device.ResourceDevice(),
|
||||
"unifi_dynamic_dns": dns.ResourceDynamicDNS(),
|
||||
"unifi_firewall_group": firewall.ResourceFirewallGroup(),
|
||||
"unifi_firewall_rule": firewall.ResourceFirewallRule(),
|
||||
"unifi_network": network.ResourceNetwork(),
|
||||
"unifi_port_forward": routing.ResourcePortForward(),
|
||||
"unifi_static_route": routing.ResourceStaticRoute(),
|
||||
"unifi_wlan": network.ResourceWLAN(),
|
||||
"unifi_port_profile": network.ResourcePortProfile(),
|
||||
"unifi_site": site.ResourceSite(),
|
||||
"unifi_account": radius.ResourceAccount(),
|
||||
"unifi_radius_profile": radius.ResourceRadiusProfile(),
|
||||
|
||||
"unifi_setting_mgmt": settings.ResourceSettingMgmt(),
|
||||
"unifi_setting_radius": settings.ResourceSettingRadius(),
|
||||
"unifi_setting_usg": settings.ResourceSettingUsg(),
|
||||
"unifi_user_group": user.ResourceUserGroup(),
|
||||
"unifi_user": user.ResourceUser(),
|
||||
},
|
||||
}
|
||||
|
||||
p.ConfigureContextFunc = configure(version, p)
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
func createHTTPTransport(insecure bool, subsystem string) http.RoundTripper {
|
||||
transport := base.CreateHttpTransport(insecure)
|
||||
t := logging.NewSubsystemLoggingHTTPTransport(subsystem, transport)
|
||||
return t
|
||||
}
|
||||
|
||||
func configure(v string, p *schema.Provider) schema.ConfigureContextFunc {
|
||||
return func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
|
||||
user := d.Get("username").(string)
|
||||
pass := d.Get("password").(string)
|
||||
apiKey := d.Get("api_key").(string)
|
||||
if apiKey != "" && (user != "" || pass != "") {
|
||||
return nil, diag.FromErr(errors.New("only one of `username`/`password` or `api_key` can be set"))
|
||||
} else if apiKey == "" && (user == "" || pass == "") {
|
||||
return nil, diag.FromErr(errors.New("either `username` and `password` or `api_key` must be set"))
|
||||
}
|
||||
baseURL := d.Get("api_url").(string)
|
||||
site := d.Get("site").(string)
|
||||
insecure := d.Get("allow_insecure").(bool)
|
||||
|
||||
c, err := base.NewClient(&base.ClientConfig{
|
||||
Username: user,
|
||||
Password: pass,
|
||||
ApiKey: apiKey,
|
||||
Url: baseURL,
|
||||
Site: site,
|
||||
HttpConfigurer: func() http.RoundTripper {
|
||||
return createHTTPTransport(insecure, "unifi")
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, diag.FromErr(err)
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package v2
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
up "github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/dns"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
@@ -15,7 +16,7 @@ import (
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
)
|
||||
|
||||
func New(version string) func() provider.Provider {
|
||||
func NewV2(version string) func() provider.Provider {
|
||||
return func() provider.Provider {
|
||||
return &unifiProvider{
|
||||
version: version,
|
||||
@@ -45,32 +46,32 @@ func (p *unifiProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp
|
||||
resp.Schema = schema.Schema{
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"username": schema.StringAttribute{
|
||||
MarkdownDescription: up.ProviderUsernameDescription,
|
||||
MarkdownDescription: ProviderUsernameDescription,
|
||||
Optional: true,
|
||||
},
|
||||
"password": schema.StringAttribute{
|
||||
MarkdownDescription: up.ProviderPasswordDescription,
|
||||
MarkdownDescription: ProviderPasswordDescription,
|
||||
Optional: true,
|
||||
Sensitive: true,
|
||||
},
|
||||
"api_key": schema.StringAttribute{
|
||||
MarkdownDescription: up.ProviderAPIKeyDescription,
|
||||
MarkdownDescription: ProviderAPIKeyDescription,
|
||||
Optional: true,
|
||||
Sensitive: true,
|
||||
},
|
||||
"api_url": schema.StringAttribute{
|
||||
MarkdownDescription: up.ProviderAPIURLDescription,
|
||||
MarkdownDescription: ProviderAPIURLDescription,
|
||||
Validators: []validator.String{
|
||||
stringvalidator.LengthAtLeast(1), // workaround for `required: true`, because it fails on doc generation due to incorrectly detected difference between v1 and v2
|
||||
},
|
||||
Optional: true,
|
||||
},
|
||||
"site": schema.StringAttribute{
|
||||
MarkdownDescription: up.ProviderSiteDescription,
|
||||
MarkdownDescription: ProviderSiteDescription,
|
||||
Optional: true,
|
||||
},
|
||||
"allow_insecure": schema.BoolAttribute{
|
||||
MarkdownDescription: up.ProviderAllowInsecureDescription,
|
||||
MarkdownDescription: ProviderAllowInsecureDescription,
|
||||
Optional: true,
|
||||
},
|
||||
},
|
||||
@@ -97,7 +98,7 @@ func (p *unifiProvider) Configure(ctx context.Context, req provider.ConfigureReq
|
||||
"Unknown UniFi Controller API URL",
|
||||
"The provider cannot create the UniFi Controller API client as there is an unknown configuration value "+
|
||||
"for the API endpoint. Either target apply the source of the value first, set the value statically in "+
|
||||
"the configuration, or use the UNIFI_API_URL environment variable.",
|
||||
"the configuration, or use the UNIFI_API environment variable.",
|
||||
)
|
||||
}
|
||||
|
||||
@@ -111,7 +112,7 @@ func (p *unifiProvider) Configure(ctx context.Context, req provider.ConfigureReq
|
||||
username := utils.GetAnyStringEnv("UNIFI_USERNAME")
|
||||
password := utils.GetAnyStringEnv("UNIFI_PASSWORD")
|
||||
apiKey := utils.GetAnyStringEnv("UNIFI_API_KEY")
|
||||
apiUrl := utils.GetAnyStringEnv("UNIFI_API_URL")
|
||||
apiUrl := utils.GetAnyStringEnv("UNIFI_API")
|
||||
site := utils.GetAnyStringEnv("UNIFI_SITE")
|
||||
insecure := utils.GetAnyBoolEnv("UNIFI_INSECURE")
|
||||
|
||||
@@ -147,7 +148,7 @@ func (p *unifiProvider) Configure(ctx context.Context, req provider.ConfigureReq
|
||||
if site == "" {
|
||||
site = "default" // set default site if not provided
|
||||
}
|
||||
c, err := up.NewClient(&up.ClientConfig{
|
||||
c, err := base.NewClient(&base.ClientConfig{
|
||||
Username: username,
|
||||
Password: password,
|
||||
ApiKey: apiKey,
|
||||
@@ -164,9 +165,14 @@ func (p *unifiProvider) Configure(ctx context.Context, req provider.ConfigureReq
|
||||
}
|
||||
|
||||
func (p *unifiProvider) Resources(_ context.Context) []func() resource.Resource {
|
||||
return []func() resource.Resource{}
|
||||
return []func() resource.Resource{
|
||||
dns.NewDnsRecordResource,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *unifiProvider) DataSources(_ context.Context) []func() datasource.DataSource {
|
||||
return []func() datasource.DataSource{}
|
||||
return []func() datasource.DataSource{
|
||||
dns.NewDnsRecordsDatasource,
|
||||
dns.NewDnsRecordDatasource,
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,13 @@
|
||||
package v1
|
||||
package radius
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
)
|
||||
|
||||
func dataAccount() *schema.Resource {
|
||||
func DataAccount() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "unifi_account data source can be used to retrieve RADIUS user accounts",
|
||||
|
||||
@@ -58,7 +57,7 @@ func dataAccount() *schema.Resource {
|
||||
}
|
||||
|
||||
func dataAccountRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
name := d.Get("name").(string)
|
||||
site := d.Get("site").(string)
|
||||
@@ -1,14 +1,13 @@
|
||||
package v1
|
||||
package radius
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
)
|
||||
|
||||
func dataRADIUSProfile() *schema.Resource {
|
||||
func DataRADIUSProfile() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_radius_profile` data source can be used to retrieve the ID for a RADIUS profile by name.",
|
||||
|
||||
@@ -37,7 +36,7 @@ func dataRADIUSProfile() *schema.Resource {
|
||||
}
|
||||
|
||||
func dataRADIUSProfileRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
name := d.Get("name").(string)
|
||||
site := d.Get("site").(string)
|
||||
@@ -1,9 +1,10 @@
|
||||
package v1
|
||||
package radius
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
@@ -11,7 +12,7 @@ import (
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
|
||||
)
|
||||
|
||||
func resourceAccount() *schema.Resource {
|
||||
func ResourceAccount() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_account` manages a RADIUS user account\n\n" +
|
||||
"To authenticate devices based on MAC address, use the MAC address as the username and password under client creation. \n" +
|
||||
@@ -24,7 +25,7 @@ func resourceAccount() *schema.Resource {
|
||||
UpdateContext: resourceAccountUpdate,
|
||||
DeleteContext: resourceAccountDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
StateContext: importSiteAndID,
|
||||
StateContext: utils.ImportSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
@@ -75,7 +76,7 @@ func resourceAccount() *schema.Resource {
|
||||
}
|
||||
|
||||
func resourceAccountCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceAccountGetResourceData(d)
|
||||
if err != nil {
|
||||
@@ -98,7 +99,7 @@ func resourceAccountCreate(ctx context.Context, d *schema.ResourceData, meta int
|
||||
}
|
||||
|
||||
func resourceAccountUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
site := d.Get("site").(string)
|
||||
if site == "" {
|
||||
@@ -122,7 +123,7 @@ func resourceAccountUpdate(ctx context.Context, d *schema.ResourceData, meta int
|
||||
}
|
||||
|
||||
func resourceAccountDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
//name := d.Get("name").(string)
|
||||
site := d.Get("site").(string)
|
||||
@@ -139,7 +140,7 @@ func resourceAccountDelete(ctx context.Context, d *schema.ResourceData, meta int
|
||||
}
|
||||
|
||||
func resourceAccountRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package v1
|
||||
package radius
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"strings"
|
||||
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
|
||||
)
|
||||
|
||||
func resourceRadiusProfile() *schema.Resource {
|
||||
func ResourceRadiusProfile() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_radius_profile` manages RADIUS profiles.",
|
||||
|
||||
@@ -235,7 +235,7 @@ func fromAcctServer(sshKey unifi.RADIUSProfileAcctServers) (map[string]interface
|
||||
}
|
||||
|
||||
func resourceRadiusProfileCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
req, err := resourceRadiusProfileGetResourceData(d)
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
@@ -303,7 +303,7 @@ func resourceRadiusProfileSetResourceData(resp *unifi.RADIUSProfile, d *schema.R
|
||||
}
|
||||
|
||||
func resourceRadiusProfileRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -324,7 +324,7 @@ func resourceRadiusProfileRead(ctx context.Context, d *schema.ResourceData, meta
|
||||
}
|
||||
|
||||
func resourceRadiusProfileUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceRadiusProfileGetResourceData(d)
|
||||
if err != nil {
|
||||
@@ -348,7 +348,7 @@ func resourceRadiusProfileUpdate(ctx context.Context, d *schema.ResourceData, me
|
||||
}
|
||||
|
||||
func resourceRadiusProfileDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -362,7 +362,7 @@ func resourceRadiusProfileDelete(ctx context.Context, d *schema.ResourceData, me
|
||||
}
|
||||
|
||||
func importRadiusProfile(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
id := d.Id()
|
||||
site := d.Get("site").(string)
|
||||
if site == "" {
|
||||
@@ -1,9 +1,9 @@
|
||||
package v1
|
||||
package routing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
|
||||
)
|
||||
|
||||
func resourcePortForward() *schema.Resource {
|
||||
func ResourcePortForward() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_port_forward` manages a port forwarding rule on the gateway.",
|
||||
|
||||
@@ -21,7 +21,7 @@ func resourcePortForward() *schema.Resource {
|
||||
UpdateContext: resourcePortForwardUpdate,
|
||||
DeleteContext: resourcePortForwardDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
StateContext: importSiteAndID,
|
||||
StateContext: utils.ImportSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
@@ -41,7 +41,7 @@ func resourcePortForward() *schema.Resource {
|
||||
Description: "The destination port for the forwarding.",
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
ValidateFunc: validatePortRange,
|
||||
ValidateFunc: utils.ValidatePortRange,
|
||||
},
|
||||
// TODO: remove this, disabled rules should just be deleted.
|
||||
"enabled": {
|
||||
@@ -62,7 +62,7 @@ func resourcePortForward() *schema.Resource {
|
||||
Description: "The port to forward traffic to.",
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
ValidateFunc: validatePortRange,
|
||||
ValidateFunc: utils.ValidatePortRange,
|
||||
},
|
||||
"log": {
|
||||
Description: "Specifies whether to log forwarded traffic or not.",
|
||||
@@ -104,7 +104,7 @@ func resourcePortForward() *schema.Resource {
|
||||
}
|
||||
|
||||
func resourcePortForwardCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourcePortForwardGetResourceData(d)
|
||||
if err != nil {
|
||||
@@ -155,7 +155,7 @@ func resourcePortForwardSetResourceData(resp *unifi.PortForward, d *schema.Resou
|
||||
}
|
||||
|
||||
func resourcePortForwardRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -176,7 +176,7 @@ func resourcePortForwardRead(ctx context.Context, d *schema.ResourceData, meta i
|
||||
}
|
||||
|
||||
func resourcePortForwardUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourcePortForwardGetResourceData(d)
|
||||
if err != nil {
|
||||
@@ -200,7 +200,7 @@ func resourcePortForwardUpdate(ctx context.Context, d *schema.ResourceData, meta
|
||||
}
|
||||
|
||||
func resourcePortForwardDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package v1
|
||||
package routing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
|
||||
)
|
||||
|
||||
func resourceStaticRoute() *schema.Resource {
|
||||
func ResourceStaticRoute() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_static_route` manages a static route.",
|
||||
|
||||
@@ -22,7 +22,7 @@ func resourceStaticRoute() *schema.Resource {
|
||||
UpdateContext: resourceStaticRouteUpdate,
|
||||
DeleteContext: resourceStaticRouteDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
StateContext: importSiteAndID,
|
||||
StateContext: utils.ImportSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
@@ -49,7 +49,7 @@ func resourceStaticRoute() *schema.Resource {
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ValidateFunc: utils.CidrValidate,
|
||||
DiffSuppressFunc: cidrDiffSuppress,
|
||||
DiffSuppressFunc: utils.CidrDiffSuppress,
|
||||
},
|
||||
"type": {
|
||||
Description: "The type of static route. Can be `interface-route`, `nexthop-route`, or `blackhole`.",
|
||||
@@ -79,7 +79,7 @@ func resourceStaticRoute() *schema.Resource {
|
||||
}
|
||||
|
||||
func resourceStaticRouteCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceStaticRouteGetResourceData(d)
|
||||
if err != nil {
|
||||
@@ -154,7 +154,7 @@ func resourceStaticRouteSetResourceData(resp *unifi.Routing, d *schema.ResourceD
|
||||
}
|
||||
|
||||
func resourceStaticRouteRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -176,7 +176,7 @@ func resourceStaticRouteRead(ctx context.Context, d *schema.ResourceData, meta i
|
||||
}
|
||||
|
||||
func resourceStaticRouteUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceStaticRouteGetResourceData(d)
|
||||
if err != nil {
|
||||
@@ -200,7 +200,7 @@ func resourceStaticRouteUpdate(ctx context.Context, d *schema.ResourceData, meta
|
||||
}
|
||||
|
||||
func resourceStaticRouteDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package v1
|
||||
package settings
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
@@ -14,7 +15,7 @@ import (
|
||||
// TODO: probably need to update this to be more like setting_usg,
|
||||
// using locking, and upsert, more computed, etc.
|
||||
|
||||
func resourceSettingMgmt() *schema.Resource {
|
||||
func ResourceSettingMgmt() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_setting_mgmt` manages settings for a unifi site.",
|
||||
|
||||
@@ -23,7 +24,7 @@ func resourceSettingMgmt() *schema.Resource {
|
||||
UpdateContext: resourceSettingMgmtUpdate,
|
||||
DeleteContext: resourceSettingMgmtDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
StateContext: importSiteAndID,
|
||||
StateContext: utils.ImportSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
@@ -142,7 +143,7 @@ func resourceSettingMgmtGetResourceData(d *schema.ResourceData, meta interface{}
|
||||
}
|
||||
|
||||
func resourceSettingMgmtCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceSettingMgmtGetResourceData(d, meta)
|
||||
if err != nil {
|
||||
@@ -178,7 +179,7 @@ func resourceSettingMgmtSetResourceData(resp *unifi.SettingMgmt, d *schema.Resou
|
||||
}
|
||||
|
||||
func resourceSettingMgmtRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
site := d.Get("site").(string)
|
||||
if site == "" {
|
||||
@@ -198,7 +199,7 @@ func resourceSettingMgmtRead(ctx context.Context, d *schema.ResourceData, meta i
|
||||
}
|
||||
|
||||
func resourceSettingMgmtUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceSettingMgmtGetResourceData(d, meta)
|
||||
if err != nil {
|
||||
@@ -1,9 +1,10 @@
|
||||
package v1
|
||||
package settings
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
@@ -11,7 +12,7 @@ import (
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
|
||||
)
|
||||
|
||||
func resourceSettingRadius() *schema.Resource {
|
||||
func ResourceSettingRadius() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_setting_radius` manages settings for the built-in RADIUS server.",
|
||||
|
||||
@@ -20,7 +21,7 @@ func resourceSettingRadius() *schema.Resource {
|
||||
UpdateContext: resourceSettingRadiusUpdate,
|
||||
DeleteContext: schema.NoopContext,
|
||||
Importer: &schema.ResourceImporter{
|
||||
StateContext: importSiteAndID,
|
||||
StateContext: utils.ImportSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
@@ -99,7 +100,7 @@ func resourceSettingRadiusGetResourceData(d *schema.ResourceData, meta interface
|
||||
}
|
||||
|
||||
func resourceSettingRadiusCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceSettingRadiusGetResourceData(d, meta)
|
||||
if err != nil {
|
||||
@@ -134,7 +135,7 @@ func resourceSettingRadiusSetResourceData(resp *unifi.SettingRadius, d *schema.R
|
||||
}
|
||||
|
||||
func resourceSettingRadiusRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
site := d.Get("site").(string)
|
||||
if site == "" {
|
||||
@@ -154,7 +155,7 @@ func resourceSettingRadiusRead(ctx context.Context, d *schema.ResourceData, meta
|
||||
}
|
||||
|
||||
func resourceSettingRadiusUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceSettingRadiusGetResourceData(d, meta)
|
||||
if err != nil {
|
||||
@@ -1,10 +1,10 @@
|
||||
package v1
|
||||
package settings
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
"sync"
|
||||
|
||||
@@ -24,7 +24,7 @@ func resourceSettingUsgLocker(f func(context.Context, *schema.ResourceData, inte
|
||||
}
|
||||
}
|
||||
|
||||
func resourceSettingUsg() *schema.Resource {
|
||||
func ResourceSettingUsg() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_setting_usg` manages settings for a Unifi Security Gateway.",
|
||||
|
||||
@@ -33,7 +33,7 @@ func resourceSettingUsg() *schema.Resource {
|
||||
UpdateContext: resourceSettingUsgLocker(resourceSettingUsgUpsert),
|
||||
DeleteContext: schema.NoopContext,
|
||||
Importer: &schema.ResourceImporter{
|
||||
StateContext: importSiteAndID,
|
||||
StateContext: utils.ImportSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
@@ -75,7 +75,7 @@ func resourceSettingUsg() *schema.Resource {
|
||||
}
|
||||
|
||||
func resourceSettingUsgUpdateResourceData(d *schema.ResourceData, meta interface{}, setting *unifi.SettingUsg) error {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
//nolint // GetOkExists is deprecated, but using here:
|
||||
if mdns, hasMdns := d.GetOkExists("multicast_dns_enabled"); hasMdns {
|
||||
@@ -100,7 +100,7 @@ func resourceSettingUsgUpdateResourceData(d *schema.ResourceData, meta interface
|
||||
}
|
||||
|
||||
func resourceSettingUsgUpsert(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
site := d.Get("site").(string)
|
||||
if site == "" {
|
||||
@@ -149,7 +149,7 @@ func resourceSettingUsgSetResourceData(resp *unifi.SettingUsg, d *schema.Resourc
|
||||
}
|
||||
|
||||
func resourceSettingUsgRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
site := d.Get("site").(string)
|
||||
if site == "" {
|
||||
@@ -1,17 +1,16 @@
|
||||
package v1
|
||||
package site
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
)
|
||||
|
||||
func resourceSite() *schema.Resource {
|
||||
func ResourceSite() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_site` manages Unifi sites",
|
||||
|
||||
@@ -44,7 +43,7 @@ func resourceSite() *schema.Resource {
|
||||
}
|
||||
|
||||
func resourceSiteImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
_, err := c.GetSite(ctx, id)
|
||||
@@ -74,7 +73,7 @@ func resourceSiteImport(ctx context.Context, d *schema.ResourceData, meta interf
|
||||
}
|
||||
|
||||
func resourceSiteCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
description := d.Get("description").(string)
|
||||
|
||||
@@ -96,7 +95,7 @@ func resourceSiteSetResourceData(resp *unifi.Site, d *schema.ResourceData) diag.
|
||||
}
|
||||
|
||||
func resourceSiteRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -113,7 +112,7 @@ func resourceSiteRead(ctx context.Context, d *schema.ResourceData, meta interfac
|
||||
}
|
||||
|
||||
func resourceSiteUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
site := &unifi.Site{
|
||||
ID: d.Id(),
|
||||
@@ -130,7 +129,7 @@ func resourceSiteUpdate(ctx context.Context, d *schema.ResourceData, meta interf
|
||||
}
|
||||
|
||||
func resourceSiteDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
id := d.Id()
|
||||
_, err := c.DeleteSite(ctx, id)
|
||||
return diag.FromErr(err)
|
||||
277
internal/provider/testing/test_environment.go
Normal file
277
internal/provider/testing/test_environment.go
Normal file
@@ -0,0 +1,277 @@
|
||||
package testing
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
"github.com/testcontainers/testcontainers-go/modules/compose"
|
||||
)
|
||||
|
||||
const (
|
||||
TestEnvStarting testEnvironmentStatus = iota
|
||||
TestEnvReady
|
||||
TestEnvDown
|
||||
TestEnvUnknown
|
||||
)
|
||||
|
||||
type testEnvironmentStatus int
|
||||
|
||||
type TestEnvironment struct {
|
||||
Client unifi.Client
|
||||
Endpoint string
|
||||
Shutdown func()
|
||||
ctx context.Context
|
||||
internalClient *http.Client
|
||||
mutex sync.Mutex
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
type envStatus struct {
|
||||
Meta struct {
|
||||
Up bool `json:"up"`
|
||||
} `json:"meta"`
|
||||
}
|
||||
|
||||
func Run(m *testing.M, callback func(env *TestEnvironment)) int {
|
||||
if os.Getenv(resource.EnvTfAcc) == "" {
|
||||
// short circuit non-acceptance test runs
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
env := NewTestEnvironment(5 * time.Minute)
|
||||
return env.run(m, callback)
|
||||
}
|
||||
|
||||
func NewTestEnvironment(startupTimeout time.Duration) *TestEnvironment {
|
||||
c := http.Client{Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
}}
|
||||
ctx := context.Background()
|
||||
return &TestEnvironment{
|
||||
Endpoint: "https://localhost:8443", // default endpoint, assumed
|
||||
timeout: startupTimeout,
|
||||
mutex: sync.Mutex{},
|
||||
ctx: ctx,
|
||||
internalClient: &c,
|
||||
Shutdown: func() {},
|
||||
}
|
||||
}
|
||||
|
||||
func (te *TestEnvironment) isReady() bool {
|
||||
if st, _ := te.readStatus(te.ctx); st != TestEnvReady {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (te *TestEnvironment) run(m *testing.M, callback func(env *TestEnvironment)) int {
|
||||
err := te.Start()
|
||||
defer func() {
|
||||
te.Shutdown()
|
||||
}()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = te.WaitUntilReady()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
c, err := te.newTestClient()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
te.Client = c
|
||||
callback(te)
|
||||
return m.Run()
|
||||
}
|
||||
|
||||
func (te *TestEnvironment) readStatus(ctx context.Context) (testEnvironmentStatus, error) {
|
||||
req, err := http.NewRequest("GET", fmt.Sprintf("%s/status", te.Endpoint), nil)
|
||||
if err != nil {
|
||||
return TestEnvUnknown, err
|
||||
}
|
||||
req = req.WithContext(ctx)
|
||||
r, err := te.internalClient.Do(req)
|
||||
if err != nil {
|
||||
return TestEnvDown, err
|
||||
}
|
||||
resp := envStatus{}
|
||||
err = json.NewDecoder(r.Body).Decode(&resp)
|
||||
if err != nil {
|
||||
return TestEnvUnknown, err
|
||||
}
|
||||
if resp.Meta.Up {
|
||||
return TestEnvReady, nil
|
||||
}
|
||||
return TestEnvStarting, nil
|
||||
}
|
||||
|
||||
func findFileInProject(filename string) (string, error) {
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// Walk up the directory tree until we find a file
|
||||
for {
|
||||
path := filepath.Join(wd, filename)
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return path, nil
|
||||
}
|
||||
if wd == "/" {
|
||||
break
|
||||
}
|
||||
wd = filepath.Dir(wd)
|
||||
}
|
||||
return "", fmt.Errorf("file %s not found in project", filename)
|
||||
}
|
||||
|
||||
func (te *TestEnvironment) startDockerController(ctx context.Context) error {
|
||||
composeFile, err := findFileInProject("docker-compose.yaml")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to find docker-compose.yaml file: %w", err)
|
||||
}
|
||||
dc, err := compose.NewDockerCompose(composeFile)
|
||||
shutdown := func() {
|
||||
if dc != nil {
|
||||
if err := dc.Down(context.Background(), compose.RemoveOrphans(true), compose.RemoveImagesLocal); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
te.Shutdown = shutdown
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = dc.WithOsEnv().Up(ctx, compose.Wait(true)); err != nil {
|
||||
return fmt.Errorf("failed to Start docker-compose. Controller container might be already running or starting: %w", err)
|
||||
}
|
||||
container, err := dc.ServiceContainer(ctx, "unifi")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Dump the container logs on exit.
|
||||
//
|
||||
// TODO: Use https://pkg.go.dev/github.com/testcontainers/testcontainers-go#LogConsumer instead.
|
||||
te.Shutdown = func() {
|
||||
shutdown()
|
||||
|
||||
if os.Getenv("UNIFI_STDOUT") == "" {
|
||||
return
|
||||
}
|
||||
|
||||
stream, err := container.Logs(ctx)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to get logs from container: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
buffer := new(bytes.Buffer)
|
||||
buffer.ReadFrom(stream)
|
||||
testcontainers.Logger.Printf("%s", buffer)
|
||||
}
|
||||
endpoint, err := container.PortEndpoint(ctx, "8443/tcp", "https")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
te.Endpoint = endpoint
|
||||
return nil
|
||||
}
|
||||
|
||||
func (te *TestEnvironment) WaitUntilReady() error {
|
||||
te.mutex.Lock()
|
||||
ctx, cancel := context.WithTimeoutCause(te.ctx, te.timeout, fmt.Errorf("controller was not ready within %s", te.timeout))
|
||||
defer cancel()
|
||||
defer te.mutex.Unlock()
|
||||
if st, _ := te.readStatus(ctx); st == TestEnvDown || st == TestEnvUnknown {
|
||||
return fmt.Errorf("controller is not starting nor running. Use Start() first to Start the controller")
|
||||
}
|
||||
te.waitForController(ctx)
|
||||
if !te.isReady() {
|
||||
return fmt.Errorf("controller is not ready within %s", te.timeout)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (te *TestEnvironment) Start() error {
|
||||
tflog.Error(te.ctx, "Starting test environment")
|
||||
if te.isReady() {
|
||||
tflog.Warn(te.ctx, "Environment is already running at "+te.Endpoint)
|
||||
if te.Client == nil {
|
||||
c, err := te.newTestClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
te.Client = c
|
||||
}
|
||||
return nil
|
||||
}
|
||||
ctx, cancel := context.WithTimeoutCause(te.ctx, te.timeout, fmt.Errorf("controller did not Start within %s", te.timeout))
|
||||
defer cancel()
|
||||
err := te.startDockerController(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tflog.Info(te.ctx, "Environment is starting at "+te.Endpoint)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (te *TestEnvironment) waitForController(ctx context.Context) {
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
for {
|
||||
if st, err := te.readStatus(ctx); err != nil {
|
||||
return
|
||||
} else if st == TestEnvReady {
|
||||
wg.Done()
|
||||
return
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}()
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func (te *TestEnvironment) newTestClient() (unifi.Client, error) {
|
||||
const user = "admin"
|
||||
const password = "admin"
|
||||
var err error
|
||||
if err = os.Setenv("UNIFI_USERNAME", user); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = os.Setenv("UNIFI_PASSWORD", password); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = os.Setenv("UNIFI_INSECURE", "true"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = os.Setenv("UNIFI_API", te.Endpoint); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return unifi.NewClient(&unifi.ClientConfig{
|
||||
URL: te.Endpoint,
|
||||
User: user,
|
||||
Password: password,
|
||||
VerifySSL: false,
|
||||
ValidationMode: unifi.DisableValidation,
|
||||
Logger: unifi.NewDefaultLogger(unifi.WarnLevel),
|
||||
})
|
||||
}
|
||||
78
internal/provider/testing/test_helpers.go
Normal file
78
internal/provider/testing/test_helpers.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package testing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
"github.com/hashicorp/terraform-plugin-testing/plancheck"
|
||||
"github.com/hashicorp/terraform-plugin-testing/terraform"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// MarkAccTest marks the test as acceptance test. Useful when executing code before resource.ParallelTest or resource.Test
|
||||
// to bring acceptance test check earlier when test environment is required
|
||||
func MarkAccTest(t *testing.T) {
|
||||
t.Helper()
|
||||
if os.Getenv(resource.EnvTfAcc) == "" {
|
||||
t.Skipf("Acceptance tests skipped unless env '%s' set", resource.EnvTfAcc)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func ImportStep(name string, ignore ...string) resource.TestStep {
|
||||
step := resource.TestStep{
|
||||
ResourceName: name,
|
||||
ImportState: true,
|
||||
ImportStateVerify: true,
|
||||
}
|
||||
|
||||
if len(ignore) > 0 {
|
||||
step.ImportStateVerifyIgnore = ignore
|
||||
}
|
||||
|
||||
return step
|
||||
}
|
||||
|
||||
// SiteAndIDImportStateIDFunc returns a function that can be used to import resources that require site and id.
|
||||
func SiteAndIDImportStateIDFunc(resourceName string) func(*terraform.State) (string, error) {
|
||||
return func(s *terraform.State) (string, error) {
|
||||
rs, ok := s.RootModule().Resources[resourceName]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("not found: %s", resourceName)
|
||||
}
|
||||
id := rs.Primary.Attributes["id"]
|
||||
site := rs.Primary.Attributes["site"]
|
||||
return site + ":" + id, nil
|
||||
}
|
||||
}
|
||||
|
||||
// PreCheck checks if provided environment variables are set. If not, it will fail the test.
|
||||
func PreCheck(t *testing.T) {
|
||||
variables := []string{
|
||||
"UNIFI_USERNAME",
|
||||
"UNIFI_PASSWORD",
|
||||
"UNIFI_API",
|
||||
}
|
||||
|
||||
for _, variable := range variables {
|
||||
value := os.Getenv(variable)
|
||||
if value == "" {
|
||||
t.Fatalf("`%s` must be set for acceptance tests!", variable)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CheckPlanPreApply(checks ...plancheck.PlanCheck) resource.ConfigPlanChecks {
|
||||
return resource.ConfigPlanChecks{
|
||||
PreApply: checks,
|
||||
}
|
||||
}
|
||||
|
||||
func CheckResourceAction(resourceAddress string, action plancheck.ResourceActionType) resource.ConfigPlanChecks {
|
||||
return CheckPlanPreApply(plancheck.ExpectResourceAction(resourceAddress, action))
|
||||
}
|
||||
|
||||
func ComposeConfig(configs ...string) string {
|
||||
return strings.Join(configs, "\n")
|
||||
}
|
||||
@@ -1,34 +1,48 @@
|
||||
package v1
|
||||
package testing
|
||||
|
||||
import (
|
||||
"github.com/apparentlymart/go-cidr/cidr"
|
||||
mapset "github.com/deckarep/golang-set/v2"
|
||||
"math"
|
||||
"net"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
)
|
||||
|
||||
var macAddressRegexp = regexp.MustCompile("^([0-9a-fA-F][0-9a-fA-F][-:]){5}([0-9a-fA-F][0-9a-fA-F])$")
|
||||
|
||||
func cleanMAC(mac string) string {
|
||||
return strings.TrimSpace(strings.ReplaceAll(strings.ToLower(mac), "-", ":"))
|
||||
}
|
||||
|
||||
func macDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
|
||||
old = cleanMAC(old)
|
||||
new = cleanMAC(new)
|
||||
return old == new
|
||||
}
|
||||
const (
|
||||
vlanMin = 2
|
||||
vlanMax = 4095
|
||||
)
|
||||
|
||||
var (
|
||||
macInit sync.Once
|
||||
macPool = mapset.NewSet[*net.HardwareAddr]()
|
||||
network = &net.IPNet{
|
||||
IP: net.IPv4(10, 0, 0, 0).To4(),
|
||||
Mask: net.IPv4Mask(255, 0, 0, 0),
|
||||
}
|
||||
|
||||
vlanLock sync.Mutex
|
||||
vlanNext = vlanMin
|
||||
)
|
||||
|
||||
func allocateTestMac(t *testing.T) (string, func()) {
|
||||
func GetTestVLAN(t *testing.T) (*net.IPNet, int) {
|
||||
vlanLock.Lock()
|
||||
defer vlanLock.Unlock()
|
||||
|
||||
vlan := vlanNext
|
||||
vlanNext++
|
||||
|
||||
subnet, err := cidr.Subnet(network, int(math.Ceil(math.Log2(vlanMax))), vlan)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
return subnet, vlan
|
||||
}
|
||||
|
||||
func AllocateTestMac(t *testing.T) (string, func()) {
|
||||
MarkAccTest(t)
|
||||
macInit.Do(func() {
|
||||
// for test MAC addresses, see https://tools.ietf.org/html/rfc7042#section-2.1.
|
||||
for i := 0; i < 512; i++ {
|
||||
23
internal/provider/testing/test_helpers_random.go
Normal file
23
internal/provider/testing/test_helpers_random.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package testing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
|
||||
)
|
||||
|
||||
func RandHostname() string {
|
||||
return RandHostnameWithSuffix("test.com")
|
||||
}
|
||||
|
||||
func RandHostnameWithSuffix(suffix string) string {
|
||||
return fmt.Sprintf("%s.%s", RandAlpha(10), suffix)
|
||||
}
|
||||
|
||||
func RandAlpha(len int) string {
|
||||
return acctest.RandStringFromCharSet(len, acctest.CharSetAlpha)
|
||||
}
|
||||
|
||||
func RandIpAddress() string {
|
||||
ip, _ := acctest.RandIpAddress("192.168.0.1/24")
|
||||
return ip
|
||||
}
|
||||
10
internal/provider/testing/test_helpers_regex.go
Normal file
10
internal/provider/testing/test_helpers_regex.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package testing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func MissingArgumentErrorRegex(arg string) *regexp.Regexp {
|
||||
return regexp.MustCompile(fmt.Sprintf(`%q is required`, arg))
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
package v1
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
@@ -10,7 +11,7 @@ import (
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
|
||||
)
|
||||
|
||||
func dataUser() *schema.Resource {
|
||||
func DataUser() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_user` retrieves properties of a user (or \"client\" in the UI) of the network by MAC address.",
|
||||
|
||||
@@ -27,8 +28,8 @@ func dataUser() *schema.Resource {
|
||||
Description: "The MAC address of the user.",
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
DiffSuppressFunc: macDiffSuppressFunc,
|
||||
ValidateFunc: validation.StringMatch(macAddressRegexp, "Mac address is invalid"),
|
||||
DiffSuppressFunc: utils.MacDiffSuppressFunc,
|
||||
ValidateFunc: validation.StringMatch(utils.MacAddressRegexp, "Mac address is invalid"),
|
||||
},
|
||||
|
||||
// read-only / computed
|
||||
@@ -92,7 +93,7 @@ func dataUser() *schema.Resource {
|
||||
}
|
||||
|
||||
func dataUserRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
site := d.Get("site").(string)
|
||||
if site == "" {
|
||||
@@ -1,14 +1,13 @@
|
||||
package v1
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
)
|
||||
|
||||
func dataUserGroup() *schema.Resource {
|
||||
func DataUserGroup() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_user_group` data source can be used to retrieve the ID for a user group by name.",
|
||||
|
||||
@@ -46,7 +45,7 @@ func dataUserGroup() *schema.Resource {
|
||||
}
|
||||
|
||||
func dataUserGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
name := d.Get("name").(string)
|
||||
site := d.Get("site").(string)
|
||||
@@ -1,16 +1,17 @@
|
||||
package v1
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
|
||||
)
|
||||
|
||||
func resourceUser() *schema.Resource {
|
||||
func ResourceUser() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_user` manages a user (or \"client\" in the UI) of the network, these are identified " +
|
||||
"by unique MAC addresses.\n\n" +
|
||||
@@ -22,7 +23,7 @@ func resourceUser() *schema.Resource {
|
||||
UpdateContext: resourceUserUpdate,
|
||||
DeleteContext: resourceUserDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
StateContext: importSiteAndID,
|
||||
StateContext: utils.ImportSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
@@ -43,8 +44,8 @@ func resourceUser() *schema.Resource {
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
DiffSuppressFunc: macDiffSuppressFunc,
|
||||
ValidateFunc: validation.StringMatch(macAddressRegexp, "Mac address is invalid"),
|
||||
DiffSuppressFunc: utils.MacDiffSuppressFunc,
|
||||
ValidateFunc: validation.StringMatch(utils.MacAddressRegexp, "Mac address is invalid"),
|
||||
},
|
||||
"name": {
|
||||
Description: "The name of the user.",
|
||||
@@ -119,7 +120,7 @@ func resourceUser() *schema.Resource {
|
||||
}
|
||||
|
||||
func resourceUserCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceUserGetResourceData(d)
|
||||
if err != nil {
|
||||
@@ -135,7 +136,7 @@ func resourceUserCreate(ctx context.Context, d *schema.ResourceData, meta interf
|
||||
|
||||
resp, err := c.CreateUser(ctx, site, req)
|
||||
if err != nil {
|
||||
if !provider.IsServerErrorContains(err, "api.err.MacUsed") || !allowExisting {
|
||||
if !base.IsServerErrorContains(err, "api.err.MacUsed") || !allowExisting {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
|
||||
@@ -227,7 +228,7 @@ func resourceUserSetResourceData(resp *unifi.User, d *schema.ResourceData, site
|
||||
}
|
||||
|
||||
func resourceUserRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -263,7 +264,7 @@ func resourceUserRead(ctx context.Context, d *schema.ResourceData, meta interfac
|
||||
}
|
||||
|
||||
func resourceUserUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
site := d.Get("site").(string)
|
||||
if site == "" {
|
||||
@@ -316,7 +317,7 @@ func resourceUserUpdate(ctx context.Context, d *schema.ResourceData, meta interf
|
||||
}
|
||||
|
||||
func resourceUserDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
package v1
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider/base"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/utils"
|
||||
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
)
|
||||
|
||||
func resourceUserGroup() *schema.Resource {
|
||||
func ResourceUserGroup() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Description: "`unifi_user_group` manages a user group (called \"client group\" in the UI), which can be used " +
|
||||
"to limit bandwidth for groups of users.",
|
||||
@@ -20,7 +21,7 @@ func resourceUserGroup() *schema.Resource {
|
||||
UpdateContext: resourceUserGroupUpdate,
|
||||
DeleteContext: resourceUserGroupDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
StateContext: importSiteAndID,
|
||||
StateContext: utils.ImportSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
@@ -60,7 +61,7 @@ func resourceUserGroup() *schema.Resource {
|
||||
}
|
||||
|
||||
func resourceUserGroupCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceUserGroupGetResourceData(d)
|
||||
if err != nil {
|
||||
@@ -99,7 +100,7 @@ func resourceUserGroupSetResourceData(resp *unifi.UserGroup, d *schema.ResourceD
|
||||
}
|
||||
|
||||
func resourceUserGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -121,7 +122,7 @@ func resourceUserGroupRead(ctx context.Context, d *schema.ResourceData, meta int
|
||||
}
|
||||
|
||||
func resourceUserGroupUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
req, err := resourceUserGroupGetResourceData(d)
|
||||
if err != nil {
|
||||
@@ -145,7 +146,7 @@ func resourceUserGroupUpdate(ctx context.Context, d *schema.ResourceData, meta i
|
||||
}
|
||||
|
||||
func resourceUserGroupDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*provider.Client)
|
||||
c := meta.(*base.Client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccDataPortProfile_default(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
preCheckVersionConstraint(t, "< 7.4")
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccDataPortProfileConfig_default,
|
||||
Check: resource.ComposeTestCheckFunc(),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccDataPortProfile_multiple_providers(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
preCheckVersionConstraint(t, "< 7.4")
|
||||
},
|
||||
ProviderFactories: map[string]func() (*schema.Provider, error){
|
||||
"unifi2": func() (*schema.Provider, error) {
|
||||
return New("acctest")(), nil
|
||||
},
|
||||
"unifi3": func() (*schema.Provider, error) {
|
||||
return New("acctest")(), nil
|
||||
},
|
||||
},
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: `
|
||||
data "unifi_port_profile" "unifi2" {
|
||||
provider = "unifi2"
|
||||
}
|
||||
data "unifi_port_profile" "unifi3" {
|
||||
provider = "unifi3"
|
||||
}
|
||||
`,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const testAccDataPortProfileConfig_default = `
|
||||
data "unifi_port_profile" "default" {
|
||||
}
|
||||
`
|
||||
@@ -1,59 +0,0 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccDataUserGroup_default(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccDataUserGroupConfig_default,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccDataUserGroup_multiple_providers(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: map[string]func() (*schema.Provider, error){
|
||||
"unifi2": func() (*schema.Provider, error) {
|
||||
return New("acctest")(), nil
|
||||
},
|
||||
"unifi3": func() (*schema.Provider, error) {
|
||||
return New("acctest")(), nil
|
||||
},
|
||||
},
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: `
|
||||
data "unifi_user_group" "unifi2" {
|
||||
provider = "unifi2"
|
||||
}
|
||||
data "unifi_user_group" "unifi3" {
|
||||
provider = "unifi3"
|
||||
}
|
||||
`,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const testAccDataUserGroupConfig_default = `
|
||||
data "unifi_user_group" "default" {
|
||||
}
|
||||
`
|
||||
@@ -1,20 +0,0 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
"net"
|
||||
)
|
||||
|
||||
func cidrDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
|
||||
_, oldNet, err := net.ParseCIDR(old)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
_, newNet, err := net.ParseCIDR(new)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return oldNet.String() == newNet.String()
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
)
|
||||
|
||||
func importSiteAndID(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
||||
if id := d.Id(); strings.Contains(id, ":") {
|
||||
importParts := strings.SplitN(id, ":", 2)
|
||||
d.SetId(importParts[1])
|
||||
d.Set("site", importParts[0])
|
||||
}
|
||||
return []*schema.ResourceData{d}, nil
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
|
||||
)
|
||||
|
||||
var (
|
||||
portRangeRegexp = regexp.MustCompile("(([1-9][0-9]{0,3}|[1-5][0-9]{4}|[6][0-4][0-9]{3}|[6][5][0-4][0-9]{2}|[6][5][5][0-2][0-9]|[6][5][5][3][0-5])|([1-9][0-9]{0,3}|[1-5][0-9]{4}|[6][0-4][0-9]{3}|[6][5][0-4][0-9]{2}|[6][5][5][0-2][0-9]|[6][5][5][3][0-5])-([1-9][0-9]{0,3}|[1-5][0-9]{4}|[6][0-4][0-9]{3}|[6][5][0-4][0-9]{2}|[6][5][5][0-2][0-9]|[6][5][5][3][0-5]))+(,([1-9][0-9]{0,3}|[1-5][0-9]{4}|[6][0-4][0-9]{3}|[6][5][0-4][0-9]{2}|[6][5][5][0-2][0-9]|[6][5][5][3][0-5])|,([1-9][0-9]{0,3}|[1-5][0-9]{4}|[6][0-4][0-9]{3}|[6][5][0-4][0-9]{2}|[6][5][5][0-2][0-9]|[6][5][5][3][0-5])-([1-9][0-9]{0,3}|[1-5][0-9]{4}|[6][0-4][0-9]{3}|[6][5][0-4][0-9]{2}|[6][5][5][0-2][0-9]|[6][5][5][3][0-5])){0,14}")
|
||||
validatePortRange = validation.StringMatch(portRangeRegexp, "invalid port range")
|
||||
)
|
||||
@@ -1,165 +0,0 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/filipowm/terraform-provider-unifi/internal/provider"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
)
|
||||
|
||||
func init() {
|
||||
schema.DescriptionKind = schema.StringMarkdown
|
||||
|
||||
schema.SchemaDescriptionBuilder = func(s *schema.Schema) string {
|
||||
desc := s.Description
|
||||
if s.Default != nil {
|
||||
desc += fmt.Sprintf(" Defaults to `%v`.", s.Default)
|
||||
}
|
||||
if s.Deprecated != "" {
|
||||
desc += " " + s.Deprecated
|
||||
}
|
||||
return strings.TrimSpace(desc)
|
||||
}
|
||||
}
|
||||
|
||||
func New(version string) func() *schema.Provider {
|
||||
return func() *schema.Provider {
|
||||
p := &schema.Provider{
|
||||
Schema: map[string]*schema.Schema{
|
||||
"username": {
|
||||
Description: provider.ProviderUsernameDescription,
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("UNIFI_USERNAME", ""),
|
||||
},
|
||||
"password": {
|
||||
Description: provider.ProviderPasswordDescription,
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Sensitive: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("UNIFI_PASSWORD", ""),
|
||||
},
|
||||
"api_key": {
|
||||
Description: provider.ProviderAPIKeyDescription,
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Sensitive: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("UNIFI_API_KEY", ""),
|
||||
},
|
||||
"api_url": {
|
||||
Description: provider.ProviderAPIURLDescription,
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("UNIFI_API", ""),
|
||||
},
|
||||
"site": {
|
||||
Description: provider.ProviderSiteDescription,
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("UNIFI_SITE", "default"),
|
||||
},
|
||||
"allow_insecure": {
|
||||
Description: provider.ProviderAllowInsecureDescription,
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("UNIFI_INSECURE", false),
|
||||
},
|
||||
},
|
||||
DataSourcesMap: map[string]*schema.Resource{
|
||||
"unifi_ap_group": dataAPGroup(),
|
||||
"unifi_network": dataNetwork(),
|
||||
"unifi_port_profile": dataPortProfile(),
|
||||
"unifi_radius_profile": dataRADIUSProfile(),
|
||||
"unifi_user_group": dataUserGroup(),
|
||||
"unifi_user": dataUser(),
|
||||
"unifi_account": dataAccount(),
|
||||
},
|
||||
ResourcesMap: map[string]*schema.Resource{
|
||||
// TODO: "unifi_ap_group"
|
||||
"unifi_device": resourceDevice(),
|
||||
"unifi_dynamic_dns": resourceDynamicDNS(),
|
||||
"unifi_firewall_group": resourceFirewallGroup(),
|
||||
"unifi_firewall_rule": resourceFirewallRule(),
|
||||
"unifi_network": resourceNetwork(),
|
||||
"unifi_port_forward": resourcePortForward(),
|
||||
"unifi_port_profile": resourcePortProfile(),
|
||||
"unifi_radius_profile": resourceRadiusProfile(),
|
||||
"unifi_site": resourceSite(),
|
||||
"unifi_static_route": resourceStaticRoute(),
|
||||
"unifi_user_group": resourceUserGroup(),
|
||||
"unifi_user": resourceUser(),
|
||||
"unifi_wlan": resourceWLAN(),
|
||||
"unifi_account": resourceAccount(),
|
||||
|
||||
"unifi_setting_mgmt": resourceSettingMgmt(),
|
||||
"unifi_setting_radius": resourceSettingRadius(),
|
||||
"unifi_setting_usg": resourceSettingUsg(),
|
||||
},
|
||||
}
|
||||
|
||||
p.ConfigureContextFunc = configure(version, p)
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
func createHTTPTransport(insecure bool, subsystem string) http.RoundTripper {
|
||||
transport := &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
DualStack: true,
|
||||
}).DialContext,
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: insecure,
|
||||
},
|
||||
}
|
||||
|
||||
t := logging.NewSubsystemLoggingHTTPTransport(subsystem, transport)
|
||||
return t
|
||||
}
|
||||
|
||||
func configure(v string, p *schema.Provider) schema.ConfigureContextFunc {
|
||||
return func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
|
||||
user := d.Get("username").(string)
|
||||
pass := d.Get("password").(string)
|
||||
apiKey := d.Get("api_key").(string)
|
||||
if apiKey != "" && (user != "" || pass != "") {
|
||||
return nil, diag.FromErr(errors.New("only one of `username`/`password` or `api_key` can be set"))
|
||||
} else if apiKey == "" && (user == "" || pass == "") {
|
||||
return nil, diag.FromErr(errors.New("either `username` and `password` or `api_key` must be set"))
|
||||
}
|
||||
baseURL := d.Get("api_url").(string)
|
||||
site := d.Get("site").(string)
|
||||
insecure := d.Get("allow_insecure").(bool)
|
||||
|
||||
c, err := provider.NewClient(&provider.ClientConfig{
|
||||
Username: user,
|
||||
Password: pass,
|
||||
ApiKey: apiKey,
|
||||
Url: baseURL,
|
||||
Site: site,
|
||||
HttpConfigurer: func() http.RoundTripper {
|
||||
return createHTTPTransport(insecure, "unifi")
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, diag.FromErr(err)
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
}
|
||||
@@ -1,192 +0,0 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/apparentlymart/go-cidr/cidr"
|
||||
"github.com/filipowm/go-unifi/unifi"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
"github.com/hashicorp/terraform-plugin-testing/terraform"
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
"github.com/testcontainers/testcontainers-go/modules/compose"
|
||||
)
|
||||
|
||||
var providerFactories = map[string]func() (*schema.Provider, error){
|
||||
"unifi": func() (*schema.Provider, error) {
|
||||
return New("acctest")(), nil
|
||||
},
|
||||
}
|
||||
|
||||
var testClient unifi.Client
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
if os.Getenv("TF_ACC") == "" {
|
||||
// short circuit non-acceptance test runs
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
os.Exit(runAcceptanceTests(m))
|
||||
}
|
||||
|
||||
func runAcceptanceTests(m *testing.M) int {
|
||||
dc, err := compose.NewDockerCompose("../../../docker-compose.yaml")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
if err = dc.WithOsEnv().Up(ctx, compose.Wait(true)); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := dc.Down(context.Background(), compose.RemoveOrphans(true), compose.RemoveImagesLocal); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
|
||||
container, err := dc.ServiceContainer(ctx, "unifi")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Dump the container logs on exit.
|
||||
//
|
||||
// TODO: Use https://pkg.go.dev/github.com/testcontainers/testcontainers-go#LogConsumer instead.
|
||||
defer func() {
|
||||
if os.Getenv("UNIFI_STDOUT") == "" {
|
||||
return
|
||||
}
|
||||
|
||||
stream, err := container.Logs(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
buffer := new(bytes.Buffer)
|
||||
buffer.ReadFrom(stream)
|
||||
testcontainers.Logger.Printf("%s", buffer)
|
||||
}()
|
||||
|
||||
endpoint, err := container.PortEndpoint(ctx, "8443/tcp", "https")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
const user = "admin"
|
||||
const password = "admin"
|
||||
|
||||
if err = os.Setenv("UNIFI_USERNAME", user); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err = os.Setenv("UNIFI_PASSWORD", password); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err = os.Setenv("UNIFI_INSECURE", "true"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err = os.Setenv("UNIFI_API", endpoint); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
testClient, err = unifi.NewClient(&unifi.ClientConfig{
|
||||
URL: endpoint,
|
||||
User: user,
|
||||
Password: password,
|
||||
HttpRoundTripperProvider: func() http.RoundTripper {
|
||||
return createHTTPTransport(true, "unifi")
|
||||
},
|
||||
ValidationMode: unifi.DisableValidation,
|
||||
Logger: unifi.NewDefaultLogger(unifi.WarnLevel),
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return m.Run()
|
||||
}
|
||||
|
||||
func importStep(name string, ignore ...string) resource.TestStep {
|
||||
step := resource.TestStep{
|
||||
ResourceName: name,
|
||||
ImportState: true,
|
||||
ImportStateVerify: true,
|
||||
}
|
||||
|
||||
if len(ignore) > 0 {
|
||||
step.ImportStateVerifyIgnore = ignore
|
||||
}
|
||||
|
||||
return step
|
||||
}
|
||||
|
||||
func siteAndIDImportStateIDFunc(resourceName string) func(*terraform.State) (string, error) {
|
||||
return func(s *terraform.State) (string, error) {
|
||||
rs, ok := s.RootModule().Resources[resourceName]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("not found: %s", resourceName)
|
||||
}
|
||||
networkID := rs.Primary.Attributes["id"]
|
||||
site := rs.Primary.Attributes["site"]
|
||||
return site + ":" + networkID, nil
|
||||
}
|
||||
}
|
||||
|
||||
func preCheck(t *testing.T) {
|
||||
variables := []string{
|
||||
"UNIFI_USERNAME",
|
||||
"UNIFI_PASSWORD",
|
||||
"UNIFI_API",
|
||||
}
|
||||
|
||||
for _, variable := range variables {
|
||||
value := os.Getenv(variable)
|
||||
if value == "" {
|
||||
t.Fatalf("`%s` must be set for acceptance tests!", variable)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
vlanMin = 2
|
||||
vlanMax = 4095
|
||||
)
|
||||
|
||||
var (
|
||||
network = &net.IPNet{
|
||||
IP: net.IPv4(10, 0, 0, 0).To4(),
|
||||
Mask: net.IPv4Mask(255, 0, 0, 0),
|
||||
}
|
||||
|
||||
vlanLock sync.Mutex
|
||||
vlanNext = vlanMin
|
||||
)
|
||||
|
||||
func getTestVLAN(t *testing.T) (*net.IPNet, int) {
|
||||
vlanLock.Lock()
|
||||
defer vlanLock.Unlock()
|
||||
|
||||
vlan := vlanNext
|
||||
vlanNext++
|
||||
|
||||
subnet, err := cidr.Subnet(network, int(math.Ceil(math.Log2(vlanMax))), vlan)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
return subnet, vlan
|
||||
}
|
||||
Reference in New Issue
Block a user