use of org.platformlayer.core.model.TagChanges in project platformlayer by platformlayer.
the class AddTag method runCommand.
@Override
public Object runCommand() throws PlatformLayerClientException {
PlatformLayerClient client = getPlatformLayerClient();
PlatformLayerKey resolved = path.resolve(getContext());
TagChanges tagChanges = new TagChanges();
Tag tag = Tag.build(tagKey, tagValue);
tagChanges.addTags.add(tag);
return client.changeTags(resolved, tagChanges, null);
}
use of org.platformlayer.core.model.TagChanges in project platformlayer by platformlayer.
the class DeleteTag method runCommand.
@Override
public Object runCommand() throws PlatformLayerClientException {
PlatformLayerClient client = getPlatformLayerClient();
PlatformLayerKey key = path.resolve(getContext());
UntypedItem ret = client.getItemUntyped(key, Format.XML);
TagChanges tagChanges = new TagChanges();
for (Tag tag : ret.getTags()) {
if (!tagKey.equals(tag.getKey())) {
continue;
}
if (tagValue != null && !tagValue.equals(tag.getValue())) {
continue;
}
tagChanges.removeTags.add(tag);
}
Tags newTags = client.changeTags(key, tagChanges, null);
return newTags;
}
use of org.platformlayer.core.model.TagChanges in project platformlayer by platformlayer.
the class PublicEndpoint method addChildren.
@Override
protected void addChildren() throws OpsException {
final OwnedEndpoint endpoint;
{
endpoint = addChild(OwnedEndpoint.class);
endpoint.publicPort = publicPort;
endpoint.publicPortCluster = publicPortCluster;
endpoint.backendPort = backendPort;
endpoint.parentItem = parentItem;
endpoint.transport = transport;
}
if (tagItem != null) {
Tagger tagger = injected(Tagger.class);
OpsProvider<TagChanges> tagChanges = new OpsProvider<TagChanges>() {
@Override
public TagChanges get() throws OpsException {
int maxAttempts = 5;
int attempt = 0;
PublicEndpointBase item = endpoint.getItem();
while (true) {
attempt++;
if (item == null) {
if (!OpsContext.isDelete()) {
throw new OpsException("Endpoint not created");
} else {
log.warn("No endpoint => no tagging to be done");
return null;
}
}
List<EndpointInfo> endpointInfos = EndpointInfo.findEndpoints(item.getTags(), publicPort);
if (!endpointInfos.isEmpty()) {
TagChanges tagChanges = new TagChanges();
for (EndpointInfo endpointInfo : endpointInfos) {
tagChanges.addTags.add(endpointInfo.toTag());
}
return tagChanges;
}
if (attempt != maxAttempts) {
log.info("Endpoint not yet found; sleeping and retrying");
TimeSpan.FIVE_SECONDS.doSafeSleep();
item = platformLayerClient.getItem(item.getKey());
continue;
} else {
throw new OpsException("Cannot find endpoint for port: " + publicPort);
}
}
}
};
tagger.platformLayerKey = tagItem;
tagger.tagChangesProvider = tagChanges;
addChild(tagger);
}
if (defaultBlocked) {
// Block on machine's firewall
log.warn("Not adding firewall block; relying on default block");
// addChild(IptablesFirewallEntry.build(FirewallRecord.buildBlockPort(protocol, backendPort)));
}
if (!Strings.isNullOrEmpty(dnsName)) {
EndpointDnsRecord dns = injected(EndpointDnsRecord.class);
dns.destinationPort = publicPort;
dns.endpointProvider = new Provider<PublicEndpointBase>() {
@Override
public PublicEndpointBase get() {
return endpoint.getItem();
}
};
dns.dnsName = dnsName;
addChild(dns);
}
}
use of org.platformlayer.core.model.TagChanges in project platformlayer by platformlayer.
the class PlatformLayerHelpers method setCollectionValues.
public void setCollectionValues(PlatformLayerKey key, String tagKey, Collection<String> newTagValues) throws OpsException {
Set<String> newTagValuesSet = Sets.newHashSet(newTagValues);
Tags existingTags = getItemTags(key);
TagChanges tagChanges = new TagChanges();
Set<String> foundValues = Sets.newHashSet();
for (Tag existingTag : existingTags) {
String existingTagKey = existingTag.getKey();
if (!tagKey.equals(existingTagKey)) {
continue;
}
String existingTagValue = existingTag.getValue();
if (!newTagValuesSet.contains(existingTagValue)) {
tagChanges.removeTags.add(existingTag);
} else {
foundValues.add(existingTagValue);
}
}
for (String newTagValue : newTagValues) {
if (foundValues.contains(newTagValue)) {
continue;
}
tagChanges.addTags.add(Tag.build(tagKey, newTagValue));
}
if (!tagChanges.isEmpty()) {
changeTags(key, tagChanges);
}
}
use of org.platformlayer.core.model.TagChanges in project platformlayer by platformlayer.
the class PlatformLayerHelpers method setUniqueTags.
public void setUniqueTags(PlatformLayerKey key, Iterable<Tag> uniqueTags) throws OpsException {
Tags tags = getItemTags(key);
TagChanges tagChanges = new TagChanges();
for (Tag setTag : uniqueTags) {
String tagKey = setTag.getKey();
List<String> existing = tags.findAll(tagKey);
if (existing == null || existing.isEmpty()) {
tagChanges.addTags.add(setTag);
} else if (existing.size() == 1) {
String existingValue = existing.get(0);
if (!Objects.equal(existingValue, setTag.value)) {
tagChanges.addTags.add(setTag);
tagChanges.removeTags.add(Tag.build(tagKey, existingValue));
}
} else {
// We probably should replace existing tags...
throw new OpsException("Found duplicate tag for: " + setTag.key);
}
}
if (!tagChanges.isEmpty()) {
changeTags(key, tagChanges);
}
}
Aggregations