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

Vaerh/issue593 #594

Merged
merged 3 commits into from
Nov 13, 2024
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
5 changes: 5 additions & 0 deletions examples/resources/routeros_interface_6to4/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#The ID can be found via API or the terminal
#The command for the terminal is -> :put [/interface/6to4 get [print show-ids]]
terraform import routeros_interface_6to4.test *3
#Or you can import a resource using one of its attributes
terraform import routeros_interface_6to4.test "name=6to4-tunnel1"
4 changes: 4 additions & 0 deletions examples/resources/routeros_interface_6to4/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "routeros_interface_6to4" "test" {
name = "6to4-tunnel1"
keepalive = "10,10"
}
1 change: 1 addition & 0 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func Provider() *schema.Provider {
"routeros_dns_record": ResourceDnsRecord(),

// Interface Objects
"routeros_interface_6to4": ResourceInterface6to4(),
"routeros_interface_bonding": ResourceInterfaceBonding(),
"routeros_interface_bridge_port": ResourceInterfaceBridgePort(),
"routeros_interface_bridge_settings": ResourceInterfaceBridgeSettings(),
Expand Down
11 changes: 7 additions & 4 deletions routeros/provider_schema_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ var (
PropKeepaliveRw = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "10s,10",
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^(\d+[smhdw]?)+(,\d+)?$`),
"value must be integer[/time],integer 0..4294967295 (https://help.mikrotik.com/docs/display/ROS/GRE)"),
Description: "Tunnel keepalive parameter sets the time interval in which the tunnel running flag will " +
Expand All @@ -346,14 +345,18 @@ var (
return true
}

if AlwaysPresentNotUserProvided(k, old, new, d) {
return true
}

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

o := strings.Split(old, ",")
n := strings.Split(new, ",")
if len(o) != 2 || len(n) != 2 {
panic(fmt.Sprintf("[GRE keepalive] wrong keepalive format, old: '%v', new: '%v'", old, new))
panic(fmt.Sprintf("[Keepalive] wrong keepalive format, old: '%v', new: '%v'", old, new))
}

// Compare keepalive retries.
Expand All @@ -364,12 +367,12 @@ var (
// Compare keepalive intervals.
oDuration, err := ParseDuration(o[0])
if err != nil {
panic("[GRE keepalive] parse 'old' duration error: " + err.Error())
panic("[Keepalive] parse 'old' duration error: " + err.Error())
}

nDuration, err := ParseDuration(n[0])
if err != nil {
panic("[GRE keepalive] parse 'new' duration error: " + err.Error())
panic("[Keepalive] parse 'new' duration error: " + err.Error())
}

return oDuration.Seconds() == nDuration.Seconds()
Expand Down
56 changes: 56 additions & 0 deletions routeros/resource_interface_6to4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package routeros

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

/*
{
".id": "*3",
"actual-mtu": "1480",
"clamp-tcp-mss": "true",
"disabled": "false",
"dont-fragment": "no",
"dscp": "inherit",
"local-address": "0.0.0.0",
"mtu": "auto",
"name": "6to4-tunnel1",
"remote-address": "unspecified",
"running": "true"
}
*/

// https://help.mikrotik.com/docs/spaces/ROS/pages/135004174/6to4
func ResourceInterface6to4() *schema.Resource {
resSchema := map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/interface/6to4"),
MetaId: PropId(Id),

KeyActualMtu: PropActualMtuRo,
KeyClampTcpMss: PropClampTcpMssRw,
KeyComment: PropCommentRw,
KeyDisabled: PropDisabledRw,
KeyDontFragment: PropDontFragmentRw,
KeyDscp: PropDscpRw,
KeyIpsecSecret: PropIpsecSecretRw,
KeyKeepalive: PropKeepaliveRw,
KeyLocalAddress: PropLocalAddressRw,
KeyMtu: PropMtuRw(),
KeyName: PropName("Interface name."),
KeyRemoteAddress: PropRemoteAddressRw,
KeyRunning: PropRunningRo,
}

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

Importer: &schema.ResourceImporter{
StateContext: ImportStateCustomContext(resSchema),
},

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

import (
"fmt"
"testing"

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

const testInterface6to4 = "routeros_interface_6to4.test"

func TestAccInterface6to4Test_basic(t *testing.T) {
t.Parallel()
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("/interface/6to4", "routeros_interface_6to4"),
Steps: []resource.TestStep{
{
Config: testAccInterface6to4Config(),
Check: resource.ComposeTestCheckFunc(
testResourcePrimaryInstanceId(testInterface6to4),
resource.TestCheckResourceAttr(testInterface6to4, "name", "6to4-tunnel1"),
resource.TestCheckResourceAttr(testInterface6to4, "keepalive", "10s,10"),
),
},
},
})

})
}
}

func testAccInterface6to4Config() string {
return fmt.Sprintf(`%v

resource "routeros_interface_6to4" "test" {
name = "6to4-tunnel1"
keepalive = "10,10"
}
`, providerConfig)
}
Loading