Пример #1
0
    def irange(self, nas):
        tmp_url = "/core/range/usage_text/"
        url = "{0}{1}".format(REMOTE, tmp_url)
        headers = {'content-type': 'application/json'}
        start, end = nas.irange.split(',')
        search = {'start': start, 'end': end}
        if nas.d_integers:
            search['format'] = 'integers'
        resp = requests.get(url, params=search, headers=headers,
                            auth=auth())
        if resp.status_code == 500:
            resp_list = [
                "CLIENT ERROR! (Please email this output to a code monkey)"
            ]
            self.error_out(nas, search, resp, resp_list=resp_list)
            return
        results = self.get_resp_dict(resp)

        def display_ranges(free_ranges):
            ret_list = []
            for fstart, fend in free_ranges:
                ret_list.append("{0} to {1}".format(fstart, fend))
            return ret_list

        if not results:
            return 1, []
        else:
            if nas.p_json:
                return 0, [json.dumps(results, indent=2)]
            resp_list = ["# of Used IPs: {0}".format(results['used']),
                         "# of Unused IPs: {0}".format(results['unused']),
                         "------ Vacant IP ranges ------"]
            resp_list += display_ranges(results['free_ranges'])
            return 0, resp_list
Пример #2
0
 def detail(self, nas):
     url = self.object_url.format(API_MAJOR_VERSION, self.resource_name,
                                  nas.pk)
     url = "{0}{1}?format=json".format(REMOTE, url)
     headers = {'content-type': 'application/json'}
     resp = requests.get(url, headers=headers, auth=auth())
     return self.handle_resp(nas, {}, resp)
Пример #3
0
 def detail(self, nas):
     url = "{0}{1}?format=json".format(REMOTE, self.detail_url(nas))
     headers = {'content-type': 'application/json'}
     if nas.DEBUG:
         sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format(
             'get', url, {}
         ))
     resp = requests.get(url, headers=headers, auth=auth())
     return self.handle_resp(nas, {}, resp)
Пример #4
0
 def do_import(self, nas, json_data):
     tmp_url = "/en-US/core/service/import/"
     url = "{0}{1}".format(REMOTE, tmp_url)
     headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
     resp = requests.post(url, data=json_data, headers=headers, auth=auth())
     if nas.DEBUG:
         sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format(
             'get', url, json_data))
     nas.p_json = True  # Do this so we can play with the response
     return self.handle_service_resp(nas, json_data, resp)
Пример #5
0
 def service_export(self, nas):
     tmp_url = "/core/service/export/"
     url = "{0}{1}".format(REMOTE, tmp_url)
     headers = {'content-type': 'application/json'}
     query = {'search': nas.query}
     resp = requests.get(url, params=query, headers=headers, auth=auth())
     if nas.DEBUG:
         sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format(
             'get', url, query))
     nas.p_json = True  # Do this so we can play with the response
     return self.handle_service_resp(nas, query, resp)
Пример #6
0
 def do_import(self, nas, json_data):
     tmp_url = "/en-US/core/service/import/"
     url = "{0}{1}".format(REMOTE, tmp_url)
     headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
     resp = requests.post(url, data=json_data, headers=headers, auth=auth())
     if nas.DEBUG:
         sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format(
             'get', url, json_data
         ))
     nas.p_json = True  # Do this so we can play with the response
     return self.handle_service_resp(nas, json_data, resp)
Пример #7
0
 def service_export(self, nas):
     tmp_url = "/core/service/export/"
     url = "{0}{1}".format(REMOTE, tmp_url)
     headers = {'content-type': 'application/json'}
     query = {'search': nas.query}
     resp = requests.get(url, params=query, headers=headers, auth=auth())
     if nas.DEBUG:
         sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format(
             'get', url, query
         ))
     nas.p_json = True  # Do this so we can play with the response
     return self.handle_service_resp(nas, query, resp)
Пример #8
0
    def action(self, nas, url, method, data, form_encode=True):
        headers = {'content-type': 'application/json'}
        if form_encode:
            wire_data = json.dumps(data, indent=2)
        else:
            wire_data = data

        if nas.DEBUG:
            sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format(
                method.__name__, url, json.dumps(data, indent=2)
            ))
        resp = method(url, headers=headers, data=wire_data, auth=auth())
        return self.handle_resp(nas, data, resp)
