From 733932c5bbca367fb5f461f3508b485ed428fb14 Mon Sep 17 00:00:00 2001 From: Michael Dokolin Date: Sun, 19 Nov 2023 19:12:12 +0100 Subject: [PATCH] feat: Add user manager profile limitation resource --- .../import.sh | 3 + .../resource.tf | 23 ++++++ routeros/provider.go | 13 ++-- ...esource_user_manager_profile_limitation.go | 73 +++++++++++++++++++ 4 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 examples/resources/routeros_user_manager_profile_limitation/import.sh create mode 100644 examples/resources/routeros_user_manager_profile_limitation/resource.tf create mode 100644 routeros/resource_user_manager_profile_limitation.go diff --git a/examples/resources/routeros_user_manager_profile_limitation/import.sh b/examples/resources/routeros_user_manager_profile_limitation/import.sh new file mode 100644 index 00000000..7239de15 --- /dev/null +++ b/examples/resources/routeros_user_manager_profile_limitation/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/profile-limitation get [print show-ids]] +terraform import routeros_user_manager_profile_limitation.weekend_night '*1' diff --git a/examples/resources/routeros_user_manager_profile_limitation/resource.tf b/examples/resources/routeros_user_manager_profile_limitation/resource.tf new file mode 100644 index 00000000..1dabd236 --- /dev/null +++ b/examples/resources/routeros_user_manager_profile_limitation/resource.tf @@ -0,0 +1,23 @@ +resource "routeros_user_manager_limitation" "test" { + name = "test" + download_limit = 1024 + upload_limit = 1024 + uptime_limit = "10d" +} + +resource "routeros_user_manager_profile" "test" { + name = "test" + name_for_users = "Test" + price = 0.02 +} + +resource "routeros_user_manager_profile_limitation" "weekend_night" { + limitation = routeros_user_manager_limitation.test.name + profile = routeros_user_manager_profile.test.name + from_time = "0s" + till_time = "6h" + weekdays = [ + "sunday", + "saturday", + ] +} diff --git a/routeros/provider.go b/routeros/provider.go index b51d3cf2..0ca6356f 100644 --- a/routeros/provider.go +++ b/routeros/provider.go @@ -191,12 +191,13 @@ func Provider() *schema.Provider { "routeros_wireguard_keys": ResourceWireguardKeys(), // User Manager - "routeros_user_manager_advanced": ResourceUserManagerAdvanced(), - "routeros_user_manager_attribute": ResourceUserManagerAttribute(), - "routeros_user_manager_database": ResourceUserManagerDatabase(), - "routeros_user_manager_limitation": ResourceUserManagerLimitation(), - "routeros_user_manager_profile": ResourceUserManagerProfile(), - "routeros_user_manager_settings": ResourceUserManagerSettings(), + "routeros_user_manager_advanced": ResourceUserManagerAdvanced(), + "routeros_user_manager_attribute": ResourceUserManagerAttribute(), + "routeros_user_manager_database": ResourceUserManagerDatabase(), + "routeros_user_manager_limitation": ResourceUserManagerLimitation(), + "routeros_user_manager_profile": ResourceUserManagerProfile(), + "routeros_user_manager_profile_limitation": ResourceUserManagerProfileLimitation(), + "routeros_user_manager_settings": ResourceUserManagerSettings(), }, DataSourcesMap: map[string]*schema.Resource{ "routeros_firewall": DatasourceFirewall(), diff --git a/routeros/resource_user_manager_profile_limitation.go b/routeros/resource_user_manager_profile_limitation.go new file mode 100644 index 00000000..ec1f9afb --- /dev/null +++ b/routeros/resource_user_manager_profile_limitation.go @@ -0,0 +1,73 @@ +package routeros + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" +) + +/* +{ + ".id": "*1", + "from-time": "0s", + "limitation": "test", + "profile": "test", + "till-time": "23h59m59s", + "weekdays": "sunday,monday,tuesday,wednesday,thursday,friday,saturday" +} +*/ + +// https://help.mikrotik.com/docs/display/ROS/User+Manager#UserManager-ProfileLimitations +func ResourceUserManagerProfileLimitation() *schema.Resource { + resSchema := map[string]*schema.Schema{ + MetaResourcePath: PropResourcePath("/user-manager/profile-limitation"), + MetaId: PropId(Id), + + "from_time": { + Type: schema.TypeString, + Optional: true, + Default: "0s", + Description: "Time of the day when the limitation should take place.", + DiffSuppressFunc: TimeEquall, + }, + "limitation": { + Type: schema.TypeString, + Required: true, + Description: "The limitation name.", + }, + "profile": { + Type: schema.TypeString, + Required: true, + Description: "The profile name.", + }, + "till_time": { + Type: schema.TypeString, + Optional: true, + Default: "23h59m59s", + Description: "Time of the day when the limitation should end.", + DiffSuppressFunc: TimeEquall, + }, + "weekdays": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validation.StringInSlice([]string{"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"}, false), + }, + Description: "Days of the week when the limitation is active.", + DiffSuppressFunc: AlwaysPresentNotUserProvided, + }, + } + + return &schema.Resource{ + CreateContext: DefaultCreate(resSchema), + ReadContext: DefaultRead(resSchema), + UpdateContext: DefaultUpdate(resSchema), + DeleteContext: DefaultDelete(resSchema), + + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + + Schema: resSchema, + } +}