jhg03a commented on a change in pull request #3585:
URL: https://github.com/apache/trafficcontrol/pull/3585#discussion_r494570913



##########
File path: infrastructure/ansible/dynamic.inventory/TO.py
##########
@@ -0,0 +1,243 @@
+#!/usr/bin/env python3
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import json
+import argparse
+import logging
+import os
+import collections
+from trafficops.tosession import TOSession
+
+def empty_inventory():
+    """Generate a valid empty inventory"""
+    return {'_meta': {'hostvars': {}}}
+
+
+class AnsibleInventory():
+    """Wrapper class for needed methods"""
+
+    def __init__(self, user, password, url, verify_cert):
+        """Init base members"""
+        self.to_user = user
+        self.to_pass = password
+        self.to_url = url
+        self.verify_cert = verify_cert
+
+    @staticmethod
+    def populate_server_profile_vars(api, profile_id):
+        """Generate the server profile variables once as we see it"""
+        server_vars = {}
+        server_vars['hosts'] = []
+        server_vars['vars'] = {}
+        profile = api.get_profiles(id=profile_id)[0]
+        server_vars['vars']['server_profile_description'] = 
profile[0]['description']
+        server_vars['vars']['server_profile_type'] = profile[0]['type']
+        server_vars['vars']['server_profile_routingDisabled'] = 
profile[0]['routingDisabled']
+        server_vars['vars']['server_profile_parameters'] = []
+        params = api.get_parameters_by_profile_id(id=profile_id)[0]
+        for param in params:
+            tmp_param = {
+                'name': param['name'],
+                'value': param['value'],
+                'configFile': param['configFile']}
+            server_vars['vars']['server_profile_parameters'].append(tmp_param)
+        return server_vars
+
+    @staticmethod
+    def populate_cachegroups(api, cachegroup_id):
+        """Generate the values for cachegroups once on first sight"""
+        var_data = {}
+        cgdata = collections.namedtuple('Cgdata', ['cgvars',
+                                                   'primary_parent_group_name',
+                                                   
'secondary_parent_group_name'])
+        var_data['hosts'] = []
+        var_data['vars'] = {}
+        cachegroup = api.get_cachegroups(id=cachegroup_id)[0]
+        var_data['vars']['cachegroup_name'] = cachegroup[0]['name']
+        var_data['vars']['cachegroup_shortName'] = cachegroup[0]['shortName']
+        var_data['vars']['cachegroup_parentCachegroupName'] = \
+            cachegroup[0]['parentCachegroupName']
+        var_data['vars']['cachegroup_secondaryParentCachegroupName'] = \
+            cachegroup[0]['secondaryParentCachegroupName']
+        var_data['vars']['cachegroup_typeName'] = cachegroup[0]['typeName']
+        if cachegroup[0]['parentCachegroupName'] is None:
+            flat_parent_cg = "parentCachegroup|None"
+        else:
+            flat_parent_cg = "parentCachegroup|" + \
+                cachegroup[0]['parentCachegroupName']
+
+        if cachegroup[0]['secondaryParentCachegroupName'] is None:
+            flat_second_parent_cg = "secondaryParentCachegroup|None"
+        else:
+            flat_second_parent_cg = "secondaryParentCachegroup|" + \
+                cachegroup[0]['secondaryParentCachegroupName']
+        out = cgdata(cgvars=var_data,
+                     primary_parent_group_name=flat_parent_cg,
+                     secondary_parent_group_name=flat_second_parent_cg)
+        return out
+
+    def generate_inventory_list(self, target_to):  # pylint: 
disable=too-many-statements
+        """Generate the inventory list for the specified TrafficOps instance"""
+        with TOSession(self.to_url, verify_cert=self.verify_cert) as 
traffic_ops_api:
+            traffic_ops_api.login(self.to_user, self.to_pass)
+            servers = traffic_ops_api.get_servers()[0]
+            out = {}
+            out['_meta'] = {}
+            out['_meta']['hostvars'] = {}
+            out[target_to] = {}
+            out[target_to]['hosts'] = []
+            out["ungrouped"] = {}
+            out['ungrouped']['hosts'] = []
+            out['cachegroup'] = {}
+            out['cachegroup']['children'] = []
+            out['server_type'] = {}
+            out['server_type']['children'] = []
+            out['server_cdnName'] = {}
+            out['server_cdnName']['children'] = []
+            out['server_profile'] = {}
+            out['server_profile']['children'] = []
+            out['server_status'] = {}
+            out['server_status']['children'] = []
+            for server in servers:
+                fqdn = server['hostName'] + '.' + server['domainName']
+                out["ungrouped"]['hosts'].append(fqdn)
+                out[target_to]['hosts'].append(fqdn)
+                out['_meta']['hostvars'][fqdn] = {}
+                out['_meta']['hostvars'][fqdn]['server_toFQDN'] = target_to
+                out['_meta']['hostvars'][fqdn]['server_cachegroup'] = 
server['cachegroup']
+                out['_meta']['hostvars'][fqdn]['server_cdnName'] = 
server['cdnName']
+                out['_meta']['hostvars'][fqdn]['server_id'] = server['id']
+                out['_meta']['hostvars'][fqdn]['server_ipAddress'] = 
server['ipAddress']
+                out['_meta']['hostvars'][fqdn]['server_ip6Address'] = 
server['ip6Address']
+                out['_meta']['hostvars'][fqdn]['server_offlineReason'] = 
server['offlineReason']
+                out['_meta']['hostvars'][fqdn]['server_physLocation'] = 
server['physLocation']
+                out['_meta']['hostvars'][fqdn]['server_profile'] = 
server['profile']
+                out['_meta']['hostvars'][fqdn]['server_profileDesc'] = 
server['profileDesc']
+                out['_meta']['hostvars'][fqdn]['server_status'] = 
server['status']
+                out['_meta']['hostvars'][fqdn]['server_type'] = server['type']
+                flat_server_profile = "server_profile|" + server['profile']
+                flat_cachegroup = "cachegroup|" + server['cachegroup']
+                flat_server_type = "server_type|" + server['type']
+                flat_server_cdn_name = "server_cdnName|" + server['cdnName']
+                flat_server_status = "server_status|" + server['status']
+                if flat_server_profile not in out:
+                    
out['server_profile']['children'].append(flat_server_profile)
+                    out[flat_server_profile] = 
self.populate_server_profile_vars(
+                        traffic_ops_api,
+                        server['profileId'])
+                out[flat_server_profile]['hosts'].append(fqdn)
+                if flat_cachegroup not in out:
+                    out['cachegroup']['children'].append(flat_cachegroup)
+                    cgdata = self.populate_cachegroups(
+                        traffic_ops_api,
+                        server['cachegroupId'])
+                    out[flat_cachegroup] = cgdata.cgvars
+                    flat_parent_cg = cgdata.primary_parent_group_name
+                    flat_second_parent_cg = cgdata.secondary_parent_group_name
+                    if flat_parent_cg not in out:
+                        out[flat_parent_cg] = {}
+                        out[flat_parent_cg]['children'] = []
+                    if flat_second_parent_cg not in out:
+                        out[flat_second_parent_cg] = {}
+                        out[flat_second_parent_cg]['children'] = []
+                    out[flat_parent_cg]['children'].append(flat_cachegroup)
+                    
out[flat_second_parent_cg]['children'].append(flat_cachegroup)
+                out[flat_cachegroup]['hosts'].append(fqdn)
+                if flat_server_type not in out:
+                    out['server_type']['children'].append(flat_server_type)
+                    out[flat_server_type] = {}
+                    out[flat_server_type]['hosts'] = []
+                out[flat_server_type]['hosts'].append(fqdn)
+                if flat_server_cdn_name not in out:
+                    
out['server_cdnName']['children'].append(flat_server_cdn_name)
+                    out[flat_server_cdn_name] = {}
+                    out[flat_server_cdn_name]['hosts'] = []
+                out[flat_server_cdn_name]['hosts'].append(fqdn)
+                if flat_server_status not in out:
+                    out['server_status']['children'].append(flat_server_status)
+                    out[flat_server_status] = {}
+                    out[flat_server_status]['hosts'] = []
+                out[flat_server_status]['hosts'].append(fqdn)
+        return out
+
+    def to_inventory(self):
+        return self.generate_inventory_list(self.to_url)
+
+#
+# Thanks to Maxim for the snipit on handling bool parameters.
+# 
https://stackoverflow.com/questions/15008758/parsing-boolean-values-with-argparse
+#
+
+
+def str2bool(v):
+    if isinstance(v, bool):
+        return v
+    if v.lower() in ('yes', 'true', 't', 'y', '1'):
+        return True
+    elif v.lower() in ('no', 'false', 'f', 'n', '0'):

