Support importing networks and wlans from other sites
This commit is contained in:
@@ -76,4 +76,14 @@ resource "unifi_network" "wan" {
|
||||
|
||||
- **id** (String, Read-only) The ID of the network.
|
||||
|
||||
## Import
|
||||
|
||||
Import is supported using the following syntax:
|
||||
|
||||
```shell
|
||||
# import from provider configured site
|
||||
terraform import unifi_network.mynetwork 5dc28e5e9106d105bdc87217
|
||||
|
||||
# import from another site
|
||||
terraform import unifi_network.mynetwork bfa2l6i7:5dc28e5e9106d105bdc87217
|
||||
```
|
||||
|
||||
@@ -66,4 +66,14 @@ Required:
|
||||
- **block_start** (String, Required) Time of day to start the block.
|
||||
- **day_of_week** (String, Required) Day of week for the block. Valid values are `sun`, `mon`, `tue`, `wed`, `thu`, `fri`, `sat`.
|
||||
|
||||
## Import
|
||||
|
||||
Import is supported using the following syntax:
|
||||
|
||||
```shell
|
||||
# import from provider configured site
|
||||
terraform import unifi_wlan.mywlan 5dc28e5e9106d105bdc87217
|
||||
|
||||
# import from another site
|
||||
terraform import unifi_wlan.mywlan bfa2l6i7:5dc28e5e9106d105bdc87217
|
||||
```
|
||||
|
||||
5
examples/resources/unifi_network/import.sh
Normal file
5
examples/resources/unifi_network/import.sh
Normal file
@@ -0,0 +1,5 @@
|
||||
# import from provider configured site
|
||||
terraform import unifi_network.mynetwork 5dc28e5e9106d105bdc87217
|
||||
|
||||
# import from another site
|
||||
terraform import unifi_network.mynetwork bfa2l6i7:5dc28e5e9106d105bdc87217
|
||||
5
examples/resources/unifi_wlan/import.sh
Normal file
5
examples/resources/unifi_wlan/import.sh
Normal file
@@ -0,0 +1,5 @@
|
||||
# import from provider configured site
|
||||
terraform import unifi_wlan.mywlan 5dc28e5e9106d105bdc87217
|
||||
|
||||
# import from another site
|
||||
terraform import unifi_wlan.mywlan bfa2l6i7:5dc28e5e9106d105bdc87217
|
||||
16
internal/provider/importer.go
Normal file
16
internal/provider/importer.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
)
|
||||
|
||||
func importSiteAndID(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
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
"github.com/hashicorp/go-version"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
|
||||
"github.com/paultyng/go-unifi/unifi"
|
||||
)
|
||||
|
||||
@@ -56,6 +58,18 @@ func importStep(name string, ignore ...string) resource.TestStep {
|
||||
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",
|
||||
|
||||
@@ -17,7 +17,7 @@ func resourceFirewallGroup() *schema.Resource {
|
||||
Update: resourceFirewallGroupUpdate,
|
||||
Delete: resourceFirewallGroupDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: schema.ImportStatePassthrough,
|
||||
State: importSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
|
||||
@@ -20,7 +20,7 @@ func resourceFirewallRule() *schema.Resource {
|
||||
Update: resourceFirewallRuleUpdate,
|
||||
Delete: resourceFirewallRuleDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: schema.ImportStatePassthrough,
|
||||
State: importSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
|
||||
@@ -33,7 +33,7 @@ func resourceNetwork() *schema.Resource {
|
||||
Update: resourceNetworkUpdate,
|
||||
Delete: resourceNetworkDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: schema.ImportStatePassthrough,
|
||||
State: importSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
|
||||
@@ -34,6 +34,13 @@ func TestAccNetwork_basic(t *testing.T) {
|
||||
),
|
||||
},
|
||||
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"),
|
||||
ImportStateVerify: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -162,6 +169,7 @@ func TestAccNetwork_wan(t *testing.T) {
|
||||
|
||||
func TestAccNetwork_differentSite(t *testing.T) {
|
||||
vlanID1 := getTestVLAN(t)
|
||||
vlanID2 := getTestVLAN(t)
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() { preCheck(t) },
|
||||
ProviderFactories: providerFactories,
|
||||
@@ -173,6 +181,24 @@ func TestAccNetwork_differentSite(t *testing.T) {
|
||||
resource.TestCheckResourceAttrPair("unifi_network.test", "site", "unifi_site.test", "name"),
|
||||
),
|
||||
},
|
||||
{
|
||||
ResourceName: "unifi_network.test",
|
||||
ImportState: true,
|
||||
ImportStateIdFunc: siteAndIDImportStateIDFunc("unifi_network.test"),
|
||||
ImportStateVerify: true,
|
||||
},
|
||||
{
|
||||
Config: testAccNetworkWithSiteConfig(vlanID2),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttrPair("unifi_network.test", "site", "unifi_site.test", "name"),
|
||||
),
|
||||
},
|
||||
{
|
||||
ResourceName: "unifi_network.test",
|
||||
ImportState: true,
|
||||
ImportStateIdFunc: siteAndIDImportStateIDFunc("unifi_network.test"),
|
||||
ImportStateVerify: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ func resourcePortForward() *schema.Resource {
|
||||
Update: resourcePortForwardUpdate,
|
||||
Delete: resourcePortForwardDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: schema.ImportStatePassthrough,
|
||||
State: importSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
|
||||
@@ -20,7 +20,7 @@ func resourceUser() *schema.Resource {
|
||||
Update: resourceUserUpdate,
|
||||
Delete: resourceUserDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: schema.ImportStatePassthrough,
|
||||
State: importSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
|
||||
@@ -17,7 +17,7 @@ func resourceUserGroup() *schema.Resource {
|
||||
Update: resourceUserGroupUpdate,
|
||||
Delete: resourceUserGroupDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: schema.ImportStatePassthrough,
|
||||
State: importSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
|
||||
@@ -20,7 +20,7 @@ func resourceWLAN() *schema.Resource {
|
||||
Update: resourceWLANUpdate,
|
||||
Delete: resourceWLANDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: schema.ImportStatePassthrough,
|
||||
State: importSiteAndID,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
|
||||
Reference in New Issue
Block a user