Skip to content

Commit

Permalink
added ipv6 filter data entry
Browse files Browse the repository at this point in the history
Signed-off-by: dn <dn@nuvotex.de>
  • Loading branch information
danielnachtrub committed Jun 17, 2024
1 parent f081bbf commit 97c4ff1
Show file tree
Hide file tree
Showing 9 changed files with 350 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,39 @@ package routeros

import (
"context"

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

var firewallSections = []string{"address_list", "nat", "mangle", "rules"}
var ipfirewallSections = []string{"address_list", "nat", "mangle", "rules"}

func DatasourceFirewall() *schema.Resource {
func DatasourceIPFirewall() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceFirewallFilterRead,
ReadContext: datasourceIPFirewallFilterRead,
Description: `This datasource contains all supported firewall resources:
- address_list
- nat
- mangle
- rules (aka filter)
`,
Schema: map[string]*schema.Schema{
"address_list": getFirewallAddrListSchema(),
"mangle": getFirewallMangleSchema(),
"nat": getFirewallNatSchema(),
"rules": getFirewallFilterSchema(),
"address_list": getIPFirewallAddrListSchema(),
"mangle": getIPFirewallMangleSchema(),
"nat": getIPFirewallNatSchema(),
"rules": getIPFirewallFilterSchema(),
},
}
}

func datasourceFirewallFilterRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
func datasourceIPFirewallFilterRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
basePath := "/ip/firewall/"

s := DatasourceFirewall().Schema
s := DatasourceIPFirewall().Schema

var isEmpty = true
for _, section := range firewallSections {
for _, section := range ipfirewallSections {
isEmpty = isEmpty && len(d.Get(section).([]interface{})) == 0
}

Expand All @@ -48,7 +49,7 @@ func datasourceFirewallFilterRead(ctx context.Context, d *schema.ResourceData, m
}
}

for _, section := range firewallSections {
for _, section := range ipfirewallSections {
if len(d.Get(section).([]interface{})) == 0 {
continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package routeros

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

func getFirewallAddrListSchema() *schema.Schema {
func getIPFirewallAddrListSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Computed: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package routeros

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

func getFirewallFilterSchema() *schema.Schema {
func getIPFirewallFilterSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Computed: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func getFirewallMangleSchema() *schema.Schema {
func getIPFirewallMangleSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Computed: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func getFirewallNatSchema() *schema.Schema {
func getIPFirewallNatSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Computed: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

const testDatasourceFirewall = "data.routeros_firewall.fw"
const testDatasourceIpFirewall = "data.routeros_ip_firewall.fw"

func TestAccDatasourceFirewallTest_basic(t *testing.T) {
func TestAccDatasourceIpFirewallTest_basic(t *testing.T) {
for _, name := range testNames {
t.Run(name, func(t *testing.T) {
resource.Test(t, resource.TestCase{
Expand All @@ -19,9 +19,9 @@ func TestAccDatasourceFirewallTest_basic(t *testing.T) {
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDatasourceFirewallConfig(),
Config: testAccDatasourceIpFirewallConfig(),
Check: resource.ComposeTestCheckFunc(
testResourcePrimaryInstanceId(testDatasourceFirewall),
testResourcePrimaryInstanceId(testDatasourceIpFirewall),
),
},
},
Expand All @@ -31,7 +31,7 @@ func TestAccDatasourceFirewallTest_basic(t *testing.T) {
}
}

func testAccDatasourceFirewallConfig() string {
func testAccDatasourceIpFirewallConfig() string {
return providerConfig + `
data "routeros_firewall" "fw" {
address_list {}
Expand Down
76 changes: 76 additions & 0 deletions routeros/datasource_ipv6_firewall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package routeros

import (
"context"

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

var ipv6firewallSections = []string{"rules"}

func DatasourceIPv6Firewall() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceIPv6FirewallFilterRead,
Description: `This datasource contains all supported firewall resources:
- rules (aka filter)
`,
Schema: map[string]*schema.Schema{
"rules": getIPv6FirewallFilterSchema(),
},
}
}

func datasourceIPv6FirewallFilterRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
basePath := "/ipv6/firewall/"

s := DatasourceIPv6Firewall().Schema

var isEmpty = true
for _, section := range ipv6firewallSections {
isEmpty = isEmpty && len(d.Get(section).([]interface{})) == 0
}

if isEmpty {
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: "You must specify at least one return section of the resource.",
Detail: "Please specify one or more sections of the firewall, information from which will be " +
"returned as a result of the data source query: rules{}, nat { filter = {...}}, etc.",
},
}
}

for _, section := range ipv6firewallSections {
if len(d.Get(section).([]interface{})) == 0 {
continue
}

path := basePath
// The filtering section is named 'rules' to avoid confusion: filter { filter = { ... }}.
if section == "rules" {
path += "filter"
} else {
// Kebab case!
path += SnakeToKebab(section)
}

// Snake case!
var res []MikrotikItem

for _, sectionResourceData := range d.Get(section).([]interface{}) {
filter := sectionResourceData.(map[string]interface{})[KeyFilter].(map[string]interface{})

r, err := ReadItemsFiltered(buildReadFilter(filter), path, m.(Client))
if err != nil {
return diag.FromErr(err)
}

res = append(res, *r...)
}
diags = append(diags, MikrotikResourceDataToTerraformDatasource(&res, section, s, d)...)
}
return diags
}
Loading

0 comments on commit 97c4ff1

Please sign in to comment.