Beispiel #1
0
 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
Beispiel #2
0
 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
Beispiel #3
0
 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
Beispiel #4
0
    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
Beispiel #5
0
    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
Beispiel #6
0
    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
Beispiel #7
0
    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
Beispiel #8
0
    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
Beispiel #10
0
    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
Beispiel #11
0
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
Beispiel #12
0
    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)
Beispiel #13
0
    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
Beispiel #14
0
 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