Skip to content

Commit

Permalink
Last polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
Reimann, Timo committed Sep 29, 2021
1 parent 14da2fb commit b06d49b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 65 deletions.
2 changes: 1 addition & 1 deletion develop/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
context: ../
dockerfile: develop/Dockerfile
args:
- netbox_ver=v2.11.12
- netbox_ver=v3.0.3
command: >
sh -c "python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
Expand Down
31 changes: 21 additions & 10 deletions netbox_cisco_support/management/commands/sync_eox_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,28 @@ def add_arguments(self, parser):
help='Manufacturer name (default: Cisco)',
)

# Updates a single device with current EoX Data
def update_device_eox_data(self, device):
self.stdout.write(self.style.SUCCESS("Trying to update device %s" % device['sr_no']))

# Get the device object from NetBox
d = Device.objects.get(serial=device['sr_no'])

# Check if a CiscoSupport object already exists, if not, create a new one
try:
ds = CiscoSupport.objects.get(device=d)
except CiscoSupport.DoesNotExist:
ds = CiscoSupport(device=d)

# Control variable to only save the object if something has changed
value_changed = False

# A "YES" string is not quite boolean :-)
covered = True if device['is_covered'] == "YES" else False

self.stdout.write(self.style.SUCCESS("%s - covered: %s" % (device['sr_no'], covered)))

# Check if the Coverage in the CiscoSupport object equals API answer. If not, change it
if ds.is_covered != covered:
ds.is_covered = covered
value_changed = True
Expand Down Expand Up @@ -113,7 +120,7 @@ def update_device_type_eox_data(self, pid, eox_data):
if not eox_data["EOXRecord"][0]["EndOfSWMaintenanceReleases"]["value"]:
self.stdout.write(self.style.NOTICE("%s has no end_of_sw_maintenance_releases" % pid))
else:
end_of_sw_maintenance_releases_string = eox_data["EOXRecord"][0]["EndOfSaleDate"]["value"]
end_of_sw_maintenance_releases_string = eox_data["EOXRecord"][0]["EndOfSWMaintenanceReleases"]["value"]
end_of_sw_maintenance_releases = datetime.strptime(end_of_sw_maintenance_releases_string, '%Y-%m-%d').date()
self.stdout.write(self.style.SUCCESS("%s - end_of_sw_maintenance_releases: %s" % (pid, end_of_sw_maintenance_releases)))

Expand Down Expand Up @@ -169,7 +176,7 @@ def update_device_type_eox_data(self, pid, eox_data):
if not eox_data["EOXRecord"][0]["LastDateOfSupport"]["value"]:
self.stdout.write(self.style.NOTICE("%s has no last_date_of_support" % pid))
else:
last_date_of_support_string = eox_data["EOXRecord"][0]["EndOfSaleDate"]["value"]
last_date_of_support_string = eox_data["EOXRecord"][0]["LastDateOfSupport"]["value"]
last_date_of_support = datetime.strptime(last_date_of_support_string, '%Y-%m-%d').date()
self.stdout.write(self.style.SUCCESS("%s - last_date_of_support: %s" % (pid, last_date_of_support)))

Expand Down Expand Up @@ -273,18 +280,17 @@ def logon(self):

return api_call_headers

# Main entry point for the sync_eox_data command of manage.py
def handle(self, *args, **kwargs):
# Logon one time and gather the required API key
api_call_headers = self.logon()

"""
# Step 1: Get all PIDs for all Device Types of that particular manufacturer
product_ids = self.get_product_ids(kwargs['manufacturer'])
self.stdout.write(self.style.SUCCESS('Gathering data for these PIDs: ' + ', '.join(product_ids)))

i = 1
for pid in product_ids:
# if i == 10:
# break
url = 'https://api.cisco.com/supporttools/eox/rest/5/EOXByProductID/1/%s?responseencoding=json' % pid
api_call_response = requests.get(url, headers=api_call_headers)
self.stdout.write(self.style.SUCCESS('Call ' + url))
Expand All @@ -293,16 +299,18 @@ def handle(self, *args, **kwargs):
filename = django.utils.text.get_valid_filename('%s.json' % pid)

# debug API answer to text file
with open('/source/netbox_cisco_support/api-answer/%s' % filename, 'w') as outfile:
outfile.write(api_call_response.text)
# with open('/source/netbox_cisco_support/api-answer/%s' % filename, 'w') as outfile:
# outfile.write(api_call_response.text)

# Deserialize JSON API Response into Python object "data"
data = json.loads(api_call_response.text)

# Call our Device Type Update method for that particular PID
self.update_device_type_eox_data(pid, data)

i += 1
"""

# Step 2: Get all Serial Numbers for all Devices of that particular manufacturer
serial_numbers = self.get_serial_numbers(kwargs['manufacturer'])
self.stdout.write(self.style.SUCCESS('Gathering data for these Serial Numbers: ' + ', '.join(serial_numbers)))

Expand All @@ -318,10 +326,13 @@ def handle(self, *args, **kwargs):
api_call_response = requests.get(url, headers=api_call_headers)
self.stdout.write(self.style.SUCCESS('Call ' + url))

# Deserialize JSON API Response into Python object "data"
data = json.loads(api_call_response.text)

# Iterate through all serial numbers included in the API response
for device in data['serial_numbers']:
# print("Serial: %s - Is Covered?: %s - Warranty End Date: %s - Covered Product Line End Date: %s" % (device['sr_no'], device['is_covered'], device['warranty_end_date'], device['covered_product_line_end_date']))

# Call our Device Update method for that particular Device
self.update_device_eox_data(device)

i += 1
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
{% load filters %}

