Skip to content

Commit

Permalink
Add skip_version_check attribute to provider (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
fbreckle committed May 23, 2022
1 parent 5ec413c commit 0b0b2ff
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.6.6 (unreleased)

ENHANCEMENTS

* provider: Add `skip_version_check` attribute

## 1.6.5 (May 18th, 2022)

ENHANCEMENTS
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ provider "netbox" {

- `allow_insecure_https` (Boolean) Flag to set whether to allow https with invalid certificates
- `headers` (Map of String) Set these header on all requests to Netbox
- `skip_version_check` (Boolean) If true, do not try to determine the running Netbox version at provider startup. Disables warnings about possibly unsupported Netbox version. Also useful for local testing on terraform plans.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/hashicorp/terraform-plugin-sdk/v2 v2.16.0
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.1
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,8 @@ golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf h1:oXVg4h2qJDd9htKxb5SCpFBHLipW6hXmL3qpUixS2jw=
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf/go.mod h1:yh0Ynu2b5ZUe3MQfp2nM0ecK7wsgouWTDN0FNeJuIys=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
Expand Down
41 changes: 28 additions & 13 deletions netbox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package netbox
import (
"context"
"fmt"
"strings"

"github.com/fbreckle/go-netbox/netbox/client"
"github.com/fbreckle/go-netbox/netbox/client/status"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"golang.org/x/exp/slices"
)

// Provider returns a schema.Provider for Netbox.
Expand Down Expand Up @@ -93,6 +95,12 @@ func Provider() *schema.Provider {
DefaultFunc: schema.EnvDefaultFunc("NETBOX_HEADERS", map[string]interface{}{}),
Description: "Set these header on all requests to Netbox",
},
"skip_version_check": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NETBOX_SKIP_VERSION_CHECK", false),
Description: "If true, do not try to determine the running Netbox version at provider startup. Disables warnings about possibly unsupported Netbox version. Also useful for local testing on terraform plans.",
},
},
ConfigureContextFunc: providerConfigure,
}
Expand All @@ -115,24 +123,31 @@ func providerConfigure(ctx context.Context, data *schema.ResourceData) (interfac
return nil, diag.FromErr(clientError)
}

req := status.NewStatusListParams()
res, err := netboxClient.(*client.NetBoxAPI).Status.StatusList(req, nil)
// Unless explicitly switched off, use the client to retrieve the Netbox version
// so we can determine compatibility of the provider with the used Netbox
skipVersionCheck := data.Get("skip_version_check").(bool)

if err != nil {
return nil, diag.FromErr(err)
}
if !skipVersionCheck {
req := status.NewStatusListParams()
res, err := netboxClient.(*client.NetBoxAPI).Status.StatusList(req, nil)

if err != nil {
return nil, diag.FromErr(err)
}

netboxVersion := res.GetPayload().(map[string]interface{})["netbox-version"]
netboxVersion := res.GetPayload().(map[string]interface{})["netbox-version"].(string)

supportedVersion := "3.1.9"
supportedVersions := []string{"3.1.9"}

if netboxVersion != supportedVersion {
if !slices.Contains(supportedVersions, netboxVersion) {

diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "Possibly unsupported Netbox version",
Detail: fmt.Sprintf("This provider was tested against Netbox v%s. Your Netbox version is v%v. Unexpected errors may occur.", supportedVersion, netboxVersion),
})
// Currently, there is no way to test these warnings. There is an issue to track this: https://github.com/hashicorp/terraform-plugin-sdk/issues/864
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "Possibly unsupported Netbox version",
Detail: fmt.Sprintf("Your Netbox version is v%v. The provider was successfully tested against the following versions:\n\n %v\n\nUnexpected errors may occur.", netboxVersion, strings.Join(supportedVersions, ", ")),
})
}
}

return netboxClient, diags
Expand Down

0 comments on commit 0b0b2ff

Please sign in to comment.