Skip to content

Commit 6fc62d0

Browse files
committed
fix: properly detect netbox version when using netbox-docker
1 parent fdc8a7c commit 6fc62d0

3 files changed

Lines changed: 59 additions & 2 deletions

File tree

netbox/provider.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,12 @@ func providerConfigure(ctx context.Context, data *schema.ResourceData) (interfac
331331
return nil, diag.FromErr(err)
332332
}
333333

334-
netboxVersion := res.GetPayload().(map[string]interface{})["netbox-version"].(string)
334+
netboxVersionStringFromAPI := res.GetPayload().(map[string]interface{})["netbox-version"].(string)
335+
336+
netboxVersion, err := extractSemanticVersionFromString(netboxVersionStringFromAPI)
337+
if err != nil {
338+
return nil, diag.FromErr(fmt.Errorf("error extracting netbox version. try using the `skip_version_check` provider parameter to bypass this error. original error: %w", err))
339+
}
335340

336341
supportedVersions := []string{"4.2.2", "4.2.3", "4.2.4", "4.2.5", "4.2.6", "4.2.7", "4.2.8", "4.2.9"}
337342

@@ -340,7 +345,7 @@ func providerConfigure(ctx context.Context, data *schema.ResourceData) (interfac
340345
diags = append(diags, diag.Diagnostic{
341346
Severity: diag.Warning,
342347
Summary: "Possibly unsupported Netbox version",
343-
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, ", ")),
348+
Detail: fmt.Sprintf("Your Netbox reports version %v. From that, the provider extracted Netbox version %v.\nThe provider was successfully tested against the following versions:\n\n %v\n\nUnexpected errors may occur.", netboxVersionStringFromAPI, netboxVersion, strings.Join(supportedVersions, ", ")),
344349
})
345350
}
346351
}

netbox/util.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"reflect"
7+
"regexp"
78
"strings"
89

910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -132,3 +133,18 @@ func jsonSemanticCompare(a, b string) (equal bool, err error) {
132133

133134
return reflect.DeepEqual(aDecoded, bDecoded), nil
134135
}
136+
137+
// extractNetboxVersionFromString extracts the first semantic versioning string
138+
// from a given string. The string must be preceded with "v".
139+
// This is needed since netbox-docker recently started displaying its version string
140+
// as, for example, v4.2.8-Docker-3.2.1 and we want to ignore the Docker part
141+
// ref: https://github.com/e-breuninger/terraform-provider-netbox/issues/729
142+
func extractSemanticVersionFromString(s string) (version string, err error) {
143+
re := regexp.MustCompile(`^v?(\d+\.\d+\.\d+)`)
144+
145+
matches := re.FindStringSubmatch(s)
146+
if len(matches) < 2 {
147+
return "", fmt.Errorf("no semantic version found in version string")
148+
}
149+
return matches[1], nil
150+
}

netbox/util_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,39 @@ func TestJsonSemanticCompareUnequal(t *testing.T) {
8484
t.Errorf("expected 'a' and 'b' to be semantically unequal\n\na: %s\nb: %s\n", a, b)
8585
}
8686
}
87+
88+
func TestExtractSemanticVersionFromString(t *testing.T) {
89+
for _, tt := range []struct {
90+
name string
91+
input string
92+
expected string
93+
}{
94+
{
95+
name: "Incomplete",
96+
input: "v1.3",
97+
expected: "",
98+
},
99+
{
100+
name: "SimpleWithV",
101+
input: "v1.2.3",
102+
expected: "1.2.3",
103+
},
104+
{
105+
name: "SimpleWithoutV",
106+
input: "1.2.3",
107+
expected: "1.2.3",
108+
},
109+
{
110+
name: "Docker",
111+
input: "v4.5.6-Docker-3.2",
112+
expected: "4.5.6",
113+
},
114+
} {
115+
t.Run(tt.name, func(t *testing.T) {
116+
actual, _ := extractSemanticVersionFromString(tt.input)
117+
if actual != tt.expected {
118+
t.Fatalf("\n\nexpected:\n\n%#v\n\ngot:\n\n%#v\n\n", tt.expected, actual)
119+
}
120+
})
121+
}
122+
}

0 commit comments

Comments
 (0)