Skip to content

Commit

Permalink
Merge pull request #233 from mrhamburg/pr/mrhamburg/227
Browse files Browse the repository at this point in the history
feat: OSPF resource
  • Loading branch information
vaerh committed Jun 19, 2023
2 parents 9e60752 + f222f71 commit f615304
Show file tree
Hide file tree
Showing 17 changed files with 632 additions and 12 deletions.
3 changes: 3 additions & 0 deletions examples/resources/routeros_routing_ospf_area/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 -> /routing/ospf/area/print show-ids
terraform import routeros_routing_ospf_area.test_routing_ospf_area "*0"
8 changes: 8 additions & 0 deletions examples/resources/routeros_routing_ospf_area/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "routeros_routing_ospf_instance" "test_routing_ospf_instance" {
name = "test_routing_ospf_instance"
}

resource "routeros_routing_ospf_area" "test_routing_ospf_area" {
name = "test_routing_ospf_area"
instance = routeros_routing_ospf_instance.test_routing_ospf_instance.name
}
3 changes: 3 additions & 0 deletions examples/resources/routeros_routing_ospf_instance/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 -> /routing/ospf/instance/print show-ids
terraform import routeros_routing_ospf_instance.test_routing_ospf_instance "*0"
3 changes: 3 additions & 0 deletions examples/resources/routeros_routing_ospf_instance/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "routeros_routing_ospf_instance" "test_routing_ospf_instance" {
name = "test_routing_ospf_instance"
}
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 -> /routing/ospf/interface-template/print show-ids
terraform import routeros_routing_ospf_interface_template.test_routing_ospf_interface_template "*0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "routeros_routing_ospf_instance" "test_routing_ospf_instance" {
name = "test_routing_ospf_instance"
}

resource "routeros_routing_ospf_area" "test_routing_ospf_area" {
name = "test_routing_ospf_area"
instance = routeros_routing_ospf_instance.test_routing_ospf_instance.name
}

