Skip to content

Commit

Permalink
Misc cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanGiles committed Jul 3, 2024
1 parent c32b9be commit f39cbf0
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -27,6 +26,6 @@ public List<Class<? extends Resource>> getAvailableResources() {
}

public AzureStorageResource addAzureStorage(String name) {
return DistributedApplicationHelper.getDistributedApplication().addResource(new AzureStorageResource(name));
return DistributedApplication.getInstance().addResource(new AzureStorageResource(name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<AzureStorageBlobsResource> {
public final class AzureStorageBlobsResource extends Value<AzureStorageBlobsResource>
implements ResourceWithConnectionString<AzureStorageBlobsResource>,
ResourceWithParent<AzureStorageResource> {
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";
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -17,19 +17,10 @@ public class AzureStorageResource extends AzureBicepResource<AzureStorageResourc

public AzureStorageResource(String name) {
super(AZURE_STORAGE, name);

// This is the path to the output bicep file
// bicepOutputFilename = String.format(BICEP_OUTPUT_FILE, name);
// withPath(bicepOutputFilename);

// FIXME just here because I saw other samples with it
// withParameter("principalId", "");
// withParameter("principalType", "");
}

public AzureStorageBlobsResource addBlobs(String name) {
return DistributedApplicationHelper.getDistributedApplication().addValue(
new AzureStorageBlobsResource(name, this)); // "{images.outputs.blobEndpoint}"
return DistributedApplication.getInstance().addValue(new AzureStorageBlobsResource(name, this));
}

@Override
Expand All @@ -40,16 +31,19 @@ public List<EndpointReference> getEndpoints() {

@Override
public List<TemplateFileOutput> processTemplate(Path outputPath) {
List<TemplateDescriptor> templateFiles = List.of(
new TemplateDescriptor("/templates/bicep/storage.module.bicep", "${name}.module.bicep")
);
final String templatePath = "/templates/bicep/";
final String outputRootPath = "";
List<TemplateDescriptor> templateFiles = TemplateDescriptorsBuilder.begin(templatePath, outputRootPath)
.with("storage.module.bicep", "${name}.module.bicep")
.build();

List<TemplateFileOutput> 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,10 +26,10 @@ public List<Class<? extends Resource>> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.logging.LogManager;
import java.util.logging.Logger;

// Not public API
class AppHostBootstrap {

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.ServiceLoader;

public class DistributedApplication {
private static DistributedApplication INSTANCE;

@Valid
final AspireManifest manifest;
Expand All @@ -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() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.fasterxml.jackson.annotation.JsonIgnore;

public interface ResourceWithConnectionString<T extends ResourceWithConnectionString<T>> {
public interface ResourceWithConnectionString<T extends ResourceWithConnectionString<T>> extends ValueProvider {

// T withConnectionString(String connectionString);

Expand All @@ -13,11 +13,12 @@ public interface ResourceWithConnectionString<T extends ResourceWithConnectionSt
// @JsonIgnore
// String getConnectionString();

// /**
// * The environment variable name to use for the connection string.
// * @return
// */
// String getConnectionStringEnvironmentVariable();
/**
* The environment variable name to use for the connection string.
* @return
*/
@JsonIgnore
String getConnectionStringEnvironmentVariable();

// /**
// * An override of the source resource's name for the connection string. The resulting connection string will be
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.microsoft.aspire.resources.traits;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
* Represents a resource that has a parent resource.
* @param <T>
*/
public interface ResourceWithParent<T> {

/**
* Gets the parent resource.
* @return
*/
@JsonIgnore
T getParent();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.microsoft.aspire.resources.traits;

import com.fasterxml.jackson.annotation.JsonIgnore;

public interface ValueProvider {

@JsonIgnore
String getValue();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
public interface TemplateEngine {

static TemplateEngine getTemplateEngine() {
// TODO cache this
return new FreeMarkerTemplateProcessor();
return FreeMarkerTemplateProcessor.getTemplateEngine();
}

static List<ResourceWithTemplate.TemplateFileOutput> process(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit f39cbf0

Please sign in to comment.