def test_connection_get(self, setup_vcenter, rest_yaml, vmon_json):
     api_target = rest_yaml['vmonservice']['api_target']
     con = Connection(setup_vcenter)
     response = con.get_request(api_target)
     assert response is False, 'Response fail expected, because there was no previous login (SessionID missing).'
     con.login()
     response = con.get_request(api_target)
     assert vmon_json == response, 'Identical vmon json data expected.'
    def collect(self):
        for vc in self.vcenter.vcenter_list:
            g = GaugeMetricFamily('vcsa_service_status',
                                  'Status of vCSA Services',
                                  labels=['service', 'vccluster'])

            rest_yaml = self.read_rest_yaml()

            api_target = rest_yaml['vmonservice']['api_target']

            # TODO: move session handling to base class. implement reuse of sessions
            session_id = Connection.login(vc, self.vcenter.user,
                                          self.vcenter.generate_pw(vc))
            if not session_id:
                print("skipping vc", vc, ", login not possible")
                continue
            fetched_data = Connection.get_request(vc, api_target, session_id)
            Connection.logout(vc, session_id)
            if not fetched_data:
                print("skipping vc", vc,
                      "fetched data did not return anything")
                continue

            services = dict()
            for service in fetched_data['value']:
                service_name = service['key']
                state = service['value']['state']
                if state == "STOPPED":
                    service_health = "STOPPED"
                else:
                    service_health = service['value']['health']

                g.add_metric(labels=[service_name, vc],
                             value=self.health_states[service_health])

            yield g