Skip to content

Commit

Permalink
feat: Implement routeros_system_logging resource (#261)
Browse files Browse the repository at this point in the history
* Implement routeros_system_logging resource

* remove leftovers from validation

* Add new line to trigger build

* Improve documentation, re-add validation
  • Loading branch information
jlpedrosa committed Sep 21, 2023
1 parent b94fe74 commit f8c89aa
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/resources/routeros_system_logging/import.sh
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 5 additions & 0 deletions examples/resources/routeros_system_logging/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

resource "routeros_system_logging" "log_snmp_disk" {
action = "disk"
topics = ["snmp"]
}
1 change: 1 addition & 0 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,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(),
Expand Down
79 changes: 79 additions & 0 deletions routeros/resource_system_logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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",
}

// 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"),
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, 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`,
},
}

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_logging_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 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"]
}
`
}

0 comments on commit f8c89aa

Please sign in to comment.