-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#The ID can be found via API or the terminal | ||
#The command for the terminal is -> :put [/user get [print show-ids]] | ||
terraform import routeros_system_user.test *1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
resource "routeros_system_user" "test" { | ||
name = "test-user-1" | ||
address = "0.0.0.0/0" | ||
group = "read" | ||
password = "secret" | ||
comment = "Test User" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package routeros | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
/* | ||
{ | ||
".id": "*1", | ||
"address": "0.0.0.0/0", | ||
"comment": "system default user", | ||
"disabled": "false", | ||
"expired": "true", | ||
"group": "full", | ||
"last-logged-in": "may/02/2023 05:21:45", | ||
"name": "admin" | ||
} | ||
*/ | ||
|
||
// https://help.mikrotik.com/docs/display/ROS/User#User-RouterUsers | ||
func ResourceUser() *schema.Resource { | ||
resSchema := map[string]*schema.Schema{ | ||
MetaResourcePath: PropResourcePath("/user"), | ||
MetaId: PropId(Id), | ||
|
||
"address": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "Host or network address from which the user is allowed to log in.", | ||
}, | ||
KeyComment: PropCommentRw, | ||
KeyDisabled: PropDisabledRw, | ||
"expired": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "Password expired.", | ||
}, | ||
"group": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Name of the group the user belongs to.", | ||
}, | ||
KeyName: PropName("User name. Although it must start with an alphanumeric character, it may contain '*', " + | ||
"'_', '.' and '@' symbols."), | ||
"password": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Sensitive: true, | ||
Description: "User password. If not specified, it is left blank (hit [Enter] when logging in). It " + | ||
"conforms to standard Unix characteristics of passwords and may contain letters, digits, " + | ||
"'*' and '_' symbols.", | ||
}, | ||
"last_logged_in": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Read-only field. Last time and date when a user logged in.", | ||
}, | ||
} | ||
|
||
return &schema.Resource{ | ||
CreateContext: DefaultCreate(resSchema), | ||
ReadContext: DefaultRead(resSchema), | ||
UpdateContext: DefaultUpdate(resSchema), | ||
DeleteContext: DefaultDelete(resSchema), | ||
|
||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
|
||
Schema: resSchema, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package routeros | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/terraform" | ||
) | ||
|
||
const testUserAddress = "routeros_system_user.test" | ||
|
||
func TestAccUserTest_basic(t *testing.T) { | ||
t.Parallel() | ||
for _, name := range testNames { | ||
t.Run(name, func(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
testSetTransportEnv(t, name) | ||
}, | ||
ProviderFactories: testAccProviderFactories, | ||
CheckDestroy: testCheckResourceDestroy("/user", "routeros_system_user"), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccUserConfig(), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckUserExists(testUserAddress), | ||
resource.TestCheckResourceAttr(testUserAddress, "name", "test-user-1"), | ||
), | ||
}, | ||
}, | ||
}) | ||
|
||
}) | ||
} | ||
} | ||
|
||
func testAccCheckUserExists(name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return fmt.Errorf("not found: %s", name) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("no id is set") | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccUserConfig() string { | ||
return providerConfig + ` | ||
resource "routeros_system_user" "test" { | ||
name = "test-user-1" | ||
address = "0.0.0.0/0" | ||
group = "read" | ||
password = "secret" | ||
comment = "Test User" | ||
} | ||
` | ||
} |