Skip to content

Commit 0b0b2ff

Browse files
authored
Add skip_version_check attribute to provider (#177)
1 parent 5ec413c commit 0b0b2ff

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 1.6.6 (unreleased)
2+
3+
ENHANCEMENTS
4+
5+
* provider: Add `skip_version_check` attribute
6+
17
## 1.6.5 (May 18th, 2022)
28

39
ENHANCEMENTS

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ provider "netbox" {
4646

4747
- `allow_insecure_https` (Boolean) Flag to set whether to allow https with invalid certificates
4848
- `headers` (Map of String) Set these header on all requests to Netbox
49+
- `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.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/hashicorp/terraform-plugin-sdk/v2 v2.16.0
1212
github.com/sirupsen/logrus v1.8.1
1313
github.com/stretchr/testify v1.7.1
14+
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf
1415
)
1516

1617
require (

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,8 @@ golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5y
471471
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
472472
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
473473
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
474+
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf h1:oXVg4h2qJDd9htKxb5SCpFBHLipW6hXmL3qpUixS2jw=
475+
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf/go.mod h1:yh0Ynu2b5ZUe3MQfp2nM0ecK7wsgouWTDN0FNeJuIys=
474476
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
475477
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
476478
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=

netbox/provider.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package netbox
33
import (
44
"context"
55
"fmt"
6+
"strings"
67

78
"github.com/fbreckle/go-netbox/netbox/client"
89
"github.com/fbreckle/go-netbox/netbox/client/status"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"golang.org/x/exp/slices"
1113
)
1214

1315
// Provider returns a schema.Provider for Netbox.
@@ -93,6 +95,12 @@ func Provider() *schema.Provider {
9395
DefaultFunc: schema.EnvDefaultFunc("NETBOX_HEADERS", map[string]interface{}{}),
9496
Description: "Set these header on all requests to Netbox",
9597
},
98+
"skip_version_check": {
99+
Type: schema.TypeBool,
100+
Optional: true,
101+
DefaultFunc: schema.EnvDefaultFunc("NETBOX_SKIP_VERSION_CHECK", false),
102+
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.",
103+
},
96104
},
97105
ConfigureContextFunc: providerConfigure,
98106
}
@@ -115,24 +123,31 @@ func providerConfigure(ctx context.Context, data *schema.ResourceData) (interfac
115123
return nil, diag.FromErr(clientError)
116124
}
117125

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

121-
if err != nil {
122-
return nil, diag.FromErr(err)
123-
}
130+
if !skipVersionCheck {
131+
req := status.NewStatusListParams()
132+
res, err := netboxClient.(*client.NetBoxAPI).Status.StatusList(req, nil)
133+
134+
if err != nil {
135+
return nil, diag.FromErr(err)
136+
}
124137

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

127-
supportedVersion := "3.1.9"
140+
supportedVersions := []string{"3.1.9"}
128141

129-
if netboxVersion != supportedVersion {
142+
if !slices.Contains(supportedVersions, netboxVersion) {
130143

131-
diags = append(diags, diag.Diagnostic{
132-
Severity: diag.Warning,
133-
Summary: "Possibly unsupported Netbox version",
134-
Detail: fmt.Sprintf("This provider was tested against Netbox v%s. Your Netbox version is v%v. Unexpected errors may occur.", supportedVersion, netboxVersion),
135-
})
144+
// 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
145+
diags = append(diags, diag.Diagnostic{
146+
Severity: diag.Warning,
147+
Summary: "Possibly unsupported Netbox version",
148+
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, ", ")),
149+
})
150+
}
136151
}
137152

138153
return netboxClient, diags

0 commit comments

Comments
 (0)