def test_update(self): domain_id = '111' map_id = '222' name = 'new name' description = 'new desc' source_group = 'ng1' dest_group = 'ng2' service1_id = 'nc1' service2_id = 'nc2' 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(domain_id, map_id, name=name, description=description, service_ids=[service1_id, service2_id], source_groups=[source_group], dest_groups=[dest_group], tenant=TEST_TENANT) expected_map_def = policy_defs.CommunicationMapDef( domain_id=domain_id, map_id=map_id, tenant=TEST_TENANT) expected_map_dict = { 'display_name': name, 'description': description } self.assert_called_with_def(get_call, expected_map_def) self.assert_called_with_def_and_dict(update_call, expected_map_def, expected_map_dict)
def list(self, domain_id, tenant=policy_constants.POLICY_INFRA_TENANT): """List all the map entries of a specific domain.""" map_def = policy_defs.CommunicationMapDef( domain_id=domain_id, tenant=tenant) return self.policy_api.list(map_def)['results']
def test_list(self): domain_id = '111' with mock.patch.object(self.policy_api, "list") as api_call: self.resourceApi.list(domain_id, tenant=TEST_TENANT) expected_def = policy_defs.CommunicationMapDef(domain_id=domain_id, tenant=TEST_TENANT) self.assert_called_with_def(api_call, expected_def)
def get(self, domain_id, map_id, tenant=policy_constants.POLICY_INFRA_TENANT): map_def = policy_defs.CommunicationMapDef( domain_id=domain_id, map_id=map_id, tenant=tenant) return self.policy_api.get(map_def)
def delete(self, domain_id, map_id, tenant=policy_constants.POLICY_INFRA_TENANT): map_def = policy_defs.CommunicationMapDef( domain_id=domain_id, map_id=map_id, tenant=tenant) self.policy_api.delete(map_def)
def test_delete(self): domain_id = '111' id = '222' with mock.patch.object(self.policy_api, "delete") as api_call: self.resourceApi.delete(domain_id, id, tenant=TEST_TENANT) expected_def = policy_defs.CommunicationMapDef(domain_id=domain_id, map_id=id, tenant=TEST_TENANT) self.assert_called_with_def(api_call, expected_def)
def test_create_with_one_entry(self): map_def = policy.CommunicationMapDef(domain_id='d1', map_id='cm1') self.policy_api.create_with_parent(map_def, self.entry1) expected_data = map_def.get_obj_dict() expected_data['communication_entries'] = [self.expected_data1] self.assert_json_call('PATCH', self.client, 'infra/domains/d1/communication-maps/cm1', data=expected_data)
def test_get_by_name(self): domain_id = '111' name = 'cm1' with mock.patch.object( self.policy_api, "list", return_value={'results': [{ 'display_name': name }]}) as api_call: obj = self.resourceApi.get_by_name(domain_id, name, tenant=TEST_TENANT) self.assertIsNotNone(obj) expected_def = policy_defs.CommunicationMapDef(domain_id=domain_id, tenant=TEST_TENANT) self.assert_called_with_def(api_call, expected_def)
def test_create_first_seqnum(self): domain_id = '111' map_id = '222' name = 'cm1' description = 'desc' source_group = 'g1' dest_group = 'g2' service_id = 'c1' category = 'Emergency' get_return_value = {'communication_entries': []} with mock.patch.object(self.policy_api, "create_or_update") as api_call, \ mock.patch.object(self.resourceApi, "get", return_value=get_return_value): self.resourceApi.create_or_overwrite(name, domain_id, map_id=map_id, description=description, service_ids=[service_id], source_groups=[source_group], dest_groups=[dest_group], category=category, tenant=TEST_TENANT) expected_def = policy_defs.CommunicationMapDef( domain_id=domain_id, map_id=map_id, name=name, description=description, category=category, precedence=0, tenant=TEST_TENANT) self.assert_called_with_def(api_call, expected_def) expected_def = policy_defs.CommunicationMapEntryDef( domain_id=domain_id, map_id=map_id, entry_id=map_id, name=name, description=description, sequence_number=1, service_ids=[service_id], source_groups=[source_group], dest_groups=[dest_group], tenant=TEST_TENANT) self.assert_called_with_def(api_call, expected_def, call_num=1)
def update(self, domain_id, map_id, name=None, description=None, sequence_number=None, service_ids=None, action=None, source_groups=None, dest_groups=None, precedence=None, category=None, tenant=policy_constants.POLICY_INFRA_TENANT): # Get the current data of communication map & its' entry comm_map = self.get(domain_id, map_id, tenant=tenant) # update the communication map itself: comm_def = policy_defs.CommunicationMapDef( domain_id=domain_id, map_id=map_id, tenant=tenant) if name is not None: comm_map['display_name'] = name if description is not None: comm_map['description'] = description if category is not None: comm_map['category'] = category if precedence is not None: comm_map['precedence'] = precedence if (comm_map.get('communication_entries') and len(comm_map['communication_entries']) == 1): # update the entry body comm_entry = comm_map['communication_entries'][0] entry_id = comm_entry['id'] entry_def = policy_defs.CommunicationMapEntryDef( domain_id=domain_id, map_id=map_id, entry_id=entry_id, tenant=tenant) entry_def.update_attributes_in_body( body=comm_entry, name=name, description=description, service_ids=service_ids, source_groups=source_groups, dest_groups=dest_groups, sequence_number=sequence_number, action=action) else: LOG.error("Cannot update communication map %s - expected 1 entry", map_id) comm_def.body = comm_map self.policy_api.create_or_update(comm_def) # re-read the map from the backend to return the current data return self.get(domain_id, map_id, tenant=tenant)
def test_create_without_seqnum(self): domain_id = '111' name = 'cm1' description = 'desc' source_group = 'g1' dest_group = 'g2' service1_id = 'c1' service2_id = 'c2' with mock.patch.object(self.policy_api, "create_with_parent") as api_call: self.resourceApi.create_or_overwrite( name, domain_id, description=description, service_ids=[service1_id, service2_id], source_groups=[source_group], dest_groups=[dest_group], tenant=TEST_TENANT) expected_map_def = policy_defs.CommunicationMapDef( domain_id=domain_id, map_id=mock.ANY, name=name, description=description, category=policy_constants.CATEGORY_DEFAULT, precedence=0, tenant=TEST_TENANT) expected_entry_def = policy_defs.CommunicationMapEntryDef( domain_id=domain_id, map_id=mock.ANY, entry_id=mock.ANY, name=name, description=description, sequence_number=1, service_ids=[service1_id, service2_id], source_groups=[source_group], dest_groups=[dest_group], tenant=TEST_TENANT) self.assert_called_with_defs( api_call, [expected_map_def, expected_entry_def])
def get_realized_state(self, domain_id, map_id, ep_id, tenant=policy_constants.POLICY_INFRA_TENANT): map_def = policy_defs.CommunicationMapDef(map_id, domain_id, tenant=tenant) path = map_def.get_realized_state_path(ep_id) return self._get_realized_state(path)
def create_or_overwrite(self, name, domain_id, map_id=None, description=None, precedence=0, category=policy_constants.CATEGORY_DEFAULT, sequence_number=None, service_ids=None, action=policy_constants.ACTION_ALLOW, source_groups=None, dest_groups=None, tenant=policy_constants.POLICY_INFRA_TENANT): """Create CommunicationMap & Entry. source_groups/dest_groups should be a list of group ids belonging to the domain. NOTE: In multi-connection environment, it is recommended to execute this call under lock to prevent race condition where two entries end up with same sequence number. """ # Validate and convert inputs if not service_ids: # service-ids must be provided err_msg = (_("Cannot create a communication map %(name)s without " "services") % {'name': name}) raise exceptions.ManagerError(details=err_msg) if map_id: # get the next available sequence number last_sequence = self._get_last_seq_num(domain_id, map_id, tenant=tenant) else: map_id = self._init_obj_uuid(map_id) last_sequence = -1 if not sequence_number: if last_sequence < 0: sequence_number = 1 else: sequence_number = last_sequence + 1 # Build the communication entry. Since we currently support only one # it will have the same id as its parent entry_def = policy_defs.CommunicationMapEntryDef( domain_id=domain_id, map_id=map_id, entry_id=map_id, name=name, description=description, sequence_number=sequence_number, source_groups=source_groups, dest_groups=dest_groups, service_ids=service_ids, action=action, tenant=tenant) map_def = policy_defs.CommunicationMapDef( domain_id=domain_id, map_id=map_id, tenant=tenant, name=name, description=description, precedence=precedence, category=category) if last_sequence < 0: # if communication map is absent, we need to create it return self.policy_api.create_with_parent(map_def, entry_def) # TODO(asarfaty) combine both calls together self.policy_api.create_or_update(map_def) self.policy_api.create_or_update(entry_def) return self.get(domain_id, map_id, tenant=tenant)