Skip to content

Commit

Permalink
Merge pull request #23 from browningluke/simplify_interface_conversion
Browse files Browse the repository at this point in the history
Add type conversion helper funcs for schema conversion
  • Loading branch information
browningluke authored Jul 29, 2023
2 parents 8cf0402 + 3b22949 commit f3cdfe9
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 175 deletions.
38 changes: 8 additions & 30 deletions internal/service/interfaces_vlan_schema.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package service

import (
"fmt"
"github.com/browningluke/opnsense-go/pkg/api"
"github.com/browningluke/opnsense-go/pkg/interfaces"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
Expand All @@ -13,7 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"strconv"
"terraform-provider-opnsense/internal/tools"
)

// InterfacesVlanResourceModel describes the resource data model.
Expand Down Expand Up @@ -112,40 +111,19 @@ func InterfacesVlanDataSourceSchema() dschema.Schema {
func convertInterfacesVlanSchemaToStruct(d *InterfacesVlanResourceModel) (*interfaces.Vlan, error) {
return &interfaces.Vlan{
Description: d.Description.ValueString(),
Tag: fmt.Sprintf("%d", d.Tag.ValueInt64()),
Priority: api.SelectedMap(fmt.Sprintf("%d", d.Priority.ValueInt64())),
Tag: tools.Int64ToString(d.Tag.ValueInt64()),
Priority: api.SelectedMap(tools.Int64ToString(d.Priority.ValueInt64())),
Parent: api.SelectedMap(d.Parent.ValueString()),
Device: d.Device.ValueString(),
}, nil
}

func convertInterfacesVlanStructToSchema(d *interfaces.Vlan) (*InterfacesVlanResourceModel, error) {
model := &InterfacesVlanResourceModel{
Description: types.StringNull(),
Tag: types.Int64Null(),
Priority: types.Int64Null(),
return &InterfacesVlanResourceModel{
Description: tools.StringOrNull(d.Description),
Tag: tools.StringToInt64Null(d.Tag),
Priority: tools.StringToInt64Null(d.Priority.String()),
Parent: types.StringValue(d.Parent.String()),
Device: types.StringValue(d.Device),
}

// Parse 'Description'
if d.Description != "" {
model.Description = types.StringValue(d.Description)
}

// Parse 'Tag'
tag, err := strconv.ParseInt(d.Tag, 10, 64)
if err != nil {
return nil, err
}
model.Tag = types.Int64Value(tag)

// Parse 'Priority'
priority, err := strconv.ParseInt(d.Priority.String(), 10, 64)
if err != nil {
return nil, err
}
model.Priority = types.Int64Value(priority)

return model, nil
}, nil
}
31 changes: 6 additions & 25 deletions internal/service/route_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"terraform-provider-opnsense/internal/tools"
)

// RouteResourceModel describes the resource data model.
Expand Down Expand Up @@ -85,39 +86,19 @@ func RouteDataSourceSchema() dschema.Schema {
}

func convertRouteSchemaToStruct(d *RouteResourceModel) (*routes.Route, error) {
// Convert 'Enabled' to 'Disabled'
var disabled string
if d.Enabled.ValueBool() {
disabled = "0"
} else {
disabled = "1"
}

return &routes.Route{
Disabled: disabled,
Disabled: tools.BoolToString(!d.Enabled.ValueBool()),
Description: d.Description.ValueString(),
Gateway: api.SelectedMap(d.Gateway.ValueString()),
Network: d.Network.ValueString(),
}, nil
}

func convertRouteStructToSchema(d *routes.Route) (*RouteResourceModel, error) {
model := &RouteResourceModel{
Enabled: types.BoolValue(true),
Description: types.StringNull(),
return &RouteResourceModel{
Enabled: types.BoolValue(!tools.StringToBool(d.Disabled)),
Description: tools.StringOrNull(d.Description),
Gateway: types.StringValue(d.Gateway.String()),
Network: types.StringValue(d.Network),
}

// Parse 'Disabled'
if d.Disabled == "1" {
model.Enabled = types.BoolValue(false)
}

// Parse 'Description'
if d.Description != "" {
model.Description = types.StringValue(d.Description)
}

return model, nil
}, nil
}
31 changes: 6 additions & 25 deletions internal/service/unbound_domain_override_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"terraform-provider-opnsense/internal/tools"
)

