Skip to content

Commit

Permalink
Merge of bucket tag lists added (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillston committed Mar 1, 2021
1 parent 98a37e0 commit da34a5c
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/main/java/com/emc/ecs/servicebroker/service/EcsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.emc.ecs.servicebroker.model.Constants.*;

Expand Down Expand Up @@ -702,6 +703,48 @@ public DataServiceReplicationGroup lookupReplicationGroup(String replicationGrou
.orElseThrow(() -> new ServiceBrokerException("ECS replication group not found: " + replicationGroup));
}

static List<Map<String, String>> mergeBucketTags(ServiceDefinitionProxy service, PlanProxy plan, Map<String, Object> requestParameters) {
List<Map<String, String>> serviceTags = (List<Map<String, String>>)service.getServiceSettings().get(TAGS);
List<Map<String, String>> planTags = (List<Map<String, String>>)plan.getServiceSettings().get(TAGS);
List<Map<String, String>> requestedTags = (List<Map<String, String>>)requestParameters.get(TAGS);
List<Map<String, String>> unmatchedTags;

if (planTags != null && serviceTags != null) {
unmatchedTags = new ArrayList<>(planTags);

for (Map<String, String> planTag: planTags) {
for (Map<String, String> serviceTag: serviceTags) {
if (planTag.get(KEY).equals(serviceTag.get(KEY))) {
unmatchedTags.remove(planTag);
}
}
}

serviceTags = Stream.concat(serviceTags.stream(), unmatchedTags.stream()).collect(Collectors.toList());
} else if (serviceTags == null && planTags != null) {
serviceTags = new ArrayList<>(planTags);
}

if (requestedTags != null && serviceTags != null) {
unmatchedTags = new ArrayList<>(requestedTags);

for (Map<String, String> requestedTag: requestedTags) {
for (Map<String, String> serviceTag: serviceTags) {
if (requestedTag.get(KEY).equals(serviceTag.get(KEY))) {
unmatchedTags.remove(requestedTag);
break;
}
}
}

serviceTags = Stream.concat(serviceTags.stream(), unmatchedTags.stream()).collect(Collectors.toList());
} else if (serviceTags == null && requestedTags != null) {
serviceTags = new ArrayList<>(requestedTags);
}

return serviceTags;
}

/**
* Merge request additional parameters with with broker, plan and service settings
* <p>
Expand All @@ -718,6 +761,12 @@ static Map<String, Object> mergeParameters(BrokerConfig brokerConfig, ServiceDef

ret.putAll(service.getServiceSettings());

List<Map<String, String>> tags = mergeBucketTags(service, plan, requestParameters);

if (tags != null) {
ret.put(TAGS, tags);
}

return ret;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,104 @@ public void changeBucketTagsCreateTest() throws Exception {
assertEquals(NAMESPACE_NAME, tagsParam.getNamespace());
}

/**
* A service can merge lists of bucket tags presented in service and plan descriptions and provided
* in request on bucket creation.
* <p>
* Bucket tags obey following priority:
* <ul>
* <li> Plan bucket tags has lower priority than service tags and therefore would be overwritten by last ones</li>
* <li> Requested bucket tags has lower priority than plan tags and therefore would be overwritten by last ones</li>
* </ul>
* </p>
*/
@Test
public void mergeBucketTagsTest() {
Map<String, Object> serviceParams = new HashMap<>();
Map<String, Object> planParams = new HashMap<>();
Map<String, Object> requestedParams = new HashMap<>();

serviceParams.put(TAGS, createListOfTags(KEY1, VALUE1));
planParams.put(TAGS, createListOfTags(KEY1, VALUE2, KEY2, VALUE2));
requestedParams.put(TAGS, createListOfTags(KEY1, VALUE3, KEY2, VALUE3, KEY3, VALUE3));

ServiceDefinitionProxy service = new ServiceDefinitionProxy();
service.setServiceSettings(serviceParams);

PlanProxy plan = new PlanProxy();
plan.setServiceSettings(planParams);

List<Map<String, String>> resultTags = EcsService.mergeBucketTags(service, plan, requestedParams);
List<Map<String, String>> expectedTags = createListOfTags(KEY1, VALUE1, KEY2, VALUE2, KEY3, VALUE3);
assertTrue(CollectionUtils.isEqualCollection(expectedTags, resultTags));
}

/**
* A service can merge lists of bucket tags presented in service and plan descriptions and provided
* in request on bucket creation when one or more lists are null.
*/
@Test
public void mergeBucketTagsWithNullValueTest() {
Map<String, Object> serviceParams = new HashMap<>();
Map<String, Object> planParams = new HashMap<>();
Map<String, Object> requestedParams = new HashMap<>();

serviceParams.put(TAGS, createListOfTags(KEY1, VALUE1));
planParams.put(TAGS, createListOfTags(KEY2, VALUE2));
requestedParams.put(TAGS, createListOfTags(KEY3, VALUE3));

ServiceDefinitionProxy service = new ServiceDefinitionProxy();
service.setServiceSettings(serviceParams);

PlanProxy plan = new PlanProxy();
plan.setServiceSettings(planParams);

Map<String, Object> serviceParamsNull = new HashMap<>();
Map<String, Object> planParamsNull = new HashMap<>();
Map<String, Object> requestedParamsNull = new HashMap<>();

serviceParamsNull.put(TAGS, null);
planParamsNull.put(TAGS, null);
requestedParamsNull.put(TAGS, null);

ServiceDefinitionProxy serviceNull = new ServiceDefinitionProxy();
serviceNull.setServiceSettings(serviceParamsNull);

PlanProxy planNull = new PlanProxy();
planNull.setServiceSettings(planParamsNull);

List<Map<String, String>> resultTags = EcsService.mergeBucketTags(serviceNull, planNull, requestedParamsNull);
assertNull(resultTags);

resultTags = EcsService.mergeBucketTags(serviceNull, planNull, requestedParams);
List<Map<String, String>> expectedTags = createListOfTags(KEY3, VALUE3);
assertTrue(CollectionUtils.isEqualCollection(expectedTags, resultTags));

resultTags = EcsService.mergeBucketTags(serviceNull, plan, requestedParamsNull);
expectedTags = createListOfTags(KEY2, VALUE2);
assertTrue(CollectionUtils.isEqualCollection(expectedTags, resultTags));

resultTags = EcsService.mergeBucketTags(service, planNull, requestedParamsNull);
expectedTags = createListOfTags(KEY1, VALUE1);
assertTrue(CollectionUtils.isEqualCollection(expectedTags, resultTags));

resultTags = EcsService.mergeBucketTags(service, plan, requestedParamsNull);
expectedTags = createListOfTags(KEY1, VALUE1, KEY2, VALUE2);
assertTrue(CollectionUtils.isEqualCollection(expectedTags, resultTags));

resultTags = EcsService.mergeBucketTags(service, planNull, requestedParams);
expectedTags = createListOfTags(KEY1, VALUE1, KEY3, VALUE3);
assertTrue(CollectionUtils.isEqualCollection(expectedTags, resultTags));

resultTags = EcsService.mergeBucketTags(serviceNull, plan, requestedParams);
expectedTags = createListOfTags(KEY2, VALUE2, KEY3, VALUE3);
assertTrue(CollectionUtils.isEqualCollection(expectedTags, resultTags));

resultTags = EcsService.mergeBucketTags(service, plan, requestedParams);
expectedTags = createListOfTags(KEY1, VALUE1, KEY2, VALUE2, KEY3, VALUE3);
assertTrue(CollectionUtils.isEqualCollection(expectedTags, resultTags));
}

private void setupInitTest() throws EcsManagementClientException {
DataServiceReplicationGroup rg = new DataServiceReplicationGroup();
rg.setName(RG_NAME);
Expand Down

0 comments on commit da34a5c

Please sign in to comment.