def test_get(self): id = '111' with mock.patch.object(self.policy_api, "get") as api_call: self.resourceApi.get(id, tenant=TEST_TENANT) expected_def = policy_defs.ServiceDef(service_id=id, tenant=TEST_TENANT) self.assert_called_with_def(api_call, expected_def)
def update(self, service_id, name=None, description=None, tenant=policy_constants.POLICY_INFRA_TENANT, **kwargs): # service name cannot contain spaces or slashes if name: name = self._canonize_name(name) # Get the current data of service & its' service entry service = self.get(service_id, tenant=tenant) # update the relevant data service itself: # TODO(asarfaty): currently updating the service itself doesn't work if name is not None: service['display_name'] = name if description is not None: service['description'] = description if (service.get('service_entries') and len(service['service_entries']) == 1): # update the service entry body self._update_service_entry( service_id, service['service_entries'][0], name=name, description=description, **kwargs) else: LOG.error("Cannot update service %s - expected 1 service " "entry", service_id) # update the backend service_def = policy_defs.ServiceDef(service_id=service_id, tenant=tenant) service_def.body = service self.policy_api.create_or_update(service_def) # return the updated service return self.get(service_id, tenant=tenant)
def test_create(self): name = 's1' description = 'desc' protocol = policy_constants.TCP dest_ports = [81, 82] with mock.patch.object(self.policy_api, "create_with_parent") as api_call: self.resourceApi.create_or_overwrite(name, description=description, protocol=protocol, dest_ports=dest_ports, tenant=TEST_TENANT) exp_srv_def = policy_defs.ServiceDef(service_id=mock.ANY, name=name, description=description, tenant=TEST_TENANT) exp_entry_def = policy_defs.L4ServiceEntryDef( service_id=mock.ANY, name=name, description=description, protocol=protocol, dest_ports=dest_ports, tenant=TEST_TENANT) self.assert_called_with_defs(api_call, [exp_srv_def, exp_entry_def])
def test_create(self): service_def = policy.ServiceDef('roomservice') self.policy_api.create_or_update(service_def) self.assert_json_call('PATCH', self.client, 'infra/services/roomservice', data=service_def.get_obj_dict())
def test_create_l4_with_parent(self): service_def = policy.ServiceDef('roomservice') entry_def = policy.L4ServiceEntryDef('roomservice', 'http', name='room http', dest_ports=[80, 8080]) self.policy_api.create_with_parent(service_def, entry_def) expected_entry = { 'id': 'http', 'resource_type': 'L4PortSetServiceEntry', 'display_name': 'room http', 'description': None, 'l4_protocol': 'TCP', 'destination_ports': [80, 8080] } expected_data = { 'id': 'roomservice', 'display_name': None, 'description': None, 'service_entries': [expected_entry] } self.assert_json_call('PATCH', self.client, 'infra/services/roomservice', data=expected_data)
def test_get_by_name(self): name = 's1' with mock.patch.object( self.policy_api, "list", return_value={'results': [{ 'display_name': name }]}) as api_call: obj = self.resourceApi.get_by_name(name, tenant=TEST_TENANT) self.assertIsNotNone(obj) expected_def = policy_defs.ServiceDef(tenant=TEST_TENANT) self.assert_called_with_def(api_call, expected_def)
def delete(self, service_id, tenant=policy_constants.POLICY_INFRA_TENANT): """Delete the service with all its entries""" service_def = policy_defs.ServiceDef(service_id=service_id, tenant=tenant) service = self.policy_api.get(service_def) # first delete all the service entries if 'service_entries' in service: for entry in service['service_entries']: entry_def = self.entry_def( service_id=service_id, service_entry_id=entry['id'], tenant=tenant) self.policy_api.delete(entry_def) self.policy_api.delete(service_def)
def test_update_all(self): id = '111' name = 'newName' description = 'new desc' version = 6 icmp_type = 3 icmp_code = 3 service_entry_id = '222' service_entry = {'id': service_entry_id} with mock.patch.object( self.policy_api, "get", return_value={'service_entries': [service_entry]}) as get_call,\ mock.patch.object(self.policy_api, "create_or_update") as update_call,\ mock.patch.object(self.policy_api, "list", return_value={'results': []}): self.resourceApi.update(id, name=name, description=description, version=version, icmp_type=icmp_type, icmp_code=icmp_code, tenant=TEST_TENANT) # get will be called for the entire service expected_def = policy_defs.ServiceDef(service_id=id, tenant=TEST_TENANT) self.assert_called_with_def(get_call, expected_def) expected_dict = { 'display_name': name, 'description': description, 'service_entries': [{ 'id': service_entry_id, 'display_name': name, 'description': description, 'protocol': 'ICMPv6', 'icmp_type': icmp_type, 'icmp_code': icmp_code }] } self.assert_called_with_def_and_dict(update_call, expected_def, expected_dict)
def test_update(self): id = '111' name = 'new_name' description = 'new desc' with mock.patch.object(self.policy_api, "get", return_value={}) as get_call,\ mock.patch.object(self.policy_api, "create_or_update") as update_call: self.resourceApi.update(id, name=name, description=description, tenant=TEST_TENANT) expected_def = policy_defs.ServiceDef(service_id=id, tenant=TEST_TENANT) expected_dict = {'display_name': name, 'description': description} self.assert_called_with_def(get_call, expected_def) self.assert_called_with_def_and_dict(update_call, expected_def, expected_dict)
def test_update_all(self): id = '111' name = 'newName' description = 'new desc' protocol = 'udp' dest_ports = [555] service_entry_id = '222' service_entry = {'id': service_entry_id} with mock.patch.object( self.policy_api, "get", return_value={'service_entries': [service_entry]}) as get_call,\ mock.patch.object(self.policy_api, "create_or_update") as update_call,\ mock.patch.object(self.policy_api, "list", return_value={'results': []}): self.resourceApi.update(id, name=name, description=description, protocol=protocol, dest_ports=dest_ports, tenant=TEST_TENANT) # get will be called for the entire service expected_def = policy_defs.ServiceDef(service_id=id, tenant=TEST_TENANT) self.assert_called_with_def(get_call, expected_def) expected_dict = { 'display_name': name, 'description': description, 'service_entries': [{ 'id': service_entry_id, 'display_name': name, 'description': description, 'l4_protocol': protocol.upper(), 'destination_ports': dest_ports }] } self.assert_called_with_def_and_dict(update_call, expected_def, expected_dict)
def create_or_overwrite(self, name, service_id=None, description=None, protocol=policy_constants.TCP, dest_ports=None, tenant=policy_constants.POLICY_INFRA_TENANT): service_id = self._init_obj_uuid(service_id) # service name cannot contain spaces or slashes name = self._canonize_name(name) service_def = policy_defs.ServiceDef(service_id=service_id, name=name, description=description, tenant=tenant) # NOTE(asarfaty) We set the service entry display name (which is also # used as the id) to be the same as the service name. In case we # support multiple service entries, we need the name to be unique. entry_def = policy_defs.L4ServiceEntryDef( service_id=service_id, name=name, description=description, protocol=protocol, dest_ports=dest_ports, tenant=tenant) return self.policy_api.create_with_parent(service_def, entry_def)
def test_create(self): name = 's1' description = 'desc' icmp_type = 2 with mock.patch.object(self.policy_api, "create_with_parent") as api_call: self.resourceApi.create_or_overwrite(name, description=description, icmp_type=icmp_type, tenant=TEST_TENANT) exp_srv_def = policy_defs.ServiceDef(service_id=mock.ANY, name=name, description=description, tenant=TEST_TENANT) exp_entry_def = policy_defs.IcmpServiceEntryDef( service_id=mock.ANY, name=name, description=description, icmp_type=icmp_type, tenant=TEST_TENANT) self.assert_called_with_defs(api_call, [exp_srv_def, exp_entry_def])
def test_create_icmp_with_parent(self): service_def = policy.ServiceDef('icmpservice') entry_def = policy.IcmpServiceEntryDef('icmpservice', 'icmp', name='icmpv4') self.policy_api.create_with_parent(service_def, entry_def) expected_entry = { 'id': 'icmp', 'resource_type': 'ICMPTypeServiceEntry', 'display_name': 'icmpv4', 'description': None, 'protocol': 'ICMPv4' } expected_data = { 'id': 'icmpservice', 'display_name': None, 'description': None, 'service_entries': [expected_entry] } self.assert_json_call('PATCH', self.client, 'infra/services/icmpservice', data=expected_data)
def list(self, tenant=policy_constants.POLICY_INFRA_TENANT): service_def = policy_defs.ServiceDef(tenant=tenant) return self.policy_api.list(service_def)['results']
def get(self, service_id, tenant=policy_constants.POLICY_INFRA_TENANT): service_def = policy_defs.ServiceDef(service_id=service_id, tenant=tenant) return self.policy_api.get(service_def)
def get_realized_state(self, service_id, ep_id, tenant=policy_constants.POLICY_INFRA_TENANT): service_def = policy_defs.ServiceDef(service_id=service_id, tenant=tenant) path = service_def.get_realized_state_path(ep_id) return self._get_realized_state(path)
def test_list(self): with mock.patch.object(self.policy_api, "list") as api_call: self.resourceApi.list(tenant=TEST_TENANT) expected_def = policy_defs.ServiceDef(tenant=TEST_TENANT) self.assert_called_with_def(api_call, expected_def)