# @Date: 2019-12-20 09:18:14 # @Last Modified by: ronanjs # @Last Modified time: 2020-07-13 15:17:13 try: from ansible.module_utils.logger import Logger except ImportError: from module_utils.logger import Logger import requests import json import re import os from functools import cmp_to_key log = Logger("linker") class Linker: def __init__(self, datamodel, rest=None): self.datamodel = datamodel self.rest = rest def resolveSingleObject(self, ref): selection = self._resolve(ref) if selection == None: return if selection != None and selection.count() > 1: raise Exception("There are too many choices for reference %s: %s" %
# @Date: 2019-12-20 09:18:14 # @Last Modified by: ronanjs # @Last Modified time: 2020-02-07 11:05:55 try: from ansible.module_utils.logger import Logger except ImportError: from module_utils.logger import Logger import requests import pickle import json import re import os log = Logger("data-model") class DataModel: def __init__(self): self._session = None self.chassis = [] self.props = {'names': [], 'ports': []} self.unserialize() def session(self): return self._session def new(self, session, chassis, props): self._session = session self.chassis = chassis
from module_utils.datamodel import DataModel from module_utils.objtree import ObjectTree from module_utils.stcrest import StcRest from module_utils.tags import TagManager from module_utils.xpath import Linker from module_utils.logger import Logger from module_utils.drv import DRV from module_utils.utils import * import requests import pickle import time import json import re log = Logger("metamodel") class MetaModel: def __init__(self, server="127.0.0.1"): self.datamodel = DataModel() self.rest = StcRest(server, self.datamodel.session()) self.xpath = Linker(self.datamodel, self.rest) self.templater = Templater(self.datamodel) self.tagMgr = TagManager(self.rest) def action(self, params): action = params["action"] count = params["count"] if "count" in params else 1
# @Last Modified by: ronanjs # @Last Modified time: 2020-07-17 11:10:11 try: from ansible.module_utils.datamodel import DataModel from ansible.module_utils.logger import Logger except ImportError: from module_utils.datamodel import DataModel from module_utils.logger import Logger import requests import pickle import json import re, fnmatch log = Logger("stc-rest") class StcRest: requests = [] def __init__(self, server="127.0.0.1", session=""): self.server = server self.session = session self.errorInfo = None self.conn = requests.Session() self.conn.headers.update({ 'Accept': 'application/json', "X-STC-API-Session": self.session })
def main(): log = Logger("main") fields = { "action": { "required": True, "type": "str" }, "count": { "required": False, "type": "int", "default": 1 }, #create "objects": { "required": False, "type": "list" }, "under": { "required": False, "type": "str" }, #config "properties": { "required": False, "type": "dict" }, "object": { "required": False, "type": "str" }, # session "user": { "required": False, "type": "str" }, "name": { "required": False, "type": "str" }, "chassis": { "required": False, "type": "str" }, "reset_existing": { "required": False, "type": "bool", "default": True, }, "kill_existing": { "required": False, "type": "bool" }, # perform "command": { "required": False, "type": "str" }, # load "datamodel": { "required": False, "type": "str" }, # wait "timeout": { "required": False, "type": "int" }, "until": { "required": False, "type": "str" }, # download "file": { "required": False, "type": "str" }, "dest": { "required": False, "type": "str" }, } module = AnsibleModule(argument_spec=fields, no_log=False) try: mm = MetaModel() result = mm.action(module.params) mm.serialize() if result.isError(): try: model = mm.datamodel.tree() except: model = None module.fail_json(msg=str(result.err).split("\n"), logs=Logger.logs, model=model) else: module.exit_json(changed=True, result=result.val, logs=Logger.logs) except Exception as error: log.error("Exception %s" % error) module.fail_json(msg="Oh no! Exception thrown...", result=str(error).split("\n"), trace=traceback.format_exc().split("\n"), logs=Logger.logs) return
# -*- coding: utf-8 -*- # @Author: rjezequel # @Date: 2019-12-20 09:18:14 # @Last Modified by: ronanjs # @Last Modified time: 2020-07-13 16:58:59 try: from ansible.module_utils.logger import Logger except ImportError: from module_utils.logger import Logger log = Logger("TagManager") class TagManager: def __init__(self, rstHdl): self.rest = rstHdl self.tags = {} def getPoppedTags(self, objects): log.info("POPPING tags From object: %s" % (str(objects))) val = objects.get('tag') if val == None: log.info("No tags contained in objects!") return {} else: objects.pop("tag") return {'tag': val}
# -*- coding: utf-8 -*- # @Author: ronanjs # @Date: 2020-01-13 14:02:28 # @Last Modified by: ronanjs # @Last Modified time: 2020-02-07 10:53:02 import sys try: from ansible.module_utils.logger import Logger except ImportError: from module_utils.logger import Logger if sys.version_info[0] >= 3: unicode = str log = Logger("obj-tree") class ObjectTree: def __init__(self, objects): self.objects = [] self.create(objects, "", {}, "", {}) self.objects.reverse() def create(self, attributes, parent, properties, under, children): # print(">>>>",type(attributes),parent,":",attributes) if type(attributes) is list: # Expect that the sub-childs are dictionnaries