Commit 0c05bc66 authored by Aliaksei Darafeyeu's avatar Aliaksei Darafeyeu
Browse files

retrieve eventGridTopicName and resourceGroupName from partitionAPI

parent b89b9c96
......@@ -56,16 +56,12 @@ public class AzureBootstrapConfig {
private String azureTenantId;
@Value("${azure.appResourceId}")
private String AzureAppResourceId;
private String azureAppResourceId;
private String keyIdentifier;
private String azureSubscriptionId;
private String resourceGroupName;
private String eventGridTopicName;
@Bean
@Named("KEY_VAULT_URL")
public String keyVaultURL() {
......@@ -78,15 +74,6 @@ public class AzureBootstrapConfig {
return cosmosDBName;
}
private void setEventGridTopicName(SecretClient kv) {
eventGridTopicName = getKeyVaultSecret(kv, "opendes-eventgrid-recordstopic").split("\\.")[0]
.replace("https://", "");
}
private void setResourceGroupName(SecretClient kv) {
resourceGroupName = getKeyVaultSecret(kv, "opendes-eventgrid-resourcegroup");
}
private void setAzureSubscriptionId(SecretClient kv) {
azureSubscriptionId = getKeyVaultSecret(kv, "subscription-id");
}
......@@ -109,9 +96,7 @@ public class AzureBootstrapConfig {
@Bean
public EventGridManager eventGridManager(SecretClient kv) {
setResourceGroupName(kv);
setAzureSubscriptionId(kv);
setEventGridTopicName(kv);
AzureTokenCredentials azureTokenCredentials = getAzureTokenCredentials();
return EventGridManager
......
package org.opengroup.osdu.register.provider.azure.subscriber;
import com.microsoft.azure.CloudException;
import com.microsoft.azure.arm.model.Indexable;
import com.microsoft.azure.management.eventgrid.v2020_04_01_preview.WebHookEventSubscriptionDestination;
import com.microsoft.azure.management.eventgrid.v2020_04_01_preview.implementation.EventGridManager;
import java.util.concurrent.atomic.AtomicReference;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.register.provider.azure.di.AzureBootstrapConfig;
import org.opengroup.osdu.register.provider.azure.util.PartitionHelper;
import org.opengroup.osdu.register.utils.AppServiceConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.concurrent.atomic.AtomicReference;
import com.microsoft.azure.CloudException;
import com.microsoft.azure.arm.model.Indexable;
import com.microsoft.azure.management.eventgrid.v2020_04_01_preview.WebHookEventSubscriptionDestination;
import com.microsoft.azure.management.eventgrid.v2020_04_01_preview.implementation.EventGridManager;
@Component
public class PushSubscription {
private static final String RESOURCE_PROVISIONING_ERROR_MESSAGE = "Resource cannot be updated during provisioning";
@Autowired
private EventGridManager eventGridManager;
@Autowired
private AzureBootstrapConfig azureBootstrapConfig;
@Autowired
private JaxRsDpsLog logger;
@Autowired
private AppServiceConfig serviceConfig;
@Autowired
private PartitionHelper partitionHelper;
public void createPushSubscription(String subscriptionId, String topicName) {
WebHookEventSubscriptionDestination subscriptionDestination = new WebHookEventSubscriptionDestination();
......@@ -35,7 +37,7 @@ public class PushSubscription {
subscriptionDestination.withAzureActiveDirectoryTenantId(azureBootstrapConfig.getAzureTenantId());
subscriptionDestination.withAzureActiveDirectoryApplicationIdOrUri(azureBootstrapConfig.getAzureAppResourceId());
String scope = String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.EventGrid/topics/%s", azureBootstrapConfig.getAzureSubscriptionId(), azureBootstrapConfig.getResourceGroupName(), topicName);
String scope = String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.EventGrid/topics/%s", azureBootstrapConfig.getAzureSubscriptionId(), partitionHelper.retrieveResourceGroup(), topicName);
AtomicReference<Throwable> error = new AtomicReference<>();
eventGridManager.eventSubscriptions().define(subscriptionId)
......@@ -63,7 +65,7 @@ public class PushSubscription {
}
public void deletePushSubscription(String subscriptionId, String topicName) {
String scope = String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.EventGrid/topics/%s", azureBootstrapConfig.getAzureSubscriptionId(), azureBootstrapConfig.getResourceGroupName(), topicName);
String scope = String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.EventGrid/topics/%s", azureBootstrapConfig.getAzureSubscriptionId(), partitionHelper.retrieveResourceGroup(), topicName);
AtomicReference<Throwable> error = new AtomicReference<>();
eventGridManager.eventSubscriptions().deleteAsync(scope, subscriptionId)
.subscribe(() -> logger.info(String.format("Push Subscription with id %s deleted successfully", subscriptionId)), error::set);
......@@ -82,7 +84,7 @@ public class PushSubscription {
}
public void checkIfPushSubscriptionExists(String subscriptionId, String topicName) {
String scope = String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.EventGrid/topics/%s", azureBootstrapConfig.getAzureSubscriptionId(), azureBootstrapConfig.getResourceGroupName(), topicName);
String scope = String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.EventGrid/topics/%s", azureBootstrapConfig.getAzureSubscriptionId(), partitionHelper.retrieveResourceGroup(), topicName);
AtomicReference<Throwable> error = new AtomicReference<>();
eventGridManager.eventSubscriptions().getAsync(scope, subscriptionId)
.subscribe(
......
package org.opengroup.osdu.register.provider.azure.subscriber;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.http.HttpStatus;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.DpsHeaders;
import org.opengroup.osdu.core.common.model.notification.Topic;
import org.opengroup.osdu.register.provider.azure.di.AzureBootstrapConfig;
import org.opengroup.osdu.register.provider.azure.util.PartitionHelper;
import org.opengroup.osdu.register.provider.interfaces.subscriber.ITopicsRepository;
import org.opengroup.osdu.register.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Repository;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
@Repository
@Primary
public class TopicsRepositoryImpl implements ITopicsRepository {
@Autowired
private JaxRsDpsLog log;
@Autowired
private AzureBootstrapConfig config;
private DpsHeaders headers;
@Autowired
private PartitionHelper partitionHelper;
private List<Topic> topics;
private Map<String, List<Topic>> topicsMap = new HashMap<>();
public List<Topic> listMessages() {
if (topics == null) {
String topicName = config.getEventGridTopicName();
Gson gson = new Gson();
java.lang.reflect.Type listType = new TypeToken<ArrayList<Topic>>() {
}.getType();
topics = gson.fromJson(getFile(), listType);
Topic topic = topics.get(0);
topic.setName(topicName);
topics.set(0, topic);
String partitionId = headers.getPartitionId();
if (topicsMap.containsKey(partitionId)) {
return topicsMap.get(partitionId);
}
Type type = new TypeToken<ArrayList<Topic>>() {}.getType();
List<Topic> topics = new Gson().fromJson(FileUtil.readFile("topics.json", log), type);
topics.set(0, getTopic(topics));
topicsMap.put(partitionId, topics);
return topics;
}
private String getFile() {
final String fileName = "topics.json";
try {
InputStream inputStream = new ClassPathResource(fileName).getInputStream();
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
return result.toString(StandardCharsets.UTF_8.name());
} catch (Exception e) {
log.error("Error retrieving topics.json", e);
throw new AppException(HttpStatus.SC_SERVICE_UNAVAILABLE, "Server error", "An unexpected error occurred");
}
private Topic getTopic(List<Topic> topics) {
Topic topic = topics.get(0);
topic.setName(partitionHelper.retrieveTopicName());
return topic;
}
}
package org.opengroup.osdu.register.provider.azure.util;
import org.opengroup.osdu.azure.partition.PartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionServiceClient;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class PartitionHelper {
@Autowired
private PartitionServiceClient partitionService;
@Autowired
private DpsHeaders headers;
public String retrieveResourceGroup() {
PartitionInfoAzure partition = partitionService.getPartition(headers.getPartitionId());
return partition.getEventGridResourceGroup();
}
public String retrieveTopicName() {
PartitionInfoAzure partition = partitionService.getPartition(headers.getPartitionId());
return partition.getEventGridRecordsTopicEndpoint().split("\\.")[0].replace("https://", "");
}
}
......@@ -30,6 +30,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.register.provider.azure.di.AzureBootstrapConfig;
import org.opengroup.osdu.register.provider.azure.util.PartitionHelper;
import org.opengroup.osdu.register.utils.AppServiceConfig;
import retrofit2.Response;
import rx.Completable;
......@@ -89,12 +90,15 @@ public class PushSubscriptionTest {
@Mock
private CloudError cloudError;
@Mock
private PartitionHelper partitionHelper;
@InjectMocks
private PushSubscription pushSubscription;
@BeforeEach
public void init() {
lenient().when(azureBootstrapConfig.getResourceGroupName()).thenReturn(resourceGroupName);
lenient().when(partitionHelper.retrieveResourceGroup()).thenReturn(resourceGroupName);
lenient().when(azureBootstrapConfig.getAzureSubscriptionId()).thenReturn(subscriptionId);
lenient().when(azureBootstrapConfig.getAzureTenantId()).thenReturn(tenantId);
lenient().when(azureBootstrapConfig.getAzureAppResourceId()).thenReturn(appResourceId);
......@@ -111,7 +115,6 @@ public class PushSubscriptionTest {
when(eventSubscriptionWithCreate.createAsync()).thenReturn(observable);
pushSubscription.createPushSubscription(subscriptionId, topicName);
verify(azureBootstrapConfig, times(1)).getResourceGroupName();
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(serviceConfig, times(1)).getRecordsChangePubsubEndpoint();
verify(eventGridManager, times(1)).eventSubscriptions();
......@@ -131,7 +134,6 @@ public class PushSubscriptionTest {
assertNotNull(exception);
assertEquals(500, exception.getError().getCode());
verify(azureBootstrapConfig, times(1)).getResourceGroupName();
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(serviceConfig, times(1)).getRecordsChangePubsubEndpoint();
verify(eventGridManager, times(1)).eventSubscriptions();
......@@ -146,7 +148,6 @@ public class PushSubscriptionTest {
pushSubscription.deletePushSubscription(subscriptionId, topicName);
verify(azureBootstrapConfig, times(1)).getResourceGroupName();
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(eventGridManager, times(1)).eventSubscriptions();
verify(logger, times(1)).info(String.format("Push Subscription with id %s deleted successfully", subscriptionId));
......@@ -170,7 +171,6 @@ public class PushSubscriptionTest {
assertEquals(409, exception.getError().getCode());
assertEquals(errorMessage, exception.getError().getReason());
assertEquals(errorMessage, exception.getError().getMessage());
verify(azureBootstrapConfig, times(1)).getResourceGroupName();
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(eventGridManager, times(1)).eventSubscriptions();
}
......@@ -190,7 +190,6 @@ public class PushSubscriptionTest {
assertEquals(500, exception.getError().getCode());
assertEquals("Server Error", exception.getError().getReason());
assertEquals("Unexpected error deleting Push subscription", exception.getError().getMessage());
verify(azureBootstrapConfig, times(1)).getResourceGroupName();
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(eventGridManager, times(1)).eventSubscriptions();
}
......@@ -202,7 +201,6 @@ public class PushSubscriptionTest {
pushSubscription.checkIfPushSubscriptionExists(subscriptionId, topicName);
verify(azureBootstrapConfig, times(1)).getResourceGroupName();
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(eventGridManager, times(1)).eventSubscriptions();
verify(logger, times(1)).info(String.format("Push Subscription with id %s fetched successfully", subscriptionId));
......@@ -221,7 +219,6 @@ public class PushSubscriptionTest {
assertEquals(404, exception.getError().getCode());
assertEquals("Not found", exception.getError().getReason());
assertEquals(String.format("Push Subscription with id %s does not exist.", subscriptionId), exception.getError().getMessage());
verify(azureBootstrapConfig, times(1)).getResourceGroupName();
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(eventGridManager, times(1)).eventSubscriptions();
verify(logger, times(1)).error(String.format("Push Subscription with id %s does not exist.", subscriptionId));
......@@ -240,7 +237,6 @@ public class PushSubscriptionTest {
assertEquals(500, exception.getError().getCode());
assertEquals("Server Error", exception.getError().getReason());
assertEquals("Unexpected error while Fetching Push subscription", exception.getError().getMessage());
verify(azureBootstrapConfig, times(1)).getResourceGroupName();
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(eventGridManager, times(1)).eventSubscriptions();
}
......
......@@ -18,18 +18,14 @@ package org.opengroup.osdu.register.subscriber.persistence;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.http.HttpStatus;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.notification.Topic;
import org.opengroup.osdu.register.provider.interfaces.subscriber.ITopicsRepository;
import org.opengroup.osdu.register.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Repository;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
......@@ -46,25 +42,9 @@ public class TopicsRepository implements ITopicsRepository {
Gson gson = new Gson();
java.lang.reflect.Type listType = new TypeToken<ArrayList<Topic>>() {
}.getType();
topics = gson.fromJson(getFile(), listType);
topics = gson.fromJson(FileUtil.readFile("topics.json", log), listType);
}
return topics;
}
private String getFile() {
final String fileName = "topics.json";
try {
InputStream inputStream = new ClassPathResource(fileName).getInputStream();
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
return result.toString(StandardCharsets.UTF_8.name());
} catch (Exception e) {
log.error("Error retrieving topics.json", e);
throw new AppException(HttpStatus.SC_SERVICE_UNAVAILABLE, "Server error", "An unexpected error occurred");
}
}
}
......@@ -40,7 +40,7 @@ public class AvailableTopics {
}
public List<String> listNames() {
return topicRepo.listMessages().stream().map(m -> m.getName()).collect(Collectors.toList());
return topicRepo.listMessages().stream().map(Topic::getName).collect(Collectors.toList());
}
public List<Topic> list() {
......
package org.opengroup.osdu.register.utils;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.apache.http.HttpStatus;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.springframework.core.io.ClassPathResource;
public class FileUtil {
private FileUtil() {
}
public static String readFile(String fileName, JaxRsDpsLog log) {
try {
InputStream inputStream = new ClassPathResource(fileName).getInputStream();
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
return result.toString(StandardCharsets.UTF_8.name());
} catch (Exception e) {
log.error(String.format("Error retrieving %s", fileName), e);
throw new AppException(HttpStatus.SC_SERVICE_UNAVAILABLE, "Server error", "An unexpected error occurred");
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment