-
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.
Issue #232
- Loading branch information
Showing
5 changed files
with
222 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 @@ | ||
terraform import routeros_snmp.test . |
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,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 | ||
} |
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,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, | ||
} | ||
} |
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,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] | ||
} |