Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate bridge_vlan resource to PluginFramework #193

Merged
10 changes: 5 additions & 5 deletions client/bridge_vlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (

// BridgeVlan defines vlan filtering in bridge resource
type BridgeVlan struct {
Id string `mikrotik:".id"`
Bridge string `mikrotik:"bridge"`
Tagged types.MikrotikList `mikrotik:"tagged"`
Untagged types.MikrotikList `mikrotik:"untagged"`
VlanIds types.MikrotikIntList `mikrotik:"vlan-ids"`
Id string `mikrotik:".id" codegen:"id,mikrotikID"`
Bridge string `mikrotik:"bridge" codegen:"bridge,required"`
Tagged types.MikrotikList `mikrotik:"tagged" codegen:"tagged,elemType=String"`
Untagged types.MikrotikList `mikrotik:"untagged" codegen:"untagged,elemType=String"`
VlanIds types.MikrotikIntList `mikrotik:"vlan-ids" codegen:"vlan_ids,elemType=Int64"`
}

var _ Resource = (*BridgeVlan)(nil)
Expand Down
10 changes: 5 additions & 5 deletions docs/resources/bridge_vlan.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# mikrotik_bridge_vlan (Resource)
Adds VLAN aware Layer2 forwarding and VLAN tag modifications within the bridge.
Creates a MikroTik BridgeVlan.

## Example Usage
```terraform
Expand All @@ -24,13 +24,13 @@ resource "mikrotik_bridge_vlan" "testacc" {

### Optional

- `tagged` (List of String) Interface list with a VLAN tag adding action in egress.
- `untagged` (List of String) Interface list with a VLAN tag removing action in egress.
- `vlan_ids` (List of Number) The list of VLAN IDs for certain port configuration. Ranges are not supported yet.
- `tagged` (Set of String) Interface list with a VLAN tag adding action in egress.
- `untagged` (Set of String) Interface list with a VLAN tag removing action in egress.
- `vlan_ids` (Set of Number) The list of VLAN IDs for certain port configuration. Ranges are not supported yet.

### Read-Only

- `id` (String) The ID of this resource.
- `id` (String) A unique ID for this resource.

## Import
Import is supported using the following syntax:
Expand Down
63 changes: 57 additions & 6 deletions mikrotik/internal/utils/struct_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,41 @@ func coreTypeToTerraformType(src, dest reflect.Value) error {
case reflect.Float32, reflect.Float64:
tfValue = tftypes.Float64Value(src.Float())
case reflect.Slice:
var diag diag.Diagnostics
var diags diag.Diagnostics
var elements []interface{}
for i := 0; i < src.Len(); i++ {
elements = append(elements, src.Index(i).Interface())
}
var tfType attr.Type
switch kind := src.Type().Elem().Kind(); kind {
case reflect.Bool:
tfValue, diag = tftypes.ListValueFrom(context.TODO(), tftypes.BoolType, elements)
tfType = tftypes.BoolType
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
tfValue, diag = tftypes.ListValueFrom(context.TODO(), tftypes.Int64Type, elements)
tfType = tftypes.Int64Type
case reflect.String:
tfValue, diag = tftypes.ListValueFrom(context.TODO(), tftypes.StringType, elements)
tfType = tftypes.StringType
default:
return fmt.Errorf("unsupported slice element type %q", kind)
}
var valueFromFunc func(t attr.Type, elements []interface{}) (attr.Value, diag.Diagnostics)

if diag.HasError() {
return fmt.Errorf("error creating Terraform type: %v", diag.Errors())
switch dest.Interface().(type) {
case tftypes.List:
valueFromFunc = func(t attr.Type, elements []interface{}) (attr.Value, diag.Diagnostics) {
return tftypes.ListValueFrom(context.TODO(), t, elements)
}
case tftypes.Set:
valueFromFunc = func(t attr.Type, elements []interface{}) (attr.Value, diag.Diagnostics) {
return tftypes.SetValueFrom(context.TODO(), t, elements)
}
default:
return fmt.Errorf("unsupported destination Terraform type %v", reflect.TypeOf(dest).Name())
}

tfValue, diags = valueFromFunc(tfType, elements)

if diags.HasError() {
return fmt.Errorf("error creating Terraform type: %v", diags.Errors())
}
}

Expand Down Expand Up @@ -165,6 +182,40 @@ func terraformTypeToCoreType(src, dest reflect.Value) error {

dest.Set(targetPtr.Elem())

return nil
case tftypes.Set:
var diag diag.Diagnostics
var sliceType reflect.Type

switch dest.Type().Elem().Kind() {
case reflect.Bool:
sliceType = reflect.TypeOf(true)
case reflect.Int:
sliceType = reflect.TypeOf(int(0))
case reflect.Int8:
sliceType = reflect.TypeOf(int8(0))
case reflect.Int16:
sliceType = reflect.TypeOf(int16(0))
case reflect.Int32:
sliceType = reflect.TypeOf(int32(0))
case reflect.Int64:
sliceType = reflect.TypeOf(int64(0))
case reflect.String:
sliceType = reflect.TypeOf("")
default:
return fmt.Errorf("unsupported list element types: %s -> []%s", src.Type().Name(), dest.Type().Elem().Kind())
}
targetPtr := reflect.New(reflect.SliceOf(sliceType))
if len(f.Elements()) > 0 {
diag = f.ElementsAs(context.TODO(), targetPtr.Interface(), false)
}

if diag.HasError() {
return fmt.Errorf("%s", diag.Errors())
}

dest.Set(targetPtr.Elem())

return nil
default:
return fmt.Errorf("unsupported field type assignment: %s -> %s", src.Type().Name(), dest.Kind())
Expand Down
4 changes: 2 additions & 2 deletions mikrotik/internal/utils/struct_copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,13 @@ func TestCopyTerraformToMikrotik(t *testing.T) {
src: struct {
Id tftypes.String
Bridge tftypes.String
Tagged tftypes.List
Tagged tftypes.Set
Untagged tftypes.List
VlanIds tftypes.List
}{
Id: tftypes.StringValue("new id field"),
Bridge: tftypes.StringValue("new bridge"),
Tagged: tftypes.ListValueMust(tftypes.StringType, []attr.Value{
Tagged: tftypes.SetValueMust(tftypes.StringType, []attr.Value{
tftypes.StringValue("new tagged 3"),
}),
Untagged: tftypes.ListValueMust(tftypes.StringType, []attr.Value{
Expand Down
3 changes: 1 addition & 2 deletions mikrotik/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,12 @@ func Provider(client *mt.Mikrotik) *schema.Provider {
ResourcesMap: map[string]*schema.Resource{
"mikrotik_bgp_instance": resourceBgpInstance(),
"mikrotik_bridge_port": resourceBridgePort(),
"mikrotik_bridge_vlan": resourceBridgeVlan(),
"mikrotik_dhcp_server_network": resourceDhcpServerNetwork(),
"mikrotik_dns_record": resourceRecord(),
"mikrotik_firewall_filter_rule": resourceFirewallFilterRule(),
"mikrotik_interface_list_member": resourceInterfaceListMember(),
"mikrotik_interface_list": resourceInterfaceList(),
"mikrotik_ip_address": resourceIpAddress(),
"mikrotik_firewall_filter_rule": resourceFirewallFilterRule(),
},
}

Expand Down
1 change: 1 addition & 0 deletions mikrotik/provider_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func (p *ProviderFramework) Resources(ctx context.Context) []func() resource.Res
return []func() resource.Resource{
NewBgpPeerResource,
NewBridgeResource,
NewBridgeVlanResource,
NewDhcpLeaseResource,
NewDhcpServerResource,
NewInterfaceWireguardPeerResource,
Expand Down
Loading