-
Notifications
You must be signed in to change notification settings - Fork 4
/
meta2model.py
204 lines (190 loc) · 4.93 KB
/
meta2model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python
from __future__ import print_function
import sys
from pprint import pprint
from warnings import warn
from xml.etree import ElementTree as ET
from pynetdot.api import NetdotAPI
from pynetdot.fields import *
api = NetdotAPI(url=sys.argv[1], username=sys.argv[2], password=sys.argv[3])
# TODO get this list dynamically from netdot (somehow)
resources = [
#'accessright',
'audit',
'arpcache',
'arpcacheentry',
'asn',
'asset',
'availability',
'bgppeering',
'backbonecable',
'cablestrand',
'cabletype',
'circuit',
'circuitstatus',
'circuittype',
'closet',
'sitelink',
'contact',
'contactlist',
'contacttype',
#'datacache',
'device',
'deviceattr',
'deviceattrname',
'devicemodule',
'devicecontacts',
'dhcpattr',
'dhcpattrname',
'dhcpscope',
'dhcpscopeuse',
'dhcpscopetype',
'entity',
'entityrole',
'entitysite',
'entitytype',
'fwtable',
'fwtableentry',
'fibertype',
'floor',
'groupright',
'horizontalcable',
'interface',
'interfacevlan',
'ipservice',
'ipblock',
'ipblockattr',
'ipblockattrname',
'ipblockstatus',
'maintcontract',
'monitorstatus',
'oui',
'person',
'physaddr',
'physaddrattr',
'physaddrattrname',
'product',
'producttype',
'rr',
'rraddr',
'rrcname',
'rrds',
'rrhinfo',
'rrloc',
'rrmx',
'rrnaptr',
'rrns',
'rrptr',
'rrsrv',
'rrtxt',
'room',
'service',
#'schemainfo',
'site',
'sitesubnet',
'stpinstance',
#'closetpicture',
#'floorpicture',
#'sitepicture',
'splice',
'strandstatus',
'subnetzone',
'usertype',
#'userright',
'vlan',
'vlangroup',
'zonealias',
'zone',
'hostaudit',
#'savedqueries',
]
#resources = ['entity']
supported_types = {
'bigint': 'IntegerField',
'bool': 'BoolField',
'date': 'DateField',
'integer': 'IntegerField',
#'longblob': NOT supported
'numeric': 'StringField', # FIXME this is used for ip addresses which are rendered as strings by netdot REST
'text': 'StringField',
'timestamp': 'DateTimeField',
'varchar': 'StringField',
}
models = {}
reverse_links = {}
for r in resources:
resp = api.get('%s/meta_data' % r)
xml = ET.fromstring(resp.text)
model = {}
model['description'] = xml.attrib['description']
model['resource'] = xml.attrib['name']
model['class_name'] = model['resource']
model['id_field'] = xml.attrib['primary_key']
fields = []
# column tags
for c in xml.findall('columns'):
f_class = ''
f_args = ''
f_name = c.attrib['id']
f_reverse = ''
f_description = c.attrib['description']
c_type = c.attrib.get('type', '')
c_tag = c.attrib.get('tag', f_name)
f_args += ', display_name=\'%s\'' % c_tag
for c_child in c:
# check of linksto child tags and configure a link field
if c_child.tag == 'linksto':
f_class = 'LinkField'
f_args += ', link_to=\'%s\'' % c_child.attrib['table']
link_target = reverse_links.get(c_child.attrib['table'], [])
f_reverse = c_child.attrib.get('method', None)
if f_reverse:
reverse_def = {
'method': f_reverse,
'target': model['resource'],
'field': f_name
}
link_target.append(reverse_def)
reverse_links[c_child.attrib['table']] = link_target
if not f_class:
# this is a regular field
f_class = supported_types.get(c_type)
# skip id_fields
if f_name == model['id_field']:
continue
if not f_class:
warn('Usupported column type: %s %s %s' % (class_name, f_name, c_type))
continue
fields.append({'class':f_class, 'name':f_name, 'args':f_args, 'description':f_description})
model['fields'] = fields
# label tag
model['label'] = []
for c in xml.findall('label'):
model['label'].append(c.text)
# views
views = {}
views_element = xml.find('views')
for c in views_element:
view = views.get(c.tag, [])
view.append(c.text)
views[c.tag] = view
model['views'] = views
models[model['resource']] = model
#break
#pprint(models)
#sys.exit(1)
# print(results)
print('''from __future__ import absolute_import, division, print_function, unicode_literals
from builtins import str
import pynetdot.netdot as n
import pynetdot.fields as f
import pynetdot.models
''')
from jinja2 import Environment, FileSystemLoader
env = Environment(
loader=FileSystemLoader('./'),
trim_blocks=True,
lstrip_blocks=True
)
template = env.get_template('BaseModel.tmpl')
print(template.render(models=models, reverse_links=reverse_links))