Skip to content

Commit

Permalink
Make the provider more idiomatic
Browse files Browse the repository at this point in the history
The provider was originally written on the old SDK and some of the
patterns were not updated. This more closely follows the pattern from
hashicorp scaffolding provider.

There is no need for the API to be public, hence they are in the
internal directory.

Also included is a bump in go-virtualbox version to include the new
Manager interface which will be called in resources and data sources
improving testability of provider.
  • Loading branch information
VoyTechnology committed Sep 2, 2022
1 parent 069c75f commit 4bbe421
Show file tree
Hide file tree
Showing 14 changed files with 24 additions and 13 deletions.
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -206,6 +207,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/terra-farm/go-virtualbox v0.0.4 h1:UAEVNAYLA3g80Z//fMZFVSWHEPqRWkWP2gmFh2p/guQ=
github.com/terra-farm/go-virtualbox v0.0.4/go.mod h1:5n2X+HKR2eAzHfuGFnrZlCrgiYrseNHIcNTSpA/ViyU=
github.com/terra-farm/go-virtualbox v0.0.5-0.20220817001340-1829d466472a h1:ol4xE71sjt+acgiWFOVlgdv3RZ87yCsP9FKHcrzu9d0=
github.com/terra-farm/go-virtualbox v0.0.5-0.20220817001340-1829d466472a/go.mod h1:zzjz3gJPkYrCEpNqr3m4oc/ml00W1ug5Mar136txVNo=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down Expand Up @@ -303,6 +306,7 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
Expand Down
2 changes: 1 addition & 1 deletion virtualbox/image.go → internal/provider/image.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package virtualbox
package provider

import (
"crypto/md5"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package virtualbox
package provider

import (
"errors"
Expand Down
16 changes: 13 additions & 3 deletions virtualbox/provider.go → internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

// Package virtualbox serves as an entrypoint, returning the list of available
// resources for the plugin.
package virtualbox
package provider

import (
"context"
"fmt"
"log"
"os"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terra-farm/go-virtualbox"
)

func init() {
Expand All @@ -20,11 +23,18 @@ func init() {
log.SetPrefix(fmt.Sprintf("pid-%d-", os.Getpid()))
}

// Provider returns a resource provider for virtualbox.
func Provider() *schema.Provider {
// New returns a resource provider for virtualbox.
func New() *schema.Provider {
return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"virtualbox_vm": resourceVM(),
},
ConfigureContextFunc: configure,
}
}

// configure creates a new instance of the new virtualbox manager which will be
// used for communication with virtualbox.
func configure(context.Context, *schema.ResourceData) (any, diag.Diagnostics) {
return virtualbox.NewManager(), nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package virtualbox
package provider

import (
"context"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion virtualbox/utils.go → internal/provider/utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package virtualbox
package provider

import (
"fmt"
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (
"flag"

"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
"github.com/terra-farm/terraform-provider-virtualbox/virtualbox"
"github.com/terra-farm/terraform-provider-virtualbox/internal/provider"
)

func main() {
debug := flag.Bool("debug", false, "run the provider in debug mode")
flag.Parse()

plugin.Serve(&plugin.ServeOpts{
ProviderFunc: virtualbox.Provider,
ProviderFunc: provider.New,
ProviderAddr: "registry.terraform.io/terra-farm/virtualbox",
Debug: *debug,
})
}
4 changes: 0 additions & 4 deletions virtualbox/resource_vm_test.go

This file was deleted.

0 comments on commit 4bbe421

Please sign in to comment.