Skip to content

Commit

Permalink
Pushing code as-is to allow for concurrent development on the Spring …
Browse files Browse the repository at this point in the history
…introspection feature
  • Loading branch information
JonathanGiles committed Jul 1, 2024
1 parent f6dc146 commit 9ea3251
Show file tree
Hide file tree
Showing 30 changed files with 350 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import com.microsoft.aspire.DistributedApplicationHelper;
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;

public class AzureStorageExtension implements Extension {

static final ResourceType AZURE_STORAGE = ResourceType.fromString("azure.storage.v0");

@Override
public String getName() {
return "Azure Storage";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.microsoft.aspire.extensions.azure.storage;
package com.microsoft.aspire.extensions.azure.storage.resources;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.microsoft.aspire.resources.Value;
import com.microsoft.aspire.resources.traits.ResourceWithConnectionString;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package com.microsoft.aspire.extensions.azure.storage;
package com.microsoft.aspire.extensions.azure.storage.resources;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.microsoft.aspire.resources.AzureBicepResource;
import com.microsoft.aspire.resources.ResourceType;
import com.microsoft.aspire.resources.properties.EndpointReference;
import com.microsoft.aspire.resources.traits.ResourceWithConnectionString;
import com.microsoft.aspire.DistributedApplicationHelper;
import com.microsoft.aspire.resources.traits.ResourceWithEndpoints;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import static com.microsoft.aspire.extensions.azure.storage.AzureStorageExtension.AZURE_STORAGE;

public class AzureStorageResource extends AzureBicepResource
public class AzureStorageResource extends AzureBicepResource<AzureStorageResource>
implements ResourceWithEndpoints<AzureStorageResource> {
private static final String BICEP_TEMPLATE_FILE = "storage.module.bicep";
private static final ResourceType AZURE_STORAGE = ResourceType.fromString("azure.storage.v0");

private static final String BICEP_TEMPLATE_FILE = "templates/bicep/storage.module.bicep";
private static final String BICEP_OUTPUT_FILE = "%s.module.bicep";

private final String bicepOutputFilename;
Expand All @@ -38,24 +37,26 @@ public AzureStorageBlobsResource addBlobs(String name) {
}

@Override
@JsonIgnore
public List<BicepFileOutput> getBicepFiles() {
public List<EndpointReference> getEndpoints() {
// TODO how do I know which endpoints are available?
return List.of();
}

@Override
public List<TemplateFileOutput> processTemplate() {
// read the file from our local resources directory
InputStream resourceAsStream = AzureStorageResource.class.getResourceAsStream(BICEP_TEMPLATE_FILE);
if (resourceAsStream == null) {
throw new RuntimeException("Resource file not found: " + BICEP_TEMPLATE_FILE);
}
try {
String bicepTemplate = new String(resourceAsStream.readAllBytes());

// If necessary, modify template variables

return List.of(new BicepFileOutput(bicepOutputFilename, bicepTemplate));
return List.of(new TemplateFileOutput(bicepOutputFilename, bicepTemplate));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public List<EndpointReference> getEndpoints() {
// TODO how do I know which endpoints are available?
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
exports com.microsoft.aspire.extensions.azure.storage;

opens com.microsoft.aspire.extensions.azure.storage to org.hibernate.validator, com.fasterxml.jackson.databind;
exports com.microsoft.aspire.extensions.azure.storage.resources;
opens com.microsoft.aspire.extensions.azure.storage.resources to com.fasterxml.jackson.databind, org.hibernate.validator;

provides com.microsoft.aspire.Extension with AzureStorageExtension;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.microsoft.aspire.DistributedApplicationHelper;
import com.microsoft.aspire.Extension;
import com.microsoft.aspire.extensions.spring.resources.EurekaServiceDiscovery;
import com.microsoft.aspire.extensions.spring.resources.SpringProject;
import com.microsoft.aspire.resources.Resource;

import java.util.List;
Expand All @@ -26,4 +28,8 @@ public List<Class<? extends Resource>> getAvailableResources() {
public SpringProject addSpringProject(String name) {
return DistributedApplicationHelper.getDistributedApplication().addResource(new SpringProject(name));
}

public EurekaServiceDiscovery addEurekaServiceDiscovery(String name) {
return DistributedApplicationHelper.getDistributedApplication().addResource(new EurekaServiceDiscovery(name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.github.javaparser.ast.expr.NormalAnnotationExpr;
import com.github.javaparser.ast.expr.SingleMemberAnnotationExpr;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import com.microsoft.aspire.extensions.spring.SpringProject;
import com.microsoft.aspire.extensions.spring.resources.SpringProject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
Expand Down Expand Up @@ -156,7 +156,8 @@ private void introspectJavaFiles(SpringProject project) {
.filter(path -> path.toString().endsWith(".java"))
.forEach(path -> parseAndVisit(javaParser, path.toFile()));
} catch (IOException e) {
e.printStackTrace();
LOGGER.severe("Failed to walk Spring project path '" + projectPath + "' relative to working directory.");
// e.printStackTrace();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.microsoft.aspire.extensions.spring.resources;

import com.microsoft.aspire.resources.DockerFile;
import com.microsoft.aspire.resources.ResourceType;
import com.microsoft.aspire.resources.traits.ResourceWithTemplate;

import java.util.List;

public class EurekaServiceDiscovery extends DockerFile<EurekaServiceDiscovery>
implements ResourceWithTemplate<EurekaServiceDiscovery> {

public EurekaServiceDiscovery(String name) {
super(ResourceType.fromString("spring.eureka.server.v0"), name);

// TODO
// We have a template for the eureka server, consisting of a minimal Spring Boot application, as well as
// pom.xml, configuration file, and a Dockerfile to build the image. We need to allow for the template properties
// to be set by the user, such as the port, the name of the service, etc, and then we need to write these out
// to a temporary location, and then build the image from that location.
}

@Override
public List<TemplateFileOutput> processTemplate() {
return List.of();
}

@Override
public EurekaServiceDiscovery self() {
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.microsoft.aspire.extensions.spring;
package com.microsoft.aspire.extensions.spring.resources;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.microsoft.aspire.DistributedApplicationHelper;
Expand All @@ -11,9 +11,8 @@
import jakarta.validation.Valid;

import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

public class SpringProject extends Project implements IntrospectiveResource {
public class SpringProject extends Project<SpringProject> implements IntrospectiveResource {
private static final ResourceType SPRING_PROJECT = ResourceType.fromString("project.spring.v0");

@Valid
Expand All @@ -36,7 +35,7 @@ public void introspect() {
.findFirst().ifPresent(s -> {
// we need to set the service name (to the existing spring project name), the path to the Dockerfile, and the
// context name (which is the directory containing the Dockerfile)
DockerFile dockerFile = new DockerFile(getName());
DockerFile<?> dockerFile = new DockerFile<>(getName());

this.copyInto(dockerFile);
dockerFile.withPath(s.getCommands().get(0))
Expand All @@ -46,4 +45,9 @@ public void introspect() {
DistributedApplicationHelper.getDistributedApplication().substituteResource(this, dockerFile);
});
}

@Override
public SpringProject self() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
exports com.microsoft.aspire.extensions.spring;

opens com.microsoft.aspire.extensions.spring to org.hibernate.validator, com.fasterxml.jackson.databind;
exports com.microsoft.aspire.extensions.spring.resources;
opens com.microsoft.aspire.extensions.spring.resources to com.fasterxml.jackson.databind, org.hibernate.validator;

provides com.microsoft.aspire.Extension with SpringExtension;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM maven:3.9.7-eclipse-temurin-21 AS maven
MAINTAINER microsoft.com
COPY src eureka-service/src
COPY pom.xml eureka-service/
RUN mvn package -f eureka-service/pom.xml
RUN cp eureka-service/target/*.jar /eureka-service.jar
ENTRYPOINT ["java","-jar","/eureka-service.jar"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.microsoft.aspire</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<java.version>17</java.version>
<spring-cloud.version>2023.0.2</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.microsoft.aspire.spring.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spring:
application:
name: eureka-server
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private void loadExtensions() {
* @param <T>
* @return
*/
public <T extends Resource> T addResource(T r) {
public <T extends Resource<T>> T addResource(T r) {
return manifest.addResource(r);
}

Expand All @@ -53,7 +53,7 @@ public <T extends Resource> T addResource(T r) {
* @param oldResource The resource to remove.
* @param newResources The resource(s) to add in the place of the old resource.
*/
public void substituteResource(Resource oldResource, Resource... newResources) {
public void substituteResource(Resource<?> oldResource, Resource<?>... newResources) {
manifest.substituteResource(oldResource, newResources);
}

Expand Down Expand Up @@ -170,8 +170,8 @@ public <T extends Executable> T addExecutable(T executable) {
* @param args
* @return
*/
public Executable addExecutable(String name, String command, String workingDirectory, String... args) {
return manifest.addResource(new Executable(name, command, workingDirectory).withArguments(args));
public Executable<?> addExecutable(String name, String command, String workingDirectory, String... args) {
return manifest.addResource(new Executable<>(name, command, workingDirectory).withArguments(args));
}


Expand Down
Loading

0 comments on commit 9ea3251

Please sign in to comment.