Skip to content

Commit

Permalink
Merge pull request #12 from cscaglioned42/D42-31465
Browse files Browse the repository at this point in the history
D42-31465 - GitHub Freshservice - Asset type not being set correctly for Hosts
Fixed host devices from D42 becoming Unix Server and Windows Server asset types in Freshservice instead of the Host asset type. This issue was being caused because we were checking for Unix Server and Windows Server before checking for Host.
Updated sync to be able to correct these incorrect asset types.
  • Loading branch information
cscaglioned42 committed Nov 17, 2023
2 parents a23e92e + c13065f commit 60a4807
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
21 changes: 19 additions & 2 deletions d42_sd_sync.py
Expand Up @@ -29,6 +29,9 @@
RELATIONSHIPS_JOB_WAIT_SECONDS = int(math.ceil(RELATIONSHIP_BATCH_SIZE / float(RELATIONSHIPS_CREATED_PER_SECOND)))
ASSET_TYPE_BUSINESS_SERVICE = "Business Service"
ASSET_TYPE_SERVER = "Server"
ASSET_TYPE_UNIX_SERVER = "Unix Server"
ASSET_TYPE_WINDOWS_SERVER = "Windows Server"
ASSET_TYPE_HOST = "Host"

parser = argparse.ArgumentParser(description="freshservice")

Expand Down Expand Up @@ -227,14 +230,25 @@ def update_objects_from_server(sources, _target, mapping):

asset_type_fields_map = dict()
server_asset_type_id = find_object_id_in_map(asset_types_map, ASSET_TYPE_SERVER)
unix_server_asset_type_id = find_object_id_in_map(asset_types_map, ASSET_TYPE_UNIX_SERVER)
windows_server_asset_type_id = find_object_id_in_map(asset_types_map, ASSET_TYPE_WINDOWS_SERVER)
host_asset_type_id = find_object_id_in_map(asset_types_map, ASSET_TYPE_HOST)

for source in sources:
error_skip = False
while True:
try:
existing_object = find_object_in_map(existing_objects_map, source["name"])
if existing_object is None or existing_object["asset_type_id"] == server_asset_type_id:
asset_type_id = find_object_id_in_map(asset_types_map, source["asset_type"])
source_asset_type_id = find_object_id_in_map(asset_types_map, source["asset_type"])

# If we have an existing asset with an asset type of Windows Server or Unix Server and
# we determined that the asset type should be Host, we will update it to Host since previously
# we were bringing in host devices as Windows Server or Unix Server asset types instead of the
# Host asset type.
if existing_object is None or existing_object["asset_type_id"] == server_asset_type_id or \
(existing_object["asset_type_id"] in [unix_server_asset_type_id, windows_server_asset_type_id] and \
source_asset_type_id == host_asset_type_id):
asset_type_id = source_asset_type_id
else:
asset_type_id = existing_object["asset_type_id"]

Expand Down Expand Up @@ -363,6 +377,9 @@ def update_objects_from_server(sources, _target, mapping):
data["agent_id"] = existing_object["agent_id"]
updated_asset_id = freshservice.update_asset(data, existing_object["display_id"])
logger.info("updated existing asset %d" % updated_asset_id)
# If the asset type changed for this asset, update it in the cache.
if existing_object["asset_type_id"] != asset_type_id:
existing_object["asset_type_id"] = asset_type_id

break
except FreshServiceDuplicateValueError:
Expand Down
16 changes: 8 additions & 8 deletions mapping.xml.sample
Expand Up @@ -187,6 +187,8 @@