Review comment:
       Addressed with commit

##########
File path: infrastructure/ansible/roles/dataset_loader/files/selection.set.py
##########
@@ -0,0 +1,49 @@
+#!/usr/bin/python

Review comment:
       Addressed with commit

##########
File path: infrastructure/ansible/roles/dataset_loader/files/selection.set.py
##########
@@ -0,0 +1,49 @@
+#!/usr/bin/python
+
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# This script is used to provide a round-robin merging of two lists
+
+import sys
+import json
+
+if len(sys.argv) < 3 or len(sys.argv) > 4:
+    print "{}"

Review comment:
       Addressed with commit

##########
File path: infrastructure/ansible/roles/dataset_loader/files/selection.set.py
##########
@@ -0,0 +1,49 @@
+#!/usr/bin/python
+
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# This script is used to provide a round-robin merging of two lists
+
+import sys
+import json
+
+if len(sys.argv) < 3 or len(sys.argv) > 4:
+    print "{}"
+    sys.exit(0)
+
+cdn_csv_list = sys.argv[1].split(',')
+fqdn_csv_list = sys.argv[2].split(',')
+option = ''
+if len(sys.argv) == 4:
+    option = sys.argv[3]
+cdn_csv_list.sort()
+fqdn_csv_list.sort()
+
+step_size = len(cdn_csv_list)
+out_list_normal = {}
+for i, val in enumerate(cdn_csv_list):
+    sublist = fqdn_csv_list[i:]
+    out_list_normal[val] = ','.join(sublist[::step_size])
+
+out_list_denormal = {}
+for val, csvlist in out_list_normal.items():
+    for i in csvlist.split(','):
+        if i != "":
+            out_list_denormal[i] = val
+
+if option == 'denormalize':
+    print json.dumps(out_list_denormal)

