Skip to content

Commit

Permalink
Fixed creation (or update) of similar endpoints in different scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
angelo.andreussi authored and Coduz committed May 22, 2023
1 parent 6438acf commit 1eb7762
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public EndpointInfo create(EndpointInfoCreator endpointInfoCreator)
null,
endpointInfoCreator.getSchema(),
endpointInfoCreator.getDns(),
endpointInfoCreator.getPort());
endpointInfoCreator.getPort(),
endpointInfoCreator.getEndpointType());

//
// Do create
Expand Down Expand Up @@ -146,7 +147,8 @@ public EndpointInfo update(EndpointInfo endpointInfo) throws KapuaException {
endpointInfo.getId(),
endpointInfo.getSchema(),
endpointInfo.getDns(),
endpointInfo.getPort());
endpointInfo.getPort(),
endpointInfo.getEndpointType());

//
// Do update
Expand Down Expand Up @@ -280,6 +282,7 @@ public long count(KapuaQuery query, String section)
AUTHORIZATION_SERVICE.checkPermission(
PERMISSION_FACTORY.newPermission(EndpointInfoDomains.ENDPOINT_INFO_DOMAIN, Actions.read, query.getScopeId())
);
addSectionToPredicate(query, section);

//
// Do count
Expand All @@ -302,6 +305,11 @@ public long count(KapuaQuery query, String section)
if (account == null) {
throw new KapuaEntityNotFoundException(Account.TYPE, query.getScopeId());
}
if (account.getScopeId() == null) {
// Query was originally on root account, and querying on parent scope id would mean querying in null,
// i.e. querying on all accounts. Since that's not what we want, break away.
break;
}

query.setScopeId(account.getScopeId());
endpointInfoCount = EndpointInfoDAO.count(em, query);
Expand All @@ -320,17 +328,18 @@ public long count(KapuaQuery query, String section)
//

/**
* Checks whether or not another {@link EndpointInfo} already exists with the given values.
* Checks whether another {@link EndpointInfo} already exists with the given values.
*
* @param scopeId The ScopeId of the {@link EndpointInfo}
* @param entityId The entity id, if exists. On update you need to exclude the same entity.
* @param schema The {@link EndpointInfo#getSchema()} value.
* @param dns The {@link EndpointInfo#getDns()} value.
* @param port The {@link EndpointInfo#getPort()} value.
* @param type The {@link EndpointInfo#getEndpointType()} value.
* @throws KapuaException if the values provided matches another {@link EndpointInfo}
* @since 1.0.0
*/
private void checkDuplicateEndpointInfo(KapuaId scopeId, KapuaId entityId, String schema, String dns, int port) throws KapuaException {
private void checkDuplicateEndpointInfo(KapuaId scopeId, KapuaId entityId, String schema, String dns, int port, String type) throws KapuaException {

EndpointInfoQuery query = new EndpointInfoQueryImpl(scopeId);

Expand All @@ -346,7 +355,7 @@ private void checkDuplicateEndpointInfo(KapuaId scopeId, KapuaId entityId, Strin

query.setPredicate(andPredicate);

if (count(query) > 0) {
if (count(query,type) > 0) {
List<Map.Entry<String, Object>> uniquesFieldValues = new ArrayList<>();
uniquesFieldValues.add(new AbstractMap.SimpleEntry<>(KapuaEntityAttributes.SCOPE_ID, scopeId));
uniquesFieldValues.add(new AbstractMap.SimpleEntry<>(EndpointInfoAttributes.SCHEMA, schema));
Expand Down

0 comments on commit 1eb7762

Please sign in to comment.