def get_value(self): if "executor" not in self.pipeline_data or "project_id" not in self.pipeline_data: return "ERROR: executor and project_id of pipeline is needed" username = self.pipeline_data["executor"] project_id = self.pipeline_data["project_id"] project = Project.objects.get(id=project_id) bk_biz_id = project.bk_biz_id if project.from_cmdb else "" bk_supplier_account = supplier_account_for_project(project_id) ip_selector = self.value ip_result = get_ip_picker_result(username, bk_biz_id, bk_supplier_account, ip_selector) if not ip_result["result"]: logger.error(f"[ip_selector get_value] error: {ip_result}") return f'error: {ip_result["message"]}' separator = self.value.get("separator", ",") # get for old value compatible if self.value.get("with_cloud_id", False): ip = separator.join([ "{}:{}".format(host["bk_cloud_id"], host["bk_host_innerip"]) for host in ip_result["data"] ]) else: ip = separator.join( [host["bk_host_innerip"] for host in ip_result["data"]]) return ip
def test_get_ip_picker_result__selector_topo(self): mock_get_client_by_user.success = True topo_kwargs = { 'bk_biz_id': self.bk_biz_id, 'selectors': ['topo'], 'topo': [ { 'bk_obj_id': 'set', 'bk_inst_id': 2 }, ], 'ip': [], 'filters': [], 'excludes': [], } ip_data = get_ip_picker_result(self.username, self.bk_biz_id, self.bk_supplier_account, topo_kwargs)['data'] ip = [host['bk_host_innerip'] for host in ip_data] self.assertEquals(ip, ['1.1.1.1']) topo_kwargs = { 'bk_biz_id': self.bk_biz_id, 'selectors': ['topo'], 'topo': [ { 'bk_obj_id': 'set', 'bk_inst_id': 3 }, { 'bk_obj_id': 'module', 'bk_inst_id': 5 }, ], 'ip': [], 'filters': [], 'excludes': [], } ip_data = get_ip_picker_result(self.username, self.bk_biz_id, self.bk_supplier_account, topo_kwargs)['data'] ip = [host['bk_host_innerip'] for host in ip_data] self.assertEquals(ip, ['2.2.2.2'])
def get_value(self): username = self.pipeline_data['executor'] bk_biz_id = self.pipeline_data['biz_cc_id'] bk_supplier_account = self.pipeline_data['biz_supplier_account'] value = self.value ip_result = get_ip_picker_result(username, bk_biz_id, bk_supplier_account, value) ip = ','.join([host['bk_host_innerip'] for host in ip_result['data']]) return ip
def get_value(self): username = self.pipeline_data['executor'] project_id = self.pipeline_data['project_id'] project = Project.objects.get(id=project_id) bk_biz_id = project.bk_biz_id if project.from_cmdb else '' bk_supplier_account = supplier_account_for_project(project_id) ip_selector = self.value ip_result = get_ip_picker_result(username, bk_biz_id, bk_supplier_account, ip_selector) ip = ','.join([host['bk_host_innerip'] for host in ip_result['data']]) return ip
def test_get_ip_picker_result__selector_ip(self): mock_get_client_by_user.success = True topo_kwargs = { 'bk_biz_id': self.bk_biz_id, 'selectors': ['ip'], 'topo': [], 'ip': [{ 'bk_host_name': 'host1', 'bk_host_id': 2, 'agent': 1, 'cloud': [{ 'bk_obj_name': '', 'id': '0', 'bk_obj_id': 'plat', 'bk_obj_icon': '', 'bk_inst_id': 0, 'bk_inst_name': 'default area' }], 'bk_host_innerip': '1.1.1.1' }, { 'bk_host_name': 'host2', 'bk_host_id': 2, 'agent': 1, 'cloud': [{ 'bk_obj_name': '', 'id': '0', 'bk_obj_id': 'plat', 'bk_obj_icon': '', 'bk_inst_id': 0, 'bk_inst_name': 'default area' }], 'bk_host_innerip': '2.2.2.2' }], 'filters': [], 'excludes': [], } ip_data = get_ip_picker_result(self.username, self.bk_biz_id, self.bk_supplier_account, topo_kwargs)['data'] ip = [host['bk_host_innerip'] for host in ip_data] self.assertEquals(ip, ['1.1.1.1', '2.2.2.2'])
def test_get_ip_picker_result__boundary_value(self): mock_get_client_by_user.success = True topo_kwargs = { 'bk_biz_id': self.bk_biz_id, 'selectors': ['topo'], 'topo': [], 'ip': [], 'filters': [], 'excludes': [], } self.assertFalse( get_ip_picker_result(self.username, self.bk_biz_id, self.bk_supplier_account, topo_kwargs)['result'])
def get_value(self): username = self.pipeline_data['executor'] bk_biz_id = self.pipeline_data['biz_cc_id'] bk_supplier_account = self.pipeline_data['biz_supplier_account'] ip_selector = self.value ip_result = get_ip_picker_result(username, bk_biz_id, bk_supplier_account, ip_selector) # get for old value compatible if self.value.get('with_cloud_id', False): ip = ','.join([ '{}:{}'.format(host['bk_cloud_id'], host['bk_host_innerip']) for host in ip_result['data'] ]) else: ip = ','.join( [host['bk_host_innerip'] for host in ip_result['data']]) return ip