Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DATAGO-64296: initial commit for terraform plugin #132

Merged
merged 22 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion service/application/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?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">
<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>com.solace.maas</groupId>
Expand Down Expand Up @@ -218,6 +219,11 @@
<artifactId>confluent-schema-registry-plugin</artifactId>
<version>1.3.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.solace.maas.plugin.terraform</groupId>
<artifactId>terraform-plugin</artifactId>
<version>1.3.1-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>io.micrometer</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.solace.maas.ep.common.messages;

import com.solace.maas.ep.event.management.agent.plugin.command.model.CommandBundle;
import com.solace.maas.ep.event.management.agent.plugin.mop.MOPMessage;
import com.solace.maas.ep.event.management.agent.plugin.mop.MOPMessageType;
import com.solace.maas.ep.event.management.agent.plugin.mop.MOPProtocol;
import com.solace.maas.ep.event.management.agent.plugin.mop.MOPUHFlag;
import lombok.Data;

import java.util.List;

@Data
public class CommandMessage extends MOPMessage {

private String correlationId;
private String context;
private String messagingServiceId;
gregmeldrum marked this conversation as resolved.
Show resolved Hide resolved
private List<CommandBundle> commandBundles;

public CommandMessage() {
super();
}

public CommandMessage(String messagingServiceId,
String correlationId,
String context,
List<CommandBundle> commandBundles) {
super();
withMessageType(MOPMessageType.generic)
.withProtocol(MOPProtocol.commandProtocol)
.withVersion("1")
.withUhFlag(MOPUHFlag.ignore);
this.messagingServiceId = messagingServiceId;
this.correlationId = correlationId;
this.context = context;
this.commandBundles = commandBundles;
}

@Override
public String toLog() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.solace.maas.ep.event.management.agent.command;

import com.solace.maas.ep.event.management.agent.plugin.command.model.Command;
import com.solace.maas.ep.event.management.agent.plugin.command.model.CommandBundle;
import com.solace.maas.ep.event.management.agent.plugin.command.model.CommandRequest;
import com.solace.maas.ep.event.management.agent.plugin.service.MessagingServiceDelegateService;
import com.solace.maas.ep.event.management.agent.plugin.solace.processor.semp.SempClient;
import com.solace.maas.ep.event.management.agent.plugin.solace.processor.semp.SolaceHttpSemp;
import com.solace.maas.ep.event.management.agent.plugin.terraform.manager.TerraformManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class CommandManager {
private final TerraformManager terraformManager;

private final MessagingServiceDelegateService messagingServiceDelegateService;

public CommandManager(TerraformManager terraformManager, MessagingServiceDelegateService messagingServiceDelegateService) {
this.terraformManager = terraformManager;
this.messagingServiceDelegateService = messagingServiceDelegateService;
}

public void execute(CommandRequest request) {
Map<String, String> envVars = setBrokerSpecificEnvVars(request.getMessagingServiceId());
for (CommandBundle bundle : request.getCommandBundles()) {
// For now everything is run serially
for (Command command : bundle.getCommands()) {
switch (command.getCommandType()) {
case terraform:
terraformManager.execute(request, command, envVars);
break;
default:
throw new IllegalStateException("Unexpected value: " + command.getCommandType());
}
}
}
}

private Map<String, String> setBrokerSpecificEnvVars(String messagingServiceId) {
Map<String, String> envVars = new HashMap<>();
Object client = messagingServiceDelegateService.getMessagingServiceClient(messagingServiceId);
if (client instanceof SolaceHttpSemp) {
SolaceHttpSemp solaceClient = (SolaceHttpSemp) client;
SempClient sempClient = solaceClient.getSempClient();
envVars.put("TF_VAR_username", sempClient.getUsername());
envVars.put("TF_VAR_password", sempClient.getPassword());
envVars.put("TF_VAR_url", sempClient.getConnectionUrl());
}
return envVars;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.solace.maas.ep.event.management.agent.command.rest;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code will be removed once the messaging I/F is added.


import com.solace.maas.ep.event.management.agent.command.CommandManager;
import com.solace.maas.ep.event.management.agent.plugin.command.model.CommandRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
@RequestMapping("/api/v2/ema/command")

public class CommandController {
private final CommandManager commandManager;

public CommandController(CommandManager commandManager) {
this.commandManager = commandManager;
}

@PostMapping
public ResponseEntity<CommandRequest> executeTfCommand(@RequestBody CommandRequest commandRequest) {
commandManager.execute(commandRequest);
return ResponseEntity.ok(commandRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.ProducerTemplate;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;

import java.util.List;
Expand All @@ -26,14 +27,16 @@ public class StreamingAppender extends AppenderBase<ILoggingEvent> {
@Override
protected void append(ILoggingEvent event) {
if (!standalone) {
if (!event.getMDCPropertyMap().get(RouteConstants.SCAN_ID).isEmpty()) {
if (StringUtils.isNotEmpty(event.getMDCPropertyMap().get(RouteConstants.SCAN_ID))) {
sendLogsAsync(event,
event.getMDCPropertyMap().get(RouteConstants.SCAN_ID),
event.getMDCPropertyMap().get(RouteConstants.TRACE_ID),
event.getMDCPropertyMap().get(RouteConstants.ACTOR_ID),
event.getMDCPropertyMap().get(RouteConstants.SCAN_TYPE),
event.getMDCPropertyMap().get(RouteConstants.SCHEDULE_ID),
event.getMDCPropertyMap().get(RouteConstants.MESSAGING_SERVICE_ID));
} else if (StringUtils.isNotEmpty(event.getMDCPropertyMap().get(RouteConstants.COMMAND_CORRELATION_ID))) {
log.trace("This is a placeholder for DATAGO-64298");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the hook where we will add the terraform logs in the future.

}
}
}
Expand Down
3 changes: 2 additions & 1 deletion service/plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.solace.maas</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.solace.maas.ep.event.management.agent.plugin.command.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@SuppressWarnings("PMD.AvoidFieldNameMatchingTypeName")
public class Command {
private CommandType commandType; // "terraform", "bash", SEMP, etc...
private String command; // A tf command like `apply` or `state rm`
private String body; // The body of the .tf file
private Map<String, String> parameters;
private Boolean ignoreResult;
private CommandResult result;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.solace.maas.ep.event.management.agent.plugin.command.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CommandBundle {
private String executionType; // (serial / parallel)
gregmeldrum marked this conversation as resolved.
Show resolved Hide resolved
private Boolean exitOnFailure;
private List<Command> commands;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.solace.maas.ep.event.management.agent.plugin.command.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CommandRequest {
private String correlationId;
private String context;
private String messagingServiceId;
private List<CommandBundle> commandBundles;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.solace.maas.ep.event.management.agent.plugin.command.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CommandResult {
private JobStatus status;
private Map<String, Object> result;
private List<Map<String, Object>> logs;
private List<Map<String, Object>> errors;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.solace.maas.ep.event.management.agent.plugin.command.model;

public enum CommandType {
terraform("terraform");

private final String type;

CommandType(String type) {
this.type = type;
}

public String getType() {
return type;
}

@Override
public String toString() {
return getType();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.solace.maas.ep.event.management.agent.plugin.command.model;

public enum JobStatus {
in_progress,
error,
validation_error,
success;

JobStatus() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class RouteConstants {

public static final String SCAN_STATUS = "SCAN_STATUS";

public static final String COMMAND_CORRELATION_ID = "COMMAND_CORRELATION_ID";

public static final String SCAN_STATUS_DESC = "SCAN_STATUS_DESCRIPTION";

public static final String AGGREGATION_ID = "AGGREGATION_ID";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
public enum MOPProtocol {
scanData(2850),
scanDataControl(2851),
EMAHeartbeat(2852);
EMAHeartbeat(2852),
commandProtocol(2853);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added for future considerations when we add the messaging API



private final int id;

Expand Down
26 changes: 7 additions & 19 deletions service/pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?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">
<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.1.5</version>
<relativePath /> <!-- lookup parent from repository -->
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.solace.maas</groupId>
<artifactId>maas-event-management-agent-parent</artifactId>
Expand All @@ -18,6 +19,7 @@
<module>rabbitmq-plugin</module>
<module>kafka-plugin</module>
<module>solace-plugin</module>
<module>terraform-plugin</module>
<module>local-storage-plugin</module>
<module>confluent-schema-registry-plugin</module>
<module>application</module>
Expand Down Expand Up @@ -237,22 +239,6 @@
</resources>
</configuration>
</execution>
<execution>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused execution

<id>copy-docker</id>
<phase>compile</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<resources>
<resource>
<directory>docker</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
Expand Down Expand Up @@ -314,7 +300,9 @@
<version>3.0.0-M1</version>
<configuration>
<tagNameFormat>v@{project.version}</tagNameFormat>
<arguments>-Dmaven.test.skip=true -Dcheckstyle.skip -Dpmd.skip -Dcpd.skip -Dfindbugs.skip -Dspotbugs.skip</arguments>
<arguments>-Dmaven.test.skip=true -Dcheckstyle.skip -Dpmd.skip -Dcpd.skip -Dfindbugs.skip
-Dspotbugs.skip
</arguments>
<scmCommentPrefix>[ci skip]</scmCommentPrefix>
</configuration>
</plugin>
Expand Down
Loading
Loading