From f39cbf01319637e1db3b9b1dad4096093a0ea141 Mon Sep 17 00:00:00 2001 From: Jonathan Giles Date: Wed, 3 Jul 2024 12:19:55 +1200 Subject: [PATCH] Misc cleanups --- .../azure/storage/AzureStorageExtension.java | 5 +- .../resources/AzureStorageBlobsResource.java | 29 +++++++---- .../resources/AzureStorageResource.java | 24 ++++------ .../extensions/spring/SpringExtension.java | 6 +-- .../spring/resources/SpringProject.java | 4 +- .../microsoft/aspire/AppHostBootstrap.java | 1 + .../aspire/DistributedApplication.java | 9 +++- .../aspire/DistributedApplicationHelper.java | 18 ------- .../properties/EndpointReference.java | 9 +++- .../traits/ResourceWithConnectionString.java | 13 ++--- .../resources/traits/ResourceWithParent.java | 18 +++++++ .../resources/traits/ValueProvider.java | 9 ++++ .../FreeMarkerTemplateProcessor.java | 9 +++- .../utils/templates/TemplateEngine.java | 3 +- .../templates/VelocityTemplateProcessor.java | 48 ------------------- .../StorageExplorerAppHost.java | 7 +-- 16 files changed, 98 insertions(+), 114 deletions(-) delete mode 100644 aspire4j/aspire4j/src/main/java/com/microsoft/aspire/DistributedApplicationHelper.java create mode 100644 aspire4j/aspire4j/src/main/java/com/microsoft/aspire/resources/traits/ResourceWithParent.java create mode 100644 aspire4j/aspire4j/src/main/java/com/microsoft/aspire/resources/traits/ValueProvider.java delete mode 100644 aspire4j/aspire4j/src/main/java/com/microsoft/aspire/utils/templates/VelocityTemplateProcessor.java diff --git a/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/AzureStorageExtension.java b/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/AzureStorageExtension.java index c5788ea..9df1e6a 100644 --- a/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/AzureStorageExtension.java +++ b/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/AzureStorageExtension.java @@ -1,11 +1,10 @@ package com.microsoft.aspire.extensions.azure.storage; -import com.microsoft.aspire.DistributedApplicationHelper; +import com.microsoft.aspire.DistributedApplication; import com.microsoft.aspire.Extension; import com.microsoft.aspire.extensions.azure.storage.resources.AzureStorageBlobsResource; import com.microsoft.aspire.extensions.azure.storage.resources.AzureStorageResource; import com.microsoft.aspire.resources.Resource; -import com.microsoft.aspire.resources.ResourceType; import java.util.List; @@ -27,6 +26,6 @@ public List> getAvailableResources() { } public AzureStorageResource addAzureStorage(String name) { - return DistributedApplicationHelper.getDistributedApplication().addResource(new AzureStorageResource(name)); + return DistributedApplication.getInstance().addResource(new AzureStorageResource(name)); } } diff --git a/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/resources/AzureStorageBlobsResource.java b/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/resources/AzureStorageBlobsResource.java index 6941bab..42df5e9 100644 --- a/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/resources/AzureStorageBlobsResource.java +++ b/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/resources/AzureStorageBlobsResource.java @@ -2,22 +2,31 @@ import com.microsoft.aspire.resources.Value; import com.microsoft.aspire.resources.traits.ResourceWithConnectionString; +import com.microsoft.aspire.resources.traits.ResourceWithParent; -public final class AzureStorageBlobsResource extends Value - implements ResourceWithConnectionString { +public final class AzureStorageBlobsResource extends Value + implements ResourceWithConnectionString, + ResourceWithParent { private final AzureStorageResource storageResource; - private final String connectionString; public AzureStorageBlobsResource(String name, AzureStorageResource storageResource) { super(name); this.storageResource = storageResource; - this.connectionString = storageResource.getName() + ".outputs.blobEndpoint"; - withProperty("connectionString", connectionString); + withProperty(getConnectionStringEnvironmentVariable(), getValue()); } -// @Override -// @JsonIgnore -// public String getConnectionString() { -// return connectionString; -// } + @Override + public AzureStorageResource getParent() { + return storageResource; + } + + @Override + public String getConnectionStringEnvironmentVariable() { + return "connectionString"; + } + + @Override + public String getValue() { + return storageResource.getName() + ".outputs.blobEndpoint"; + } } diff --git a/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/resources/AzureStorageResource.java b/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/resources/AzureStorageResource.java index 8d0600f..05e3bdb 100644 --- a/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/resources/AzureStorageResource.java +++ b/aspire4j/aspire4j-extensions-azure-storage/src/main/java/com/microsoft/aspire/extensions/azure/storage/resources/AzureStorageResource.java @@ -1,9 +1,9 @@ package com.microsoft.aspire.extensions.azure.storage.resources; +import com.microsoft.aspire.DistributedApplication; import com.microsoft.aspire.resources.AzureBicepResource; import com.microsoft.aspire.resources.ResourceType; import com.microsoft.aspire.resources.properties.EndpointReference; -import com.microsoft.aspire.DistributedApplicationHelper; import com.microsoft.aspire.resources.traits.ResourceWithEndpoints; import com.microsoft.aspire.utils.templates.TemplateEngine; @@ -17,19 +17,10 @@ public class AzureStorageResource extends AzureBicepResource getEndpoints() { @Override public List processTemplate(Path outputPath) { - List templateFiles = List.of( - new TemplateDescriptor("/templates/bicep/storage.module.bicep", "${name}.module.bicep") - ); + final String templatePath = "/templates/bicep/"; + final String outputRootPath = ""; + List templateFiles = TemplateDescriptorsBuilder.begin(templatePath, outputRootPath) + .with("storage.module.bicep", "${name}.module.bicep") + .build(); List templateOutput = TemplateEngine.process(AzureStorageResource.class, templateFiles, Map.of( "name", getName() )); // we know that we need to get the output filename from the first element, and set that as the path - withPath(outputPath.toString()); + // FIXME we need a better way of determining the output path of the template + withPath(templateOutput.get(0).filename()); return templateOutput; } diff --git a/aspire4j/aspire4j-extensions-spring/src/main/java/com/microsoft/aspire/extensions/spring/SpringExtension.java b/aspire4j/aspire4j-extensions-spring/src/main/java/com/microsoft/aspire/extensions/spring/SpringExtension.java index 7933912..e2af84c 100644 --- a/aspire4j/aspire4j-extensions-spring/src/main/java/com/microsoft/aspire/extensions/spring/SpringExtension.java +++ b/aspire4j/aspire4j-extensions-spring/src/main/java/com/microsoft/aspire/extensions/spring/SpringExtension.java @@ -1,6 +1,6 @@ package com.microsoft.aspire.extensions.spring; -import com.microsoft.aspire.DistributedApplicationHelper; +import com.microsoft.aspire.DistributedApplication; import com.microsoft.aspire.Extension; import com.microsoft.aspire.extensions.spring.resources.EurekaServiceDiscovery; import com.microsoft.aspire.extensions.spring.resources.SpringProject; @@ -26,10 +26,10 @@ public List> getAvailableResources() { } public SpringProject addSpringProject(String name) { - return DistributedApplicationHelper.getDistributedApplication().addResource(new SpringProject(name)); + return DistributedApplication.getInstance().addResource(new SpringProject(name)); } public EurekaServiceDiscovery addEurekaServiceDiscovery(String name) { - return DistributedApplicationHelper.getDistributedApplication().addResource(new EurekaServiceDiscovery(name)); + return DistributedApplication.getInstance().addResource(new EurekaServiceDiscovery(name)); } } diff --git a/aspire4j/aspire4j-extensions-spring/src/main/java/com/microsoft/aspire/extensions/spring/resources/SpringProject.java b/aspire4j/aspire4j-extensions-spring/src/main/java/com/microsoft/aspire/extensions/spring/resources/SpringProject.java index 217f9c3..c7b0e43 100644 --- a/aspire4j/aspire4j-extensions-spring/src/main/java/com/microsoft/aspire/extensions/spring/resources/SpringProject.java +++ b/aspire4j/aspire4j-extensions-spring/src/main/java/com/microsoft/aspire/extensions/spring/resources/SpringProject.java @@ -1,7 +1,7 @@ package com.microsoft.aspire.extensions.spring.resources; import com.fasterxml.jackson.annotation.JsonProperty; -import com.microsoft.aspire.DistributedApplicationHelper; +import com.microsoft.aspire.DistributedApplication; import com.microsoft.aspire.extensions.spring.implementation.SpringDeploymentStrategy; import com.microsoft.aspire.extensions.spring.implementation.SpringIntrospector; import com.microsoft.aspire.resources.DockerFile; @@ -43,7 +43,7 @@ public void introspect() { .withContext(getName()) .withExternalHttpEndpoints(); // FIXME this is not really the context - DistributedApplicationHelper.getDistributedApplication().substituteResource(this, dockerFile); + DistributedApplication.getInstance().substituteResource(this, dockerFile); }); } diff --git a/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/AppHostBootstrap.java b/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/AppHostBootstrap.java index ad49d1d..b8494f7 100644 --- a/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/AppHostBootstrap.java +++ b/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/AppHostBootstrap.java @@ -8,6 +8,7 @@ import java.util.logging.LogManager; import java.util.logging.Logger; +// Not public API class AppHostBootstrap { static { diff --git a/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/DistributedApplication.java b/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/DistributedApplication.java index f68f3a1..599c4ae 100644 --- a/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/DistributedApplication.java +++ b/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/DistributedApplication.java @@ -10,6 +10,7 @@ import java.util.ServiceLoader; public class DistributedApplication { + private static DistributedApplication INSTANCE; @Valid final AspireManifest manifest; @@ -20,8 +21,12 @@ public class DistributedApplication { manifest = new AspireManifest(); loadExtensions(); - // FIXME We could make this a static field, but it all feels hacky, so we will go with this for now - DistributedApplicationHelper.setAccessor(() -> this); + // FIXME This is hacky + INSTANCE = this; + } + + public static DistributedApplication getInstance() { + return INSTANCE; } private void loadExtensions() { diff --git a/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/DistributedApplicationHelper.java b/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/DistributedApplicationHelper.java deleted file mode 100644 index 59b7f38..0000000 --- a/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/DistributedApplicationHelper.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.microsoft.aspire; - -// FIXME this is hacky -public class DistributedApplicationHelper { - private static DistributedApplicationAccessor accessor; - - public interface DistributedApplicationAccessor { - DistributedApplication getDistributedApplication(); - } - - public static void setAccessor(DistributedApplicationAccessor accessor) { - DistributedApplicationHelper.accessor = accessor; - } - - public static DistributedApplication getDistributedApplication() { - return accessor.getDistributedApplication(); - } -} diff --git a/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/resources/properties/EndpointReference.java b/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/resources/properties/EndpointReference.java index f1aaa7b..e2b7357 100644 --- a/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/resources/properties/EndpointReference.java +++ b/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/resources/properties/EndpointReference.java @@ -34,8 +34,15 @@ Gets the URL for this endpoint. */ +import com.microsoft.aspire.resources.traits.ValueProvider; + /** * Represents an endpoint reference for a resource with endpoints. */ -public class EndpointReference { +public class EndpointReference implements ValueProvider { + @Override + public String getValue() { +// return ""; + throw new UnsupportedOperationException("Not implemented"); + } } diff --git a/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/resources/traits/ResourceWithConnectionString.java b/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/resources/traits/ResourceWithConnectionString.java index a346088..82134b4 100644 --- a/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/resources/traits/ResourceWithConnectionString.java +++ b/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/resources/traits/ResourceWithConnectionString.java @@ -2,7 +2,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; -public interface ResourceWithConnectionString> { +public interface ResourceWithConnectionString> extends ValueProvider { // T withConnectionString(String connectionString); @@ -13,11 +13,12 @@ public interface ResourceWithConnectionString + */ +public interface ResourceWithParent { + + /** + * Gets the parent resource. + * @return + */ + @JsonIgnore + T getParent(); + +} diff --git a/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/resources/traits/ValueProvider.java b/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/resources/traits/ValueProvider.java new file mode 100644 index 0000000..8c1e026 --- /dev/null +++ b/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/resources/traits/ValueProvider.java @@ -0,0 +1,9 @@ +package com.microsoft.aspire.resources.traits; + +import com.fasterxml.jackson.annotation.JsonIgnore; + +public interface ValueProvider { + + @JsonIgnore + String getValue(); +} diff --git a/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/utils/templates/FreeMarkerTemplateProcessor.java b/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/utils/templates/FreeMarkerTemplateProcessor.java index d936c70..3799db5 100644 --- a/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/utils/templates/FreeMarkerTemplateProcessor.java +++ b/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/utils/templates/FreeMarkerTemplateProcessor.java @@ -10,7 +10,14 @@ import java.io.StringWriter; import java.util.Map; -public class FreeMarkerTemplateProcessor implements TemplateEngine { +class FreeMarkerTemplateProcessor implements TemplateEngine { + private static final TemplateEngine INSTANCE = new FreeMarkerTemplateProcessor(); + + private FreeMarkerTemplateProcessor() { } + + public static TemplateEngine getTemplateEngine() { + return INSTANCE; + } @Override public String processTemplate(String templateContent, Map context) { diff --git a/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/utils/templates/TemplateEngine.java b/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/utils/templates/TemplateEngine.java index 39141fc..18701b3 100644 --- a/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/utils/templates/TemplateEngine.java +++ b/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/utils/templates/TemplateEngine.java @@ -11,8 +11,7 @@ public interface TemplateEngine { static TemplateEngine getTemplateEngine() { - // TODO cache this - return new FreeMarkerTemplateProcessor(); + return FreeMarkerTemplateProcessor.getTemplateEngine(); } static List process( diff --git a/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/utils/templates/VelocityTemplateProcessor.java b/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/utils/templates/VelocityTemplateProcessor.java deleted file mode 100644 index 4fa5b4e..0000000 --- a/aspire4j/aspire4j/src/main/java/com/microsoft/aspire/utils/templates/VelocityTemplateProcessor.java +++ /dev/null @@ -1,48 +0,0 @@ -//package com.microsoft.aspire.utils.templates; -// -//import org.apache.velocity.app.Velocity; -//import org.apache.velocity.app.VelocityEngine; -//import org.apache.velocity.Template; -//import org.apache.velocity.VelocityContext; -// -//import java.io.InputStream; -//import java.io.InputStreamReader; -//import java.io.Reader; -//import java.io.StringWriter; -//import java.util.Properties; -// -//public class VelocityTemplateProcessor { -// private static final VelocityEngine velocityEngine; -// static { -// velocityEngine = new VelocityEngine(); -// Properties properties = new Properties(); -// properties.setProperty("resource.loader", "class"); -// properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); -// velocityEngine.init(properties); -// } -// -// private VelocityTemplateProcessor() { } -// -// public static String processTemplate(String templateName, VelocityContext context) { -// Template template = velocityEngine.getTemplate(templateName); -// -// // Merge the context with the template -// StringWriter writer = new StringWriter(); -// template.merge(context, writer); -// -// return writer.toString(); -// } -// -// public static String processTemplateFromString(String templateContent, VelocityContext context) { -// StringWriter writer = new StringWriter(); -// velocityEngine.evaluate(context, writer, "TemplateProcessor", templateContent); -// return writer.toString(); -// } -// -// public static String processTemplateFromStream(InputStream templateStream, VelocityContext context) { -// Reader reader = new InputStreamReader(templateStream); -// StringWriter writer = new StringWriter(); -// velocityEngine.evaluate(context, writer, "TemplateProcessor", reader); -// return writer.toString(); -// } -//} \ No newline at end of file diff --git a/samples/storage-explorer/storage-explorer-apphost/src/main/java/com/microsoft/aspire/storageexplorer/StorageExplorerAppHost.java b/samples/storage-explorer/storage-explorer-apphost/src/main/java/com/microsoft/aspire/storageexplorer/StorageExplorerAppHost.java index 070b459..31399cb 100644 --- a/samples/storage-explorer/storage-explorer-apphost/src/main/java/com/microsoft/aspire/storageexplorer/StorageExplorerAppHost.java +++ b/samples/storage-explorer/storage-explorer-apphost/src/main/java/com/microsoft/aspire/storageexplorer/StorageExplorerAppHost.java @@ -10,9 +10,10 @@ public class StorageExplorerAppHost implements AppHost { @Override public void configureApplication(DistributedApplication app) { app.printExtensions(); - var blobStorage = app.withExtension(AzureStorageExtension.class) - .addAzureStorage("storage") - .addBlobs("storage-explorer-blobs"); + var azureStorage = app.withExtension(AzureStorageExtension.class) + .addAzureStorage("storage"); + + var blobStorage = azureStorage.addBlobs("storage-explorer-blobs"); var eurekaServiceDiscovery = app.withExtension(SpringExtension.class) .addEurekaServiceDiscovery("eureka");