Skip to content
Snippets Groups Projects
Commit 46ab712e authored by Rucha Deshpande's avatar Rucha Deshpande
Browse files

Merge branch 'feat/aws-inttest-updates' into 'master'

Add List and Update Partition implementation and Int. tests

See merge request !42
parents 708d83df 3f6a2298
No related branches found
No related tags found
1 merge request!42Add List and Update Partition implementation and Int. tests
Pipeline #29851 failed
......@@ -106,7 +106,71 @@ public class PartitionServiceImpl implements IPartitionService {
@Override
public PartitionInfo updatePartition(String partitionId, PartitionInfo partitionInfo) {
throw new AppException(HttpStatus.SC_NOT_IMPLEMENTED, "Not implemented", "Not implemented");
if (!ssmHelper.partitionExists(partitionId)) {
throw new AppException(HttpStatus.SC_NOT_FOUND, "partition does not exist", "Partition doesn't exist");
}
if(partitionInfo.getProperties().containsKey("id")) {
throw new AppException(HttpStatus.SC_BAD_REQUEST, "Cannot update id", "the field id cannot be updated");
}
try {
for (Map.Entry<String, Property> entry : partitionInfo.getProperties().entrySet()) {
ssmHelper.createOrUpdateSecret(partitionId, entry.getKey(), entry.getValue().getValue());
}
/**
* SSM parameters are not immediately available after pushing to System Manager.
* This API is expected to return a 200 response meaning that the parameters should be available immediately.
* This logic is added to validate when the parameters become available before returning the 200 response.
* The performance hit is acceptable because partitions are only created as an early operation and shouldn't affect
* the performance of runtime workflows
*/
int retryCount = 10;
boolean partitionReady = false;
while (!partitionReady && retryCount > 0) {
retryCount--;
List<String> partitionCheck = ssmHelper.getSsmParamsPathsForPartition(partitionId);
if (partitionCheck.size() == partitionInfo.getProperties().size())
partitionReady = true;
else
Thread.sleep(500);
}
String rollbackSuccess = "Failed";
if (!partitionReady) {
try {
ssmHelper.deletePartitionSecrets(partitionId);
rollbackSuccess = "Succeeded";
}
catch (Exception e){
}
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Partition update Failed", "One or more secrets couldn't be stored. Rollback " + rollbackSuccess);
}
}
catch (AppException appE) {
throw appE;
}
catch (Exception e) {
try {
Thread.sleep(2000); //wait for any existing ssm parameters that got added to normalize
ssmHelper.deletePartitionSecrets(partitionId);
}
catch (Exception deleteE) {
//if the partition didnt get created at all deletePartition will throw an exception. Eat it so we return the creation exception.
}
logger.error("Failed to update partition due to key creation failure in ssm", e.getMessage());
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Partition update Failure", e.getMessage(), e);
}
return partitionInfo;
}
@Override
......@@ -135,8 +199,8 @@ public class PartitionServiceImpl implements IPartitionService {
@Override
public List<String> getAllPartitions() {
//TODO: Pending to be implemented
return null;
return ssmHelper.getPartitions();
}
}
......@@ -15,10 +15,7 @@
package org.opengroup.osdu.partition.provider.aws.util;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
import javax.inject.Inject;
......@@ -75,6 +72,10 @@ public final class SSMHelper {
return URI.create(getTenantPrefix() + '/' + partitionName + '/').normalize().toString();
}
private String getSsmPathForPartititions() {
return URI.create(getTenantPrefix() + '/').normalize().toString();
}
private String getSsmPathForPartititionSecret(String partitionName, String secretName) {
return URI.create(getSsmPathForPartitition(partitionName) + '/' + secretName).normalize().toString();
}
......@@ -162,6 +163,7 @@ public final class SSMHelper {
PutParameterRequest request = new PutParameterRequest()
.withName(ssmPath)
.withType(ParameterType.SecureString)
.withOverwrite(true)
.withValue(String.valueOf(secretValue));
......@@ -201,6 +203,42 @@ public final class SSMHelper {
}
public List<String> getPartitions() {
List<String> partitions = new ArrayList<>();
Set<String> uniquePartitions = new HashSet<String>();
String ssmPath = getSsmPathForPartititions();
String nextToken = null;
GetParametersByPathResult result=null;
do {
GetParametersByPathRequest request = new GetParametersByPathRequest()
.withPath(ssmPath)
.withRecursive(true)
.withNextToken(nextToken);
result = ssmManager.getParametersByPath(request);
nextToken = result.getNextToken();
}
while (nextToken != null);
for(Parameter p: result.getParameters())
{
String dp = (p.getName().substring(ssmPath.length()).split("/")[0]);
uniquePartitions.add(dp);
}
partitions.addAll(uniquePartitions);
return partitions;
}
private String getTenantPrefix() {
return awsServiceConfig.ssmPartitionPrefix;
}
......
// Copyright © 2021 Amazon Web Services
//
// 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.partition.api;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.opengroup.osdu.partition.util.AwsTestUtils;
public class TestListPartitions extends ListPartitionsApitTest {
@Before
@Override
public void setup() {
this.testUtils = new AwsTestUtils();
}
@After
@Override
public void tearDown() {
this.testUtils = null;
}
}
// Copyright © 2021 Amazon Web Services
//
// 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.partition.api;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.opengroup.osdu.partition.util.AwsTestUtils;
public class TestUpdatePartition extends UpdatePartitionTest {
@Before
@Override
public void setup() {
this.testUtils = new AwsTestUtils();
}
@After
@Override
public void tearDown() {
this.testUtils = null;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment