Skip to content

Commit

Permalink
make ap versions configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
darey-io committed Jan 15, 2024
1 parent 84b33f0 commit 419818e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
3 changes: 3 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
FROM python:3.11.5

# Set a default value for AP_VERSIONS
ENV AP_VERSIONS='{"AP45": "0.12.27139", "AP32": "0.12.27139"}'

# Set the working directory to /app
WORKDIR /app

Expand Down
27 changes: 21 additions & 6 deletions src/juniper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import json
import requests
import json
import getpass
Expand All @@ -9,6 +11,15 @@

class Admin:

@staticmethod
def get_ap_versions():
ap_versions_str = os.getenv('AP_VERSIONS', '{}')
try:
return json.loads(ap_versions_str)
except json.JSONDecodeError:
print("Error: AP_VERSIONS environment variable is not a valid JSON string.")
return {}

def login_via_username_and_password(self, username):
# If no username defined ask user
if username is None:
Expand Down Expand Up @@ -125,6 +136,9 @@ def build_payload(
network_template_id,
site_group_ids
):
# Get AP versions from environment variable
ap_versions = Admin.get_ap_versions()

site = {'name': d.get('Site Name', ''),
'address': d.get('Site Address', ''),
"latlng": {"lat": d.get('gps', '')[0], "lng": d.get('gps', '')[1]},
Expand All @@ -145,10 +159,7 @@ def build_payload(
"enabled": True,
"version": "custom",
"time_of_day": "02:00",
"custom_versions": {
"AP45": "0.12.27139",
"AP32": "0.12.27139"
},
"custom_versions": ap_versions,
"day_of_week": ""
},

Expand Down Expand Up @@ -331,11 +342,12 @@ def juniper_script(
mist_login_method=None,
site_group_ids=None,
rf_template_id=None,
network_template_id=None
network_template_id=None,
ap_versions=None
):
# Configure True/False to enable/disable additional logging of the API response objects
show_more_details = True

ap_versions = Admin.get_ap_versions()
# Check for required variables
if org_id is None or org_id == '':
raise ValueError('Please provide Mist org_id')
Expand All @@ -348,6 +360,9 @@ def juniper_script(
if mist_login_method is None:
print("mist_login_method not defined. Defaulting to credentials")
mist_login_method = 'credentials'
if ap_versions is None or not isinstance(ap_versions, dict):
raise ValueError('Must provide a valid dictionary for ap_versions')


# Prompt user if we are using production org_id
warn_if_using_org_id_production(org_id)
Expand Down
15 changes: 15 additions & 0 deletions test/test_juniper.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,21 @@ def test_put(self, mock_put, mock_successful_login, input_api_token):
# Assert that the method returns the expected result
self.assertIsNotNone(result)

@patch('src.juniper.Admin.get_ap_versions')
def test_ap_versions_handling(self, mock_get_ap_versions):
# Set up a valid AP_VERSIONS environment variable
valid_ap_versions = {"AP45": "0.12.27139", "AP32": "0.12.27139"}
mock_get_ap_versions.return_value = valid_ap_versions

# Test if juniper_script or Admin class handles valid AP_VERSIONS correctly
admin = Admin()
self.assertEqual(admin.get_ap_versions(), valid_ap_versions)

# Test with invalid AP_VERSIONS
with patch.dict('os.environ', {'AP_VERSIONS': 'invalid_json'}, clear=True):
with self.assertRaises(ValueError):
# Attempt to parse invalid JSON, should raise ValueError
admin.get_ap_versions()

class TestCheckIfNeedToAppend(unittest.TestCase):

Expand Down

0 comments on commit 419818e

Please sign in to comment.