From 0d55cb34c03facc7d2460b2413cf124423c0c09c Mon Sep 17 00:00:00 2001 From: Jose Luis Pedrosa Date: Wed, 20 Sep 2023 22:33:35 +0100 Subject: [PATCH 1/4] Implement routeros_system_logging resource --- .../routeros_system_logging/import.sh | 4 + .../routeros_system_logging/resource.tf | 5 ++ routeros/provider.go | 1 + routeros/resource_system_logging.go | 73 +++++++++++++++++++ routeros/resource_system_logging_test.go | 65 +++++++++++++++++ 5 files changed, 148 insertions(+) create mode 100644 examples/resources/routeros_system_logging/import.sh create mode 100644 examples/resources/routeros_system_logging/resource.tf create mode 100644 routeros/resource_system_logging.go create mode 100644 routeros/resource_system_logging_test.go diff --git a/examples/resources/routeros_system_logging/import.sh b/examples/resources/routeros_system_logging/import.sh new file mode 100644 index 00000000..60108f52 --- /dev/null +++ b/examples/resources/routeros_system_logging/import.sh @@ -0,0 +1,4 @@ +# The ID can be found via API or the terminal +# The command for the terminal is -> :put [/system/logging/print get [print show-ids]] + +terraform import routeros_system_logging.log_snmp_disk "*4" \ No newline at end of file diff --git a/examples/resources/routeros_system_logging/resource.tf b/examples/resources/routeros_system_logging/resource.tf new file mode 100644 index 00000000..61e63838 --- /dev/null +++ b/examples/resources/routeros_system_logging/resource.tf @@ -0,0 +1,5 @@ + +resource "routeros_system_logging" "log_snmp_disk" { + action = "disk" + topics = ["snmp"] +} \ No newline at end of file diff --git a/routeros/provider.go b/routeros/provider.go index 36699d28..6f6408db 100644 --- a/routeros/provider.go +++ b/routeros/provider.go @@ -126,6 +126,7 @@ func Provider() *schema.Provider { // System Objects "routeros_ip_cloud": ResourceIpCloud(), "routeros_system_identity": ResourceSystemIdentity(), + "routeros_system_logging": ResourceSystemLogging(), "routeros_system_scheduler": ResourceSystemScheduler(), "routeros_system_certificate": ResourceSystemCertificate(), "routeros_system_user": ResourceUser(), diff --git a/routeros/resource_system_logging.go b/routeros/resource_system_logging.go new file mode 100644 index 00000000..0d8108ba --- /dev/null +++ b/routeros/resource_system_logging.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": "*4", + "action": "echo", + "default": "true", + "disabled": "false", + "invalid": "false", + "prefix": "", + "topics": "critical" +} */ + +var validTopics = []string{ + "account", " bfd", "caps", "ddns", "dns", "error", "gsm", "info", "iscsi", "l2tp", "manager", "ntp", "packet", + "pppoe", "radvd", "rip", "script", "smb", "sstp", "system", "timer", "vrrp", "web-proxy", "async", "bgp", + "certificate", "debug", "dot1x", "dude", "event", "hotspot", "interface", "isdn", "ldp", "mme", "ospf", "pim", + "pptp", "raw", "route", "sertcp", "snmp", "state", "telephony", "upnp", "warning", "wireless", "backup", "calc", + "critical", "dhcp", "e-mail", "firewall", "igmp-proxy", "ipsec", "kvm", "lte", "mpls", "ovpn", "ppp", "radius", + "read", "rsvp", "simulator", "ssh", "store", "tftp", "ups", "watchdog", "write", +} + +func ResourceSystemLogging() *schema.Resource { + resSchema := map[string]*schema.Schema{ + MetaResourcePath: PropResourcePath("/system/logging"), + MetaId: PropId(Id), + "action": { + Type: schema.TypeString, + Required: true, + Description: "specifies one of the system default actions or user specified action listed in actions menu", + ValidateFunc: validation.StringInSlice([]string{"disk", "echo", "memory", "remote"}, false), + }, + "prefix": { + Type: schema.TypeString, + Optional: true, + Description: "prefix added at the beginning of log messages", + Default: "", + }, + KeyDisabled: { + Type: schema.TypeBool, + Optional: true, + Default: false, + Description: "Whether or not this logging should be disabled", + }, + "invalid": { + Type: schema.TypeBool, + Computed: true, + }, + "topics": { + Type: schema.TypeList, + Elem: &schema.Schema{Type: schema.TypeString}, + Optional: true, + Description: "prefix added at the beginning of log messages", + }, + } + + return &schema.Resource{ + CreateContext: DefaultCreate(resSchema), + ReadContext: DefaultRead(resSchema), + UpdateContext: DefaultUpdate(resSchema), + DeleteContext: DefaultDelete(resSchema), + + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + + Schema: resSchema, + } +} diff --git a/routeros/resource_system_logging_test.go b/routeros/resource_system_logging_test.go new file mode 100644 index 00000000..3641dccc --- /dev/null +++ b/routeros/resource_system_logging_test.go @@ -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 testSystemSimpleLoggingTask = "routeros_system_logging.simple_logging" + +func TestAccSystemLoggingTest_basic(t *testing.T) { + 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, + Steps: []resource.TestStep{ + { + Config: testAccSystemLoggingConfig(), + Check: resource.ComposeTestCheckFunc( + testAccCheckLoggingExists(testSystemSimpleLoggingTask), + resource.TestCheckResourceAttr(testSystemSimpleLoggingTask, "action", "echo"), + resource.TestCheckResourceAttr(testSystemSimpleLoggingTask, "disabled", "false"), + resource.TestCheckResourceAttr(testSystemSimpleLoggingTask, "invalid", "false"), + resource.TestCheckResourceAttr(testSystemSimpleLoggingTask, "prefix", "simple_prefix"), + resource.TestCheckResourceAttr(testSystemSimpleLoggingTask, "topics.0", "snmp"), + resource.TestCheckResourceAttr(testSystemSimpleLoggingTask, "topics.1", "gsm"), + ), + }, + }, + }) + + }) + } +} + +func testAccCheckLoggingExists(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 testAccSystemLoggingConfig() string { + return providerConfig + ` +resource "routeros_system_logging" "simple_logging" { + action = "echo" + prefix = "simple_prefix" + topics = ["snmp", "gsm"] +} +` +} From eab68adaed6715f6ac96d349b4283f2f1a0b4fee Mon Sep 17 00:00:00 2001 From: Jose Luis Pedrosa Date: Thu, 21 Sep 2023 10:10:29 +0100 Subject: [PATCH 2/4] remove leftovers from validation --- routeros/resource_system_logging.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/routeros/resource_system_logging.go b/routeros/resource_system_logging.go index 0d8108ba..8b59b53c 100644 --- a/routeros/resource_system_logging.go +++ b/routeros/resource_system_logging.go @@ -15,15 +15,6 @@ import ( "topics": "critical" } */ -var validTopics = []string{ - "account", " bfd", "caps", "ddns", "dns", "error", "gsm", "info", "iscsi", "l2tp", "manager", "ntp", "packet", - "pppoe", "radvd", "rip", "script", "smb", "sstp", "system", "timer", "vrrp", "web-proxy", "async", "bgp", - "certificate", "debug", "dot1x", "dude", "event", "hotspot", "interface", "isdn", "ldp", "mme", "ospf", "pim", - "pptp", "raw", "route", "sertcp", "snmp", "state", "telephony", "upnp", "warning", "wireless", "backup", "calc", - "critical", "dhcp", "e-mail", "firewall", "igmp-proxy", "ipsec", "kvm", "lte", "mpls", "ovpn", "ppp", "radius", - "read", "rsvp", "simulator", "ssh", "store", "tftp", "ups", "watchdog", "write", -} - func ResourceSystemLogging() *schema.Resource { resSchema := map[string]*schema.Schema{ MetaResourcePath: PropResourcePath("/system/logging"), From 5bab71d1c48c5ee97c64b6bf2429dfe060c9dcb7 Mon Sep 17 00:00:00 2001 From: Jose Luis Pedrosa Date: Thu, 21 Sep 2023 10:13:13 +0100 Subject: [PATCH 3/4] Add new line to trigger build --- routeros/resource_system_logging.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/routeros/resource_system_logging.go b/routeros/resource_system_logging.go index 8b59b53c..7f775ac4 100644 --- a/routeros/resource_system_logging.go +++ b/routeros/resource_system_logging.go @@ -5,7 +5,8 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) -/* { +/* +{ ".id": "*4", "action": "echo", "default": "true", @@ -13,7 +14,8 @@ import ( "invalid": "false", "prefix": "", "topics": "critical" -} */ +} +*/ func ResourceSystemLogging() *schema.Resource { resSchema := map[string]*schema.Schema{ From e05b827db62a5e27bd12e0e83c7eb285727390f8 Mon Sep 17 00:00:00 2001 From: Jose Luis Pedrosa Date: Thu, 21 Sep 2023 10:42:47 +0100 Subject: [PATCH 4/4] Improve documentation, re-add validation --- routeros/resource_system_logging.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/routeros/resource_system_logging.go b/routeros/resource_system_logging.go index 7f775ac4..92b60ce4 100644 --- a/routeros/resource_system_logging.go +++ b/routeros/resource_system_logging.go @@ -17,6 +17,17 @@ import ( } */ +var validTopics = []string{ + "account", " bfd", "caps", "ddns", "dns", "error", "gsm", "info", "iscsi", "l2tp", "manager", "ntp", "packet", + "pppoe", "radvd", "rip", "script", "smb", "sstp", "system", "timer", "vrrp", "web-proxy", "async", "bgp", + "certificate", "debug", "dot1x", "dude", "event", "hotspot", "interface", "isdn", "ldp", "mme", "ospf", "pim", + "pptp", "raw", "route", "sertcp", "snmp", "state", "telephony", "upnp", "warning", "wireless", "backup", "calc", + "critical", "dhcp", "e-mail", "firewall", "igmp-proxy", "ipsec", "kvm", "lte", "mpls", "ovpn", "ppp", "radius", + "read", "rsvp", "simulator", "ssh", "store", "tftp", "ups", "watchdog", "write", +} + +// ResourceSystemLogging defines the resource for configuring logging rules +// https://wiki.mikrotik.com/wiki/Manual:System/Log func ResourceSystemLogging() *schema.Resource { resSchema := map[string]*schema.Schema{ MetaResourcePath: PropResourcePath("/system/logging"), @@ -44,10 +55,12 @@ func ResourceSystemLogging() *schema.Resource { Computed: true, }, "topics": { - Type: schema.TypeList, - Elem: &schema.Schema{Type: schema.TypeString}, - Optional: true, - Description: "prefix added at the beginning of log messages", + Type: schema.TypeList, + Elem: &schema.Schema{Type: schema.TypeString, ValidateFunc: validation.StringInSlice(validTopics, false)}, + Optional: true, + Description: `log all messages that falls into specified topic or list of topics. + '!' character can be used before topic to exclude messages falling under this topic. For example, we want to log NTP debug info without too much details: + /system logging add topics=ntp,debug,!packet`, }, }