Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature 182 assign tags to bucket #189

Merged
merged 9 commits into from
Nov 24, 2020
31 changes: 31 additions & 0 deletions src/main/java/com/emc/ecs/management/sdk/BucketTagsAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.emc.ecs.management.sdk;

import com.emc.ecs.management.sdk.model.BucketTagsParam;
import com.emc.ecs.management.sdk.model.BucketTagsParamAdd;
import com.emc.ecs.management.sdk.model.BucketTagsParamDelete;
import com.emc.ecs.management.sdk.model.BucketTagsParamUpdate;
import com.emc.ecs.servicebroker.exception.EcsManagementClientException;

import javax.ws.rs.core.UriBuilder;

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

public class BucketTagsAction {

private BucketTagsAction(){}

public static void create(Connection connection, String id, BucketTagsParamAdd tagsParam)
throws EcsManagementClientException {
UriBuilder uri = connection.getUriBuilder().segment(OBJECT, BUCKET, id, TAGS);
connection.handleRemoteCall(POST, uri, tagsParam);
}

public static void update(Connection connection, String id, BucketTagsParamUpdate tagsParam) {
UriBuilder uri = connection.getUriBuilder().segment(OBJECT, BUCKET, id, TAGS);
connection.handleRemoteCall(PUT, uri, tagsParam);
}
public static void delete(Connection connection, String id, BucketTagsParamDelete tagsParam) {
UriBuilder uri = connection.getUriBuilder().segment(OBJECT, BUCKET, id, TAGS);
connection.handleRemoteCall(DELETE, uri, tagsParam);
}
}
1 change: 1 addition & 0 deletions src/main/java/com/emc/ecs/management/sdk/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public final class Constants {
public static final String INFO = "info";
public static final String DEACTIVATE = "deactivate";
public static final String RETENTION = "retention";
public static final String TAGS = "tags";
public static final String USERS = "users";
public static final String USER_SECRET_KEYS = "user-secret-keys";
public static final String VDC = "vdc";
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/com/emc/ecs/management/sdk/model/BucketTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.emc.ecs.management.sdk.model;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Tag")
@XmlAccessorType(XmlAccessType.FIELD)
public class BucketTag {

@XmlElement(name = "Key")
private String key;

@XmlElement(name = "Value")
private String value;

public BucketTag() {
super();
}

public BucketTag(String key, String value) {
super();
this.key = key;
this.value = value;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

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

import javax.xml.bind.annotation.*;
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";

private List<BucketTag> tagSet;

public BucketTagSetRootElement(){};

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

@XmlElementWrapper(name = "TagSet")
@XmlElement(name = "Tag")
public List<BucketTag> getTagSet() {
return tagSet;
}

public void setTagSet(List<BucketTag> tagSet) {
this.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
@@ -0,0 +1,29 @@
package com.emc.ecs.management.sdk.model;

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

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

private String namespace;

public BucketTagsParam(){
super();
};

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

public String getNamespace() {
return namespace;
}

public void setNamespace(String namespace) {
this.namespace = namespace;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.emc.ecs.management.sdk.model;

import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
import java.util.Map;

@XmlRootElement(name = "add_bucket_tags")
public class BucketTagsParamAdd extends BucketTagsParam {

public BucketTagsParamAdd(){
super();
};

public BucketTagsParamAdd(String namespace, List<Map<String, String>> tags) {
super(namespace, tags);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.emc.ecs.management.sdk.model;

import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
import java.util.Map;

@XmlRootElement(name = "delete_bucket_tags")
public class BucketTagsParamDelete extends BucketTagsParam {

public BucketTagsParamDelete(){
super();
};

public BucketTagsParamDelete(String namespace, List<Map<String, String>> tags) {
super(namespace, tags);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.emc.ecs.management.sdk.model;

import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
import java.util.Map;

@XmlRootElement(name = "update_bucket_tags")
public class BucketTagsParamUpdate extends BucketTagsParam {

public BucketTagsParamUpdate(){
super();
};

public BucketTagsParamUpdate(String namespace, List<Map<String, String>> tags) {
super(namespace, tags);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;

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

private String id;
private String name;
Expand All @@ -27,7 +26,6 @@ public class ObjectBucketInfo {
private Boolean internal;
private Boolean inactive;
private Vdc vdc;
private List<String> tags;

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

public List<String> getTags() {
return tags;
}

public void setTags(List<String> tags) {
this.tags = tags;
}
}
62 changes: 62 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 @@ -38,6 +38,7 @@ public class EcsService {
private static final String LIMIT = "limit";
private static final String QUOTA = "quota";
private static final String RETENTION = "retention";
private static final String TAGS = "tags";
private static final String DEFAULT_RETENTION = "default-retention";
private static final String INVALID_RECLAIM_POLICY = "Invalid reclaim-policy: ";
private static final String INVALID_ALLOWED_RECLAIM_POLICIES = "Invalid reclaim-policies: ";
Expand Down Expand Up @@ -149,6 +150,12 @@ Map<String, Object> createBucket(String id, String bucketName, ServiceDefinition
logger.info("Applying bucket retention policy on '{}': {}", bucketName, parameters.get(DEFAULT_RETENTION));
BucketRetentionAction.update(connection, broker.getNamespace(), prefix(bucketName), (int) parameters.get(DEFAULT_RETENTION));
}

if (parameters.containsKey(TAGS) && parameters.get(TAGS) != null) {
logger.info("Applying bucket tags on '{}': {}", bucketName, parameters.get(TAGS));
BucketTagsAction.create(connection, prefix(bucketName),
new BucketTagsParamAdd(broker.getNamespace(), (List<Map<String, String> >) parameters.get(TAGS)));
}
} catch (Exception e) {
String errorMessage = String.format("Failed to create bucket '%s': %s", bucketName, e.getMessage());
logger.error(errorMessage, e);
Expand Down Expand Up @@ -184,6 +191,10 @@ Map<String, Object> changeBucketPlan(String bucketName, ServiceDefinitionProxy s
BucketRetentionAction.update(connection, broker.getNamespace(), prefix(bucketName), newRetention);
parameters.put(DEFAULT_RETENTION, newRetention);
}

if (parameters.containsKey(TAGS) && parameters.get(TAGS) != null) {
changeBucketTags(bucketName, parameters);
}
} catch (EcsManagementClientException e) {
throw new ServiceBrokerException(e.getMessage(), e);
}
Expand Down Expand Up @@ -619,4 +630,55 @@ static Map<String, Object> mergeParameters(ServiceDefinitionProxy service, PlanP
parameters.putAll(service.getServiceSettings());
return parameters;
}

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);
BucketTagsAction.create(connection, prefix(bucketName), new BucketTagsParamAdd(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);
BucketTagsAction.update(connection, prefix(bucketName), new BucketTagsParamUpdate(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);
}
return parameters;
}
}
6 changes: 6 additions & 0 deletions src/test/java/com/emc/ecs/common/Fixtures.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ public class Fixtures {
public static final String SECRET_KEY = "testKEY@ключ:/-s#cr#T";
public static final String REMOTE_CONNECTION = "remote_connection";
public static final String SHOULD_RAISE_AN_EXCEPTION = "should raise an exception";
public static final String KEY1 = "tag1";
public static final String KEY2 = "tag2";
public static final String KEY3 = "tag3";
public static final String VALUE1 = "value1";
public static final String VALUE2 = "value2";
public static final String VALUE3 = "value3";

public static ServiceDefinitionProxy bucketServiceFixture() {
/*
Expand Down
Loading