-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathcrawler.py
More file actions
85 lines (70 loc) · 2.84 KB
/
crawler.py
File metadata and controls
85 lines (70 loc) · 2.84 KB
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
from __future__ import absolute_import
# Copyright 2016 F5 Networks Inc.
#
# 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.
#
from f5.devtools.code_generator import DEVICECONFDIR
import json
import os
from pprint import pprint as pp
from urlparse import urlparse
class OCCrawler(object):
def __init__(self, bigip, OC_path_element):
self.bigip = bigip
self.session = self.bigip._meta_data[u"icr_session"]
self.uri = self.bigip._meta_data['uri'] + OC_path_element
self.configs = [self.session.get(self.uri).json()]
self.build_referenced_uris()
def _get_uri_from_OC_item(self, item):
if u"reference" in item and u"link" in item[u"reference"]:
return item[u"reference"][u"link"]\
.replace("localhost",
self.bigip._meta_data[u"hostname"])
def build_referenced_uris(self):
self.referenced = []
for item in self.configs[0][u"items"]:
self.referenced.append(self._get_uri_from_OC_item(item))
def get_referenced_configs(self):
for uri in self.referenced:
self.configs.append(self.session.get(uri).json())
class ConfigWriter(object):
def __init__(self, config_list, complete_oc_name):
self.oc_name = complete_oc_name
self.oc_basename = self.oc_name.split('/')[-1]
self.configs = config_list
def _get_fname(self, conf):
sl = conf[u"selfLink"]
scheme, netloc, path, params, qargs, frags = urlparse(sl)
ps = path.split('/')
if ps[-1] == self.oc_basename:
return self.oc_basename + '_GET'
else:
return self.oc_basename + '_' + ps[-1] + '_GET'
def dump_configs(self):
for conf in self.configs:
fname = self._get_fname(conf)
if not os.path.exists(os.path.join(DEVICECONFDIR, fname)):
outname = os.path.join(DEVICECONFDIR, fname) + ".json"
with open(outname, 'w') as fh:
json.dump(conf, fh)
def main():
from f5.bigip import BigIP
b = BigIP('10.190.5.7', 'admin', 'admin')
occrawler = OCCrawler(b, 'ltm/persistence')
pp(occrawler.referenced)
occrawler.get_referenced_configs()
pp(occrawler.configs)
config_writer = ConfigWriter(occrawler.configs, u"ltm/persistence")
config_writer.dump_configs()
if __name__ == '__main__':
main()