Skip to content

Commit

Permalink
BucketTagsSet common XML root element added (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillston committed Nov 17, 2020
1 parent 38bf9a5 commit 2d5fe75
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 159 deletions.
32 changes: 10 additions & 22 deletions src/main/java/com/emc/ecs/management/sdk/BucketTagsAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,29 @@
import com.emc.ecs.management.sdk.model.BucketTagsParamUpdate;
import com.emc.ecs.servicebroker.exception.EcsManagementClientException;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

import java.util.List;
import java.util.Map;

import static com.emc.ecs.management.sdk.Constants.*;
import static com.emc.ecs.management.sdk.Constants.GET;

public class BucketTagsAction {

private BucketTagsAction(){
}
private BucketTagsAction(){}

public static void create(Connection connection, String id,
String namespace, List<Map<String, String> > tags)
public static void create(Connection connection, String id, String namespace, List<Map<String, String> > tags)
throws EcsManagementClientException {
UriBuilder uri = connection.getUriBuilder().
segment(OBJECT, BUCKET, id, TAGS);
connection.handleRemoteCall(POST, uri,
new BucketTagsParamAdd(namespace, tags));
UriBuilder uri = connection.getUriBuilder().segment(OBJECT, BUCKET, id, TAGS);
connection.handleRemoteCall(POST, uri, new BucketTagsParamAdd(namespace, tags));
}

public static void update(Connection connection, String id,
String namespace, List<Map<String, String> > tags) {
UriBuilder uri = connection.getUriBuilder()
.segment(OBJECT, BUCKET, id, TAGS);
connection.handleRemoteCall(PUT, uri,
new BucketTagsParamUpdate(namespace, tags));
public static void update(Connection connection, String id, String namespace, List<Map<String, String> > tags) {
UriBuilder uri = connection.getUriBuilder().segment(OBJECT, BUCKET, id, TAGS);
connection.handleRemoteCall(PUT, uri, new BucketTagsParamUpdate(namespace, tags));
}
public static void delete(Connection connection, String id,
String namespace, List<Map<String, String> > tags) {
UriBuilder uri = connection.getUriBuilder()
.segment(OBJECT, BUCKET, id, TAGS);
connection.handleRemoteCall(DELETE, uri,
new BucketTagsParamDelete(namespace, tags));
public static void delete(Connection connection, String id, String namespace, List<Map<String, String> > tags) {
UriBuilder uri = connection.getUriBuilder().segment(OBJECT, BUCKET, id, TAGS);
connection.handleRemoteCall(DELETE, uri, new BucketTagsParamDelete(namespace, tags));
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/emc/ecs/management/sdk/model/BucketTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public String getValue() {
public void setValue(String value) {
Value = value;
}

@Override
public String toString() {
return Key + ':' + Value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.emc.ecs.management.sdk.model;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@XmlRootElement
public class BucketTagSetRootElement {

private static final String KEY = "key";
private static final String VALUE = "value";

@XmlElementWrapper(name = "TagSet")
@XmlElement(name = "Tag")
private List<BucketTag> TagSet;

public BucketTagSetRootElement(){};

public BucketTagSetRootElement(List<Map<String, String>> tags) {
super();
setTagSetAsListOfMaps(tags);
}

public List<BucketTag> getTagSet() {
return TagSet;
}

public void setTagSet(List<BucketTag> tagSet) {
TagSet = tagSet;
}

public List<Map<String, String> > getTagSetAsListOfTags() {
List<Map<String, String> > list = new ArrayList<Map<String, String> >();
for (BucketTag tag: TagSet) {
Map<String, String> map = new HashMap<String, String>() {{
put(KEY, tag.getKey());
put(VALUE, tag.getValue());
}};
list.add(map);
}
return list;
}

public void setTagSetAsListOfMaps(List<Map<String, String> > tags) throws IllegalArgumentException {
List<BucketTag> tagList = new ArrayList<BucketTag>();
for (Map<String, String> tag: tags) {
try {
String key = tag.get(KEY);
String value = tag.get(VALUE);
tagList.add(new BucketTag(key, value));
} catch (Exception e) {
throw new IllegalArgumentException("Key and Value should be specified for bucket tag", e);
}
}
TagSet = tagList;
}

@Override
public String toString() {
StringBuilder output = new StringBuilder("{");
for (BucketTag tag: TagSet) {
output.append(tag.toString()).append(", ");
}
int length = output.length();
if (length != 1)
output.delete(length - 2, length);
output.append("}");
return output.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
package com.emc.ecs.management.sdk.model;

import org.springframework.cloud.servicebroker.exception.ServiceBrokerException;

import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class BucketTagsParam {

@XmlElementWrapper(name = "TagSet")
@XmlElement(name = "Tag")
private List<BucketTag> TagSet;
public class BucketTagsParam extends BucketTagSetRootElement{

private String namespace;

Expand All @@ -23,9 +15,8 @@ public BucketTagsParam(){
};

public BucketTagsParam(String namespace, List<Map<String, String> > tags) {
super();
super(tags);
this.namespace = namespace;
setTagSetAsListOfMaps(tags);
}

public String getNamespace() {
Expand All @@ -35,38 +26,4 @@ public String getNamespace() {
public void setNamespace(String namespace) {
this.namespace = namespace;
}

public List<BucketTag> getTagSet() {
return TagSet;
}

public void setTagSet(List<BucketTag> tagSet) {
TagSet = tagSet;
}

public List<Map<String, String> > getTagSetAsListOfTags() {
List<Map<String, String> > list = new ArrayList<Map<String, String> >();
for (BucketTag tag: TagSet) {
Map<String, String> map = new HashMap<String, String>() {{
put("key", tag.getKey());
put("value", tag.getValue());
}};
list.add(map);
}
return list;
}

public void setTagSetAsListOfMaps(List<Map<String, String> > tags) throws ServiceBrokerException {
List<BucketTag> tagList = new ArrayList<BucketTag>();
for (Map<String, String> tag: tags) {
try {
String key = tag.get("key");
String value = tag.get("value");
tagList.add(new BucketTag(key, value));
} catch (Exception e) {
throw new ServiceBrokerException("Key and Value should be specified for bucket tag", e);
}
}
TagSet = tagList;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package com.emc.ecs.management.sdk.model;

import org.springframework.cloud.servicebroker.exception.ServiceBrokerException;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@XmlRootElement(name = "bucket_info")
public class ObjectBucketInfo {
public class ObjectBucketInfo extends BucketTagSetRootElement{

private String id;
private String name;
Expand All @@ -34,10 +27,6 @@ public class ObjectBucketInfo {
private Boolean inactive;
private Vdc vdc;

@XmlElementWrapper(name = "TagSet")
@XmlElement(name = "Tag")
private List<BucketTag> TagSet;

public String getCreated() {
return created;
}
Expand Down Expand Up @@ -204,38 +193,4 @@ public String getName() {
public void setName(String name) {
this.name = name;
}

public List<BucketTag> getTagSet() {
return TagSet;
}

public void setTagSet(List<BucketTag> tagSet) {
TagSet = tagSet;
}

public List<Map<String, String>> getTagSetAsListOfTags() {
List<Map<String, String> > list = new ArrayList<Map<String, String> >();
for (BucketTag tag: TagSet) {
Map<String, String> map = new HashMap<String, String>() {{
put("key", tag.getKey());
put("value", tag.getValue());
}};
list.add(map);
}
return list;
}

public void setTagSetAsListOfMaps(List<Map<String, String> > tags) throws ServiceBrokerException {
List<BucketTag> tagList = new ArrayList<BucketTag>();
for (Map<String, String> tag: tags) {
try {
String key = tag.get("key");
String value = tag.get("value");
tagList.add(new BucketTag(key, value));
} catch (Exception e) {
throw new ServiceBrokerException("Key and Value should be specified for bucket tag", e);
}
}
TagSet = tagList;
}
}
98 changes: 52 additions & 46 deletions src/main/java/com/emc/ecs/servicebroker/service/EcsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,43 +192,7 @@ Map<String, Object> changeBucketPlan(String bucketName, ServiceDefinitionProxy s
}

if (parameters.containsKey(TAGS) && parameters.get(TAGS) != null) {
ObjectBucketInfo bucketInfo = BucketAction.get(connection, prefix(bucketName), broker.getNamespace());
List<Map<String, String> > currentTags = bucketInfo.getTagSetAsListOfTags();
List<Map<String, String> > newTags = (List<Map<String, String>>) parameters.get(TAGS);

List<Map<String, String> > updateTags = new ArrayList<Map<String, String> >();
List<Map<String, String> > createTags = new ArrayList<Map<String, String> >();
List<Map<String, String> > paramsTags = new ArrayList<Map<String, String> >();

do {
Map<String, String> newTag = newTags.get(0);
boolean isNew = true;
for (Map<String, String> currentTag: currentTags) {
if (newTag.get("key").equals(currentTag.get("key"))) {
if (!newTag.get("value").equals(currentTag.get("value"))) {
updateTags.add(newTag);
}
isNew = false;
currentTags.remove(currentTag);
break;
}
}
paramsTags.add(newTag);
if (isNew) {
createTags.add(newTag);
}
newTags.remove(newTag);
} while (!newTags.isEmpty());
paramsTags.addAll(currentTags);

logger.info("Setting new bucket tags on '{}': {}", prefix(bucketName), tagsOutput(createTags));
BucketTagsAction.create(connection, prefix(bucketName), broker.getNamespace(), createTags);

logger.info("Setting new values of existing bucket tags on '{}': {}", prefix(bucketName), tagsOutput(updateTags));
BucketTagsAction.update(connection, prefix(bucketName), broker.getNamespace(), updateTags);

logger.info("Full set of bucket tags on '{}' is {}", prefix(bucketName), tagsOutput(paramsTags));
parameters.put(TAGS, paramsTags);
changeBucketTags(bucketName, parameters);
}
} catch (EcsManagementClientException e) {
throw new ServiceBrokerException(e.getMessage(), e);
Expand Down Expand Up @@ -354,15 +318,6 @@ String prefix(String string) {
return broker.getPrefix() + string;
}

String tagsOutput(List<Map<String, String> > tags) {
String output = new String("{");
for (Map<String, String> tag: tags) {
output += tag.get("key") + ":" + tag.get("value") + ", ";
}
output = output.substring(0, output.length() - 2) + "}";
return output;
}

private void lookupObjectEndpoints() throws EcsManagementClientException {
if (broker.getObjectEndpoint() != null) {
try {
Expand Down Expand Up @@ -674,4 +629,55 @@ static Map<String, Object> mergeParameters(ServiceDefinitionProxy service, PlanP
parameters.putAll(service.getServiceSettings());
return parameters;
}

private Map<String, Object> changeBucketTags(String bucketName, Map<String, Object> parameters) {
List<BucketTag> requestedTags = new BucketTagSetRootElement((List<Map<String, String>>) parameters.get(TAGS)).getTagSet();
List<BucketTag> currentTags = BucketAction.get(connection, prefix(bucketName), broker.getNamespace()).getTagSet();

List<BucketTag> updateTags = new ArrayList<>();
List<BucketTag> createTags = new ArrayList<>();
List<BucketTag> paramsTags = new ArrayList<>();

do {
BucketTag requestedTag = requestedTags.get(0);
boolean isNew = true;
for (BucketTag currentTag: currentTags) {
if (requestedTag.getKey().equals(currentTag.getKey())) {
if (!requestedTag.getValue().equals(currentTag.getValue())) {
updateTags.add(requestedTag);
}
isNew = false;
currentTags.remove(currentTag);
break;
}
}
paramsTags.add(requestedTag);
if (isNew) {
createTags.add(requestedTag);
}
requestedTags.remove(requestedTag);
} while (!requestedTags.isEmpty());
paramsTags.addAll(currentTags);

if (createTags.size() != 0) {
BucketTagSetRootElement createTagSet = new BucketTagSetRootElement();
createTagSet.setTagSet(createTags);
logger.info("Setting new bucket tags on '{}': {}", prefix(bucketName), createTagSet.toString());
BucketTagsAction.create(connection, prefix(bucketName), broker.getNamespace(), createTagSet.getTagSetAsListOfTags());
}
if (updateTags.size() != 0) {
BucketTagSetRootElement updateTagSet = new BucketTagSetRootElement();
updateTagSet.setTagSet(updateTags);
logger.info("Setting new values of existing bucket tags on '{}': {}", prefix(bucketName), updateTagSet.toString());
BucketTagsAction.update(connection, prefix(bucketName), broker.getNamespace(), updateTagSet.getTagSetAsListOfTags());
}

BucketTagSetRootElement paramsTagSet = new BucketTagSetRootElement();
paramsTagSet.setTagSet(paramsTags);
parameters.put(TAGS, paramsTagSet.getTagSetAsListOfTags());
if (updateTags.size() + createTags.size() != 0) {
logger.info("Full set of bucket tags on '{}' is {}", prefix(bucketName), paramsTagSet.toString());
}
return parameters;
}
}

0 comments on commit 2d5fe75

Please sign in to comment.