Search in sources :

Example 11 with TagChanges

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);
}
Also used : PlatformLayerClient(org.platformlayer.PlatformLayerClient) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) Tag(org.platformlayer.core.model.Tag) TagChanges(org.platformlayer.core.model.TagChanges)

Example 12 with TagChanges

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;
}
Also used : PlatformLayerClient(org.platformlayer.PlatformLayerClient) UntypedItem(org.platformlayer.common.UntypedItem) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) Tag(org.platformlayer.core.model.Tag) TagChanges(org.platformlayer.core.model.TagChanges) Tags(org.platformlayer.core.model.Tags)

Example 13 with TagChanges

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);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Tagger(org.platformlayer.ops.tagger.Tagger) TagChanges(org.platformlayer.core.model.TagChanges) EndpointInfo(org.platformlayer.core.model.EndpointInfo) OpsProvider(org.platformlayer.ops.OpsProvider) EndpointDnsRecord(org.platformlayer.ops.dns.EndpointDnsRecord) PublicEndpointBase(org.platformlayer.core.model.PublicEndpointBase)

Example 14 with TagChanges

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);
    }
}
Also used : Tag(org.platformlayer.core.model.Tag) TagChanges(org.platformlayer.core.model.TagChanges) Tags(org.platformlayer.core.model.Tags)

Example 15 with 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);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Tag(org.platformlayer.core.model.Tag) TagChanges(org.platformlayer.core.model.TagChanges) Tags(org.platformlayer.core.model.Tags)

Aggregations

TagChanges (org.platformlayer.core.model.TagChanges)20 OpsException (org.platformlayer.ops.OpsException)11 OpsProvider (org.platformlayer.ops.OpsProvider)9 Tag (org.platformlayer.core.model.Tag)7 Tags (org.platformlayer.core.model.Tags)5 Tagger (org.platformlayer.ops.tagger.Tagger)5 EndpointInfo (org.platformlayer.core.model.EndpointInfo)4 ItemBase (org.platformlayer.core.model.ItemBase)4 File (java.io.File)3 IOException (java.io.IOException)3 List (java.util.List)3 AddressModel (org.platformlayer.core.model.AddressModel)3 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)3 DirectInstance (org.platformlayer.service.cloud.direct.model.DirectInstance)3 PublicKey (java.security.PublicKey)2 PlatformLayerClient (org.platformlayer.PlatformLayerClient)2 Command (org.platformlayer.ops.Command)2 Handler (org.platformlayer.ops.Handler)2 Machine (org.platformlayer.ops.Machine)2 NetworkAddressPoolAssignment (org.platformlayer.ops.pool.NetworkAddressPoolAssignment)2