def delete(self, find_by="name"): ret = super(IcingaServiceObject, self).call_url( path="/service" + "?" + "name=" + to_text(urlquote(self.data["object_name"])) + "&" + "host=" + to_text(urlquote(self.data["host"])), method="DELETE", ) return ret
def modify(self, find_by="name"): ret = super(IcingaServiceObject, self).call_url( path="/service" + "?" + "name=" + to_text(urlquote(self.data["object_name"])) + "&" + "host=" + to_text(urlquote(self.data["host"])), data=self.module.jsonify(self.data), method="POST", ) return ret
def exists(self, find_by="name"): ret = super( IcingaServiceObject, self).call_url(path="/service" + "?" + "name=" + to_text(urlquote(self.data["object_name"])) + "&" + "host=" + to_text(urlquote(self.data["host"]))) self.object_id = to_text(urlquote(self.data["object_name"])) if ret["code"] == 200: return True return False
def search_roles(self, search, **kwargs): search_url = _urljoin(self.api_server, self.available_api_versions['v1'], "search", "roles", "?") if search: search_url += '&autocomplete=' + to_text(urlquote(to_bytes(search))) tags = kwargs.get('tags', None) platforms = kwargs.get('platforms', None) page_size = kwargs.get('page_size', None) author = kwargs.get('author', None) if tags and isinstance(tags, string_types): tags = tags.split(',') search_url += '&tags_autocomplete=' + '+'.join(tags) if platforms and isinstance(platforms, string_types): platforms = platforms.split(',') search_url += '&platforms_autocomplete=' + '+'.join(platforms) if page_size: search_url += '&page_size=%s' % page_size if author: search_url += '&username_autocomplete=%s' % author data = self._call_galaxy(search_url) return data
def search_roles(self, search, **kwargs): search_url = self.baseurl + '/search/roles/?' if search: search_url += '&autocomplete=' + to_text(urlquote( to_bytes(search))) tags = kwargs.get('tags', None) platforms = kwargs.get('platforms', None) page_size = kwargs.get('page_size', None) author = kwargs.get('author', None) if tags and isinstance(tags, string_types): tags = tags.split(',') search_url += '&tags_autocomplete=' + '+'.join(tags) if platforms and isinstance(platforms, string_types): platforms = platforms.split(',') search_url += '&platforms_autocomplete=' + '+'.join(platforms) if page_size: search_url += '&page_size=%s' % page_size if author: search_url += '&username_autocomplete=%s' % author data = self.__call_galaxy(search_url) return data
def lookup_role_by_name(self, role_name, notify=True): """ Find a role by name. """ role_name = to_text(urlquote(to_bytes(role_name))) try: parts = role_name.split(".") user_name = ".".join(parts[0:-1]) role_name = parts[-1] if notify: display.display("- downloading role '%s', owned by %s" % (role_name, user_name)) except Exception: raise AnsibleError( "Invalid role name (%s). Specify role as format: username.rolename" % role_name) url = _urljoin(self.api_server, self.available_api_versions['v1'], "roles", "?owner__username=%s&name=%s" % (user_name, role_name)) data = self._call_galaxy(url) if len(data["results"]) != 0: return data["results"][0] return None
def search_roles(self, search, **kwargs): search_url = self.baseurl + '/search/roles/?' if search: search_url += '&autocomplete=' + urlquote(search) tags = kwargs.get('tags', None) platforms = kwargs.get('platforms', None) page_size = kwargs.get('page_size', None) author = kwargs.get('author', None) if tags and isinstance(tags, string_types): tags = tags.split(',') search_url += '&tags_autocomplete=' + '+'.join(tags) if platforms and isinstance(platforms, string_types): platforms = platforms.split(',') search_url += '&platforms_autocomplete=' + '+'.join(platforms) if page_size: search_url += '&page_size=%s' % page_size if author: search_url += '&username_autocomplete=%s' % author data = self.__call_galaxy(search_url) return data
def query(self, query="", resolved=False): """ Find all matching objects in the director and return the result of the api-call. Parameters: query: type str, default "", searchstring to limit the results. By default Director will search in the name of the resource. Usually that means 'object_name', but for services it also covers the host name. resolved: type bool, default False, resolve all object variables. If True, this will include all variables inherited via imports. Returns: the result of the api-call """ try: ret = self.call_url(path=self.path + "?q=" + to_text(urlquote(query)) + ("&resolved" if resolved else "")) if ret["code"] != 200: self.module.fail_json( msg="bad return code while querying: %d. Error message: %s" % (ret["code"], ret["error"])) except Exception as e: self.module.fail_json(msg="exception when querying: " + str(e)) return ret
def exists(self, find_by="name"): """ Check if the object already exists in the director. Parameters: find_by: type str, default "name", the object key to search for. by default 'name' of the object, however service apply rules have no name and have to be found by their id. Returns: boolean that tells wether the object exists """ ret = self.call_url(path=self.path + "?" + find_by + "=" + to_text(urlquote(self.data["object_name"]))) self.object_id = to_text(urlquote(self.data["object_name"])) if ret["code"] == 200: return True return False
def diff(self, find_by="name"): ret = super(IcingaServiceObject, self).call_url( path="/service" + "?" + "name=" + to_text(urlquote(self.data["object_name"])) + "&" + "host=" + to_text(urlquote(self.data["host"])), method="GET", ) data_from_director = json.loads(self.module.jsonify(ret["data"])) data_from_task = json.loads(self.module.jsonify(self.data)) diff = defaultdict(dict) for key, value in data_from_director.items(): value = self.scrub_diff_value(value) if key in data_from_task.keys() and value != data_from_task[key]: diff["before"][key] = "{val}".format(val=value) diff["after"][key] = "{val}".format(val=data_from_task[key]) return diff
def find(compute_api, wished_server, per_page=1): compute_api.module.debug("Getting inside find") # Only the name attribute is accepted in the Compute query API url = 'servers?name=%s&per_page=%d' % (urlquote(wished_server["name"]), per_page) response = compute_api.get(url) if not response.ok: msg = 'Error during server search: (%s) %s' % (response.status_code, response.json) compute_api.module.fail_json(msg=msg) search_results = response.json["servers"] return search_results
def __init__(self, module, path, data): super(IcingaServiceObject, self).__init__(module, path, data) self.module = module self.params = module.params self.path = path self.data = data self.object_id = None # set url parameters when service is assigned to a host if "host" in self.data: if self.data["host"]: param_service_type = "host=" param_service_type_filter = to_text(urlquote( self.data["host"])) # set url parameters when service is assigned to a serviceset if "service_set" in self.data: if self.data["service_set"]: param_service_type = "set=" param_service_type_filter = to_text( urlquote(self.data["service_set"])) self.url = ("/service" + "?" + "name=" + to_text(urlquote(self.data["object_name"])) + "&" + param_service_type + param_service_type_filter)
def lookup_role_by_name(self, role_name, notify=True): """ Find a role by name. """ role_name = urlquote(role_name) try: parts = role_name.split(".") user_name = ".".join(parts[0:-1]) role_name = parts[-1] if notify: display.display("- downloading role '%s', owned by %s" % (role_name, user_name)) except: raise AnsibleError("Invalid role name (%s). Specify role as format: username.rolename" % role_name) url = '%s/roles/?owner__username=%s&name=%s' % (self.baseurl, user_name, role_name) data = self.__call_galaxy(url) if len(data["results"]) != 0: return data["results"][0] return None
def exists(self, find_by="name"): ret = super(IcingaServiceObject, self).call_url(path=self.url) self.object_id = to_text(urlquote(self.data["object_name"])) if ret["code"] == 200: return True return False