fix(xo-server-netbox): support older versions of Netbox (#5946)

Fixes #5898
See https://netbox.readthedocs.io/en/stable/release-notes/version-2.7/#api-choice-fields-now-use-string-values-3569
This commit is contained in:
Pierre Donias 2021-10-13 09:28:46 +02:00 committed by GitHub
parent 9c83e70a28
commit d19a748f0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 3 deletions

View File

@ -13,6 +13,8 @@
> Users must be able to say: “I had this issue, happy to know it's fixed”
- [Netbox] Support older versions of Netbox and prevent "active is not a valid choice" error [#5898](https://github.com/vatesfr/xen-orchestra/issues/5898) (PR [#5946](https://github.com/vatesfr/xen-orchestra/pull/5946))
### Packages to release
> Packages will be released in the order they are here, therefore, they should
@ -30,6 +32,7 @@
>
> In case of conflict, the highest (lowest in previous list) `$version` wins.
- xo-server-netbox patch
- @xen-orchestra/backups minor
- @xen-orchestra/proxy minor
- xo-server patch

View File

@ -31,7 +31,8 @@
"dependencies": {
"@xen-orchestra/log": "^0.3.0",
"ipaddr.js": "^2.0.1",
"lodash": "^4.17.21"
"lodash": "^4.17.21",
"semver": "^7.3.5"
},
"devDependencies": {
"@babel/cli": "^7.13.16",

View File

@ -1,5 +1,6 @@
import assert from 'assert'
import ipaddr from 'ipaddr.js'
import semver from 'semver'
import { createLogger } from '@xen-orchestra/log'
import { find, flatten, forEach, groupBy, isEmpty, keyBy, mapValues, omit, trimEnd, zipObject } from 'lodash'
@ -44,6 +45,7 @@ class Netbox {
#endpoint
#intervalToken
#loaded
#netboxApiVersion
#pools
#removeApiMethods
#syncInterval
@ -113,6 +115,7 @@ class Netbox {
const httpRequest = async () => {
try {
const response = await this.#xo.httpRequest(url, options)
this.#netboxApiVersion = response.headers['api-version']
const body = await response.readAll()
if (body.length > 0) {
return JSON.parse(body)
@ -332,10 +335,18 @@ class Netbox {
vcpus: vm.CPUs.number,
disk,
memory: Math.floor(vm.memory.dynamic[1] / M),
status: vm.power_state === 'Running' ? 'active' : 'offline',
custom_fields: { uuid: vm.uuid },
}
if (this.#netboxApiVersion !== undefined) {
// https://netbox.readthedocs.io/en/stable/release-notes/version-2.7/#api-choice-fields-now-use-string-values-3569
if (semver.satisfies(semver.coerce(this.#netboxApiVersion).version, '>=2.7.0')) {
updatedVm.status = vm.power_state === 'Running' ? 'active' : 'offline'
} else {
updatedVm.status = vm.power_state === 'Running' ? 1 : 0
}
}
if (oldNetboxVm === undefined) {
vmsToCreate.push(updatedVm)
} else {

View File

@ -14667,7 +14667,7 @@ semver@^6.0.0, semver@^6.1.0, semver@^6.1.1, semver@^6.1.2, semver@^6.2.0, semve
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==
semver@^7.2.1, semver@^7.3.2, semver@^7.3.4:
semver@^7.2.1, semver@^7.3.2, semver@^7.3.4, semver@^7.3.5:
version "7.3.5"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.5.tgz#0b621c879348d8998e4b0e4be94b3f12e6018ef7"
integrity sha512-PoeGJYh8HK4BTO/a9Tf6ZG3veo/A7ZVsYrSA6J8ny9nb3B1VrpkuN+z9OE5wfE5p6H4LchYZsegiQgbJD94ZFQ==