Skip to content

Commit

Permalink
feat: Support creating users #200 (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaerh committed May 5, 2023
1 parent a079182 commit 78191e2
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/resources/routeros_system_user/import.sh
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
7 changes: 7 additions & 0 deletions examples/resources/routeros_system_user/resource.tf
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"
}
1 change: 1 addition & 0 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func Provider() *schema.Provider {
"routeros_system_identity": ResourceSystemIdentity(),
"routeros_system_scheduler": ResourceSystemScheduler(),
"routeros_system_certificate": ResourceSystemCertificate(),
"routeros_system_user": ResourceUser(),

// Aliases for system objects to retain compatibility between original and fork
"routeros_identity": ResourceSystemIdentity(),
Expand Down
72 changes: 72 additions & 0 deletions routeros/resource_system_user.go
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,
}
}
65 changes: 65 additions & 0 deletions routeros/resource_system_user_test.go
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"
}
`
}

0 comments on commit 78191e2

Please sign in to comment.