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 test_connection_post(self, setup_vcenter, rest_yaml, logging_json): api_target = rest_yaml['logging']['api_target'] data = rest_yaml['logging']['action'] con = Connection(setup_vcenter) response = con.post_request(api_target, data) assert response is False, 'Response fail expected, because there was no previous login (SessionID missing).' con.login() response = con.post_request(api_target, data) assert logging_json == response, 'Identical logging json data expected.'
class Vcenter: def __init__(self, name, mpw, user, password): self.user = user self.mpw = mpw self.pw = password if not password: self.pw_handle = self.generate_pw_handle() self.pw = self.generate_pw(name).replace('/', '') self.name = name @staticmethod def get_vcs_from_atlas(atlas_endpoint): response = requests.get(url=atlas_endpoint) netbox_json = response.json() vcenter_list = list() for target in netbox_json: if target['labels']['job'] == "vcenter": vcenter = target['labels']['server_name'] vcenter_list.append(vcenter) return vcenter_list def generate_pw_handle(self): return master_password.MPW(self.user, self.mpw) def generate_pw(self, url): if self.pw: return self.pw return self.pw_handle.password(url) def login(self): self.con = Connection(self) self.con.login() def logout(self): if self.con.session_id: self.con.logout()
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
def test_login_failure(self, setup_vcenter): connection = Connection(setup_vcenter) connection.pw = 'False' response = connection.login() assert response is False, 'Expected the response "False" for a failed login.'
def test_logout_success(self, setup_vcenter): connection = Connection(setup_vcenter) connection.login() response = connection.logout() assert response is True, 'Expected the response "True" for a successful logout.'