Skip to content

Commit

Permalink
Netbox v2.11.12 (#81)
Browse files Browse the repository at this point in the history
* Support netbox 2.11.12

* Update vm.vcpus from int to string

* Update CHANGELOG

* Update README

* Increase max attempts in docker startup script
  • Loading branch information
fbreckle authored Oct 19, 2021
1 parent db0d916 commit 20b832c
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 27 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 0.3.0 (Oct 19, 2021)

FEATURES

* provider: Now supports NetBox v2.11.12

BREAKING CHANGES

* resource/virtual_machine: `vcpus` is now a string to match upstream API

## 0.2.5 (Oct 8, 2021)

ENHANCEMENTS
Expand Down
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ default: testacc
# Run acceptance tests
.PHONY: testacc
testacc:
@NETBOX_VERSION=v2.10.10 sh -c "'$(CURDIR)/docker/start.sh' $(NETBOX_SERVER_URL)"
@NETBOX_VERSION=v2.11.12 sh -c "'$(CURDIR)/docker/start.sh' $(NETBOX_SERVER_URL)"
TF_ACC=1 NETBOX_SERVER_URL=$(NETBOX_SERVER_URL) NETBOX_API_TOKEN=0123456789abcdef0123456789abcdef01234567 go test -v -cover $(TEST)

.PHONY: test
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Netbox often makes API-breaking changes even in non-major releases. We aim to al

Provider version | Netbox version
--- | ---
v0.3.x | v2.11.12
v0.2.x | v2.10.10
v0.1.x | v2.9
v0.0.x | v2.9
Expand Down
2 changes: 1 addition & 1 deletion docker/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ docker-compose -f $SCRIPTPATH/docker-compose.yml up -d
echo "### Waiting for Netbox to become available on ${SERVER_URL} \n\n"

attempt_counter=0
max_attempts=24
max_attempts=48
until $(curl --connect-timeout 1 --output /dev/null --silent --head --fail ${SERVER_URL}); do
if [ ${attempt_counter} -eq ${max_attempts} ];then
echo "Max attempts reached"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.17

require (
github.com/davecgh/go-spew v1.1.1
github.com/fbreckle/go-netbox v0.0.0-20211007213915-a62446ca58e7
github.com/fbreckle/go-netbox v0.0.0-20211019120953-a22717727647
github.com/go-openapi/runtime v0.19.32-0.20210924162202-f7fb72beda3a
github.com/goware/urlx v0.3.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.8.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fbreckle/go-netbox v0.0.0-20211007211747-91c0f8773a43 h1:UBnifqgHAIegovxjUA8xXxwz/Pb4sBzSjNkV7AuWzGo=
github.com/fbreckle/go-netbox v0.0.0-20211007211747-91c0f8773a43/go.mod h1:Nf2bxylehF4ROF+s2paaRCKLIeYcUb2+NK1R/nOsE0o=
github.com/fbreckle/go-netbox v0.0.0-20211007213915-a62446ca58e7 h1:M9zkDx7r4b2mh/FRHGRs0n9XWiB5j7FdwHdnoN2EBQI=
github.com/fbreckle/go-netbox v0.0.0-20211007213915-a62446ca58e7/go.mod h1:Nf2bxylehF4ROF+s2paaRCKLIeYcUb2+NK1R/nOsE0o=
github.com/fbreckle/go-netbox v0.0.0-20211019120953-a22717727647 h1:kpryjXA8l3bEfAtnUls0zj+CADWlxXw/FVv+BIqHLLw=
github.com/fbreckle/go-netbox v0.0.0-20211019120953-a22717727647/go.mod h1:Nf2bxylehF4ROF+s2paaRCKLIeYcUb2+NK1R/nOsE0o=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/globalsign/mgo v0.0.0-20180905125535-1ca0a4f7cbcb/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
Expand Down
2 changes: 1 addition & 1 deletion netbox/data_source_netbox_virtual_machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func dataSourceNetboxVirtualMachine() *schema.Resource {
Computed: true,
},
"vcpus": {
Type: schema.TypeInt,
Type: schema.TypeString,
Computed: true,
},
"vm_id": {
Expand Down
2 changes: 1 addition & 1 deletion netbox/data_source_netbox_virtual_machines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestAccNetboxVirtualMachinesDataSource_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.netbox_virtual_machines.test", "vms.#", "1"),
resource.TestCheckResourceAttr("data.netbox_virtual_machines.test", "vms.0.name", testName+"_0"),
resource.TestCheckResourceAttr("data.netbox_virtual_machines.test", "vms.0.vcpus", "4"),
resource.TestCheckResourceAttr("data.netbox_virtual_machines.test", "vms.0.vcpus", "4.00"),
resource.TestCheckResourceAttr("data.netbox_virtual_machines.test", "vms.0.memory_mb", "1024"),
resource.TestCheckResourceAttr("data.netbox_virtual_machines.test", "vms.0.disk_size_gb", "256"),
resource.TestCheckResourceAttr("data.netbox_virtual_machines.test", "vms.0.comments", "thisisacomment"),
Expand Down
2 changes: 1 addition & 1 deletion netbox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func providerConfigure(ctx context.Context, data *schema.ResourceData) (interfac
res, _ := netboxClient.(*client.NetBoxAPI).Status.StatusList(req, nil)
netboxVersion := res.GetPayload().(map[string]interface{})["netbox-version"]

supportedVersion := "2.10.10"
supportedVersion := "2.11.12"

if netboxVersion != supportedVersion {

Expand Down
2 changes: 2 additions & 0 deletions netbox/resource_netbox_primary_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ func resourceNetboxPrimaryIPUpdate(d *schema.ResourceData, m interface{}) error
data.Tags = vm.Tags
// the netbox API sends the URL property as part of NestedTag, but it does not accept the URL property when we send it back
// so set it to empty
// display too
for _, tag := range data.Tags {
tag.URL = ""
tag.Display = ""
}
data.Comments = vm.Comments
data.Memory = vm.Memory
Expand Down
2 changes: 1 addition & 1 deletion netbox/resource_netbox_primary_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ resource "netbox_virtual_machine" "test" {
tenant_id = netbox_tenant.test.id
role_id = netbox_device_role.test.id
platform_id = netbox_platform.test.id
vcpus = 4
vcpus = "4"
tags = [netbox_tag.test.name]
}
Expand Down
26 changes: 16 additions & 10 deletions netbox/resource_netbox_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func resourceNetboxVirtualMachine() *schema.Resource {
Optional: true,
},
"vcpus": &schema.Schema{
Type: schema.TypeInt,
Type: schema.TypeString,
Optional: true,
},
"disk_size_gb": &schema.Schema{
Expand Down Expand Up @@ -94,7 +94,7 @@ func resourceNetboxVirtualMachineCreate(ctx context.Context, d *schema.ResourceD

vcpusValue, ok := d.GetOk("vcpus")
if ok {
vcpus := int64(vcpusValue.(int))
vcpus := vcpusValue.(string)
data.Vcpus = &vcpus
}

Expand Down Expand Up @@ -196,7 +196,19 @@ func resourceNetboxVirtualMachineRead(ctx context.Context, d *schema.ResourceDat
}

d.Set("comments", res.GetPayload().Comments)
d.Set("vcpus", res.GetPayload().Vcpus)
vcpus := res.GetPayload().Vcpus
if vcpus != nil {
// vcpus comes as string with double digit precision
// for usability, we trim the .00 part if its a .00 value
// this way, we do not get configuration drift if e.g. vcpus = "4" is set
if (*vcpus)[len(*vcpus)-3:] == ".00" {
d.Set("vcpus", (*vcpus)[0:len(*vcpus)-3])
} else {
d.Set("vcpus", res.GetPayload().Vcpus)
}
} else {
d.Set("vcpus", res.GetPayload().Vcpus)
}
d.Set("memory_mb", res.GetPayload().Memory)
d.Set("disk_size_gb", res.GetPayload().Disk)
d.Set("tags", getTagListFromNestedTagList(res.GetPayload().Tags))
Expand Down Expand Up @@ -237,18 +249,12 @@ func resourceNetboxVirtualMachineUpdate(ctx context.Context, d *schema.ResourceD
if ok {
memoryMb := int64(memoryMbValue.(int))
data.Memory = &memoryMb
// } else {
// memorymb := int64(0)
// data.memory = &memorymb
}

vcpusValue, ok := d.GetOk("vcpus")
if ok {
vcpus := int64(vcpusValue.(int))
vcpus := vcpusValue.(string)
data.Vcpus = &vcpus
// } else {
// vcpus := int64(0)
// data.Vcpus = &vcpus
}

diskSizeValue, ok := d.GetOk("disk_size_gb")
Expand Down
48 changes: 46 additions & 2 deletions netbox/resource_netbox_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ resource "netbox_virtual_machine" "test" {
tenant_id = netbox_tenant.test.id
role_id = netbox_device_role.test.id
platform_id = netbox_platform.test.id
vcpus = 4
vcpus = "4"
tags = ["%[1]sa"]
}`, testName),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -113,7 +113,7 @@ resource "netbox_virtual_machine" "test" {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_virtual_machine.test", "name", testName),
resource.TestCheckResourceAttrPair("netbox_virtual_machine.test", "cluster_id", "netbox_cluster.test", "id"),
resource.TestCheckResourceAttr("netbox_virtual_machine.test", "vcpus", "0"),
resource.TestCheckResourceAttr("netbox_virtual_machine.test", "vcpus", ""),
resource.TestCheckResourceAttr("netbox_virtual_machine.test", "memory_mb", "0"),
resource.TestCheckResourceAttr("netbox_virtual_machine.test", "disk_size_gb", "0"),
resource.TestCheckResourceAttr("netbox_virtual_machine.test", "comments", ""),
Expand All @@ -128,6 +128,50 @@ resource "netbox_virtual_machine" "test" {
})
}

func TestAccNetboxVirtualMachine_fractionalVcpu(t *testing.T) {

testSlug := "vm_fracVcpu"
testName := testAccGetTestName(testSlug)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVirtualMachineDestroy,
Steps: []resource.TestStep{
{
Config: testAccNetboxVirtualMachineFullDependencies(testName) + fmt.Sprintf(`
resource "netbox_virtual_machine" "test" {
name = "%s"
cluster_id = netbox_cluster.test.id
vcpus = "2.50"
}`, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_virtual_machine.test", "name", testName),
resource.TestCheckResourceAttrPair("netbox_virtual_machine.test", "cluster_id", "netbox_cluster.test", "id"),
resource.TestCheckResourceAttr("netbox_virtual_machine.test", "vcpus", "2.50"),
),
},
{
Config: testAccNetboxVirtualMachineFullDependencies(testName) + fmt.Sprintf(`
resource "netbox_virtual_machine" "test" {
name = "%s"
cluster_id = netbox_cluster.test.id
vcpus = "4"
}`, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_virtual_machine.test", "name", testName),
resource.TestCheckResourceAttrPair("netbox_virtual_machine.test", "cluster_id", "netbox_cluster.test", "id"),
resource.TestCheckResourceAttr("netbox_virtual_machine.test", "vcpus", "4"), // netbox will return "4.00", but we parse it to improve usability
),
},
{
ResourceName: "netbox_virtual_machine.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckVirtualMachineDestroy(s *terraform.State) error {
// retrieve the connection established in Provider configuration
conn := testAccProvider.Meta().(*client.NetBoxAPI)
Expand Down
2 changes: 1 addition & 1 deletion website/docs/d/virtual_machines.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Read-Only:
- **status** (String)
- **tag_ids** (List of Number)
- **tenant_id** (Number)
- **vcpus** (Number)
- **vcpus** (String)
- **vm_id** (Number)


6 changes: 3 additions & 3 deletions website/docs/r/virtual_machine.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ resource "netbox_virtual_machine" "basic-vm" {
name = "myvm-2"
disk_size_gb = 40
memory_mb = 4092
vcpus = 2
vcpus = "2"
}
```

Expand All @@ -73,7 +73,7 @@ resource "netbox_virtual_machine" "full-vm" {
name = "myvm-3"
disk_size_gb = 40
memory_mb = 4092
vcpus = 2
vcpus = "2"
role_id = 31 // This corresponds to the Netbox ID for a given role
tenant_id = data.netbox_tenant.customer-a.id
}
Expand All @@ -98,7 +98,7 @@ resource "netbox_virtual_machine" "full-vm" {
- **role_id** (Number)
- **tags** (Set of String)
- **tenant_id** (Number)
- **vcpus** (Number)
- **vcpus** (String)

### Read-Only

Expand Down

0 comments on commit 20b832c

Please sign in to comment.