From 3e985479d859ac4475514c739d52df264b468a2b Mon Sep 17 00:00:00 2001 From: Chun-Hung Tseng Date: Wed, 18 Dec 2024 10:35:06 +0100 Subject: [PATCH] Fix netbox api --- .github/env | 2 +- .gitignore | 5 +-- .../{dockerfile => dockerfile.orig} | 2 +- kind/load-data-job/main.py | 10 ++++- kind/local-env.sh | 45 +++++++++---------- 5 files changed, 34 insertions(+), 30 deletions(-) rename kind/load-data-job/{dockerfile => dockerfile.orig} (62%) diff --git a/.github/env b/.github/env index b84dad01..fb51957d 100644 --- a/.github/env +++ b/.github/env @@ -1,3 +1,3 @@ golang-version=1.23 kind-version=v0.25.0 -kind-image=kindest/node:v1.31.2 +kind-image=kindest/node:v1.32.0 diff --git a/.gitignore b/.gitignore index 805728d2..45297fe5 100644 --- a/.gitignore +++ b/.gitignore @@ -11,9 +11,8 @@ Dockerfile.cross *.test # Test/Intermediate files -database.sql -load-data.sh -local-demo-data.sql +kind/load-data-job/load-data.sh +kind/load-data-job/dockerfile # Output of the go coverage tool, specifically when used with LiteIDE *.out diff --git a/kind/load-data-job/dockerfile b/kind/load-data-job/dockerfile.orig similarity index 62% rename from kind/load-data-job/dockerfile rename to kind/load-data-job/dockerfile.orig index 7435f45c..447d0eb9 100644 --- a/kind/load-data-job/dockerfile +++ b/kind/load-data-job/dockerfile.orig @@ -1,4 +1,4 @@ FROM python:3.12 ADD main.py . -RUN pip install pynetbox +RUN pip install -Iv pynetbox==7.4.1 CMD ["python", "./main.py"] diff --git a/kind/load-data-job/main.py b/kind/load-data-job/main.py index d9b99a1f..4b314438 100644 --- a/kind/load-data-job/main.py +++ b/kind/load-data-job/main.py @@ -94,7 +94,8 @@ class Site: # create custom fields and associate custom fields with IP/IPRange/Prefix @dataclass class CustomField: - object_types: list[str] + content_types: list[str] # for v3 + object_types: list[str] # for v4 type: str name: str label: str @@ -104,6 +105,7 @@ class CustomField: custom_fields = [ CustomField( + content_types=["ipam.ipaddress", "ipam.iprange", "ipam.prefix"], object_types=["ipam.ipaddress", "ipam.iprange", "ipam.prefix"], type="text", name="netboxOperatorRestorationHash", @@ -113,6 +115,7 @@ class CustomField: filter_logic="exact" ), CustomField( + content_types=["ipam.ipaddress", "ipam.iprange", "ipam.prefix"], object_types=["ipam.ipaddress", "ipam.iprange", "ipam.prefix"], type="text", name="example_field", @@ -122,6 +125,7 @@ class CustomField: filter_logic="exact" ), CustomField( + content_types=["ipam.prefix"], object_types=["ipam.prefix"], type="text", name="environment", @@ -131,6 +135,7 @@ class CustomField: filter_logic="exact" ), CustomField( + content_types=["ipam.prefix"], object_types=["ipam.prefix"], type="text", name="poolName", @@ -140,6 +145,7 @@ class CustomField: filter_logic="exact" ), CustomField( + content_types=["ipam.prefix"], object_types=["ipam.prefix"], type="boolean", name="cfDataTypeBool", @@ -149,6 +155,7 @@ class CustomField: filter_logic="exact" ), CustomField( + content_types=["ipam.prefix"], object_types=["ipam.prefix"], type="integer", name="cfDataTypeInteger", @@ -162,6 +169,7 @@ class CustomField: for custom_field in custom_fields: try: nb.extras.custom_fields.create( + content_types=custom_field.content_types, object_types=custom_field.object_types, type=custom_field.type, name=custom_field.name, diff --git a/kind/local-env.sh b/kind/local-env.sh index 532de87e..f0fe7668 100755 --- a/kind/local-env.sh +++ b/kind/local-env.sh @@ -40,16 +40,6 @@ fi kind create cluster || echo "cluster already exists, continuing..." kubectl wait --for=jsonpath='{.status.phase}'=Active --timeout=1s namespace/${NAMESPACE} -# build image for loading local data via NetBox API -cd ./kind/load-data-job && docker build -t netbox-load-local-data:1.0 --no-cache --progress=plain -f ./dockerfile . && cd - -# load local images -declare -a Local_Images=( \ -"netbox-load-local-data:1.0" \ -) -for img in "${Local_Images[@]}"; do - kind load docker-image "$img" -done - # load remote images if [[ "${VERSION}" == "3.7.8" ]] ;then echo "Using version ${VERSION}" @@ -64,17 +54,11 @@ if [[ "${VERSION}" == "3.7.8" ]] ;then ) NETBOX_HELM_CHART="https://github.com/netbox-community/netbox-chart/releases/download/netbox-5.0.0-beta5/netbox-5.0.0-beta5.tgz" - # # perform patching, as we need different demo data and adapt to the database schema - # # to avoid accidental check-in of the files, the base file is renamed to xx.orig.yy, and the xx.yy is added to .gitignore # patch load-data.sh sed 's/netbox-demo-v4.1.sql/netbox-demo-v3.7.sql/g' $(dirname "$0")/load-data-job/load-data.orig.sh > $(dirname "$0")/load-data-job/load-data.sh && chmod +x $(dirname "$0")/load-data-job/load-data.sh - # # patch local-demo-data.sql - # sed \ - # -e "s/public.extras_customfield_object_types (id, customfield_id, objecttype_id)/public.extras_customfield_content_types (id, customfield_id, contenttype_id)/g" \ - # -e 's/related_object_type_id/object_type_id/g' \ - # -e 's/, comments, \"unique\", related_object_filter//g' \ - # -e "s/, '', false, NULL//g" $(dirname "$0")/load-data-job/local-demo-data.orig.sql > $(dirname "$0")/load-data-job/local-demo-data.sql + # patch dockerfile (See README at https://github.com/netbox-community/pynetbox for the supported version matrix) + sed 's/RUN pip install -Iv pynetbox==7.4.1/RUN pip install -Iv pynetbox==7.3.4/g' $(dirname "$0")/load-data-job/dockerfile.orig > $(dirname "$0")/load-data-job/dockerfile elif [[ "${VERSION}" == "4.0.11" ]] ;then echo "Using version ${VERSION}" # need to align with netbox-chart otherwise the creation of the cluster will hang @@ -90,11 +74,8 @@ elif [[ "${VERSION}" == "4.0.11" ]] ;then # patch load-data.sh sed 's/netbox-demo-v4.1.sql/netbox-demo-v4.0.sql/g' $(dirname "$0")/load-data-job/load-data.orig.sh > $(dirname "$0")/load-data-job/load-data.sh && chmod +x $(dirname "$0")/load-data-job/load-data.sh - - # # patch local-demo-data.sql - # sed \ - # -e "s/comments, \"unique\", related_object_filter)/comments)/g" \ - # -e "s/'', false, NULL);/'');/g" $(dirname "$0")/load-data-job/local-demo-data.orig.sql > $(dirname "$0")/load-data-job/local-demo-data.sql + + cp $(dirname "$0")/load-data-job/dockerfile.orig $(dirname "$0")/load-data-job/dockerfile elif [[ "${VERSION}" == "4.1.8" ]] ;then echo "Using version ${VERSION}" # need to align with netbox-chart otherwise the creation of the cluster will hang @@ -107,8 +88,10 @@ elif [[ "${VERSION}" == "4.1.8" ]] ;then "ghcr.io/zalando/spilo-16:3.2-p3" \ ) + # create load-data.sh cp $(dirname "$0")/load-data-job/load-data.orig.sh $(dirname "$0")/load-data-job/load-data.sh - # cp $(dirname "$0")/load-data-job/local-demo-data.orig.sql $(dirname "$0")/load-data-job/local-demo-data.sql + + cp $(dirname "$0")/load-data-job/dockerfile.orig $(dirname "$0")/load-data-job/dockerfile else echo "Unknown version ${VERSION}" exit 1 @@ -119,6 +102,16 @@ for img in "${Remote_Images[@]}"; do kind load docker-image "$img" done +# build image for loading local data via NetBox API +cd ./kind/load-data-job && docker build -t netbox-load-local-data:1.0 --no-cache --progress=plain -f ./dockerfile . && cd - +# load local images +declare -a Local_Images=( \ +"netbox-load-local-data:1.0" \ +) +for img in "${Local_Images[@]}"; do + kind load docker-image "$img" +done + # install helm charts helm upgrade --install --namespace="${NAMESPACE}" postgres-operator \ https://opensource.zalando.com/postgres-operator/charts/postgres-operator/postgres-operator-1.12.2.tgz @@ -145,3 +138,7 @@ kubectl rollout status --namespace="${NAMESPACE}" deployment netbox kubectl create job netbox-load-local-data --image=netbox-load-local-data:1.0 kubectl wait --namespace="${NAMESPACE}" --timeout=600s --for=condition=complete job/netbox-load-local-data docker rmi netbox-load-local-data:1.0 + +# clean up +rm $(dirname "$0")/load-data-job/load-data.sh +rm $(dirname "$0")/load-data-job/dockerfile