Review comment:
       Addressed with commit

##########
File path: infrastructure/ansible/roles/dataset_loader/files/selection.set.py
##########
@@ -0,0 +1,49 @@
+#!/usr/bin/python
+
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# This script is used to provide a round-robin merging of two lists
+
+import sys
+import json
+
+if len(sys.argv) < 3 or len(sys.argv) > 4:
+    print "{}"
+    sys.exit(0)
+
+cdn_csv_list = sys.argv[1].split(',')
+fqdn_csv_list = sys.argv[2].split(',')
+option = ''
+if len(sys.argv) == 4:
+    option = sys.argv[3]
+cdn_csv_list.sort()
+fqdn_csv_list.sort()
+
+step_size = len(cdn_csv_list)
+out_list_normal = {}
+for i, val in enumerate(cdn_csv_list):
+    sublist = fqdn_csv_list[i:]
+    out_list_normal[val] = ','.join(sublist[::step_size])
+
+out_list_denormal = {}
+for val, csvlist in out_list_normal.items():
+    for i in csvlist.split(','):
+        if i != "":
+            out_list_denormal[i] = val
+
+if option == 'denormalize':
+    print json.dumps(out_list_denormal)
+else:
+    print json.dumps(out_list_normal)

Review comment:
       Addressed with commit




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to