Skip to content

Commit 2ed2cef

Browse files
authored
Refactor APIs add support AMQP (#215)
1 parent c7c399c commit 2ed2cef

File tree

64 files changed

+1871
-1376
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1871
-1376
lines changed

README.md

+36-3
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ Use the spring boot's dev profile to use default values that are just for non-pr
106106

107107
#### From Command Line
108108

109-
1.Start with oauth
109+
1. Start with oauth profile
110110

111111
```shell
112112
./mvmw clean install -DskipTests
113113
$ java -jar target/terra-boot-*.jar\
114-
--spring.profiles.active=oauth \
114+
--spring.profiles.active=oauth,dev \
115115
--authorization.token.type=${token-type} \
116116
--authorization.server.endpoint=${server-endpoint} \
117117
--authorization.api.client.id=${client-id} \
@@ -123,7 +123,7 @@ $ java -jar target/terra-boot-*.jar\
123123

124124
```shell
125125
./mvmw clean install -DskipTests
126-
$ java -jar target/terra-boot-*.jar
126+
$ java -jar --spring.profiles.active=dev target/terra-boot-*.jar
127127
```
128128

129129
#### From IDE
@@ -147,6 +147,39 @@ http://localhost:9090
147147
http://localhost:9090/swagger-ui/index.html
148148
```
149149

150+
### AMQP support
151+
#### Start AMQP Broker
152+
153+
Use RabbitMQ as the default provider for AMQP. Start the broker and create queues using the following command:
154+
155+
```shell
156+
mkdir -p ~/docker/rabbitmq/data
157+
docker run -d \
158+
--user 1000:1000 \
159+
--name xpanse-rabbitmq \
160+
-p 5672:5672 \
161+
-p 15672:15672 \
162+
-v ~/docker/rabbitmq/data:/var/lib/rabbitmq \
163+
-e RABBITMQ_DEFAULT_USER=xpanse \
164+
-e RABBITMQ_DEFAULT_PASS=Xpanse@2023 \
165+
rabbitmq:4.0.7-management
166+
```
167+
Access the RabbitMQ management console at http://localhost:15672 with username `xpanse` and password `Xpanse@2023` after the container is started.
168+
169+
#### Enable AMQP
170+
Start the application with the `amqp` profile.
171+
172+
```shell
173+
./mvmw clean install -DskipTests
174+
$ java -jar --spring.profiles.active=dev,amqp target/terra-boot-*.jar
175+
```
176+
177+
#### AsyncApi Docs and UI
178+
179+
When the application started with the `amqp` profile, the AsyncApi docs and UI Console are enabled.
180+
The AsyncApi docs can be accessed at http://localhost:9090/queues/docs.
181+
The AsyncApi UI can be accessed at http://localhost:9090/queues/asyncapi-ui.html.
182+
150183
### Production
151184

152185
#### Docker Image

pom.xml

+27
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<maven.surefire.plugin.version>3.5.2</maven.surefire.plugin.version>
3636
<maven.enforcer.plugin.version>3.5.0</maven.enforcer.plugin.version>
3737
<spotless.version>2.44.2</spotless.version>
38+
<springwolf.version>1.11.0</springwolf.version>
3839
</properties>
3940
<dependencyManagement>
4041
<dependencies>
@@ -63,6 +64,32 @@
6364
<groupId>com.github.ben-manes.caffeine</groupId>
6465
<artifactId>caffeine</artifactId>
6566
</dependency>
67+
<!-- Spring AMQP -->
68+
<dependency>
69+
<groupId>org.springframework.boot</groupId>
70+
<artifactId>spring-boot-starter-amqp</artifactId>
71+
</dependency>
72+
<!-- Springwolf AsyncAPI-->
73+
<dependency>
74+
<groupId>io.github.springwolf</groupId>
75+
<artifactId>springwolf-asyncapi</artifactId>
76+
<version>${springwolf.version}</version>
77+
</dependency>
78+
<dependency>
79+
<groupId>io.github.springwolf</groupId>
80+
<artifactId>springwolf-core</artifactId>
81+
<version>${springwolf.version}</version>
82+
</dependency>
83+
<dependency>
84+
<groupId>io.github.springwolf</groupId>
85+
<artifactId>springwolf-amqp</artifactId>
86+
<version>${springwolf.version}</version>
87+
</dependency>
88+
<dependency>
89+
<groupId>io.github.springwolf</groupId>
90+
<artifactId>springwolf-ui</artifactId>
91+
<version>${springwolf.version}</version>
92+
</dependency>
6693
<dependency>
6794
<groupId>org.projectlombok</groupId>
6895
<artifactId>lombok</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* SPDX-FileCopyrightText: Huawei Inc.
4+
*/
5+
6+
package org.eclipse.xpanse.terra.boot.api.config;
7+
8+
import com.fasterxml.jackson.databind.DeserializationFeature;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.fasterxml.jackson.databind.module.SimpleModule;
11+
import org.eclipse.xpanse.terra.boot.models.request.TerraformRequest;
12+
import org.eclipse.xpanse.terra.boot.models.request.TerraformRequestDeserializer;
13+
import org.springframework.context.annotation.Bean;
14+
import org.springframework.context.annotation.Configuration;
15+
import org.springframework.context.annotation.Primary;
16+
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
17+
18+
/** Configuration class for Jackson. */
19+
@Configuration
20+
public class JacksonConfig {
21+
22+
/**
23+
* Create a ObjectMapper with the given module.
24+
*
25+
* @return objectMapper bean.
26+
*/
27+
@Bean
28+
@Primary
29+
public ObjectMapper objectMapper() {
30+
ObjectMapper mapper = new ObjectMapper();
31+
SimpleModule terraformRequestModule = new SimpleModule();
32+
terraformRequestModule.addDeserializer(
33+
TerraformRequest.class, new TerraformRequestDeserializer());
34+
mapper.registerModule(terraformRequestModule);
35+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
36+
return mapper;
37+
}
38+
39+
/**
40+
* Define MappingJackson2HttpMessageConverter with the given objectMapper.
41+
*
42+
* @param objectMapper objectMapper
43+
* @return mappingJackson2HttpMessageConverter bean
44+
*/
45+
@Bean
46+
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(
47+
ObjectMapper objectMapper) {
48+
return new MappingJackson2HttpMessageConverter(objectMapper);
49+
}
50+
}

src/main/java/org/eclipse/xpanse/terra/boot/api/controllers/TerraBootAdminApi.java

+17-13
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,32 @@
77

88
import io.swagger.v3.oas.annotations.Operation;
99
import io.swagger.v3.oas.annotations.tags.Tag;
10+
import jakarta.annotation.Resource;
11+
import jakarta.servlet.http.HttpServletRequest;
12+
import java.util.UUID;
1013
import lombok.extern.slf4j.Slf4j;
11-
import org.eclipse.xpanse.terra.boot.models.TerraBootSystemStatus;
12-
import org.eclipse.xpanse.terra.boot.terraform.service.TerraformDirectoryService;
13-
import org.springframework.beans.factory.annotation.Autowired;
14-
import org.springframework.beans.factory.annotation.Qualifier;
14+
import org.eclipse.xpanse.terra.boot.models.response.TerraBootSystemStatus;
15+
import org.eclipse.xpanse.terra.boot.terraform.service.TerraformRequestService;
16+
import org.springframework.context.annotation.Profile;
1517
import org.springframework.http.HttpStatus;
1618
import org.springframework.http.MediaType;
1719
import org.springframework.web.bind.annotation.CrossOrigin;
1820
import org.springframework.web.bind.annotation.GetMapping;
1921
import org.springframework.web.bind.annotation.RequestMapping;
2022
import org.springframework.web.bind.annotation.ResponseStatus;
2123
import org.springframework.web.bind.annotation.RestController;
24+
import org.springframework.web.context.request.RequestContextHolder;
25+
import org.springframework.web.context.request.ServletRequestAttributes;
2226

2327
/** REST controller for admin services of terra-boot. */
2428
@Slf4j
2529
@CrossOrigin
30+
@Profile("!amqp")
2631
@RestController
2732
@RequestMapping("/terra-boot")
2833
public class TerraBootAdminApi {
2934

30-
private final TerraformDirectoryService terraformDirectoryService;
31-
32-
@Autowired
33-
public TerraBootAdminApi(
34-
@Qualifier("terraformDirectoryService")
35-
TerraformDirectoryService terraformDirectoryService) {
36-
this.terraformDirectoryService = terraformDirectoryService;
37-
}
35+
@Resource private TerraformRequestService requestService;
3836

3937
/**
4038
* Method to find out the current state of the system.
@@ -46,6 +44,12 @@ public TerraBootAdminApi(
4644
@GetMapping(value = "/health", produces = MediaType.APPLICATION_JSON_VALUE)
4745
@ResponseStatus(HttpStatus.OK)
4846
public TerraBootSystemStatus healthCheck() {
49-
return terraformDirectoryService.tfHealthCheck();
47+
TerraBootSystemStatus healthStatus = requestService.healthCheck(UUID.randomUUID());
48+
HttpServletRequest request =
49+
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
50+
.getRequest();
51+
healthStatus.setServiceType(request.getScheme());
52+
healthStatus.setServiceUrl(request.getRequestURL().toString());
53+
return healthStatus;
5054
}
5155
}

0 commit comments

Comments
 (0)