Commit a9fc1321 authored by Aman Verma's avatar Aman Verma
Browse files

Merge branch 'users/amaverma/addNewAPI' into 'master'

[azure] Adding new API to create public schemas in azure

See merge request !117
parents b956b20d 547ce1c2
Pipeline #49492 failed with stages
in 19 seconds
This diff is collapsed.
...@@ -29,15 +29,22 @@ ...@@ -29,15 +29,22 @@
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>
<osdu.corelibazure.version>0.6.1</osdu.corelibazure.version> <azure.version>2.1.7</azure.version>
<osdu.oscorecommon.version>0.6.9</osdu.oscorecommon.version> <osdu.corelibazure.version>0.11.0-SNAPSHOT</osdu.corelibazure.version>
<osdu.oscorecommon.version>0.10.0-SNAPSHOT</osdu.oscorecommon.version>
<osdu.os-schema-core.version>0.10.0-SNAPSHOT</osdu.os-schema-core.version> <osdu.os-schema-core.version>0.10.0-SNAPSHOT</osdu.os-schema-core.version>
<mockito.version>1.10.19</mockito.version> <mockito.version>1.10.19</mockito.version>
<cucumber.version>5.4.0</cucumber.version> <cucumber.version>5.4.0</cucumber.version>
<nimbus-jose-jwt-azure.version>8.20.2</nimbus-jose-jwt-azure.version>
</properties> </properties>
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>nimbus-jose-jwt</artifactId>
<version>${nimbus-jose-jwt-azure.version}</version>
</dependency>
<!-- Inherit managed dependencies from core-lib-azure --> <!-- Inherit managed dependencies from core-lib-azure -->
<dependency> <dependency>
<groupId>org.opengroup.osdu</groupId> <groupId>org.opengroup.osdu</groupId>
...@@ -56,16 +63,6 @@ ...@@ -56,16 +63,6 @@
</dependencyManagement> </dependencyManagement>
<dependencies> <dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-active-directory-spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency> <dependency>
<groupId>org.opengroup.osdu</groupId> <groupId>org.opengroup.osdu</groupId>
...@@ -117,7 +114,6 @@ ...@@ -117,7 +114,6 @@
<version>${cucumber.version}</version> <version>${cucumber.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.cucumber</groupId> <groupId>io.cucumber</groupId>
<artifactId>cucumber-guice</artifactId> <artifactId>cucumber-guice</artifactId>
...@@ -147,6 +143,17 @@ ...@@ -147,6 +143,17 @@
<artifactId>jackson-dataformat-cbor</artifactId> <artifactId>jackson-dataformat-cbor</artifactId>
<version>2.12.3</version> <version>2.12.3</version>
</dependency> </dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-boot-starter-active-directory</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-boot</artifactId>
<version>3.5.0</version>
</dependency>
</dependencies> </dependencies>
......
// Copyright © Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.opengroup.osdu.schema.azure.api;
import org.opengroup.osdu.schema.azure.interfaces.ISchemaServiceAzure;
import org.opengroup.osdu.schema.exceptions.ApplicationException;
import org.opengroup.osdu.schema.exceptions.BadRequestException;
import org.opengroup.osdu.schema.model.SchemaInfo;
import org.opengroup.osdu.schema.model.SchemaRequest;
import org.opengroup.osdu.schema.model.SchemaUpsertResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
@RestController
@RequestMapping("/schemas/system")
public class AzureSchemaApi {
@Autowired
ISchemaServiceAzure schemaService;
@PutMapping()
@PreAuthorize("@authorizationFilterSP.hasPermissions()")
public ResponseEntity<SchemaInfo> upsertSystemSchema(@Valid @RequestBody SchemaRequest schemaRequest)
throws ApplicationException, BadRequestException {
SchemaUpsertResponse upsertResp = schemaService.upsertSystemSchema(schemaRequest);
ResponseEntity<SchemaInfo> response = new ResponseEntity<>(upsertResp.getSchemaInfo(), upsertResp.getHttpCode());
return response;
}
}
package org.opengroup.osdu.schema.azure.auth;
import org.opengroup.osdu.schema.azure.interfaces.IAuthorizationServiceForServicePrincipal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;
@Component("authorizationFilterSP")
@RequestScope
public class AuthorizationFilterSP {
@Autowired
private IAuthorizationServiceForServicePrincipal authorizationService;
public boolean hasPermissions() {
return authorizationService.isDomainAdminServiceAccount();
}
}
package org.opengroup.osdu.schema.azure.di;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@Getter
@Setter
@ConfigurationProperties("azure.system")
public class SystemResourceConfig {
private String storageContainerName;
private String cosmosDatabase;
private String sharedTenant;
}
...@@ -21,6 +21,7 @@ import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; ...@@ -21,6 +21,7 @@ import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders; import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.schema.azure.definitions.AuthorityDoc; import org.opengroup.osdu.schema.azure.definitions.AuthorityDoc;
import org.opengroup.osdu.schema.azure.di.SystemResourceConfig;
import org.opengroup.osdu.schema.constants.SchemaConstants; import org.opengroup.osdu.schema.constants.SchemaConstants;
import org.opengroup.osdu.azure.cosmosdb.CosmosStore; import org.opengroup.osdu.azure.cosmosdb.CosmosStore;
...@@ -31,6 +32,7 @@ import org.opengroup.osdu.schema.exceptions.NotFoundException; ...@@ -31,6 +32,7 @@ import org.opengroup.osdu.schema.exceptions.NotFoundException;
import org.opengroup.osdu.schema.model.Authority; import org.opengroup.osdu.schema.model.Authority;
import org.opengroup.osdu.schema.provider.interfaces.schemainfostore.IAuthorityStore; import org.opengroup.osdu.schema.provider.interfaces.schemainfostore.IAuthorityStore;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.text.MessageFormat; import java.text.MessageFormat;
...@@ -58,6 +60,9 @@ public class AzureAuthorityStore implements IAuthorityStore { ...@@ -58,6 +60,9 @@ public class AzureAuthorityStore implements IAuthorityStore {
@Autowired @Autowired
JaxRsDpsLog log; JaxRsDpsLog log;
@Autowired
SystemResourceConfig systemResourceConfig;
/** /**
* Method to get Authority from Azure store * Method to get Authority from Azure store
* @param authorityId * @param authorityId
...@@ -69,9 +74,15 @@ public class AzureAuthorityStore implements IAuthorityStore { ...@@ -69,9 +74,15 @@ public class AzureAuthorityStore implements IAuthorityStore {
public Authority get(String authorityId) throws NotFoundException, ApplicationException { public Authority get(String authorityId) throws NotFoundException, ApplicationException {
String id = headers.getPartitionId() + ":" + authorityId; String id = headers.getPartitionId() + ":" + authorityId;
AuthorityDoc authorityDoc;
AuthorityDoc authorityDoc = cosmosStore.findItem(headers.getPartitionId(), cosmosDBName, authorityContainer, id, authorityId, AuthorityDoc.class)
.orElseThrow(() -> new NotFoundException("bad input parameter")); if (systemResourceConfig.getSharedTenant().equalsIgnoreCase(headers.getPartitionId())) {
authorityDoc = cosmosStore.findItem(systemResourceConfig.getCosmosDatabase(), authorityContainer, id, authorityId, AuthorityDoc.class)
.orElseThrow(() -> new NotFoundException("bad input parameter"));
} else {
authorityDoc = cosmosStore.findItem(headers.getPartitionId(), cosmosDBName, authorityContainer, id, authorityId, AuthorityDoc.class)
.orElseThrow(() -> new NotFoundException("bad input parameter"));
}
return authorityDoc.getAuthority(); return authorityDoc.getAuthority();
} }
...@@ -89,7 +100,11 @@ public class AzureAuthorityStore implements IAuthorityStore { ...@@ -89,7 +100,11 @@ public class AzureAuthorityStore implements IAuthorityStore {
try { try {
AuthorityDoc authorityDoc = new AuthorityDoc(id, authority); AuthorityDoc authorityDoc = new AuthorityDoc(id, authority);
cosmosStore.createItem(headers.getPartitionId(), cosmosDBName, authorityContainer, id, authorityDoc); if (systemResourceConfig.getSharedTenant().equalsIgnoreCase(headers.getPartitionId())) {
cosmosStore.createItem(systemResourceConfig.getCosmosDatabase(), authorityContainer, id, authorityDoc);
} else {
cosmosStore.createItem(headers.getPartitionId(), cosmosDBName, authorityContainer, id, authorityDoc);
}
} catch (AppException ex) { } catch (AppException ex) {
if (ex.getError().getCode() == 409) { if (ex.getError().getCode() == 409) {
log.warning(SchemaConstants.AUTHORITY_EXISTS_ALREADY_REGISTERED); log.warning(SchemaConstants.AUTHORITY_EXISTS_ALREADY_REGISTERED);
......
...@@ -20,6 +20,7 @@ import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; ...@@ -20,6 +20,7 @@ import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders; import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.schema.azure.definitions.EntityTypeDoc; import org.opengroup.osdu.schema.azure.definitions.EntityTypeDoc;
import org.opengroup.osdu.schema.azure.di.SystemResourceConfig;
import org.opengroup.osdu.schema.constants.SchemaConstants; import org.opengroup.osdu.schema.constants.SchemaConstants;
import org.opengroup.osdu.schema.exceptions.ApplicationException; import org.opengroup.osdu.schema.exceptions.ApplicationException;
import org.opengroup.osdu.schema.exceptions.BadRequestException; import org.opengroup.osdu.schema.exceptions.BadRequestException;
...@@ -27,6 +28,7 @@ import org.opengroup.osdu.schema.exceptions.NotFoundException; ...@@ -27,6 +28,7 @@ import org.opengroup.osdu.schema.exceptions.NotFoundException;
import org.opengroup.osdu.schema.model.EntityType; import org.opengroup.osdu.schema.model.EntityType;
import org.opengroup.osdu.schema.provider.interfaces.schemainfostore.IEntityTypeStore; import org.opengroup.osdu.schema.provider.interfaces.schemainfostore.IEntityTypeStore;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import lombok.extern.java.Log; import lombok.extern.java.Log;
...@@ -55,6 +57,9 @@ public class AzureEntityTypeStore implements IEntityTypeStore { ...@@ -55,6 +57,9 @@ public class AzureEntityTypeStore implements IEntityTypeStore {
@Autowired @Autowired
JaxRsDpsLog log; JaxRsDpsLog log;
@Autowired
SystemResourceConfig systemResourceConfig;
/** /**
* Method to get entity type from azure store * Method to get entity type from azure store
* @param entityTypeId * @param entityTypeId
...@@ -66,9 +71,15 @@ public class AzureEntityTypeStore implements IEntityTypeStore { ...@@ -66,9 +71,15 @@ public class AzureEntityTypeStore implements IEntityTypeStore {
public EntityType get(String entityTypeId) throws NotFoundException, ApplicationException { public EntityType get(String entityTypeId) throws NotFoundException, ApplicationException {
String id = headers.getPartitionId() + ":" + entityTypeId; String id = headers.getPartitionId() + ":" + entityTypeId;
EntityTypeDoc entityTypeDoc;
EntityTypeDoc entityTypeDoc = cosmosStore.findItem(headers.getPartitionId(), cosmosDBName, entityTypeContainer, id, entityTypeId, EntityTypeDoc.class)
.orElseThrow(() -> new NotFoundException("bad input parameter")); if (systemResourceConfig.getSharedTenant().equalsIgnoreCase(headers.getPartitionId())) {
entityTypeDoc = cosmosStore.findItem(systemResourceConfig.getCosmosDatabase(), entityTypeContainer, id, entityTypeId, EntityTypeDoc.class)
.orElseThrow(() -> new NotFoundException("bad input parameter"));
} else {
entityTypeDoc = cosmosStore.findItem(headers.getPartitionId(), cosmosDBName, entityTypeContainer, id, entityTypeId, EntityTypeDoc.class)
.orElseThrow(() -> new NotFoundException("bad input parameter"));
}
return entityTypeDoc.getEntityType(); return entityTypeDoc.getEntityType();
} }
...@@ -86,7 +97,11 @@ public class AzureEntityTypeStore implements IEntityTypeStore { ...@@ -86,7 +97,11 @@ public class AzureEntityTypeStore implements IEntityTypeStore {
try { try {
EntityTypeDoc entityTypeDoc = new EntityTypeDoc(id, entityType); EntityTypeDoc entityTypeDoc = new EntityTypeDoc(id, entityType);
cosmosStore.createItem(headers.getPartitionId(), cosmosDBName, entityTypeContainer, id, entityTypeDoc); if (systemResourceConfig.getSharedTenant().equalsIgnoreCase(headers.getPartitionId())) {
cosmosStore.createItem(systemResourceConfig.getCosmosDatabase(), entityTypeContainer, id, entityTypeDoc);
} else {
cosmosStore.createItem(headers.getPartitionId(), cosmosDBName, entityTypeContainer, id, entityTypeDoc);
}
} catch (AppException ex) { } catch (AppException ex) {
if (ex.getError().getCode() == 409) { if (ex.getError().getCode() == 409) {
log.warning(SchemaConstants.ENTITY_TYPE_EXISTS); log.warning(SchemaConstants.ENTITY_TYPE_EXISTS);
......
...@@ -31,6 +31,7 @@ import org.opengroup.osdu.core.common.model.tenant.TenantInfo; ...@@ -31,6 +31,7 @@ import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory; import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory;
import org.opengroup.osdu.schema.azure.definitions.FlattenedSchemaInfo; import org.opengroup.osdu.schema.azure.definitions.FlattenedSchemaInfo;
import org.opengroup.osdu.schema.azure.definitions.SchemaInfoDoc; import org.opengroup.osdu.schema.azure.definitions.SchemaInfoDoc;
import org.opengroup.osdu.schema.azure.di.SystemResourceConfig;
import org.opengroup.osdu.schema.constants.SchemaConstants; import org.opengroup.osdu.schema.constants.SchemaConstants;
import org.opengroup.osdu.schema.enums.SchemaScope; import org.opengroup.osdu.schema.enums.SchemaScope;
import org.opengroup.osdu.schema.enums.SchemaStatus; import org.opengroup.osdu.schema.enums.SchemaStatus;
...@@ -80,6 +81,9 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore { ...@@ -80,6 +81,9 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore {
@Value("${shared.tenant.name:common}") @Value("${shared.tenant.name:common}")
private String sharedTenant; private String sharedTenant;
@Autowired
SystemResourceConfig systemResourceConfig;
@Autowired @Autowired
JaxRsDpsLog log; JaxRsDpsLog log;
...@@ -99,8 +103,8 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore { ...@@ -99,8 +103,8 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore {
SchemaIdentity schemaIdentity = schemaKindToSchemaIdentity(schemaId); SchemaIdentity schemaIdentity = schemaKindToSchemaIdentity(schemaId);
String partitioningKey = createSchemaInfoPartitionKey(schemaIdentity); String partitioningKey = createSchemaInfoPartitionKey(schemaIdentity);
SchemaInfoDoc schemaInfoDoc = cosmosStore.findItem(headers.getPartitionId(), cosmosDBName, schemaInfoContainer, id, partitioningKey, SchemaInfoDoc.class) SchemaInfoDoc schemaInfoDoc = findItemInCosmosStore(headers.getPartitionId(), cosmosDBName, schemaInfoContainer, id, partitioningKey, SchemaInfoDoc.class)
.orElseThrow(() -> new NotFoundException(SchemaConstants.SCHEMA_NOT_PRESENT)); .orElseThrow(() -> new NotFoundException(SchemaConstants.SCHEMA_NOT_PRESENT));
return getSchemaInfoObject(schemaInfoDoc.getFlattenedSchemaInfo(), headers.getPartitionId()); return getSchemaInfoObject(schemaInfoDoc.getFlattenedSchemaInfo(), headers.getPartitionId());
} }
...@@ -120,7 +124,7 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore { ...@@ -120,7 +124,7 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore {
String partitionKey = createSchemaInfoPartitionKey(schema.getSchemaInfo().getSchemaIdentity()); String partitionKey = createSchemaInfoPartitionKey(schema.getSchemaInfo().getSchemaIdentity());
SchemaInfoDoc schemaInfoDoc = new SchemaInfoDoc(id, partitionKey, flattenedSchemaInfo); SchemaInfoDoc schemaInfoDoc = new SchemaInfoDoc(id, partitionKey, flattenedSchemaInfo);
try { try {
cosmosStore.createItem(headers.getPartitionId(), cosmosDBName, schemaInfoContainer, partitionKey, schemaInfoDoc); crateItemInCosmos(headers.getPartitionId(), cosmosDBName, schemaInfoContainer, partitionKey, schemaInfoDoc);
} catch (AppException ex) { } catch (AppException ex) {
if (ex.getError().getCode() == 409) { if (ex.getError().getCode() == 409) {
log.warning(SchemaConstants.SCHEMA_ID_EXISTS); log.warning(SchemaConstants.SCHEMA_ID_EXISTS);
...@@ -150,7 +154,7 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore { ...@@ -150,7 +154,7 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore {
String partitionKey = createSchemaInfoPartitionKey(schema.getSchemaInfo().getSchemaIdentity()); String partitionKey = createSchemaInfoPartitionKey(schema.getSchemaInfo().getSchemaIdentity());
SchemaInfoDoc schemaInfoDoc = new SchemaInfoDoc(id, partitionKey, flattenedSchemaInfo); SchemaInfoDoc schemaInfoDoc = new SchemaInfoDoc(id, partitionKey, flattenedSchemaInfo);
try { try {
cosmosStore.upsertItem(headers.getPartitionId(), cosmosDBName, schemaInfoContainer, partitionKey,schemaInfoDoc); upsertItemInCosmos(headers.getPartitionId(), cosmosDBName, schemaInfoContainer, partitionKey,schemaInfoDoc);
} catch (Exception ex) { } catch (Exception ex) {
log.error(MessageFormat.format(SchemaConstants.OBJECT_INVALID, ex.getMessage())); log.error(MessageFormat.format(SchemaConstants.OBJECT_INVALID, ex.getMessage()));
throw new ApplicationException(SchemaConstants.SCHEMA_CREATION_FAILED_INVALID_OBJECT); throw new ApplicationException(SchemaConstants.SCHEMA_CREATION_FAILED_INVALID_OBJECT);
...@@ -175,13 +179,13 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore { ...@@ -175,13 +179,13 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore {
String partitionKey = createSchemaInfoPartitionKey(schemaIdentity); String partitionKey = createSchemaInfoPartitionKey(schemaIdentity);
// Check whether SchemaInfo already exists // Check whether SchemaInfo already exists
Boolean exists = cosmosStore.findItem(headers.getPartitionId(), cosmosDBName, schemaInfoContainer, id, partitionKey, SchemaInfoDoc.class).isPresent(); Boolean exists = findItemInCosmosStore(headers.getPartitionId(), cosmosDBName, schemaInfoContainer, id, partitionKey, SchemaInfoDoc.class).isPresent();
if (!exists) { if (!exists) {
return false; return false;
} }
// Delete the item. // Delete the item.
cosmosStore.deleteItem(headers.getPartitionId(), cosmosDBName, schemaInfoContainer, id, headers.getPartitionId()); deleteItemInCosmos(headers.getPartitionId(), cosmosDBName, schemaInfoContainer, id, headers.getPartitionId());
return true; return true;
} }
...@@ -209,7 +213,7 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore { ...@@ -209,7 +213,7 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore {
pars.add(new SqlParameter("@entityType", schemaInfo.getSchemaIdentity().getEntityType())); pars.add(new SqlParameter("@entityType", schemaInfo.getSchemaIdentity().getEntityType()));
pars.add(new SqlParameter("@majorVersion", schemaInfo.getSchemaIdentity().getSchemaVersionMajor())); pars.add(new SqlParameter("@majorVersion", schemaInfo.getSchemaIdentity().getSchemaVersionMajor()));
List<SchemaInfoDoc> schemaInfoList = cosmosStore.queryItems(headers.getPartitionId(), cosmosDBName,schemaInfoContainer, query, options, SchemaInfoDoc.class); List<SchemaInfoDoc> schemaInfoList = queryItemsInCosmos(headers.getPartitionId(), cosmosDBName,schemaInfoContainer, query, options, SchemaInfoDoc.class);
TreeMap<Long, String> sortedMap = new TreeMap<>(Collections.reverseOrder()); TreeMap<Long, String> sortedMap = new TreeMap<>(Collections.reverseOrder());
for (SchemaInfoDoc info : schemaInfoList) for (SchemaInfoDoc info : schemaInfoList)
...@@ -244,7 +248,7 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore { ...@@ -244,7 +248,7 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore {
SchemaIdentity schemaIdentity = schemaKindToSchemaIdentity(schemaInfo.getSupersededBy().getId()); SchemaIdentity schemaIdentity = schemaKindToSchemaIdentity(schemaInfo.getSupersededBy().getId());
String partitionKey = createSchemaInfoPartitionKey(schemaIdentity); String partitionKey = createSchemaInfoPartitionKey(schemaIdentity);
if ( !cosmosStore.findItem(headers.getPartitionId(), cosmosDBName, schemaInfoContainer, id, partitionKey, FlattenedSchemaInfo.class).isPresent()) { if ( !findItemInCosmosStore(headers.getPartitionId(), cosmosDBName, schemaInfoContainer, id, partitionKey, FlattenedSchemaInfo.class).isPresent()) {
log.error(SchemaConstants.INVALID_SUPERSEDEDBY_ID); log.error(SchemaConstants.INVALID_SUPERSEDEDBY_ID);
throw new BadRequestException(SchemaConstants.INVALID_SUPERSEDEDBY_ID); throw new BadRequestException(SchemaConstants.INVALID_SUPERSEDEDBY_ID);
} }
...@@ -274,7 +278,7 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore { ...@@ -274,7 +278,7 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore {
String id = dataPartitionId + ":" + flattenedSchemaInfo.getSupersededBy(); String id = dataPartitionId + ":" + flattenedSchemaInfo.getSupersededBy();
SchemaIdentity schemaIdentity = schemaKindToSchemaIdentity(flattenedSchemaInfo.getSupersededBy()); SchemaIdentity schemaIdentity = schemaKindToSchemaIdentity(flattenedSchemaInfo.getSupersededBy());
String partitionKey = createSchemaInfoPartitionKey(schemaIdentity); String partitionKey = createSchemaInfoPartitionKey(schemaIdentity);
SchemaInfoDoc doc = cosmosStore.findItem(dataPartitionId, cosmosDBName, schemaInfoContainer, id, partitionKey, SchemaInfoDoc.class).get(); SchemaInfoDoc doc = findItemInCosmosStore(dataPartitionId, cosmosDBName, schemaInfoContainer, id, partitionKey, SchemaInfoDoc.class).get();
superSededBy = getSchemaIdentity(doc.getFlattenedSchemaInfo()); superSededBy = getSchemaIdentity(doc.getFlattenedSchemaInfo());
} }
...@@ -352,7 +356,7 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore { ...@@ -352,7 +356,7 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore {
query.getParameters().add(new SqlParameter(param, parameterMap.get(param))); query.getParameters().add(new SqlParameter(param, parameterMap.get(param)));
} }
List<SchemaInfoDoc> schemaInfoList = cosmosStore.queryItems(tenantId, cosmosDBName,schemaInfoContainer, query, options, SchemaInfoDoc.class); List<SchemaInfoDoc> schemaInfoList = queryItemsInCosmos(tenantId, cosmosDBName,schemaInfoContainer, query, options, SchemaInfoDoc.class);
List<SchemaInfo> schemaList = new LinkedList<>(); List<SchemaInfo> schemaList = new LinkedList<>();
for (SchemaInfoDoc info: schemaInfoList) for (SchemaInfoDoc info: schemaInfoList)
...@@ -386,7 +390,7 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore { ...@@ -386,7 +390,7 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore {
String id = tenant + ":" + schemaId; String id = tenant + ":" + schemaId;
String partitionKey = createSchemaInfoPartitionKey(schemaKindToSchemaIdentity(schemaId)); String partitionKey = createSchemaInfoPartitionKey(schemaKindToSchemaIdentity(schemaId));
try { try {
Boolean exists = cosmosStore.findItem(tenant, cosmosDBName, schemaInfoContainer, id, partitionKey, SchemaInfoDoc.class).isPresent(); Boolean exists = findItemInCosmosStore(tenant, cosmosDBName, schemaInfoContainer, id, partitionKey, SchemaInfoDoc.class).isPresent();
if (exists) { if (exists) {
return false; return false;
} }
...@@ -468,5 +472,72 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore { ...@@ -468,5 +472,72 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore {
":"+schemaIdentity.getEntityType()+ ":"+schemaIdentity.getEntityType()+
":"+schemaIdentity.getSchemaVersionMajor().toString(); ":"+schemaIdentity.getSchemaVersionMajor().toString();
} }
private <T> Optional<T> findItemInCosmosStore(
String dataPartitionId,
String dataBaseName,
String containerName,
String id,
String partitionKey,
Class<T> clazz) {
if (systemResourceConfig.getSharedTenant().equalsIgnoreCase(dataPartitionId)) {
return cosmosStore.findItem(systemResourceConfig.getCosmosDatabase(), containerName, id, partitionKey, clazz);
} else {
return cosmosStore.findItem(dataPartitionId, dataBaseName, containerName, id, partitionKey, clazz);
}
}
private <T> void crateItemInCosmos(
String dataPartitionId,
String dataBaseName,
String containerName,
String partitionKey,
T item) {
if (systemResourceConfig.getSharedTenant().equalsIgnoreCase(dataPartitionId)) {
cosmosStore.createItem(systemResourceConfig.getCosmosDatabase(), containerName, partitionKey, item);
} else {
cosmosStore.createItem(dataPartitionId, dataBaseName, containerName, partitionKey, item);
}
}
private <T> void upsertItemInCosmos(
String dataPartitionId,
String dataBaseName,
String containerName,
String partitionKey,
T item) {
if (systemResourceConfig.getSharedTenant().equalsIgnoreCase(dataPartitionId)) {
cosmosStore.upsertItem(systemResourceConfig.getCosmosDatabase(), containerName, partitionKey, item);
} else {
cosmosStore.upsertItem(dataPartitionId, dataBaseName, containerName, partitionKey, item);
}
}