// UnboundDomainOverrideResourceModel describes the resource data model.
Expand Down Expand Up @@ -84,39 +85,19 @@ func UnboundDomainOverrideDataSourceSchema() dschema.Schema {
}

func convertUnboundDomainOverrideSchemaToStruct(d *UnboundDomainOverrideResourceModel) (*unbound.DomainOverride, error) {
// Parse 'Enabled'
var enabled string
if d.Enabled.ValueBool() {
enabled = "1"
} else {
enabled = "0"
}

return &unbound.DomainOverride{
Enabled: enabled,
Enabled: tools.BoolToString(d.Enabled.ValueBool()),
Domain: d.Domain.ValueString(),
Server: d.Server.ValueString(),
Description: d.Description.ValueString(),
}, nil
}

func convertUnboundDomainOverrideStructToSchema(d *unbound.DomainOverride) (*UnboundDomainOverrideResourceModel, error) {
model := &UnboundDomainOverrideResourceModel{
Enabled: types.BoolValue(false),
return &UnboundDomainOverrideResourceModel{
Enabled: types.BoolValue(tools.StringToBool(d.Enabled)),
Domain: types.StringValue(d.Domain),
Server: types.StringValue(d.Server),
Description: types.StringNull(),
}

// Parse 'Enabled'
if d.Enabled == "1" {
model.Enabled = types.BoolValue(true)
}

// Parse 'Description'
if d.Description != "" {
model.Description = types.StringValue(d.Description)
}

return model, nil
Description: tools.StringOrNull(d.Description),
}, nil
}
36 changes: 7 additions & 29 deletions internal/service/unbound_forward_schema.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package service

import (
"fmt"
"github.com/browningluke/opnsense-go/pkg/unbound"
dschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand All @@ -11,7 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"strconv"
"terraform-provider-opnsense/internal/tools"
)

// UnboundForwardResourceModel describes the resource data model.
Expand Down Expand Up @@ -101,42 +100,21 @@ func UnboundForwardDataSourceSchema() dschema.Schema {
}

func convertUnboundForwardSchemaToStruct(d *UnboundForwardResourceModel) (*unbound.Forward, error) {
// Parse 'Enabled'
var enabled string
if d.Enabled.ValueBool() {
enabled = "1"
} else {
enabled = "0"
}

return &unbound.Forward{
Enabled: enabled,
Enabled: tools.BoolToString(d.Enabled.ValueBool()),
Domain: d.Domain.ValueString(),
Server: d.ServerIP.ValueString(),
Port: fmt.Sprintf("%d", d.ServerPort.ValueInt64()),
Port: tools.Int64ToString(d.ServerPort.ValueInt64()),
VerifyCN: d.VerifyCN.ValueString(),
}, nil
}

func convertUnboundForwardStructToSchema(d *unbound.Forward) (*UnboundForwardResourceModel, error) {
// Parse 'ServerPort'
serverPort, err := strconv.ParseInt(d.Port, 10, 64)
if err != nil {
return nil, err
}

model := &UnboundForwardResourceModel{
Enabled: types.BoolValue(false),
return &UnboundForwardResourceModel{
Enabled: types.BoolValue(tools.StringToBool(d.Enabled)),
Domain: types.StringValue(d.Domain),
ServerIP: types.StringValue(d.Server),
ServerPort: types.Int64Value(serverPort),
ServerPort: types.Int64Value(tools.StringToInt64(d.Port)),
VerifyCN: types.StringValue(d.VerifyCN),
}

// Parse 'Enabled'
if d.Enabled == "1" {
model.Enabled = types.BoolValue(true)
}

return model, nil
}, nil
}
31 changes: 6 additions & 25 deletions internal/service/unbound_host_alias_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"terraform-provider-opnsense/internal/tools"
)

// UnboundHostAliasResourceModel describes the resource data model.
Expand Down Expand Up @@ -97,16 +98,8 @@ func UnboundHostAliasDataSourceSchema() dschema.Schema {
}

