Skip to content

Commit

Permalink
feat: Add SNMP community settings
Browse files Browse the repository at this point in the history
  • Loading branch information
vaerh committed Jul 13, 2023
1 parent fe378fc commit 4354c41
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/resources/routeros_snmp_community/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/snmp/community get [print show-ids]]
terraform import routeros_snmp_community.test "*0"
12 changes: 12 additions & 0 deletions examples/resources/routeros_snmp_community/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "routeros_snmp_community" "test" {
authentication_password = "authpasswd"
authentication_protocol = "MD5"
comment = "Comment"
disabled = true
encryption_password = "encpassword"
encryption_protocol = "DES"
name = "private"
read_access = true
security = "private"
write_access = true
}
4 changes: 4 additions & 0 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ func Provider() *schema.Provider {
// PPP
"routeros_ppp_profile": ResourcePPPProfile(),
"routeros_ppp_secret": ResourcePPPSecret(),

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

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

/*
{
".id": "*2",
"addresses": "::/0",
"authentication-password": "",
"authentication-protocol": "MD5",
"comment": "Comment",
"default": "false",
"disabled": "true",
"encryption-password": "",
"encryption-protocol": "DES",
"name": "private",
"read-access": "true",
"security": "none",
"write-access": "false"
}
*/

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

"addresses": {
Type: schema.TypeString,
Optional: true,
Default: "::/0",
Description: "Addresses from which connections to SNMP server is allowed.",
ValidateFunc: validation.IsIPAddress,
},
"authentication_password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: "Password used to authenticate the connection to the server (SNMPv3).",
},
"authentication_protocol": {
Type: schema.TypeString,
Optional: true,
Default: "MD5",
Description: "The protocol used for authentication (SNMPv3).",
ValidateFunc: validation.StringInSlice([]string{"MD5", "SHA1"}, false),
},
KeyComment: PropCommentRw,
"default": {
Type: schema.TypeBool,
Computed: true,
Description: "It's a default community.",
},
KeyDisabled: PropDisabledRw,
"encryption_password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: "The password used for encryption (SNMPv3).",
},
"encryption_protocol": {
Type: schema.TypeString,
Optional: true,
Default: "DES",
Description: "encryption protocol to be used to encrypt the communication (SNMPv3). AES (see rfc3826) " +
"available since v6.16.",
ValidateFunc: validation.StringInSlice([]string{"DES", "AES"}, false),
},

"name": {
Type: schema.TypeString,
Optional: true,
Description: "Community Name.",
},
"read_access": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Whether read access is enabled for this community.",
},
"security": {
Type: schema.TypeString,
Optional: true,
Default: "none",
Description: "Security features.",
ValidateFunc: validation.StringInSlice([]string{"authorized", "none", "private"}, false),
},
"write_access": {
Type: schema.TypeBool,
Optional: true,
Description: "Whether write access is enabled for this community.",
},
}

return &schema.Resource{
CreateContext: DefaultCreate(resSchema),
ReadContext: DefaultRead(resSchema),
UpdateContext: DefaultUpdate(resSchema),
DeleteContext: DefaultDelete(resSchema),

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

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

import (
"fmt"
"testing"

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

const testSNMPCommunityAddress = "routeros_snmp_community.test"

func TestAccSNMPCommunityTest_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,
CheckDestroy: testCheckResourceDestroy("/snmp/community", "routeros_snmp_community"),
Steps: []resource.TestStep{
{
Config: testAccSNMPCommunityConfig(),
Check: resource.ComposeTestCheckFunc(
testAccCheckSNMPCommunityExists(testSNMPCommunityAddress),
resource.TestCheckResourceAttr(testSNMPCommunityAddress, "name", "private"),
),
},
},
})
})
}
}

func testAccCheckSNMPCommunityExists(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 testAccSNMPCommunityConfig() string {
return providerConfig + `
resource "routeros_snmp_community" "test" {
authentication_password = "authpasswd"
authentication_protocol = "MD5"
comment = "Comment"
disabled = true
encryption_password = "encpassword"
encryption_protocol = "DES"
name = "private"
read_access = true
security = "private"
write_access = true
}`
}

0 comments on commit 4354c41

Please sign in to comment.