resource "routeros_routing_ospf_interface_template" "test_routing_ospf_interface_template" {
area = routeros_routing_ospf_area.test_routing_ospf_area.name
}
5 changes: 5 additions & 0 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ func Provider() *schema.Provider {
"routeros_routing_bgp_connection": ResourceRoutingBGPConnection(),
"routeros_routing_bgp_template": ResourceRoutingBGPTemplate(),

// OSPF
"routeros_routing_ospf_instance": ResourceRoutingOspfInstance(),
"routeros_routing_ospf_area": ResourceRoutingOspfArea(),
"routeros_routing_ospf_interface_template": ResourceRoutingOspfInterfaceTemplate(),

// VPN
"routeros_ovpn_server": ResourceOpenVPNServer(),

Expand Down
5 changes: 5 additions & 0 deletions routeros/provider_schema_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
KeyDynamic = "dynamic"
KeyDisabled = "disabled"
KeyFilter = "filter"
KeyInactive = "inactive"
KeyInterface = "interface"
KeyInvalid = "invalid"
KeyL2Mtu = "l2mtu"
Expand Down Expand Up @@ -144,6 +145,10 @@ var (
Elem: schema.TypeString,
Description: "Additional request filtering options.",
}
PropInactiveRo = &schema.Schema{
Type: schema.TypeBool,
Computed: true,
}
PropInterfaceRw = &schema.Schema{
Type: schema.TypeString,
Required: true,
Expand Down
5 changes: 1 addition & 4 deletions routeros/resource_interface_bridge_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,7 @@ func ResourceInterfaceBridgePort() *schema.Resource {
Computed: true,
Description: "Switch chip used by the port.",
},
"inactive": {
Type: schema.TypeBool,
Computed: true,
},
KeyInactive: PropInactiveRo,
"ingress_filtering": {
Type: schema.TypeBool,
Optional: true,
Expand Down
5 changes: 1 addition & 4 deletions routeros/resource_ip_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ func ResourceIPRoute() *schema.Resource {
Computed: true,
Description: "Shows actual (resolved) gateway and interface that will be used for packet forwarding.",
},
"inactive": {
Type: schema.TypeBool,
Computed: true,
},
KeyInactive: PropInactiveRo,
"pref_src": {
Type: schema.TypeString,
Optional: true,
Expand Down
5 changes: 1 addition & 4 deletions routeros/resource_ipv6_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ func ResourceIPv6Route() *schema.Resource {
Computed: true,
Description: "Shows actual (resolved) gateway and interface that will be used for packet forwarding.",
},
"inactive": {
Type: schema.TypeBool,
Computed: true,
},
KeyInactive: PropInactiveRo,
"pref_src": {
Type: schema.TypeString,
Optional: true,
Expand Down
78 changes: 78 additions & 0 deletions routeros/resource_routing_ospf_area.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package routeros

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

/*
{
".id": "*54",
"name": "",
"area-id": "",
"default-cost": "",
"instance": "",
"no-summaries": "",
"nssa-translate": "",
"type": "",
}
*/

// ResourceRoutingOspfArea https://help.mikrotik.com/docs/display/ROS/OSPF
func ResourceRoutingOspfArea() *schema.Resource {
resSchema := map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/routing/ospf/area"),
MetaId: PropId(Id),

"area_id": {
Type: schema.TypeString,
Default: "0.0.0.0",
Optional: true,
Description: "OSPF area identifier.",
},
KeyComment: PropCommentRw,
"default_cost": {
Type: schema.TypeInt,
Optional: true,
Description: "Default cost of injected LSAs into the area.",
},
KeyDisabled: PropDisabledRw,
KeyInactive: PropInactiveRo,
"instance": {
Type: schema.TypeString,
Required: true,
Description: "Name of the OSPF instance this area belongs to.",
},
KeyName: PropNameForceNewRw,
"no_summaries": {
Type: schema.TypeBool,
Optional: true,
Description: "If set then the area will not flood summary LSAs in the stub area.",
},
"nssa_translate": {
Type: schema.TypeString,
Optional: true,
Description: "The parameter indicates which ABR will be used as a translator from type7 to type5 LSA.",
ValidateFunc: validation.StringInSlice([]string{"no", "yes", "candidate"}, false),
},
"type": {
Type: schema.TypeString,
Optional: true,
Default: "default",
Description: "The area type.",
ValidateFunc: validation.StringInSlice([]string{"default", "nssa", "stub"}, false),
},
}

return &schema.Resource{
CreateContext: DefaultCreate(resSchema),
ReadContext: DefaultRead(resSchema),
UpdateContext: DefaultUpdate(resSchema),
DeleteContext: DefaultDelete(resSchema),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

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

import (
"fmt"
"testing"

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

const testRoutingOspfArea = "routeros_routing_ospf_area.test_routing_ospf_area"

func TestAccRoutingOspfInstanceArea_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("/routing/ospf/area", "routeros_routing_ospf_area"),
Steps: []resource.TestStep{
{
Config: testAccCheckRoutingOspfAreaConfig(),
Check: resource.ComposeTestCheckFunc(
testAccCheckRoutingOspfAreaExists(testRoutingOspfArea),
resource.TestCheckResourceAttr(testRoutingOspfArea, "name", "test_routing_ospf_area"),
),
},
},
})

})
}
}

func testAccCheckRoutingOspfAreaExists(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 testAccCheckRoutingOspfAreaConfig() string {
return providerConfig + `
resource "routeros_routing_ospf_instance" "test_routing_ospf_instance" {
name = "test_routing_ospf_instance"
disabled = false
}
resource "routeros_routing_ospf_area" "test_routing_ospf_area" {
name = "test_routing_ospf_area"
disabled = true
instance = routeros_routing_ospf_instance.test_routing_ospf_instance.name
}
`
}
129 changes: 129 additions & 0 deletions routeros/resource_routing_ospf_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package routeros

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

/*
{
".id": "*54",
"name": "",
"domain-id": "",
"domain-tag": "",
"in-filter": "",
"mpls-te-address": "",
"mpls-te-area": "",
"originate-default": "",
"out-filter-chain": "",
"out-filter-select": "",
"redistribute": "",
"router-id": "",
"version": "",
"vrf": "",
"use-dn": "" // is not present in version 7.9
}
*/

// ResourceRoutingOspfInstance https://help.mikrotik.com/docs/display/ROS/OSPF
func ResourceRoutingOspfInstance() *schema.Resource {
resSchema := map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/routing/ospf/instance"),
MetaId: PropId(Id),

KeyComment: PropCommentRw,
KeyDisabled: PropDisabledRw,
"domain_id": {
Type: schema.TypeString,
Optional: true,
Description: "MPLS-related parameter.",
},
"domain_tag": {
Type: schema.TypeInt,
Optional: true,
Description: "if set, then used in route redistribution (as route-tag in all external LSAs " +
"generated by this router), and in route calculation (all external LSAs having this route " +
"tag are ignored). Needed for interoperability with older Cisco systems. By default not set.",
},
KeyInactive: PropInactiveRo,
"in_filter_chain": {
Type: schema.TypeString,
Optional: true,
Description: "name of the routing filter chain used for incoming prefixes",
},
"mpls_te_address": {
Type: schema.TypeString,
Optional: true,
Description: "the area used for MPLS traffic engineering.",
},
"mpls_te_area": {
Type: schema.TypeString,
Optional: true,
Description: "the area used for MPLS traffic engineering.",
},
KeyName: PropNameForceNewRw,
"originate_default": {
Type: schema.TypeString,
Optional: true,
Description: "Specifies default route (0.0.0.0/0) distribution method.",
ValidateFunc: validation.StringInSlice([]string{"always", "if-installed", "never"}, false),
},
"out_filter_chain": {
Type: schema.TypeString,
Optional: true,
Description: "name of the routing filter chain used for outgoing prefixes filtering.",
},
"out_filter_select": {
Type: schema.TypeString,
Optional: true,
Description: "name of the routing filter select chain, used for output selection.",
},
"redistribute": {
Type: schema.TypeSet,
Optional: true,
Description: "Enable redistribution of specific route types.",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: ValidationMultiValInSlice([]string{"bgp", "connected", "copy", "dhcp", "fantasy", "modem",
"ospf", "rip", "static", "vpn"}, false, false),
},
},
"router_id": {
Type: schema.TypeString,
Optional: true,
Default: "main",
Description: "OSPF Router ID. Can be set explicitly as an IP address, or as the name " +
"of the router-id instance.",
},
"routing_table": {
Type: schema.TypeString,
Optional: true,
Description: "Name of the routing table in use.",
},
"version": {
Type: schema.TypeInt,
Optional: true,
Default: 2,
Description: "OSPF version this instance will be running (v2 for IPv4, v3 for IPv6).",
ValidateFunc: validation.IntBetween(2, 3),
},
"vrf": {
Type: schema.TypeString,
Optional: true,
Default: "main",
Description: "The VRF table this OSPF instance operates on",
},
}

return &schema.Resource{
CreateContext: DefaultCreate(resSchema),
ReadContext: DefaultRead(resSchema),
UpdateContext: DefaultUpdate(resSchema),
DeleteContext: DefaultDelete(resSchema),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: resSchema,
}
}
Loading

0 comments on commit f615304

Please sign in to comment.