Ejemplo n.º 1
0
 def test_perform_web_request(self):
     mock = MagicMock()
     mock.request.return_value = mock
     mock.json.return_value = {Constants.DUMMY: [1], 'total': 50, 'limit': 1}
     requestor = HttpRequestor(client=mock)
     result = requestor.perform_web_request(Constants.DUMMY, Constants.DUMMY)
     self.assertIsNotNone(result)
     requestor = HttpRequestor(client=mock, circuit_breaker=mock)
     result = requestor.perform_web_request(Constants.DUMMY, Constants.DUMMY)
     self.assertIsNotNone(result)
Ejemplo n.º 2
0
def check_session(session_token, http_client=HttpRequestor(), logger=Logger()):
    try:
        response = http_client.get(Settings.SESSION_URL.format(session_token=session_token)).json()
        return response.get(Constants.STATUS, Constants.EMPTY) == Constants.OKTA_ACTIVE_STATUS
    except Exception as ex:  # pylint: disable=broad-except
        logger.error(f'Error checking session. ex: {ex}')
        return False
Ejemplo n.º 3
0
 def test_save_items(self):
     mock = MagicMock()
     mock.request.return_value = mock
     mock.json.return_value = {Constants.DUMMY: [1], 'total': 50, 'limit': 1}
     requestor = HttpRequestor(client=mock, save_fn=mock)
     result = requestor.save_data_items(Constants.DUMMY)
     self.assertTrue(result)
Ejemplo n.º 4
0
 def test_get_all_items(self):
     mock = MagicMock()
     mock.request.return_value = mock
     mock.json.return_value = {Constants.DUMMY: [1], 'total': 50, 'limit': 1}
     requestor = HttpRequestor(client=mock)
     result = requestor.get_all_items(Constants.DUMMY)
     self.assertIsNotNone(result)
Ejemplo n.º 5
0
def dump_mongo_data():
    request = HttpRequestor()
    url = get_production_service_url(INFRASTRUCTURE, 0) + '&limit=90'
    result = request.get(url)
    infrastructure_list = result.json()[INFRASTRUCTURE]
    with open('./data/infrastructure.json', 'w+') as infra_file:
        infra_file.write(json.dumps(infrastructure_list, default=str))
Ejemplo n.º 6
0
 def test_real_web_send(self):
     mock = MagicMock()
     mock.get_secret.return_value = ''
     stats_client = StatsWebClient(secrets_manager=mock,
                                   http_client=HttpRequestor())
     self.assertTrue(
         stats_client.add_gauge('odb', 20, {Constants.PRODUCT: 'odb'}))
Ejemplo n.º 7
0
def call_velocity_endpoint(step,
                           state,
                           token,
                           build_tag,
                           web_client=HttpRequestor()):
    url = build_velocity_url(step, state, token, build_tag)
    result = web_client.post(url)
    return result.status_code
Ejemplo n.º 8
0
def get_wu_name(name):
    wu_name = 'Unknown'
    url = f'https://wunameaas.herokuapp.com/wuami/{name}'
    web_client = HttpRequestor()
    result = web_client.get(url)
    soup = BeautifulSoup(result.text, 'html.parser')
    for bold_item in soup.find_all('b'):
        wu_name = bold_item.get_text()
    return wu_name
Ejemplo n.º 9
0
 def create_event(self, payload, requestor=HttpRequestor()):
     headers = self.generate_headers()
     url = self.url
     proxy = {'http': Settings.PROXY_URL}
     response = requestor.post(url,
                               data=json.dumps(payload),
                               headers=headers,
                               proxies=proxy)
     if response.status_code == 202:
         return True
     return False
Ejemplo n.º 10
0
 def __init__(self,
              account_name,
              http=HttpRequestor(),
              secrets_manager=SecretManager()):
     self.http = http
     self.secrets_manager = secrets_manager
     self.account_name = account_name
     self.account_id = get_newrelic_account_id(self.account_name)
     self.api_key = get_newrelic_api_key(self.account_name,
                                         self.secrets_manager)
     self.logger = Logger(Settings.SERVICE_NAME)
Ejemplo n.º 11
0
def wait_for_local_service_ping(target, service, sleep_time=10, retries=3):
    call_count = 1
    requestor = HttpRequestor()
    print(f'Waiting for {service} ping to succeed...')
    ping_url = f'{target}/{service}/ping/'
    healthy = requestor.get(ping_url).status_code == Constants.SUCCESS_RESPONSE_CODE
    while not healthy:
        sleep(sleep_time)
        call_count += 1
        healthy = requestor.get(ping_url).status_code == Constants.SUCCESS_RESPONSE_CODE
        if call_count > retries:
            return
