Skip to content

Commit

Permalink
feat: Add an SNMP resource
Browse files Browse the repository at this point in the history
Issue #232
  • Loading branch information
vaerh committed Jul 12, 2023
1 parent c595bcb commit 43c1ec9
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/resources/routeros_snmp/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import routeros_snmp.test .
9 changes: 9 additions & 0 deletions examples/resources/routeros_snmp/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "routeros_snmp" "test" {
contact = "John D."
enabled = true
engine_id_suffix = "8a3c"
location = "Backyard"
trap_community = "private"
trap_generators = "start-trap"
trap_version = 3
}
3 changes: 3 additions & 0 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ func Provider() *schema.Provider {
// PPP
"routeros_ppp_profile": ResourcePPPProfile(),
"routeros_ppp_secret": ResourcePPPSecret(),

// SNMP
"routeros_snmp": ResourceSNMP(),
},
DataSourcesMap: map[string]*schema.Resource{
"routeros_interfaces": DatasourceInterfaces(),
Expand Down
127 changes: 127 additions & 0 deletions routeros/resource_snmp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package routeros

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

/*
{
"contact": "",
"enabled": "false",
"engine-id": "80003a8c04",
"engine-id-suffix": "",
"location": "",
"src-address": "::",
"trap-community": "public",
"trap-generators": "temp-exception",
"trap-target": "",
"trap-version": "1",
"vrf": "main"
}
*/

// https://help.mikrotik.com/docs/display/ROS/SNMP
func ResourceSNMP() *schema.Resource {
resSchema := map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/snmp"),
MetaId: PropId(Id),

"contact": {
Type: schema.TypeString,
Optional: true,
Description: "Contact information.",
},
"enabled": {
Type: schema.TypeBool,
Required: true,
Description: "Used to disable/enable SNMP service",
},
"engine_id": {
Type: schema.TypeString,
Computed: true,
Description: "For SNMP v3, used as part of identifier. You can configure suffix part of engine id " +
"using this argument. If SNMP client is not capable to detect set engine-id value then " +
"this prefix hex have to be used 0x80003a8c04",
},
"engine_id_suffix": {
Type: schema.TypeString,
Optional: true,
Description: "Unique identifier for an SNMPv3 engine by configuring the suffix of the engine ID.",
},
"location": {
Type: schema.TypeString,
Optional: true,
Description: "Location information.",
},
"trap_community": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: "Which communities configured in community menu to use when sending out the trap. " +
"This name must be present in the community list.",
},
"trap_generators": {
Type: schema.TypeString,
Optional: true,
Description: "What action will generate traps: interfaces - interface changes; start-trap - snmp " +
"server starting on the router.",
ValidateFunc: validation.StringInSlice([]string{"interfaces", "start-trap", "temp-exception"}, false),
},
"trap_interfaces": {
Type: schema.TypeString,
Optional: true,
Description: "List of interfaces that traps are going to be sent out.",
},
"trap_target": {
Type: schema.TypeSet,
Optional: true,
Description: "IP (IPv4 or IPv6) addresses of SNMP data collectors that have to receive the trap.",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.IsIPAddress,
},
},
"trap_version": {
Type: schema.TypeInt,
Optional: true,
Description: "Version of SNMP protocol to use for trap.",
ValidateFunc: validation.IntBetween(1, 3),
},
"src_address": {
Type: schema.TypeString,
Optional: true,
Description: "Force the router to always use the same IP source address for all of the SNMP messages.",
ValidateFunc: validation.IsIPAddress,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == new {
return true
}

if (old == "" && new == "::") || (old == "::" && new == "") {
return true
}

if old == "" || new == "" {
return false
}

return false
},
},
KeyVrf: PropVrfRw,
}

return &schema.Resource{
CreateContext: DefaultSystemCreate(resSchema),
ReadContext: DefaultSystemRead(resSchema),
UpdateContext: DefaultSystemUpdate(resSchema),
DeleteContext: DefaultSystemDelete(resSchema),

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: resSchema,
}
}
82 changes: 82 additions & 0 deletions routeros/resource_snmp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package routeros

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

const testSNMPAddress = "routeros_snmp.test"

func TestAccSNMPTest_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: testAccSNMPConfig(0),
Check: resource.ComposeTestCheckFunc(
testAccCheckSNMPExists(testSNMPAddress),
resource.TestCheckResourceAttr(testSNMPAddress, "enabled", "true"),
),
},
{
Config: testAccSNMPConfig(1),
Check: resource.ComposeTestCheckFunc(
testAccCheckSNMPExists(testSNMPAddress),
resource.TestCheckResourceAttr(testSNMPAddress, "enabled", "false"),
),
},
},
})
})
}
}

func testAccCheckSNMPExists(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 testAccSNMPConfig(n int) string {
var conf = []string{
`
resource "routeros_snmp" "test" {
contact = "John D."
enabled = true
engine_id_suffix = "8a3c"
location = "Backyard"
trap_community = "private"
trap_generators = "start-trap"
trap_version = 3
}`,
`
resource "routeros_snmp" "test" {
contact = ""
enabled = false
engine_id_suffix = ""
location = ""
trap_community = "public"
trap_generators = "temp-exception"
trap_version = 1
}`,
}
return providerConfig + conf[n]
}

0 comments on commit 43c1ec9

Please sign in to comment.