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);
}
}
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);
}
}
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);
// }
// }
}
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());
}
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;
}
Aggregations