func convertUnboundHostAliasSchemaToStruct(d *UnboundHostAliasResourceModel) (*unbound.HostAlias, error) {
// Parse 'Enabled'
var enabled string
if d.Enabled.ValueBool() {
enabled = "1"
} else {
enabled = "0"
}

return &unbound.HostAlias{
Enabled: enabled,
Enabled: tools.BoolToString(d.Enabled.ValueBool()),
Host: api.SelectedMap(d.Override.ValueString()),
Hostname: d.Hostname.ValueString(),
Domain: d.Domain.ValueString(),
Expand All @@ -115,23 +108,11 @@ func convertUnboundHostAliasSchemaToStruct(d *UnboundHostAliasResourceModel) (*u
}

func convertUnboundHostAliasStructToSchema(d *unbound.HostAlias) (*UnboundHostAliasResourceModel, error) {
model := &UnboundHostAliasResourceModel{
Enabled: types.BoolValue(false),
return &UnboundHostAliasResourceModel{
Enabled: types.BoolValue(tools.StringToBool(d.Enabled)),
Hostname: types.StringValue(d.Hostname),
Domain: types.StringValue(d.Domain),
Description: types.StringValue(d.Description),
Description: tools.StringOrNull(d.Description),
Override: types.StringValue(d.Host.String()),
}

// Parse 'Enabled'
if d.Enabled == "1" {
model.Enabled = types.BoolValue(true)
}

// Parse 'Description'
if d.Description != "" {
model.Description = types.StringValue(d.Description)
}

return model, nil
}, nil
}
49 changes: 8 additions & 41 deletions internal/service/unbound_host_override_schema.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package service

import (
"fmt"
"github.com/browningluke/opnsense-go/pkg/api"
"github.com/browningluke/opnsense-go/pkg/unbound"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
Expand All @@ -16,7 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"strconv"
"terraform-provider-opnsense/internal/tools"
)

// UnboundHostOverrideResourceModel describes the resource data model.
Expand Down Expand Up @@ -151,59 +150,27 @@ func UnboundHostOverrideDataSourceSchema() dschema.Schema {
}

func convertUnboundHostOverrideSchemaToStruct(d *UnboundHostOverrideResourceModel) (*unbound.HostOverride, error) {
// Parse 'Enabled'
var enabled string
if d.Enabled.ValueBool() {
enabled = "1"
} else {
enabled = "0"
}

// Parse 'MXPriority'
mxPriority := fmt.Sprintf("%d", d.MXPriority.ValueInt64())
if d.MXPriority.ValueInt64() == -1 {
mxPriority = ""
}

return &unbound.HostOverride{
Enabled: enabled,
Enabled: tools.BoolToString(d.Enabled.ValueBool()),
Hostname: d.Hostname.ValueString(),
Domain: d.Domain.ValueString(),
Type: api.SelectedMap(d.Type.ValueString()),
Server: d.Server.ValueString(),
MXDomain: d.MXDomain.ValueString(),
MXPriority: mxPriority,
MXPriority: tools.Int64ToStringNegative(d.MXPriority.ValueInt64()),
Description: d.Description.ValueString(),
}, nil
}

func convertUnboundHostOverrideStructToSchema(d *unbound.HostOverride) (*UnboundHostOverrideResourceModel, error) {
model := &UnboundHostOverrideResourceModel{
Enabled: types.BoolValue(false),
return &UnboundHostOverrideResourceModel{
Enabled: types.BoolValue(tools.StringToBool(d.Enabled)),
Hostname: types.StringValue(d.Hostname),
Domain: types.StringValue(d.Domain),
Type: types.StringValue(d.Type.String()),
Server: types.StringValue(d.Server),
MXPriority: types.Int64Value(-1),
MXPriority: types.Int64Value(tools.StringToInt64(d.MXPriority)),
MXDomain: types.StringValue(d.MXDomain),
Description: types.StringNull(),
}

// Parse 'Enabled'
if d.Enabled == "1" {
model.Enabled = types.BoolValue(true)
}

// Parse 'MXPriority'
mxPriority, err := strconv.ParseInt(d.MXPriority, 10, 64)
if err == nil {
model.MXPriority = types.Int64Value(mxPriority)
}

// Parse 'Description'
if d.Description != "" {
model.Description = types.StringValue(d.Description)
}

return model, nil
Description: tools.StringOrNull(d.Description),
}, nil
}
Loading

0 comments on commit f3cdfe9

Please sign in to comment.