Пример #9
0
    def query(self, nas):
        tmp_url = "/bulk_action/export/"
        url = "{0}{1}".format(REMOTE, tmp_url)
        headers = {'content-type': 'application/json'}
        query = {'q': nas.query}
        resp = requests.get(url, params=query, headers=headers, auth=auth())
        if nas.DEBUG:
            sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format(
                'get', url, query
            ))

        nas.p_json = True  # Do this so we can play with the response

        ret_code, raw_results = self.handle_resp(nas, query, resp)
        if ret_code:
            return (ret_code, raw_results)  # repack and go home
        results = json.loads(raw_results[0])
        return ret_code, self.show_lookup_paths(results)
Пример #10
0
    def schema(self, nas):
        url = "{0}{1}".format(REMOTE, self.search_schema_url)
        schema = {'class': nas.schema}
        resp = requests.get(url, params=schema, auth=auth())
        ret_code, raw_results = self.handle_resp(nas, schema, resp)

        if ret_code:
            return (ret_code, raw_results)  # repack and go home

        data = json.loads(resp.content)

        if 'schema' not in data:
            return 1, ["No schema for {0}".format(nas.schema)]

        if nas.p_json:
            return 0, [resp.content]

        return 0, ['===== schema ======', '\n'.join(data['schema'])]
Пример #11
0
 def query(self, nas):
     tmp_url = "/core/search/search_dns_text/"
     url = "{0}{1}".format(REMOTE, tmp_url)
     headers = {'content-type': 'application/json'}
     search = {'search': nas.query}
     resp = requests.get(url, params=search, headers=headers,
                         auth=auth())
     if resp.status_code == 500:
         resp_list = [
             "CLIENT ERROR! (Please email this output to a code monkey)"
         ]
         self.error_out(nas, search, resp, resp_list=resp_list)
         return
     results = self.get_resp_dict(resp)
     if 'text_response' not in results:
         return 1, []
     else:
         if nas.p_json:
             return 0, [json.dumps(results, indent=2)]
         return 0, [results['text_response']]
Пример #12
0
 def query(self, nas):
     tmp_url = "/core/search/search_dns_text/"
     url = "{0}{1}".format(REMOTE, tmp_url)
     headers = {'content-type': 'application/json'}
     search = {'search': nas.query}
     resp = requests.get(url, params=search, headers=headers, auth=auth())
     if nas.DEBUG:
         sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format(
             'get', url, search))
     was_json = nas.p_json
     nas.p_json = True  # Do this so we can play with the response
     ret_code, raw_results = self.handle_resp(nas, search, resp)
     if ret_code:
         return (ret_code, raw_results)  # repack and go home
     results = json.loads(raw_results[0])
     if 'text_response' not in results:
         return 1, []
     else:
         if was_json:
             return 0, raw_results
         return 0, [results['text_response']]
Пример #13
0
def ba_gather_vlan_pools(site, vlan_name, vlan_number, ip_type='4'):
    """
    This function should phone home to inventory and pull down a list of ip
    ranges.
    """
    tmp_url = "/en-US/bulk_action/gather_vlan_pools/"
    url = "{0}{1}".format(REMOTE, tmp_url)
    headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
    data = {
        'vlan_name': vlan_name,
        'vlan_number': vlan_number,
        'site_name': site,
        'ip_type': ip_type
    }

    resp = requests.get(url, params=data, headers=headers, auth=auth())
    json_resp = json.loads(resp.content)
    if 'errors' in json_resp:
        return None, json_resp['errors']
    assert 'free_ranges' in json_resp, (
        "Something is broken. 'free_ranges' was not returen by Inventory")
    return json_resp['free_ranges'], None
Пример #14
0
 def query(self, nas):
     tmp_url = "/en-US/csv/ajax_csv_exporter/"
     url = "{0}{1}".format(REMOTE, tmp_url)
     headers = {'content-type': 'application/json'}
     search = {'search': nas.query}
     resp = requests.get(url, params=search, headers=headers, auth=auth())
     if nas.DEBUG:
         sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format(
             'get', url, search
         ))
     was_json = nas.p_json
     nas.p_json = True  # Do this so we can play with the response
     ret_code, raw_results = self.handle_resp(nas, search, resp)
     if ret_code:
         return (ret_code, raw_results)  # repack and go home
     results = json.loads(raw_results[0])
     if 'csv_content' not in results:
         return 1, []
     else:
         if was_json:
             return 0, raw_results
         return 0, [''.join(results['csv_content'])]
