Skip to content

Commit

Permalink
Provider/migrate to plugin framework (#134)
Browse files Browse the repository at this point in the history
* create instance of provider framework

* migrate DefaultFunc field in provider from schema to Configure() func

* fix provider_framework configuration logic

* combine SDK and Framework providers via Protocol5 mux (legacy SDK)

* improve provider_framework configure() method

* migrate shceduler resource to framework

* add id field to scheduler resource schema

* collapse go.sum in diff viewer on Github.com

* make provider fields optional to be able to be compatible with framework

* fix provider tests to use server muxer

* remove unnecessary planmodofier for interval in scheduler

* addressing PR comments

* put Computed:true back in resource_schedule
  • Loading branch information
maksym-nazarenko authored Feb 23, 2023
1 parent af8c34b commit fd2ec19
Show file tree
Hide file tree
Showing 28 changed files with 650 additions and 317 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# do not expand these files by default in diff viewer
go.sum linguist-generated
5 changes: 5 additions & 0 deletions client/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func (client Mikrotik) DeleteScheduler(name string) error {
return err
}

// AddScheduler is an alias to CreateScheduler
func (client Mikrotik) AddScheduler(s *Scheduler) (*Scheduler, error) {
return client.CreateScheduler(s)
}

func (client Mikrotik) CreateScheduler(s *Scheduler) (*Scheduler, error) {
c, err := client.getMikrotikClient()

Expand Down
20 changes: 11 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ go 1.18
require (
github.com/ddelnano/terraform-provider-mikrotik/client v0.0.0-00010101000000-000000000000
github.com/hashicorp/terraform-plugin-docs v0.13.0
github.com/hashicorp/terraform-plugin-framework v1.0.1
github.com/hashicorp/terraform-plugin-go v0.14.2
github.com/hashicorp/terraform-plugin-mux v0.8.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.20.0
)

Expand All @@ -21,25 +24,24 @@ require (
github.com/fatih/color v1.13.0 // indirect
github.com/go-routeros/routeros v0.0.0-20210123142807-2a44d57c6730 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
github.com/hashicorp/go-hclog v1.2.1 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-plugin v1.4.4 // indirect
github.com/hashicorp/go-plugin v1.4.6 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hc-install v0.4.0 // indirect
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.17.2 // indirect
github.com/hashicorp/terraform-json v0.14.0 // indirect
github.com/hashicorp/terraform-plugin-go v0.12.0 // indirect
github.com/hashicorp/terraform-plugin-log v0.7.0 // indirect
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c // indirect
github.com/hashicorp/terraform-registry-address v0.1.0 // indirect
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/huandu/xstrings v1.3.2 // indirect
Expand All @@ -62,13 +64,13 @@ require (
github.com/vmihailenco/tagparser v0.1.1 // indirect
github.com/zclconf/go-cty v1.10.0 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/genproto v0.0.0-20200711021454-869866162049 // indirect
google.golang.org/grpc v1.48.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
google.golang.org/grpc v1.51.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)

replace github.com/ddelnano/terraform-provider-mikrotik/client => ./client
79 changes: 23 additions & 56 deletions go.sum

Large diffs are not rendered by default.

45 changes: 33 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,49 @@ import (
"log"

"github.com/ddelnano/terraform-provider-mikrotik/mikrotik"
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server"
"github.com/hashicorp/terraform-plugin-mux/tf5muxserver"
"github.com/hashicorp/terraform-plugin-mux/tf6to5server"
)

// Generate the Terraform provider documentation using `tfplugindocs`:
//
//go:generate go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
func main() {
var debugMode bool

flag.BoolVar(&debugMode, "debuggable", false, "set to true to run the provider with support for debuggers like delve")
flag.Parse()

ctx := context.Background()

downgradedProviderFramework, err := tf6to5server.DowngradeServer(
ctx,
providerserver.NewProtocol6(mikrotik.NewProviderFramework(nil)),
)
if err != nil {
log.Fatal(err)
}

providers := []func() tfprotov5.ProviderServer{
mikrotik.NewProvider().GRPCProvider,
func() tfprotov5.ProviderServer { return downgradedProviderFramework },
}

muxServer, err := tf5muxserver.NewMuxServer(ctx, providers...)
if err != nil {
log.Fatal(err)
}

serverOpts := []tf5server.ServeOpt{}
if debugMode {
err := plugin.Debug(context.Background(), "registry.terraform.io/ddelnano/mikrotik",
&plugin.ServeOpts{
ProviderFunc: mikrotik.NewProvider,
})
if err != nil {
log.Println(err.Error())
}
} else {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: mikrotik.NewProvider,
})
serverOpts = append(serverOpts, tf5server.WithManagedDebug())
}

err = tf5server.Serve("registry.terraform.io/ddelnano/mikrotik", muxServer.ProviderServer, serverOpts...)
if err != nil {
log.Fatal(err)
}
}
16 changes: 16 additions & 0 deletions mikrotik/internal/utils/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package utils

import (
"fmt"
"strconv"
)

// ParseBool is wrapper around strconv.ParseBool to save few lines of code
func ParseBool(v string) (bool, error) {
res, err := strconv.ParseBool(v)
if err != nil {
return res, fmt.Errorf("could not parse %q as bool: %w", v, err)
}

return res, nil
}
46 changes: 36 additions & 10 deletions mikrotik/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package mikrotik
import (
"context"
"fmt"
"os"
"strings"

mt "github.com/ddelnano/terraform-provider-mikrotik/client"
"github.com/ddelnano/terraform-provider-mikrotik/mikrotik/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand All @@ -31,38 +33,33 @@ func Provider(client *mt.Mikrotik) *schema.Provider {
Schema: map[string]*schema.Schema{
"host": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("MIKROTIK_HOST", nil),
Optional: true,
Description: "Hostname of the MikroTik router",
},
"username": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("MIKROTIK_USER", nil),
Optional: true,
Description: "User account for MikroTik api",
},
"password": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("MIKROTIK_PASSWORD", ""),
Sensitive: true,
Description: "Password for MikroTik api",
},
"tls": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("MIKROTIK_TLS", false),
Description: "Whether to use TLS when connecting to MikroTik or not",
},
"ca_certificate": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("MIKROTIK_CA_CERTIFICATE", ""),
Description: "Path to MikroTik's certificate authority",
},
"insecure": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("MIKROTIK_INSECURE", false),
Description: "Insecure connection does not verify MikroTik's TLS certificate",
},
},
Expand All @@ -82,7 +79,6 @@ func Provider(client *mt.Mikrotik) *schema.Provider {
"mikrotik_ipv6_address": resourceIpv6Address(),
"mikrotik_firewall_filter_rule": resourceFirewallFilterRule(),
"mikrotik_pool": resourcePool(),
"mikrotik_scheduler": resourceScheduler(),
"mikrotik_script": resourceScript(),
"mikrotik_vlan_interface": resourceVlanInterface(),
},
Expand All @@ -92,6 +88,7 @@ func Provider(client *mt.Mikrotik) *schema.Provider {
if client != nil {
return client, nil
}
var diags diag.Diagnostics

address := d.Get("host").(string)
username := d.Get("username").(string)
Expand All @@ -100,7 +97,36 @@ func Provider(client *mt.Mikrotik) *schema.Provider {
caCertificate := d.Get("ca_certificate").(string)
insecure := d.Get("insecure").(bool)

return mt.NewClient(address, username, password, tls, caCertificate, insecure), nil
if v := os.Getenv("MIKROTIK_HOST"); v != "" {
address = v
}
if v := os.Getenv("MIKROTIK_USER"); v != "" {
username = v
}
if v := os.Getenv("MIKROTIK_PASSWORD"); v != "" {
password = v
}
if v := os.Getenv("MIKROTIK_TLS"); v != "" {
tlsValue, err := utils.ParseBool(v)
if err != nil {
diags = append(diags,
diag.FromErr(fmt.Errorf("could not parse MIKROTIK_TLS environment variable: %w", err))...)
}
tls = tlsValue
}
if v := os.Getenv("MIKROTIK_CA_CERTIFICATE"); v != "" {
caCertificate = v
}
if v := os.Getenv("MIKROTIK_INSECURE"); v != "" {
insecureValue, err := utils.ParseBool(v)
if err != nil {
diags = append(diags,
diag.FromErr(fmt.Errorf("could not parse MIKROTIK_INSECURE environment variable: %w", err))...)
}
insecure = insecureValue
}

return mt.NewClient(address, username, password, tls, caCertificate, insecure), diags
}

return provider
Expand Down
Loading

0 comments on commit fd2ec19

Please sign in to comment.