Skip to content

Commit

Permalink
Support importing networks and wlans from other sites
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtmc authored and paultyng committed Nov 4, 2020
1 parent 10009f9 commit 4637c33
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 7 deletions.
10 changes: 10 additions & 0 deletions docs/resources/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
10 changes: 10 additions & 0 deletions docs/resources/wlan.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 5 additions & 0 deletions examples/resources/unifi_network/import.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 0 deletions examples/resources/unifi_wlan/import.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 0 deletions internal/provider/importer.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package provider

import (
"context"
"fmt"
"os"
"sync"
"testing"

"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"
)

Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_firewall_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_firewall_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
26 changes: 26 additions & 0 deletions internal/provider/resource_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
})
}
Expand Down Expand Up @@ -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,
Expand All @@ -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,
},
},
})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_port_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_user_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_wlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 4637c33

Please sign in to comment.