select view_device_v2.*,
case
WHEN view_device_v2.virtual_host AND lower(view_device_v2.os_name) like '%esxi%' THEN 'VMware VCenter Host'
WHEN view_device_v2.virtual_host THEN 'Host'
WHEN lower(view_device_v2.type) = 'physical' AND lower(view_device_v2.physicalsubtype) = 'laptop' THEN 'Laptop'
WHEN lower(view_device_v2.type) = 'physical' AND lower(view_device_v2.physicalsubtype) = 'workstation' THEN 'Desktop'
WHEN lower(view_device_v2.type) = 'physical' AND lower(view_device_v2.physicalsubtype) = 'network printer' THEN 'Printer'
Expand All @@ -198,8 +200,6 @@
WHEN lower(view_device_v2.type) = 'physical' AND lower(view_device_v2.os_name) similar to '%(unix|z/os|z os|zos|hp-ux|os400|os/400|os 400|linux|amazon|ubuntu|centos|redhat|debian|sles|suse|gentoo|oracle|freebsd|rhel|red hat|fedora|alma|rocky|arch)%' THEN 'Unix Server'
WHEN lower(view_device_v2.type) = 'physical' AND lower(view_device_v2.os_name) similar to '%(aix)%' THEN 'AIX Server'
WHEN lower(view_device_v2.type) = 'physical' AND lower(view_device_v2.os_name) similar to '%(solaris|sunos|sun os)%' THEN 'Solaris Server'
WHEN view_device_v2.virtual_host AND lower(view_device_v2.os_name) like '%esxi%' THEN 'VMware VCenter Host'
WHEN view_device_v2.virtual_host THEN 'Host'
WHEN lower(view_device_v2.type) = 'virtual' AND lower(view_device_v2.virtualsubtype) = 'vmware' THEN 'VMware VCenter VM'
WHEN lower(view_device_v2.type) = 'virtual' AND lower(view_device_v2.virtualsubtype) = 'amazon ec2 instance' THEN 'AWS VM'
WHEN lower(view_device_v2.type) = 'virtual' AND lower(view_device_v2.virtualsubtype) = 'azure virtual machine' THEN 'Azure VM'
Expand Down Expand Up @@ -716,14 +716,14 @@

