示例#1
0
def client() -> Client:
    global CLIENT
    if CLIENT is None:
        CLIENT = cachetclient.Client(endpoint=CACHET_ENDPOINT,
                                     api_token=CACHET_API_TOKEN)

    return CLIENT
示例#2
0
def update_incident(update_message: str, component_name: str, status: int):
    """
    Update an incident with the update message and status specified for the
    component with its name specified . The status is specified by the
    cachet.enums enum. Only updates if the component is down.

    :param update_message: Update message to display on incident
    :type update_message: str
    :param component_name: Component name to create incident for
    :type component_name: str
    :param status: Status for the incident
    :type status: int
    """

    cachet_client = cachetclient.Client(endpoint=CACHET_URL,
                                        api_token=CACHET_TOKEN)

    target_comp = _get_component(cachet_client, component_name)

    target_incident = _get_incident(cachet_client, target_comp)

    if target_comp.status != enums.COMPONENT_STATUS_OPERATIONAL:
        cachet_client.incident_updates.create(incident_id=target_incident.id,
                                              status=status,
                                              message=update_message)

        if status == enums.INCIDENT_FIXED:
            target_comp.status = enums.COMPONENT_STATUS_OPERATIONAL
            target_comp.update()
示例#3
0
def create_incident(error_message: str, component_name: str,
                    incident_status: int, comp_status: int):
    """
    Create an incident with the error message and status specified for the
    component with its name and new status specified. The status is specified
    by the
    cachet.enums enum. Only creates an incident if the component isn't
    already down.

    :param error_message: Error message to display on incident
    :type error_message: str
    :param component_name: Component name to create incident for
    :type component_name: str
    :param incident_status: Status for the incident
    :type incident_status: int
    :param comp_status: Status for the component
    :type comp_status: int
    """

    cachet_client = cachetclient.Client(endpoint=CACHET_URL,
                                        api_token=CACHET_TOKEN)

    target_comp = _get_component(cachet_client, component_name)

    if target_comp.status == enums.COMPONENT_STATUS_OPERATIONAL:

        message = (f"{component_name} failed on "
                   f"{UCLAPI_DOMAIN_CURRENT}"
                   f" with error: {repr(error_message)}")

        cachet_client.incidents.create(name=f"{component_name} failed",
                                       message=message,
                                       status=incident_status,
                                       component_id=target_comp.id,
                                       component_status=comp_status)
示例#4
0
def get_cachet_client(cachet_endpoint):
    client = cachetclient.Client(
        endpoint=cachet_endpoint,
        version='1',
        verify_tls=False,
        api_token='token'
    )
    return client
示例#5
0
 def __init__(self, component_id):
     self.endpoint = endpoint
     self.api_token = api_token
     self.component_id = component_id
     self.client = cachetclient.Client(self.endpoint, self.api_token)
     if DEBUG:
         click.secho("[D] Initializing Cachet Component with ID %s" %
                     component_id,
                     fg='green')
     if not self.ping():
         click.secho("[!] Unable to connect to Cachet server at %s" %
                     endpoint,
                     fg='red')
     self.latest_incident = self._get_most_recent_incident()
示例#6
0
 def create_client(self) -> cachetclient.v1.Client:
     return cachetclient.Client(endpoint=self.endpoint,
                                api_token=self.token)
示例#7
0
 def test_missing_token_env(self):
     """Missing token env var should raise error"""
     envs = {'CACHET_ENDPOINT': self.endpoint}
     with mock.patch.dict('os.environ', envs):
         with self.assertRaises(ValueError):
             cachetclient.Client()
示例#8
0
 def test_missing_endpoint_env(self):
     """Missing endpoint env var should raise error"""
     envs = {'CACHET_API_TOKEN': self.token}
     with mock.patch.dict('os.environ', envs):
         with self.assertRaises(ValueError):
             cachetclient.Client()
示例#9
0
 def test_missing_endpoint(self):
     """Missing endpoint raises error"""
     with self.assertRaises(ValueError):
         cachetclient.Client(api_token=self.token)
示例#10
0
 def test_missing_token(self):
     """Missing token raises error"""
     with self.assertRaises(ValueError):
         cachetclient.Client(endpoint=self.endpoint)
示例#11
0
 def test_enviroment_vars(self):
     """Instantiate client using env vars"""
     envs = {'CACHET_API_TOKEN': self.token, 'CACHET_ENDPOINT': self.endpoint}
     with mock.patch.dict('os.environ', envs):
         cachetclient.Client()
示例#12
0
 def test_endpoint_supply_version(self):
     """Test supplying modifier url through proxy etc"""
     cachetclient.Client(endpoint="https://status", version='1', api_token=self.token)
示例#13
0
 def test_endpoint_no_version(self):
     """Supply an endpoint without version"""
     with self.assertRaises(ValueError):
         cachetclient.Client(endpoint="meh", api_token=self.token)
示例#14
0
 def test_basic(self):
     """Create a basic client"""
     cachetclient.Client(endpoint=self.endpoint, api_token=self.token)
示例#15
0
 def test_mock(self):
     client = cachetclient.Client(endpoint=self.endpoint, api_token=self.token)
     self.assertTrue(client._http.is_fake_client)
示例#16
0
def get_client(url: str, token: str) -> cachetclient.Client:
    cachet = cachetclient.Client(endpoint=url, api_token=token)
    cachet.ping()
    return cachet