-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement routeros_system_logging resource (#261)
* Implement routeros_system_logging resource * remove leftovers from validation * Add new line to trigger build * Improve documentation, re-add validation
- Loading branch information
Showing
5 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
` | ||
} |