Ejemplo n.º 12
0
def get_service_versions(target_url, service_list, web_client=HttpRequestor()):
    versions = dict()
    for service in service_list:
        url = f'{target_url.strip(Constants.URL_SEPARATOR)}/{service}{VERSION_ENDPOINT}'
        try:
            result = web_client.get(url)
            if result.status_code == Constants.SUCCESS_RESPONSE_CODE:
                versions[service] = get_version_from_payload(result)
            else:
                versions[service] = None
        except RequestException:
            versions[service] = None
    return versions
Ejemplo n.º 13
0
 def _create_service_client(component_type, **kwargs):
     if component_type == Components.ServiceClients.SQS:
         return MessageQueue(**kwargs)
     if component_type == Components.ServiceClients.NEWRELIC:
         return NewRelicQueryExecutor(**kwargs)
     if component_type == Components.ServiceClients.SUMOLOGIC:
         return SumologicQueryExecutor(**kwargs)
     if component_type == Components.ServiceClients.HTTP:
         return HttpRequestor(**kwargs)
     if component_type == Components.ServiceClients.AUTH:
         return Authorization()
     if component_type == Components.ServiceClients.COLLECT_MANAGER:
         return CollectionManager(**kwargs)
     return None
Ejemplo n.º 14
0
def restore_service_data(service, endpoint=Constants.EMPTY):
    requestor = HttpRequestor()
    url = get_raw_service_url(service) + Constants.RESTORE_ROUTE
    if endpoint:
        url = get_raw_service_url(service) + endpoint
    print(f'Restoring data for service {url}...')
    file_path = \
        f'backup/{service}{endpoint.strip(Constants.FORWARD_SLASH)}.json'
    if os.path.exists(file_path):
        with open(file_path) as data_file:
            json_data = json.load(data_file)
            result = requestor.post(url,
                                    data=json.dumps(json_data),
                                    headers=requestor.get_request_headers())
            if result.status_code != Constants.CREATED_SUCCESS_CODE:
                print(f'Restore of {url} failed!')
                return
            print(f'Restore of {url} succeeded!')
Ejemplo n.º 15
0
def collect_service_endpoint_data(service, endpoint, log=True):
    requestor = HttpRequestor()
    if log:
        print(f'Collecting {service}/{endpoint}...')
    requestor.save_data_items(service, endpoint, False)
Ejemplo n.º 16
0
 def setUp(self):
     mock = MagicMock()
     mock.get.return_value = mock
     mock.request.return_value = Constants.DUMMY
     self.requestor = HttpRequestor(client=mock)
Ejemplo n.º 17
0
def collect_service_data(service, log=True):
    requestor = HttpRequestor()
    if log:
        print(f'Collecting {service}...')
    requestor.save_data_items(service)
Ejemplo n.º 18
0
 def test_real_get(self):
     requestor = HttpRequestor()
     result = requestor.get(GOOGLE_URL)
     self.assertEqual(result.status_code, Constants.SUCCESS_RESPONSE_CODE)
Ejemplo n.º 19
0
 def __init__(self, **kwargs):
     self.web_client = kwargs.get(Constants.HTTP_CLIENT, HttpRequestor())
     self.secrets_manager = kwargs.get(Constants.SECRETS_MANAGER,
                                       SecretManager())
Ejemplo n.º 20
0
 def __init__(self,
              slack_client=SlackApi(Settings.SLACK_API_TOKEN),
              web_client=HttpRequestor()):
     self.client = slack_client
     self.web_client = web_client
Ejemplo n.º 21
0
def run_version_check(target_url,
                      expected_version,
                      service_list,
                      web_client=HttpRequestor()):
    versions = get_service_versions(target_url, service_list, web_client)
    return check_version_list(versions, expected_version)
Ejemplo n.º 22
0
 def __init__(self, http=HttpRequestor()):
     self.http = http
     self.logger = Logger(Settings.SERVICE_NAME)
     self.headers = {Constants.CONTENT_TYPE_HEADER: Constants.CONTENT_TYPE_JSON,
                     Constants.ACCEPT_HEADER: Constants.CONTENT_TYPE_JSON}
Ejemplo n.º 23
0
 def __init__(self, http_client=HttpRequestor(), log_url=Settings.REMOTE_LOG_URL):
     self.client = http_client
     self.log_url = log_url