{% if cisco_device_support %}
<div class="panel panel-default">
<div class="panel-heading">
<strong>Cisco Device Support Information</strong>
<div class="card">
<h5 class="card-header">Cisco Device Support Information</h5>
<div class="card-body">
<table class="table table-hover panel-body attr-table">
<tbody>
<tr >
<td style="width: 40%">Is Covered by Contract?</td>
<td><i class="mdi {% if cisco_device_support.is_covered %}mdi-check-bold text-success{% else %}mdi-close-thick text-danger{% endif %}"></i></td>
</tr>
<tr {{ cisco_device_support.coverage_end_date|expiration_class }}>
<td>End of Contract Coverage Date</td>
<td>{{ cisco_device_support.coverage_end_date }}</td>
</tr>
<tr {{ cisco_device_support.warranty_end_date|expiration_class }}>
<td>End of Warranty Date</td>
<td>{{ cisco_device_support.warranty_end_date }}</td>
</tr>
</tbody>
</table>
</div>
<table class="table table-hover panel-body attr-table">
<tbody>
<tr {{ cisco_device_support.is_covered|coverage_class }}>
<td style="width: 40%">Is Covered by Contract?</td>
<td><span class="glyphicon {% if cisco_device_support.is_covered %}glyphicon-ok{% else %}glyphicon-remove{% endif %}" aria-hidden="true"></span></td>
</tr>
<tr {{ cisco_device_support.coverage_end_date|expiration_class }}>
<td>End of Contract Coverage Date</td>
<td>{{ cisco_device_support.coverage_end_date }}</td>
</tr>
<tr {{ cisco_device_support.warranty_end_date|expiration_class }}>
<td>End of Warranty Date</td>
<td>{{ cisco_device_support.warranty_end_date }}</td>
</tr>
</tbody>
</table>
</div>
{% else %}
{# no Cisco Device Support information available #}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
{% load filters %}

{% if cisco_device_type_support %}
<div class="panel panel-default">
<div class="panel-heading">
<strong>Cisco Device Type Support Information</strong>
<div class="card">
<h5 class="card-header">Cisco Device Type Support Information</h5>
<div class="card-body">
<table class="table table-hover panel-body attr-table">
<tbody>
<tr {{ cisco_device_type_support.end_of_sale_date|expiration_class }}>
<td style="width: 40%">End-of-Sale Date</td>
<td>{{ cisco_device_type_support.end_of_sale_date }}</td>
</tr>
<tr {{ cisco_device_type_support.end_of_sw_maintenance_releases|expiration_class }}>
<td>End of SW Maintenance Releases Date</td>
<td>{{ cisco_device_type_support.end_of_sw_maintenance_releases }}</td>
</tr>
<tr {{ cisco_device_type_support.end_of_routine_failure_analysis_date|expiration_class }}>
<td>End of Routine Failure Analysis Date</td>
<td>{{ cisco_device_type_support.end_of_routine_failure_analysis_date }}</td>
</tr>
<tr {{ cisco_device_type_support.end_of_svc_attach_date|expiration_class }}>
<td>End of New Service Attachment Date</td>
<td>{{ cisco_device_type_support.end_of_svc_attach_date }}</td>
</tr>
<tr {{ cisco_device_type_support.end_of_service_contract_renewal|expiration_class }}>
<td>End of Service Contract Renewal Date</td>
<td>{{ cisco_device_type_support.end_of_service_contract_renewal }}</td>
</tr>
<tr {{ cisco_device_type_support.end_of_security_vul_support_date|expiration_class }}>
<td>End of Vulnerability/Security Support</td>
<td>{{ cisco_device_type_support.end_of_security_vul_support_date }}</td>
</tr>
<tr {{ cisco_device_type_support.last_date_of_support|expiration_class }}>
<td>Last Date of Support</td>
<td>{{ cisco_device_type_support.last_date_of_support }}</td>
</tr>
</tbody>
</table>
</div>
<table class="table table-hover panel-body attr-table">
<tbody>
<tr {{ cisco_device_type_support.end_of_sale_date|expiration_class }}>
<td style="width: 40%">End-of-Sale Date</td>
<td>{{ cisco_device_type_support.end_of_sale_date }}</td>
</tr>
<tr {{ cisco_device_type_support.end_of_sw_maintenance_releases|expiration_class }}>
<td>End of SW Maintenance Releases Date</td>
<td>{{ cisco_device_type_support.end_of_sw_maintenance_releases }}</td>
</tr>
<tr {{ cisco_device_type_support.end_of_routine_failure_analysis_date|expiration_class }}>
<td>End of Routine Failure Analysis Date</td>
<td>{{ cisco_device_type_support.end_of_routine_failure_analysis_date }}</td>
</tr>
<tr {{ cisco_device_type_support.end_of_svc_attach_date|expiration_class }}>
<td>End of New Service Attachment Date</td>
<td>{{ cisco_device_type_support.end_of_svc_attach_date }}</td>
</tr>
<tr {{ cisco_device_type_support.end_of_service_contract_renewal|expiration_class }}>
<td>End of Service Contract Renewal Date</td>
<td>{{ cisco_device_type_support.end_of_service_contract_renewal }}</td>
</tr>
<tr {{ cisco_device_type_support.end_of_security_vul_support_date|expiration_class }}>
<td>End of Vulnerability/Security Support</td>
<td>{{ cisco_device_type_support.end_of_security_vul_support_date }}</td>
</tr>
<tr {{ cisco_device_type_support.last_date_of_support|expiration_class }}>
<td>Last Date of Support</td>
<td>{{ cisco_device_type_support.last_date_of_support }}</td>
</tr>
</tbody>
</table>
</div>
{% else %}
{# no Cisco Device Type Support information available #}
Expand Down

0 comments on commit b06d49b

Please sign in to comment.