Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement routeros_system_logging resource #261

Merged
merged 4 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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(),
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{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were you planning to do validation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad: I implemented validation but terraform does not support validation for lists :( and I only found out after it was implemented (error message on the sdk), I deleted the function but not the slice.

I've just deleted the slice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I found a way to add the validation.

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