Пример #15
0
def ba_gather_vlan_pools(site, vlan_name, vlan_number, ip_type='4'):
    """
    This function should phone home to inventory and pull down a list of ip
    ranges.
    """
    tmp_url = "/en-US/bulk_action/gather_vlan_pools/"
    url = "{0}{1}".format(REMOTE, tmp_url)
    headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
    data = {
        'vlan_name': vlan_name,
        'vlan_number': vlan_number,
        'site_name': site,
        'ip_type': ip_type
    }

    resp = requests.get(url, params=data, headers=headers, auth=auth())
    json_resp = json.loads(resp.content)
    if 'errors' in json_resp:
        return None, json_resp['errors']
    assert 'free_ranges' in json_resp, (
        "Something is broken. 'free_ranges' was not returen by Inventory"
    )
    return json_resp['free_ranges'], None
Пример #16
0
    def irange(self, nas):
        tmp_url = "/core/range/usage_text/"
        url = "{0}{1}".format(REMOTE, tmp_url)
        headers = {'content-type': 'application/json'}
        start, end = nas.irange.split(',')
        search = {'start': start, 'end': end}
        if nas.d_integers:
            search['format'] = 'integers'
        resp = requests.get(url, params=search, headers=headers, auth=auth())
        if nas.DEBUG:
            sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format(
                'get', url, search))
        was_json = nas.p_json
        nas.p_json = True  # Do this so we can play with the response
        ret_code, raw_results = self.handle_resp(nas, search, resp)
        if ret_code:
            return (ret_code, raw_results)  # repack and go home

        def display_ranges(free_ranges):
            ret_list = []
            for fstart, fend in free_ranges:
                ret_list.append("{0} to {1}".format(fstart, fend))
            return ret_list

        results = json.loads(raw_results[0])
        if not results:
            return 1, []
        else:
            if was_json:
                return 0, raw_results
            resp_list = [
                "# of Used IPs: {0}".format(results['used']),
                "# of Unused IPs: {0}".format(results['unused']),
                "------ Vacant IP ranges ------"
            ]
            resp_list += display_ranges(results['free_ranges'])
            return 0, resp_list
Пример #17
0
    def irange(self, nas):
        tmp_url = "/core/range/usage_text/"
        url = "{0}{1}".format(REMOTE, tmp_url)
        headers = {'content-type': 'application/json'}
        start, end = nas.irange.split(',')
        search = {'start': start, 'end': end}
        if nas.d_integers:
            search['format'] = 'integers'
        resp = requests.get(url, params=search, headers=headers, auth=auth())
        if nas.DEBUG:
            sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format(
                'get', url, search
            ))
        was_json = nas.p_json
        nas.p_json = True  # Do this so we can play with the response
        ret_code, raw_results = self.handle_resp(nas, search, resp)
        if ret_code:
            return (ret_code, raw_results)  # repack and go home

        def display_ranges(free_ranges):
            ret_list = []
            for fstart, fend in free_ranges:
                ret_list.append("{0} to {1}".format(fstart, fend))
            return ret_list

        results = json.loads(raw_results[0])
        if not results:
            return 1, []
        else:
            if was_json:
                return 0, raw_results
            resp_list = ["# of Used IPs: {0}".format(results['used']),
                         "# of Unused IPs: {0}".format(results['unused']),
                         "------ Vacant IP ranges ------"]
            resp_list += display_ranges(results['free_ranges'])
            return 0, resp_list
Пример #18
0
    def decommission(self, nas):
        # naively read in from stdin
        tmp_url = "/en-US/decommission/hosts/"
        url = "{0}{1}".format(REMOTE, tmp_url)
        headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
        main_json = json.dumps({
            'systems': nas.hostnames,
            'options': {
                'decommission_sreg': nas.no_decommission_sreg,
                'decommission_system_status': nas.decommission_system_status,
                'convert_to_sreg': nas.no_convert_to_sreg,
                'remove_dns': nas.no_remove_dns
            },
            'commit': nas.commit,
            'comment': nas.comment
        })

        resp = requests.post(url, data=main_json, headers=headers, auth=auth())
        if nas.DEBUG:
            sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format(
                'get', url, main_json
            ))

        return self.handle_resp(nas, main_json, resp)
Пример #19
0
 def action(self, nas, url, method, data):
     headers = {'content-type': 'application/json'}
     data = json.dumps(data, indent=2)
     resp = method(url, headers=headers, data=data, auth=auth())
     return self.handle_resp(nas, data, resp)
Пример #20
0
 def delete(self, nas):
     url = "{0}{1}?format=json".format(REMOTE, self.delete_url(nas))
     headers = {'content-type': 'application/json'}
     resp = requests.delete(url, headers=headers, auth=auth())
     return self.handle_resp(nas, {}, resp)