Skip to content

Commit

Permalink
Fix netbox api
Browse files Browse the repository at this point in the history
  • Loading branch information
henrybear327 committed Dec 18, 2024
1 parent 2541ac0 commit 3e98547
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/env
Original file line number Diff line number Diff line change
@@ -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
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"]
10 changes: 9 additions & 1 deletion kind/load-data-job/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -122,6 +125,7 @@ class CustomField:
filter_logic="exact"
),
CustomField(
content_types=["ipam.prefix"],
object_types=["ipam.prefix"],
type="text",
name="environment",
Expand All @@ -131,6 +135,7 @@ class CustomField:
filter_logic="exact"
),
CustomField(
content_types=["ipam.prefix"],
object_types=["ipam.prefix"],
type="text",
name="poolName",
Expand All @@ -140,6 +145,7 @@ class CustomField:
filter_logic="exact"
),
CustomField(
content_types=["ipam.prefix"],
object_types=["ipam.prefix"],
type="boolean",
name="cfDataTypeBool",
Expand All @@ -149,6 +155,7 @@ class CustomField:
filter_logic="exact"
),
CustomField(
content_types=["ipam.prefix"],
object_types=["ipam.prefix"],
type="integer",
name="cfDataTypeInteger",
Expand All @@ -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,
Expand Down
45 changes: 21 additions & 24 deletions kind/local-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 3e98547

Please sign in to comment.