Skip to content

Commit a301353

Browse files
authored
Merge pull request #193 from ThiagoYU/DAITDS-250
DAITDS-250 - Team and user import support added
2 parents f4b15d3 + 7c0a72d commit a301353

6 files changed

Lines changed: 101 additions & 6 deletions

File tree

ns1/resource_team.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func teamResource() *schema.Resource {
4343
Read: TeamRead,
4444
Update: TeamUpdate,
4545
Delete: TeamDelete,
46+
Importer: &schema.ResourceImporter{State: teamImportStateFunc},
4647
SchemaVersion: 1,
4748
StateUpgraders: []schema.StateUpgrader{
4849
{
@@ -155,3 +156,7 @@ func TeamUpdate(d *schema.ResourceData, meta interface{}) error {
155156
// because teams don't have a concept of what users and keys are assigned to them, only the other way around.
156157
return teamToResourceData(d, &t)
157158
}
159+
160+
func teamImportStateFunc(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
161+
return []*schema.ResourceData{d}, nil
162+
}

ns1/resource_team_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,48 @@ func TestAccTeam_ManualDelete(t *testing.T) {
112112
})
113113
}
114114

115+
// Import user test
116+
func TestAccTeam_import_test(t *testing.T) {
117+
var team account.Team
118+
n := fmt.Sprintf("terraform test team %s", acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum))
119+
ignored_fields := []string{"dhcp_manage_dhcp", "dhcp_view_dhcp", "ipam_manage_ipam", "ipam_view_ipam", "dns_records"}
120+
121+
resource.Test(t, resource.TestCase{
122+
PreCheck: func() { testAccPreCheck(t) },
123+
Providers: testAccProviders,
124+
CheckDestroy: testAccCheckTeamDestroy,
125+
Steps: []resource.TestStep{
126+
{
127+
Config: fmt.Sprintf(testAccTeamBasic, n),
128+
Check: resource.ComposeTestCheckFunc(
129+
testAccCheckTeamExists("ns1_team.foobar", &team),
130+
testAccCheckTeamName(&team, n),
131+
testAccCheckTeamDNSPermission(&team, "view_zones", true),
132+
testAccCheckTeamDNSPermission(&team, "zones_allow_by_default", true),
133+
testAccCheckTeamDNSPermissionZones(&team, "zones_allow", []string{"mytest.zone"}),
134+
testAccCheckTeamDNSPermissionZones(&team, "zones_deny", []string{"myother.zone"}),
135+
testAccCheckTeamDNSPermissionRecords(&team, "dns_records_allow", []account.PermissionsRecord{
136+
{Domain: "my.ns1.com", Subdomains: false, Zone: "ns1.com", RecordType: "A"}}),
137+
testAccCheckTeamDNSPermissionRecords(&team, "dns_records_deny", []account.PermissionsRecord{
138+
{Domain: "my.test.com", Subdomains: true, Zone: "test.com", RecordType: "A"}}),
139+
testAccCheckTeamDataPermission(&team, "manage_datasources", true),
140+
testAccCheckTeamIPWhitelists(&team, []account.IPWhitelist{
141+
{Name: "whitelist-1", Values: []string{"1.1.1.1", "2.2.2.2"}},
142+
{Name: "whitelist-2", Values: []string{"3.3.3.3", "4.4.4.4"}},
143+
}),
144+
),
145+
},
146+
{
147+
ResourceName: "ns1_team.foobar",
148+
ImportState: true,
149+
ImportStateVerify: true,
150+
// Ignoring some fields because of how the dns_records work right now
151+
ImportStateVerifyIgnore: ignored_fields,
152+
},
153+
},
154+
})
155+
}
156+
115157
func testAccCheckTeamExists(n string, team *account.Team) resource.TestCheckFunc {
116158
return func(s *terraform.State) error {
117159
rs, ok := s.RootModule().Resources[n]

ns1/resource_user.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"gopkg.in/ns1/ns1-go.v2/rest/model/account"
1313
)
1414

15-
var usernameRegex = regexp.MustCompile(`^([a-zA-Z0-9_+.@]+)$`)
15+
var usernameRegex = regexp.MustCompile(`^[a-zA-Z0-9@$&'*+\-=? ^_.{|}~]{3,320}$`)
1616

1717
func userResource() *schema.Resource {
1818
s := map[string]*schema.Schema{
@@ -59,6 +59,7 @@ func userResource() *schema.Resource {
5959
Read: UserRead,
6060
Update: UserUpdate,
6161
Delete: UserDelete,
62+
Importer: &schema.ResourceImporter{State: userImportStateFunc},
6263
SchemaVersion: 1,
6364
StateUpgraders: []schema.StateUpgrader{
6465
{
@@ -72,6 +73,7 @@ func userResource() *schema.Resource {
7273

7374
func userToResourceData(d *schema.ResourceData, u *account.User) error {
7475
d.SetId(u.Username)
76+
d.Set("username", u.Username)
7577
d.Set("name", u.Name)
7678
d.Set("email", u.Email)
7779
d.Set("teams", u.TeamIDs)
@@ -214,3 +216,7 @@ func validateUsername(
214216
}
215217
return warns, errs
216218
}
219+
220+
func userImportStateFunc(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
221+
return []*schema.ResourceData{d}, nil
222+
}

ns1/resource_user_test.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,45 @@ func TestAccUser_permissions_multiple_teams_start_no_team(t *testing.T) {
435435
})
436436
}
437437

438+
// Import user test
439+
func TestAccUser_import_test(t *testing.T) {
440+
var user account.User
441+
rString := acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)
442+
name := fmt.Sprintf("terraform acc test user %s", rString)
443+
username := fmt.Sprintf("tf_acc_test_user_%s", rString)
444+
ignored_fields := []string{"dhcp_manage_dhcp", "dhcp_view_dhcp", "ipam_manage_ipam", "ipam_view_ipam"}
445+
446+
resource.Test(t, resource.TestCase{
447+
PreCheck: func() { testAccPreCheck(t) },
448+
Providers: testAccProviders,
449+
CheckDestroy: testAccCheckUserDestroy,
450+
Steps: []resource.TestStep{
451+
{
452+
Config: testAccUserBasic(rString),
453+
Check: resource.ComposeTestCheckFunc(
454+
testAccCheckUserExists("ns1_user.u", &user),
455+
resource.TestCheckResourceAttr("ns1_user.u", "email", "tf_acc_test_ns1@hashicorp.com"),
456+
resource.TestCheckResourceAttr("ns1_user.u", "name", name),
457+
resource.TestCheckResourceAttr("ns1_user.u", "teams.#", "1"),
458+
resource.TestCheckResourceAttr("ns1_user.u", "notify.%", "1"),
459+
resource.TestCheckResourceAttr("ns1_user.u", "notify.billing", "true"),
460+
resource.TestCheckResourceAttr("ns1_user.u", "username", username),
461+
testAccCheckUserIPWhitelists(&user, []string{"1.1.1.1", "2.2.2.2"}),
462+
resource.TestCheckResourceAttr("ns1_user.u", "ip_whitelist_strict", "true"),
463+
),
464+
},
465+
{
466+
ResourceName: "ns1_user.u",
467+
ImportState: true,
468+
ImportStateId: username,
469+
ImportStateVerify: true,
470+
// Ignoring some fields because of how the permissions work right now
471+
ImportStateVerifyIgnore: ignored_fields,
472+
},
473+
},
474+
})
475+
}
476+
438477
// Case when a user is on a team and that team updates it's permissions.
439478
// This test is currently failing, as this is not implemented yet - this doesn't
440479
// actually cause any issues because it's just Terraforms state that doesn't have the
@@ -516,11 +555,6 @@ func TestValidateUsername(t *testing.T) {
516555
"valid_us3r@example.com",
517556
0,
518557
},
519-
{
520-
"invalid - dash",
521-
"inv4lid-user",
522-
1,
523-
},
524558
{
525559
"invalid - punctuation",
526560
"inv@l!d_us3r",

website/docs/r/team.html.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ Only relevant for the DDI product.
9494
* `ipam_view_ipam` - (Optional) Whether the team can view IPAM.
9595
Only relevant for the DDI product.
9696

97+
## Import
98+
99+
`terraform import ns1_team.<name> <team_id>`
100+
97101
## Attributes Reference
98102

99103
All of the arguments listed above are exported as attributes, with no

website/docs/r/user.html.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ Only relevant for the DDI product.
8989
* `ipam_manage_ipam` - (Optional) Whether the user can manage IPAM.
9090
Only relevant for the DDI product.
9191

92+
## Import
93+
94+
`terraform import ns1_user.<name> <username>`
95+
9296
## Attributes Reference
9397

9498
All of the arguments listed above are exported as attributes, with no

0 commit comments

Comments
 (0)