select view_device_v1.*,
case
WHEN view_device_v1.virtual_host AND lower(view_device_v1.os_name) like '%esxi%' THEN 'VMware VCenter Host'
WHEN view_device_v1.virtual_host THEN 'Host'
WHEN lower(view_device_v1.os_name) similar to '%(f5|netscaler)%' THEN 'Load Balancer'
WHEN view_device_v1.network_device THEN 'Switch'
WHEN lower(view_device_v1.type) = 'physical' AND lower(view_device_v1.os_name) similar to '%(windows|microsoft)%' AND lower(view_device_v1.os_name) like '%server%' THEN 'Windows Server'
WHEN lower(view_device_v1.type) = 'physical' AND lower(view_device_v1.os_name) similar to '%(unix|z/os|z os|zos|hp-ux|os400|os/400|os 400|linux|amazon|ubuntu|centos|redhat|debian|sles|suse|gentoo|oracle|freebsd|rhel|red hat|fedora|alma|rocky|arch)%' THEN 'Unix Server'
WHEN lower(view_device_v1.type) = 'physical' AND lower(view_device_v1.os_name) similar to '%(aix)%' THEN 'AIX Server'
WHEN lower(view_device_v1.type) = 'physical' AND lower(view_device_v1.os_name) similar to '%(solaris|sunos|sun os)%' THEN 'Solaris Server'
WHEN view_device_v1.virtual_host AND lower(view_device_v1.os_name) like '%esxi%' THEN 'VMware VCenter Host'
WHEN view_device_v1.virtual_host THEN 'Host'
WHEN lower(view_device_v1.type) = 'virtual' AND lower(view_device_v1.virtual_subtype) = 'vmware' THEN 'VMware VCenter VM'
WHEN lower(view_device_v1.type) = 'virtual' AND lower(view_device_v1.virtual_subtype) = 'amazon ec2 instance' THEN 'AWS VM'
WHEN lower(view_device_v1.type) = 'virtual' AND lower(view_device_v1.virtual_subtype) = 'azure virtual machine' THEN 'Azure VM'
Expand Down Expand Up @@ -1135,6 +1135,8 @@
(
SELECT
case
WHEN view_device_v2.virtual_host AND lower(view_device_v2.os_name) like '%esxi%' THEN 'VMware VCenter Host'
WHEN view_device_v2.virtual_host THEN 'Host'
WHEN lower(view_device_v2.type) = 'physical' AND lower(view_device_v2.physicalsubtype) = 'laptop' THEN 'Computer'
WHEN lower(view_device_v2.type) = 'physical' AND lower(view_device_v2.physicalsubtype) = 'workstation' THEN 'Computer'
WHEN lower(view_device_v2.type) = 'physical' AND lower(view_device_v2.physicalsubtype) = 'network printer' THEN 'Printer'
Expand All @@ -1146,8 +1148,6 @@
WHEN lower(view_device_v2.type) = 'physical' AND lower(view_device_v2.os_name) similar to '%(unix|z/os|z os|zos|hp-ux|os400|os/400|os 400|linux|amazon|ubuntu|centos|redhat|debian|sles|suse|gentoo|oracle|freebsd|rhel|red hat|fedora|alma|rocky|arch)%' THEN 'Computer'
WHEN lower(view_device_v2.type) = 'physical' AND lower(view_device_v2.os_name) similar to '%(aix)%' THEN 'Computer'
WHEN lower(view_device_v2.type) = 'physical' AND lower(view_device_v2.os_name) similar to '%(solaris|sunos|sun os)%' THEN 'Computer'
WHEN view_device_v2.virtual_host AND lower(view_device_v2.os_name) like '%esxi%' THEN 'VMware VCenter Host'
WHEN view_device_v2.virtual_host THEN 'Host'
WHEN lower(view_device_v2.type) = 'virtual' AND lower(view_device_v2.virtualsubtype) = 'vmware' THEN 'Virtual Machine'
WHEN lower(view_device_v2.type) = 'virtual' AND lower(view_device_v2.virtualsubtype) = 'amazon ec2 instance' THEN 'Virtual Machine'
WHEN lower(view_device_v2.type) = 'virtual' AND lower(view_device_v2.virtualsubtype) = 'azure virtual machine' THEN 'Virtual Machine'
Expand Down Expand Up @@ -1179,14 +1179,14 @@
(
SELECT
case
WHEN view_device_v1.virtual_host AND lower(view_device_v1.os_name) like '%esxi%' THEN 'VMware VCenter Host'
WHEN view_device_v1.virtual_host THEN 'Host'
WHEN lower(view_device_v1.os_name) similar to '%(f5|netscaler)%' THEN 'Load Balancer'
WHEN view_device_v1.network_device THEN 'Switch'
WHEN lower(view_device_v1.type) = 'physical' AND lower(view_device_v1.os_name) similar to '%(windows|microsoft)%' AND lower(view_device_v1.os_name) like '%server%' THEN 'Computer'
WHEN lower(view_device_v1.type) = 'physical' AND lower(view_device_v1.os_name) similar to '%(unix|z/os|z os|zos|hp-ux|os400|os/400|os 400|linux|amazon|ubuntu|centos|redhat|debian|sles|suse|gentoo|oracle|freebsd|rhel|red hat|fedora|alma|rocky|arch)%' THEN 'Computer'
WHEN lower(view_device_v1.type) = 'physical' AND lower(view_device_v1.os_name) similar to '%(aix)%' THEN 'Computer'
WHEN lower(view_device_v1.type) = 'physical' AND lower(view_device_v1.os_name) similar to '%(solaris|sunos|sun os)%' THEN 'Computer'
WHEN view_device_v1.virtual_host AND lower(view_device_v1.os_name) like '%esxi%' THEN 'VMware VCenter Host'
WHEN view_device_v1.virtual_host THEN 'Host'
WHEN lower(view_device_v1.type) = 'virtual' AND lower(view_device_v1.virtual_subtype) = 'vmware' THEN 'Virtual Machine'
WHEN lower(view_device_v1.type) = 'virtual' AND lower(view_device_v1.virtual_subtype) = 'amazon ec2 instance' THEN 'Virtual Machine'
WHEN lower(view_device_v1.type) = 'virtual' AND lower(view_device_v1.virtual_subtype) = 'azure virtual machine' THEN 'Virtual Machine'
Expand Down

0 comments on commit 60a4807

Please sign in to comment.