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

feat(tool): add new resource routeros_tool_email #632

Merged
merged 2 commits into from
Jan 3, 2025
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
1 change: 1 addition & 0 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ func Provider() *schema.Provider {

// Tools
"routeros_tool_bandwidth_server": ResourceToolBandwidthServer(),
"routeros_tool_email": ResourceToolEmail(),
"routeros_tool_mac_server": ResourceToolMacServer(),
"routeros_tool_mac_server_winbox": ResourceToolMacServerWinBox(),
"routeros_tool_netwatch": ResourceToolNetwatch(),
Expand Down
80 changes: 80 additions & 0 deletions routeros/resource_tool_email.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package routeros

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

/*
{
"from": "<>",
"password": "",
"port": "25",
"server": "0.0.0.0",
"tls": "no",
"user": "",
"vrf": "main"
}
*/

// https://help.mikrotik.com/docs/display/ROS/E-mail
func ResourceToolEmail() *schema.Resource {
resSchema := map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/tool/e-mail"),
MetaId: PropId(Id),

"from": {
Type: schema.TypeString,
Optional: true,
Description: "Name or email address that will be shown as a receiver.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: "Password used for authenticating to an SMTP server.",
},
"port": {
Type: schema.TypeString,
Optional: true,
Description: "SMTP server's port.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"server": {
Type: schema.TypeString,
Optional: true,
Description: "SMTP server's IP address.",
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"tls": {
Type: schema.TypeString,
Optional: true,
Description: "Whether to use TLS encryption:" +
"\n * yes - sends STARTTLS and drops the session if TLS is not available on the server" +
"\n * no - do not send STARTTLS" +
"\n * starttls - sends STARTTLS and continue without TLS if a server responds that TLS is not available",
ValidateFunc: validation.StringInSlice([]string{"yes", "no", "starttls"}, false),
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"user": {
Type: schema.TypeString,
Optional: true,
Description: "The username used for authenticating to an SMTP server.",
},
"vrf": PropVrfRw,
}

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

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

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

import (
"testing"

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

const testToolEmail = "routeros_tool_email.email"

func TestAccToolEmailTest_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("/tool/e-mail", "routeros_email"),
Steps: []resource.TestStep{
{
Config: testAccToolEmailConfig(),
Check: resource.ComposeTestCheckFunc(
testResourcePrimaryInstanceId(testToolEmail),
resource.TestCheckResourceAttr(testToolEmail, "from", "John Doe <[email protected]>"),
resource.TestCheckResourceAttr(testToolEmail, "password", "password"),
resource.TestCheckResourceAttr(testToolEmail, "port", "25"),
resource.TestCheckResourceAttr(testToolEmail, "server", "smtp.example.com"),
resource.TestCheckResourceAttr(testToolEmail, "tls", "yes"),
resource.TestCheckResourceAttr(testToolEmail, "user", "jdoe"),
resource.TestCheckResourceAttr(testToolEmail, "vrf", "main"),
),
},
},
})

})
}
}

func testAccToolEmailConfig() string {
return providerConfig + `
resource "routeros_tool_email" "email" {
from = "John Doe <[email protected]>"
password = "password"
port = "25"
server = "smtp.example.com"
tls = "yes"
user = "jdoe"
vrf = "main"
}
`
}