Example #1
0
def get_data(endpoint, key=None, tags=None, value=None):
    headers = auth.get_headers()
    url = util.get_url(endpoint)
    try:
        res = requests.get(url=url, headers=headers['data'])
        res = res.json()
        res = res['data']

        check = bool(tags) & bool(value)
        data = list()
        if key != None and not check:
            data = list()
            for i in res:
                data.append(i[key])

        elif key == None and check:
            data = list()
            for i in res:
                if i[tags] == value:
                    data = i

        elif bool(key) & check:
            for i in res:
                if i[tags] == value:
                    data.append(i[key])
        else:
            data = res
        return util.generate_respons(True, "success", data)

    except Exception as e:
        util.log_err(e)
        return util.generate_respons(False, str(e))
Example #2
0
def remove_zone(zone):
    pbar = tqdm(total=100)
    pbar.set_description("Obtaining DNS Data")
    json_send = jsonmodel['rm']['zone']['data']
    pbar.update(10)
    id_zone = ls.get_data('zone', key='id_zone', tags='nm_zone', value=zone)
    pbar.update(20)
    id_zone = id_zone['data'][0]
    pbar.update(10)
    json_send['remove']['tags']['id_zone'] = id_zone
    pbar.update(10)
    try:
        pbar.set_description("Removing DNS")
        unsync_send = {"unset": "zone", "data": {"id_zone": id_zone}}
        res_sync = unsync(unsync_send)
        if not res_sync["status"]:
            util.log_err(res_sync['message'])
            return util.generate_respons(False, "Failure on removing DNS")
        res = app.send_request('zone', json_send)
        pbar.update(50)
        pbar.close()
    except Exception as e:
        util.log_err(e)
    finally:
        return res
Example #3
0
def send_todb(user_id, project_id):
    data = {"project_id": project_id, "user_id": user_id}
    headers = {'Content-Type': "application/json"}
    try:
        url = util.get_url('user')
        requests.post(url=url, data=json.dumps(data), headers=headers)
    except Exception as e:
        util.log_err(str(e))
Example #4
0
def remove_zone(zone):
    json_send = jsonmodel['rm']['zone']['data']
    id_zone = ls.get_data('zone', key='id_zone', tags='nm_zone', value=zone)
    id_zone = id_zone['data'][0]
    json_send['remove']['tags']['id_zone'] = id_zone
    try:
        res = app.send_request('zone', json_send)
    except Exception as e:
        util.log_err(e)
    finally:
        return res
Example #5
0
def remove_record(records):
    json_send = jsonmodel['rm']['record']['data']
    result = list()
    for i in records:
        json_send = jsonmodel['rm']['record']['data']
        json_send['remove']['tags']['id_record'] = i
        try:
            res = app.send_request('record', json_send)
            result.append(res)
        except Exception as e:
            util.log_err(e)
    return result
Example #6
0
def get_env_values():
    if check_env():
        load_env_file()
        restknot_env = {}
        restknot_env['username'] = os.environ.get('OS_USERNAME')
        restknot_env['password'] = os.environ.get('OS_PASSWORD')
        restknot_env['user_id'] = os.environ.get('OS_USER_ID')
        restknot_env['project_id'] = os.environ.get('OS_PROJECT_ID')
        restknot_env['token'] = os.environ.get('OS_TOKEN')
        return restknot_env
    else:
        util.log_err("Can't find restknot.env")
Example #7
0
def load_dumped_session():
    if check_session():
        sess = None
        with open('/tmp/session.pkl', 'rb') as f:
            sess = dill.load(f)
        return util.generate_respons(True, "success", sess)
    elif check_env():
        regenerate_session()
        return load_dumped_session()
    else:
        util.log_err("Loading Session Failed")
        msg = "Please login first"
        return util.generate_respons(False, msg)
Example #8
0
def create_env_file(username, password, user_id, project_id, token):
    if not os.path.isdir("{}/restknot".format(DUMP_FOLDER)):
        os.mkdir("{}/restknot".format(DUMP_FOLDER))
    try:
        env_file = open("{}/restknot/.restknot.env".format(DUMP_FOLDER), "w+")
        env_file.write("OS_USERNAME=%s\n" % username)
        env_file.write("OS_PASSWORD=%s\n" % password)
        env_file.write("OS_USER_ID=%s\n" % user_id)
        env_file.write("OS_PROJECT_ID=%s\n" % project_id)
        env_file.write("OS_TOKEN=%s\n" % token)
        env_file.close()
        return True
    except Exception as e:
        util.log_err(e)
        return False
Example #9
0
def remove_record(records):
    json_send = jsonmodel['rm']['record']['data']
    result = list()
    pbar = tqdm(total=100)
    step = (100 / (len(records)))
    for i in records:
        pbar.set_description("Removing Record Index - {}".format(
            records.index(i)))
        json_send = jsonmodel['rm']['record']['data']
        json_send['remove']['tags']['id_record'] = i
        try:
            sync_dat = {"unset": "record", "data": {"id_record": i}}
            ress = unsync(sync_dat)
            if not ress["status"]:
                util.log_err(ress["message"])
                return util.generate_respons(False, "Failure on zone removal")
            res = app.send_request('record', json_send)
            result.append(res)
            pbar.update(step)
        except Exception as e:
            util.log_err(e)
    pbar.close()
    return result
Example #10
0
def dump_session(sess):
    try:
        with open('/tmp/session.pkl', 'wb') as f:
            dill.dump(sess, f, protocol=2)
    except Exception as e:
        util.log_err("Dump session failed")