From 727cd9b196680d3649cccd506a7f88530aa735a4 Mon Sep 17 00:00:00 2001 From: Michael Dokolin Date: Wed, 22 Nov 2023 19:49:01 +0100 Subject: [PATCH] feat: Add user manager user resource --- .../routeros_user_manager_user/import.sh | 3 + .../routeros_user_manager_user/resource.tf | 25 ++++++ routeros/provider.go | 1 + routeros/resource_user_manager_user.go | 78 +++++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 examples/resources/routeros_user_manager_user/import.sh create mode 100644 examples/resources/routeros_user_manager_user/resource.tf create mode 100644 routeros/resource_user_manager_user.go diff --git a/examples/resources/routeros_user_manager_user/import.sh b/examples/resources/routeros_user_manager_user/import.sh new file mode 100644 index 00000000..56d65858 --- /dev/null +++ b/examples/resources/routeros_user_manager_user/import.sh @@ -0,0 +1,3 @@ +#The ID can be found via API or the terminal +#The command for the terminal is -> :put [/user-manager/user get [print show-ids]] +terraform import routeros_user_manager_user.test '*1' diff --git a/examples/resources/routeros_user_manager_user/resource.tf b/examples/resources/routeros_user_manager_user/resource.tf new file mode 100644 index 00000000..b5e5b72d --- /dev/null +++ b/examples/resources/routeros_user_manager_user/resource.tf @@ -0,0 +1,25 @@ +resource "routeros_user_manager_attribute" "mikrotik_wireless_comment" { + name = "Mikrotik-Wireless-Comment" + type_id = 21 + value_type = "string" +} + +resource "routeros_user_manager_attribute" "mikrotik_wireless_vlanid" { + name = "Mikrotik-Wireless-VLANID" + type_id = 26 + value_type = "uint32" +} + +resource "routeros_user_manager_user_group" "test" { + name = "test" +} + +resource "routeros_user_manager_user" "test" { + attributes = [ + "${routeros_user_manager_attribute.mikrotik_wireless_comment.name}:Test Group", + "${routeros_user_manager_attribute.mikrotik_wireless_vlanid.name}:100", + ] + group = routeros_user_manager_user_group.test.name + name = "test" + password = "password" +} diff --git a/routeros/provider.go b/routeros/provider.go index 9c62c386..95aea6b7 100644 --- a/routeros/provider.go +++ b/routeros/provider.go @@ -199,6 +199,7 @@ func Provider() *schema.Provider { "routeros_user_manager_profile_limitation": ResourceUserManagerProfileLimitation(), "routeros_user_manager_router": ResourceUserManagerRouter(), "routeros_user_manager_settings": ResourceUserManagerSettings(), + "routeros_user_manager_user": ResourceUserManagerUser(), "routeros_user_manager_user_group": ResourceUserManagerUserGroup(), }, DataSourcesMap: map[string]*schema.Resource{ diff --git a/routeros/resource_user_manager_user.go b/routeros/resource_user_manager_user.go new file mode 100644 index 00000000..4008135f --- /dev/null +++ b/routeros/resource_user_manager_user.go @@ -0,0 +1,78 @@ +package routeros + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +/* +{ + ".id": "*1", + "attributes": "", + "caller-id": "bind", + "comment": "test", + "disabled": "false", + "group": "test", + "name": "test", + "otp-secret": "", + "password": "password", + "shared-users": "1" +} +*/ + +// https://help.mikrotik.com/docs/display/ROS/User+Manager#UserManager-Users +func ResourceUserManagerUser() *schema.Resource { + resSchema := map[string]*schema.Schema{ + MetaResourcePath: PropResourcePath("/user-manager/user"), + MetaId: PropId(Id), + + "attributes": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Description: "A custom set of colon-separated attributes with their values will be added to `Access-Accept` messages for users in this group.", + }, + "caller_id": { + Type: schema.TypeString, + Optional: true, + Description: "Allow user's authentication with a specific Calling-Station-Id value.", + }, + KeyComment: PropCommentRw, + KeyDisabled: PropDisabledRw, + "group": { + Type: schema.TypeString, + Optional: true, + Description: "Name of the group the user is associated with.", + DiffSuppressFunc: AlwaysPresentNotUserProvided, + }, + KeyName: PropName("Username for session authentication."), + "otp_secret": { + Type: schema.TypeString, + Optional: true, + Description: "A token of a one-time code that will be attached to the password.", + }, + "password": { + Type: schema.TypeString, + Optional: true, + Description: "The password of the user for session authentication.", + }, + "shared_users": { + Type: schema.TypeInt, + Optional: true, + Default: 1, + Description: "The total amount of sessions the user can simultaneously establish.", + }, + } + + return &schema.Resource{ + CreateContext: DefaultCreate(resSchema), + ReadContext: DefaultRead(resSchema), + UpdateContext: DefaultUpdate(resSchema), + DeleteContext: DefaultDelete(resSchema), + + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + + Schema: resSchema, + } +}