Skip to content

Commit

Permalink
feat: Support for veth interfaces #206
Browse files Browse the repository at this point in the history
  • Loading branch information
vaerh committed May 14, 2023
1 parent 18f4bf1 commit a6fdcf8
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/resources/routeros_interface_veth/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 [/interface/veth get [print show-ids]]
terraform import routeros_interface_veth.test "*0"
6 changes: 6 additions & 0 deletions examples/resources/routeros_interface_veth/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resource "routeros_interface_veth" "test" {
name = "veth-test"
address = "192.168.120.2/24"
gateway = "192.168.120.1"
comment = "Virtual interface"
}
1 change: 1 addition & 0 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func Provider() *schema.Provider {
"routeros_interface_list": ResourceInterfaceList(),
"routeros_interface_list_member": ResourceInterfaceListMember(),
"routeros_interface_ovpn_server": ResourceInterfaceOpenVPNServer(),
"routeros_interface_veth": ResourceInterfaceVeth(),

// Aliases for interface objects to retain compatibility between original and fork
"routeros_bridge": ResourceInterfaceBridge(),
Expand Down
56 changes: 56 additions & 0 deletions routeros/resource_interface_veth.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"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

/*
{
".id": "*8",
"address": "192.168.100.2/24",
"comment": "comment",
"disabled": "false",
"gateway": "192.168.100.1",
"name": "veth1",
"running": "true"
}
*/

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

"address": {
Type: schema.TypeString,
Required: true,
Description: "IP address.",
ValidateFunc: validation.IsCIDR,
},
KeyComment: PropCommentRw,
KeyDisabled: PropDisabledRw,
"gateway": {
Type: schema.TypeString,
Optional: true,
Description: "Gateway IP address.",
ValidateFunc: validation.IsIPv4Address,
},
KeyName: PropName("Interface name."),
KeyRunning: PropRunningRo,
}

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

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

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

import (
"fmt"
"testing"

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

const testInterfaceVethAddress = "routeros_interface_veth.test"

func TestAccInterfaceVethTest_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("/interface/veth", "routeros_interface_veth"),
Steps: []resource.TestStep{
{
Config: testAccInterfaceVethConfig(),
Check: resource.ComposeTestCheckFunc(
testAccCheckInterfaceVethExists(testInterfaceVethAddress),
resource.TestCheckResourceAttr(testInterfaceVethAddress, "name", "veth-test"),
),
},
},
})
})
}
}

func testAccCheckInterfaceVethExists(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 testAccInterfaceVethConfig() string {
return providerConfig + `
resource "routeros_interface_veth" "test" {
name = "veth-test"
address = "192.168.120.2/24"
gateway = "192.168.120.1"
comment = "Virtual interface"
}
`
}

0 comments on commit a6fdcf8

Please sign in to comment.