-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support for veth interfaces #206
- Loading branch information
Showing
5 changed files
with
128 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,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" |
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,6 @@ | ||
resource "routeros_interface_veth" "test" { | ||
name = "veth-test" | ||
address = "192.168.120.2/24" | ||
gateway = "192.168.120.1" | ||
comment = "Virtual interface" | ||
} |
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,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, | ||
} | ||
} |
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,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" | ||
} | ||
` | ||
} |