/** * Get all resource metadata of the given type that belong to the given account that have the given status. * * @param account Account containing the resources * @param resourceType Type of resource to get * @param status Status of the resources to get. If null, resources of any status are returned. * @return Immutable map of resource name to resource metadata * @throws IOException if there was an error getting the resources */ public Map<String, Set<ResourceMeta>> getAll(Account account, ResourceType resourceType, ResourceStatus status) throws IOException { if (status == null) { return metaStoreService.getResourceTypeView(account, resourceType).getAll(); } return metaStoreService.getResourceTypeView(account, resourceType).getAll(status); }
/** * Get all metadata for versions of the given resource that have the given status. * * @param account Account containing the resource * @param resourceType Type of resource to get * @param name Name of the resource to get * @param status Status of the resources to get. If null, resources of any status are returned. * @return Immutable set of metadata for versions of the given module * @throws IOException if there was an error getting the module versions */ public Set<ResourceMeta> getAll(Account account, ResourceType resourceType, String name, ResourceStatus status) throws IOException { if (status == null) { return metaStoreService.getResourceTypeView(account, resourceType).getAll(name); } return metaStoreService.getResourceTypeView(account, resourceType).getAll(name, status); }
@Override public void handleError(Throwable t) { LOG.error("Error uploading version {} of resource {} of type {} for account {}.", version, name, resourceType, account, t); try { os.close(); // deletion flags the entry in the database as deleted metaStoreService.getResourceTypeView(account, resourceType).delete(name, version); // dont need the file in the plugin store if there was an error so delete it pluginStore.deleteResource(account, resourceType, name, version); responder.sendError(HttpResponseStatus.INTERNAL_SERVER_ERROR, t.getCause().getMessage()); } catch (IOException e) { LOG.error("Error uploading resource {} of type {} for account {}.", resourceMeta, resourceType, account, e); } } };
/** * Atomically stage the specified resource version for the given account and recall the previous staged version. * A staged version will get pushed to provisioners during a sync, and will stay staged unless explicitly recalled. * * @param account Account that contains the resource * @param resourceType Type of resource to stage * @param name Name of resource to stage * @param version Version of resource to stage * @throws MissingEntityException if there is no such resource version * @throws IOException if there was an error staging the resource */ public void stage(Account account, ResourceType resourceType, String name, int version) throws MissingEntityException, IOException { LOG.debug("staging version {} of resource {} of type {} for account {}.", version, name, resourceType, account); Lock lock = getResourceLock(account, resourceType, name); lock.lock(); try { PluginResourceTypeView view = metaStoreService.getResourceTypeView(account, resourceType); if (!view.exists(name, version)) { throw new MissingEntityException("Resource does not exist."); } view.stage(name, version); } finally { lock.unlock(); } }
/** * Recall the given resource for the given account. A no-op if there is no staged version. * * @param account Account that contains the resource * @param resourceType Type of resource to deactivate * @param name Name of the resource to deactivate * @param version Version of resource to stage * @throws MissingEntityException if there is no such module * @throws IOException if there was an error deactivating all versions of the module */ public void recall(Account account, ResourceType resourceType, String name, int version) throws MissingEntityException, IOException { LOG.debug("Recalling version {} of resource {} of type {} for account {}.", version, name, resourceType, account); Lock lock = getResourceLock(account, resourceType, name); lock.lock(); try { PluginResourceTypeView view = metaStoreService.getResourceTypeView(account, resourceType); if (!view.exists(name, version)) { throw new MissingEntityException("Resource " + name + " does not exist."); } view.recall(name, version); } finally { lock.unlock(); } }
PluginResourceTypeView metaView = metaStoreService.getResourceTypeView(account, type); if (!metaView.exists(name, version)) { metaView.add(meta);
lock.lock(); try { PluginResourceTypeView view = metaStoreService.getResourceTypeView(account, resourceType); ResourceMeta meta = view.get(name, version); if (meta.getStatus() != ResourceStatus.INACTIVE) {
/** * Get the resources that should be synced for a given account. * * @param account Account for which to get resources to sync * @return Resources that should be synced for the given account */ public ResourceCollection getResourcesToSync(Account account) throws IOException { ResourceCollection resourceCollection = new ResourceCollection(); Set<ImmutablePair<ResourceType, ResourceTypeSpecification>> typeFormats = getTypesAndFormats(account); Set<ResourceType> resourceTypes = Sets.newHashSet(); for (ImmutablePair<ResourceType, ResourceTypeSpecification> typeFormat : typeFormats) { ResourceType resourceType = typeFormat.getFirst(); ResourceTypeSpecification typeSpec = typeFormat.getSecond(); Set<ResourceMeta> resources = metaStoreService.getResourceTypeView(account, resourceType).getResourcesToSync(); resourceCollection.addResources(resourceType, typeSpec, resources); resourceTypes.add(resourceType); } return resourceCollection; }
/** * Bootstrap an account's resources by copying what the superadmin has into the account. * * @param account Account to bootstrap */ public void bootstrapResources(Account account) throws IOException { for (ImmutablePair<ResourceType, ResourceTypeSpecification> typePair : getTypesAndFormats(Account.SUPERADMIN)) { ResourceType type = typePair.getFirst(); Map<String, Set<ResourceMeta>> resources = metaStoreService.getResourceTypeView(Account.SUPERADMIN, type).getAll(); for (Set<ResourceMeta> metas : resources.values()) { for (ResourceMeta meta : metas) { LOG.debug("copying version {} of resource {} of type {} from superadmin account to account {}", meta.getVersion(), meta.getName(), type, account); copySuperadminResource(account, type, meta); } } } }
/** * Get an input stream for reading the plugin resource. * * @param account Account the resource belongs to * @param resourceType Type of resource * @param name Name of resource to get an input stream for * @param version Version of resource to get an input stream for * @return Input stream for reading the given plugin resource * @throws MissingEntityException if there is no such resource version * @throws IOException if there was an error getting the input stream for the resource */ public InputStream getResourceInputStream(final Account account, ResourceType resourceType, String name, int version) throws MissingEntityException, IOException { // no lock needed since each resource uploaded gets its own id. ResourceMeta meta = metaStoreService.getResourceTypeView(account, resourceType).get(name, version); if (meta == null) { throw new MissingEntityException("Resource not found."); } LOG.debug("getting input stream for version {} of resource {} of type {} for account {}.", version, name, resourceType, account); return pluginStore.getResourceInputStream(account, resourceType, meta.getName(), meta.getVersion()); }
lock.lock(); try { PluginResourceTypeView view = metaStoreService.getResourceTypeView(account, resourceType); Set<ResourceMeta> metas = view.getAll(name); for (ResourceMeta meta : metas) {
/** * Get the active resources for a given account. * * @param account Account to get active resources for * @return Active resources for the given account */ public ResourceCollection getLiveResources(Account account) throws IOException { ResourceCollection resourceCollection = new ResourceCollection(); for (ImmutablePair<ResourceType, ResourceTypeSpecification> typeSpecs : getTypesAndFormats(account)) { ResourceType resourceType = typeSpecs.getFirst(); ResourceTypeSpecification typeSpec = typeSpecs.getSecond(); Set<ResourceMeta> resources = metaStoreService.getResourceTypeView(account, resourceType).getLiveResources(); resourceCollection.addResources(resourceType, typeSpec, resources); } return resourceCollection; }
@Test public void testGetNumResources() throws Exception { PluginMetaStoreService service = getPluginResourceMetaStoreService(); // for account1 write 6 resources (7 but one is deleted) in all different states service.getResourceTypeView(account1, type1).add(new ResourceMeta("r1", 1, ResourceStatus.ACTIVE)); service.getResourceTypeView(account1, type1).add(new ResourceMeta("r1", 2, ResourceStatus.INACTIVE)); service.getResourceTypeView(account1, type1).add(new ResourceMeta("r2", 1, ResourceStatus.INACTIVE)); service.getResourceTypeView(account1, type1).add(new ResourceMeta("r2", 2, ResourceStatus.STAGED)); service.getResourceTypeView(account1, type2).add(new ResourceMeta("r3", 1, ResourceStatus.RECALLED)); service.getResourceTypeView(account1, type2).add(new ResourceMeta("r3", 2, ResourceStatus.STAGED)); service.getResourceTypeView(account1, type2).add(new ResourceMeta("r3", 3, ResourceStatus.STAGED)); service.getResourceTypeView(account1, type2).delete("r3", 3); Assert.assertEquals(6, service.getAccountView(account1).numResources()); // account 2 should have nothing Assert.assertEquals(0, service.getAccountView(account2).numResources()); }
@Test(expected = IllegalArgumentException.class) public void testOnlyAdminsHaveAccess() throws Exception { PluginMetaStoreService service = getPluginResourceMetaStoreService(); service.getResourceTypeView(new Account("notadmin", "tenant"), new ResourceType(PluginType.AUTOMATOR, "chef-solo", "cookbooks")); }
@Test public void testStageOnNothingIsNoOp() throws Exception { PluginMetaStoreService service = getPluginResourceMetaStoreService(); PluginResourceTypeView view = service.getResourceTypeView(account1, type1); ResourceMeta hadoop = new ResourceMeta("hadoop", 1, ResourceStatus.STAGED); view.add(hadoop); // if we stage a non-existent version, the current staged version should not be affected view.stage(hadoop.getName(), hadoop.getVersion() + 1); Assert.assertEquals(ResourceStatus.STAGED, view.get(hadoop.getName(), hadoop.getVersion()).getStatus()); }
@Test public void testWriteDeleteExistsGetWithinAccount() throws Exception { PluginMetaStoreService service = getPluginResourceMetaStoreService(); PluginResourceTypeView view = service.getResourceTypeView(account1, type1); String name = "name"; int version = 1; ResourceMeta meta = new ResourceMeta(name, version); view.add(meta); Assert.assertTrue(view.exists(name, version)); Assert.assertEquals(meta, view.get(name, version)); view.delete(name, version); Assert.assertFalse(view.exists(name, version)); Assert.assertNull(view.get(name, version)); }
@Test public void testTypeSeparation() throws Exception { PluginMetaStoreService service = getPluginResourceMetaStoreService(); PluginResourceTypeView view1 = service.getResourceTypeView(account1, type1); PluginResourceTypeView view2 = service.getResourceTypeView(account1, type2); String name = "name"; int version = 1; ResourceMeta meta = new ResourceMeta(name, version); view1.add(meta); Assert.assertTrue(view1.exists(name, version)); Assert.assertFalse(view2.exists(name, version)); Assert.assertEquals(meta, view1.get(name, version)); Assert.assertNull(view2.get(name, version)); view2.add(meta); Assert.assertTrue(view1.exists(name, version)); Assert.assertTrue(view2.exists(name, version)); Assert.assertEquals(meta, view1.get(name, version)); Assert.assertEquals(meta, view2.get(name, version)); view1.delete(name, version); Assert.assertFalse(view1.exists(name, version)); Assert.assertTrue(view2.exists(name, version)); Assert.assertNull(view1.get(name, version)); Assert.assertEquals(meta, view2.get(name, version)); view2.delete(name, version); Assert.assertFalse(view1.exists(name, version)); Assert.assertFalse(view2.exists(name, version)); Assert.assertNull(view1.get(name, version)); Assert.assertNull(view2.get(name, version)); }
@Test public void testAccountSeparation() throws Exception { PluginMetaStoreService service = getPluginResourceMetaStoreService(); PluginResourceTypeView view1 = service.getResourceTypeView(account1, type1); PluginResourceTypeView view2 = service.getResourceTypeView(account2, type1); String name = "name"; int version = 1; ResourceMeta meta = new ResourceMeta(name, version); view1.add(meta); Assert.assertTrue(view1.exists(name, version)); Assert.assertFalse(view2.exists(name, version)); Assert.assertEquals(meta, view1.get(name, version)); Assert.assertNull(view2.get(name, version)); view2.add(meta); Assert.assertTrue(view1.exists(name, version)); Assert.assertTrue(view2.exists(name, version)); Assert.assertEquals(meta, view1.get(name, version)); Assert.assertEquals(meta, view2.get(name, version)); view1.delete(name, version); Assert.assertFalse(view1.exists(name, version)); Assert.assertTrue(view2.exists(name, version)); Assert.assertNull(view1.get(name, version)); Assert.assertEquals(meta, view2.get(name, version)); view2.delete(name, version); Assert.assertFalse(view1.exists(name, version)); Assert.assertFalse(view2.exists(name, version)); Assert.assertNull(view1.get(name, version)); Assert.assertNull(view2.get(name, version)); }
@Test public void testStage() throws Exception { PluginMetaStoreService service = getPluginResourceMetaStoreService(); PluginResourceTypeView view = service.getResourceTypeView(account1, type1); ResourceMeta hadoop1 = new ResourceMeta("hadoop", 1, ResourceStatus.INACTIVE); ResourceMeta hadoop2 = new ResourceMeta("hadoop", 2, ResourceStatus.RECALLED);
@Test public void testRecall() throws Exception { PluginMetaStoreService service = getPluginResourceMetaStoreService(); PluginResourceTypeView view = service.getResourceTypeView(account1, type1); ResourceMeta hadoop1 = new ResourceMeta("hadoop", 1, ResourceStatus.INACTIVE); ResourceMeta hadoop2 = new ResourceMeta("hadoop", 2, ResourceStatus.RECALLED);