Skip to content

Commit

Permalink
Add multi-tenant support to the s3 store factory bean
Browse files Browse the repository at this point in the history
- so that a MultiTenantAmazonS3Provider bean can be provided through configuration
#201
  • Loading branch information
paulcwarren committed May 28, 2020
1 parent 8d63cb1 commit f19bbfb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.content.commons.repository.factory.AbstractStoreFactoryBean;
import org.springframework.content.commons.utils.PlacementService;
import org.springframework.content.s3.S3ObjectIdResolver;
import org.springframework.content.s3.config.MultiTenantAmazonS3Provider;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.versions.LockingAndVersioningProxyFactory;

Expand All @@ -26,6 +27,9 @@ public class S3StoreFactoryBean extends AbstractStoreFactoryBean {
@Autowired
private PlacementService s3StorePlacementService;

@Autowired(required=false)
private MultiTenantAmazonS3Provider s3Provider = null;

@Autowired(required=false)
private LockingAndVersioningProxyFactory versioning;

Expand Down Expand Up @@ -59,6 +63,6 @@ protected Object getContentStoreImpl() {
DefaultResourceLoader loader = new DefaultResourceLoader();
loader.addProtocolResolver(s3Protocol);

return new DefaultS3StoreImpl(loader, s3StorePlacementService, client, null);
return new DefaultS3StoreImpl(loader, s3StorePlacementService, client, s3Provider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,25 @@
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.S3ObjectId;
import com.github.paulcwarren.ginkgo4j.Ginkgo4jRunner;
import internal.org.springframework.content.s3.io.S3StoreResource;
import lombok.Data;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.content.commons.annotations.Content;
import org.springframework.content.commons.annotations.ContentId;
import org.springframework.content.commons.repository.AssociativeStore;
import org.springframework.content.commons.repository.ContentStore;
import org.springframework.content.s3.S3ObjectIdResolver;
import org.springframework.content.s3.config.EnableS3ContentRepositories;
import org.springframework.content.s3.config.EnableS3Stores;
import org.springframework.content.s3.config.S3ObjectIdResolvers;
import org.springframework.content.s3.config.S3StoreConfigurer;
import org.springframework.content.s3.config.*;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.core.io.Resource;

import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.AfterEach;
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.BeforeEach;
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.Context;
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.Describe;
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.It;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.*;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.anyObject;
Expand All @@ -45,6 +38,8 @@ public class EnableS3StoresTest {

// mocks
static S3StoreConfigurer configurer;
static AmazonS3 client;

{
Describe("EnableS3Stores", () -> {
Context("given a context and a configuration with an S3 content repository bean",
Expand Down Expand Up @@ -103,6 +98,27 @@ public class EnableS3StoresTest {
}
});
});

Context("given a context with a multi-tenant configuration", () -> {
BeforeEach(() -> {
client = mock(AmazonS3.class);

context = new AnnotationConfigApplicationContext();
context.register(MultiTenantConfig.class);
context.refresh();
});
AfterEach(() -> {
context.close();
});
It("should use the correct client", () -> {
TestEntityContentRepository repo = context.getBean(TestEntityContentRepository.class);
TestEntity tentity = new TestEntity();
tentity.setContentId("12345");
Resource r = repo.getResource(tentity);
assertThat(((S3StoreResource)r).getClient(), is(client));
});
});

});

Describe("EnableS3ContentRepositories", () -> {
Expand Down Expand Up @@ -194,6 +210,21 @@ public interface TestEntityStore extends AssociativeStore<TestEntity, S3ObjectId
public static class EnableS3ContentRepositoriesConfig {
}

@Configuration
@EnableS3Stores
@Import(InfrastructureConfig.class)
public static class MultiTenantConfig {
@Bean
public MultiTenantAmazonS3Provider s3Provider() {
return new MultiTenantAmazonS3Provider() {
@Override
public AmazonS3 getAmazonS3() {
return client;
}
};
}
}

@Configuration
public static class InfrastructureConfig {

Expand All @@ -209,7 +240,7 @@ public AmazonS3 client() {
}
}

@Content
@Data
public class TestEntity {
@ContentId
private String contentId;
Expand Down

0 comments on commit f19bbfb

Please sign in to comment.