Search in sources :

Example 6 with TagChanges

use of org.platformlayer.core.model.TagChanges in project platformlayer by platformlayer.

the class PersistentInstanceMapper method addChildren.

@Override
protected void addChildren() throws OpsException {
    final PersistentInstance model = OpsContext.get().getInstance(PersistentInstance.class);
    {
        // Add tag with instance id to persistent instance (very helpful for DNS service!)
        Tagger tagger = injected(Tagger.class);
        tagger.platformLayerKey = model.getKey();
        tagger.tagChangesProvider = new OpsProvider<TagChanges>() {

            @Override
            public TagChanges get() throws OpsException {
                Machine machine = OpsContext.get().getInstance(Machine.class);
                if (machine == null) {
                    if (OpsContext.isDelete()) {
                        return null;
                    }
                    throw new OpsException("No machine in scope");
                }
                TagChanges changeTags = new TagChanges();
                changeTags.addTags.add(Tag.INSTANCE_KEY.build(machine.getKey()));
                platformLayer.changeTags(model.getKey(), changeTags, null);
                return changeTags;
            }
        };
        addChild(tagger);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) PersistentInstance(org.platformlayer.instances.model.PersistentInstance) OpsProvider(org.platformlayer.ops.OpsProvider) Tagger(org.platformlayer.ops.tagger.Tagger) TagChanges(org.platformlayer.core.model.TagChanges) Machine(org.platformlayer.ops.Machine)

Example 7 with TagChanges

use of org.platformlayer.core.model.TagChanges in project platformlayer by platformlayer.

the class GoogleCloudPublicEndpointController method addChildren.

@Override
protected void addChildren() throws OpsException {
    final GoogleCloudPublicEndpoint model = OpsContext.get().getInstance(GoogleCloudPublicEndpoint.class);
    GoogleCloudInstance instance = client.getItem(model.instance, GoogleCloudInstance.class);
    CloudInstanceMapper instanceMapper;
    {
        instanceMapper = injected(CloudInstanceMapper.class);
        instanceMapper.instance = instance;
        addChild(instanceMapper);
    }
    final EnsureFirewallIngress ingress;
    {
        ingress = injected(EnsureFirewallIngress.class);
        ingress.model = model;
        instanceMapper.addChild(ingress);
    }
    {
        OpsProvider<TagChanges> tagChanges = new OpsProvider<TagChanges>() {

            @Override
            public TagChanges get() {
                TagChanges tagChanges = new TagChanges();
                String address = ingress.getPublicAddress();
                if (Strings.isNullOrEmpty(address)) {
                    throw new IllegalStateException();
                }
                EndpointInfo endpoint = new EndpointInfo(address, model.publicPort);
                tagChanges.addTags.add(endpoint.toTag());
                return tagChanges;
            }
        };
        Tagger tagger = injected(Tagger.class);
        tagger.platformLayerKey = model.getKey();
        tagger.tagChangesProvider = tagChanges;
        instanceMapper.addChild(tagger);
        Tagger tagInstance = injected(Tagger.class);
        tagInstance.platformLayerKey = null;
        tagInstance.platformLayerKey = model.instance;
        tagInstance.tagChangesProvider = tagChanges;
        instanceMapper.addChild(tagInstance);
    }
}
Also used : EndpointInfo(org.platformlayer.core.model.EndpointInfo) OpsProvider(org.platformlayer.ops.OpsProvider) Tagger(org.platformlayer.ops.tagger.Tagger) GoogleCloudPublicEndpoint(org.platformlayer.service.cloud.google.model.GoogleCloudPublicEndpoint) TagChanges(org.platformlayer.core.model.TagChanges) GoogleCloudInstance(org.platformlayer.service.cloud.google.model.GoogleCloudInstance)

Example 8 with TagChanges

use of org.platformlayer.core.model.TagChanges in project platformlayer by platformlayer.

the class GoogleCloudInstanceController method addChildren.

@Override
protected void addChildren() throws OpsException {
    final GoogleCloudInstance model = OpsContext.get().getInstance(GoogleCloudInstance.class);
    PublicKey rootPublicKey;
    try {
        rootPublicKey = OpenSshUtils.readSshPublicKey(model.sshPublicKey);
    } catch (IOException e) {
        throw new OpsException("Cannot read SSH key");
    }
    CloudInstanceMapper instance;
    {
        instance = injected(CloudInstanceMapper.class);
        instance.instance = model;
        addChild(instance);
    }
    {
        SshAuthorizedKey authorizeRoot = instance.addChild(SshAuthorizedKey.class);
        authorizeRoot.user = "root";
        authorizeRoot.publicKey = rootPublicKey;
    }
    {
        instance.addChild(ConfigureSshd.class);
    }
    {
        OpsProvider<TagChanges> tagChanges = new OpsProvider<TagChanges>() {

            @Override
            public TagChanges get() {
                GoogleComputeMachine machine = OpsContext.get().getInstance(GoogleComputeMachine.class);
                TagChanges tagChanges = new TagChanges();
                tagChanges.addTags.add(Tag.INSTANCE_KEY.build(model.getKey()));
                tagChanges.addTags.addAll(machine.buildAddressTags());
                return tagChanges;
            }
        };
        instance.addChild(Tagger.build(model, tagChanges));
    }
// Note: We can't bootstrap an instance, because we can't log in to it,
// because the public key is not our service's public key
// if (model.publicPorts != null) {
// for (int publicPort : model.publicPorts) {
// PublicPorts publicPortForward = injected(PublicPorts.class);
// publicPortForward.port = publicPort;
// publicPortForward.backendItem = model;
// kvm.addChild(publicPortForward);
// }
// }
}
Also used : OpsException(org.platformlayer.ops.OpsException) SshAuthorizedKey(org.platformlayer.ops.ssh.SshAuthorizedKey) OpsProvider(org.platformlayer.ops.OpsProvider) PublicKey(java.security.PublicKey) IOException(java.io.IOException) TagChanges(org.platformlayer.core.model.TagChanges) ConfigureSshd(org.platformlayer.ops.bootstrap.ConfigureSshd) GoogleComputeMachine(org.platformlayer.service.cloud.google.ops.compute.GoogleComputeMachine) GoogleCloudInstance(org.platformlayer.service.cloud.google.model.GoogleCloudInstance)

Example 9 with TagChanges

use of org.platformlayer.core.model.TagChanges in project platformlayer by platformlayer.

the class PlatformlayerBackedPool method release.

@Override
public void release(PlatformLayerKey owner, T item) throws OpsException {
    for (int i = 0; i < 10; i++) {
        ItemBase resource = platformLayer.getItem(resourceKey);
        List<Assignment> assignments = findAssignments(resource);
        String key = adapter.toKey(item);
        Assignment assigned = Assignment.find(assignments, key, subkey);
        if (assigned == null) {
            throw new OpsException("Resource not assigned");
        }
        if (!assigned.item.equals(owner.getUrl())) {
            throw new OpsException("Resource not held");
        }
        Tag assignmentTag = assigned.asTag();
        TagChanges tagChanges = new TagChanges();
        tagChanges.removeTags.add(assignmentTag);
        if (null != platformLayer.changeTags(resourceKey, tagChanges, resource.getVersion())) {
            return;
        }
        if (!TimeSpan.ONE_SECOND.doSafeSleep()) {
            break;
        }
    }
// List<PlatformLayerKey> assignedTo = Tag.POOL_ASSIGNMENT.build(t) Tag.ASSIGNED_TO.find(item);
// if (!assignedTo.contains(holder)) {
// throw new OpsException("Resource not owned");
// }
// 
// platformLayer.deleteItem(item.getKey());
}
Also used : OpsException(org.platformlayer.ops.OpsException) ItemBase(org.platformlayer.core.model.ItemBase) Tag(org.platformlayer.core.model.Tag) TagChanges(org.platformlayer.core.model.TagChanges)

Example 10 with TagChanges

use of org.platformlayer.core.model.TagChanges in project platformlayer by platformlayer.

the class ItemServiceImpl method ensureItem.

<T extends ItemBase> T ensureItem(final ProjectAuthorization auth, final T item, final boolean canExist, final boolean generateUniqueName, final String uniqueTagKey) throws OpsException {
    final ModelClass<T> modelClass = (ModelClass<T>) serviceProviderDirectory.getModelClass(item.getClass());
    if (modelClass == null) {
        throw new IllegalStateException("Unknown item type");
    }
    final Class<T> javaClass = modelClass.getJavaClass();
    // JaxbHelper jaxbHelper = JaxbHelper.get(javaClass);
    final ServiceProvider serviceProvider = modelClass.getProvider();
    String id = item.getId();
    if (Strings.isNullOrEmpty(id)) {
        if (generateUniqueName) {
            id = serviceProvider.buildItemId(modelClass, item);
        } else {
            // otherwise we end up with lots of randomly named items
            throw new OpsException("Must specify item id");
        // id = UUID.randomUUID().toString();
        // item.setId(id);
        }
    }
    ProjectId project = getProjectId(auth);
    PlatformLayerKey itemKey = new PlatformLayerKey(null, project, modelClass.getServiceType(), modelClass.getItemType(), new ManagedItemId(id));
    item.setKey(itemKey);
    item.state = ManagedItemState.CREATION_REQUESTED;
    final OpsContext opsContext = buildTemporaryOpsContext(modelClass.getServiceType(), auth);
    T created = OpsContext.runInContext(opsContext, new CheckedCallable<T, Exception>() {

        @Override
        public T call() throws Exception {
            PlatformLayerKey itemKey = item.getKey();
            T existing;
            SecretProvider secretProvider = SecretProvider.from(auth);
            if (uniqueTagKey != null) {
                boolean fetchTags = true;
                Tag uniqueTag = null;
                for (Tag tag : item.getTags()) {
                    if (Objects.equal(tag.getKey(), uniqueTagKey)) {
                        uniqueTag = tag;
                    }
                }
                if (uniqueTag == null) {
                    throw new IllegalArgumentException("Could not find unique tag");
                }
                Filter filter = TagFilter.byTag(uniqueTag);
                filter = StateFilter.excludeDeleted(filter);
                existing = null;
                List<T> existingList = repository.findAll(modelClass, itemKey.getProject(), fetchTags, secretProvider, filter);
                if (!existingList.isEmpty()) {
                    if (existingList.size() != 1) {
                        throw new IllegalArgumentException("Found multiple items with unique tag");
                    }
                    existing = existingList.get(0);
                }
                if (existing == null) {
                    itemKey = findUniqueId(item, itemKey, secretProvider);
                }
            } else {
                if (generateUniqueName) {
                    itemKey = findUniqueId(item, itemKey, secretProvider);
                }
                try {
                    boolean fetchTags = true;
                    existing = Casts.checkedCast(repository.getManagedItem(itemKey, fetchTags, secretProvider), javaClass);
                } catch (RepositoryException e) {
                    throw new OpsException("Error fetching item from database", e);
                }
            }
            if (!canExist && existing != null) {
                throw new OpsException("Item already exists");
            }
            serviceProvider.beforeCreateItem(item);
            ProjectId project = getProjectId(auth);
            T newItem;
            try {
                if (existing == null) {
                    newItem = repository.createManagedItem(project, item);
                } else {
                    item.secret = existing.secret;
                    item.setKey(existing.getKey());
                    newItem = repository.updateManagedItem(project, item);
                    TagChanges tagChanges = new TagChanges();
                    for (Tag tag : item.getTags()) {
                        if (newItem.getTags().hasTag(tag)) {
                            continue;
                        }
                        boolean uniqueTagKey = false;
                        if (tag.getKey().equals(Tag.PARENT.getKey())) {
                            uniqueTagKey = true;
                        }
                        tagChanges.addTags.add(tag);
                        if (uniqueTagKey) {
                            for (Tag oldTag : newItem.getTags().findTags(tag.getKey())) {
                                tagChanges.removeTags.add(oldTag);
                            }
                        }
                    }
                    if (!tagChanges.isEmpty()) {
                        repository.changeTags(modelClass, project, newItem.getKey().getItemId(), tagChanges, null);
                    }
                }
            } catch (RepositoryException e) {
                throw new OpsException("Error writing object to database", e);
            }
            itemKey = newItem.getKey();
            JobData jobKey = changeQueue.notifyChange(auth, itemKey, ManagedItemState.CREATION_REQUESTED);
            return newItem;
        }

        private <T extends ItemBase> PlatformLayerKey findUniqueId(final T item, final PlatformLayerKey itemKey, SecretProvider secretProvider) throws RepositoryException {
            int sequence = 0;
            while (true) {
                String tryId = item.getId();
                if (sequence != 0) {
                    tryId += sequence;
                }
                final PlatformLayerKey tryKey = itemKey.withId(new ManagedItemId(tryId));
                boolean fetchTags = false;
                ItemBase found = repository.getManagedItem(tryKey, fetchTags, secretProvider);
                if (found == null) {
                    item.setKey(tryKey);
                    return tryKey;
                }
                sequence++;
            }
        }
    });
    return created;
}
Also used : OpsException(org.platformlayer.ops.OpsException) ItemBase(org.platformlayer.core.model.ItemBase) ProjectId(org.platformlayer.ids.ProjectId) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) RepositoryException(org.platformlayer.RepositoryException) OpsContext(org.platformlayer.ops.OpsContext) TagChanges(org.platformlayer.core.model.TagChanges) ManagedItemId(org.platformlayer.ids.ManagedItemId) RepositoryException(org.platformlayer.RepositoryException) OpsException(org.platformlayer.ops.OpsException) ModelClass(org.platformlayer.xaas.services.ModelClass) StateFilter(org.platformlayer.StateFilter) TagFilter(org.platformlayer.TagFilter) Filter(org.platformlayer.Filter) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) List(java.util.List) Tag(org.platformlayer.core.model.Tag) JobData(org.platformlayer.jobs.model.JobData) SecretProvider(org.platformlayer.auth.crypto.SecretProvider)

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