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

OP-22899 Added rest api's in opsmx audit client service for account environment mapping. #493

Merged
merged 4 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion config/spinnaker.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
oes:
spinnakerVersion: "OES 1.30.1.20240300"
spinnakerVersion: "OES 1.30.1.20240300"
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
import org.springframework.web.servlet.handler.HandlerMappingIntrospector
Expand Down Expand Up @@ -198,4 +199,9 @@ public class GateWebConfig implements WebMvcConfigurer {
registry.addViewController("/login").setViewName("ssd-login")
}
}

@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseTrailingSlashMatch(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public PostConnectionConfiguringJedisConnectionFactory(
@Value("${redis.connection:redis://localhost:6379}") String connectionUri,
@Value("${redis.timeout:2000}") int timeout,
@Value(value = "${redis.certificate_location:#{null}}") String certFilePath,
@ConnectionPostProcessor Optional<ConfigureRedisAction> configureRedisAction) throws Exception {
@ConnectionPostProcessor Optional<ConfigureRedisAction> configureRedisAction)
throws Exception {

this.configureRedisAction =
configureRedisAction.orElse(new ConfigureNotifyKeyspaceEventsAction());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,4 +551,11 @@ class OpsmxOesController {
return ResponseEntity.ok().headers(headers).body(manifestFile)
}
}

@Operation(summary = "Rest api for fetching importing account environment mapping records")
@RequestMapping(value = "/acctEnvMapping/import", method = RequestMethod.POST)
Object importAccountsFromSpinnaker() {

return opsmxOesService.importAccountEnvironmentMappings();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package com.netflix.spinnaker.gate.graphql.model;

/** @link */
/**
* @link
*/
public interface Node {

String getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,7 @@ interface OpsmxOesService {
Object evaluateStaticPolicy(@Path('version') String version,
@Body Object data)

@POST("/oes/acctEnvMapping/import")
Object importAccountEnvironmentMappings()

}
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,22 @@ class OpsmxAuditClientServiceController {
return ResponseEntity.status(response.getStatus()).build()
}

@Operation(summary = "Rest api for fetching all account environment mapping records")
@RequestMapping(value = "/v3/acctEnvMapping", method = RequestMethod.GET)
Object getAllAcctEnvMappings() {
return opsmxAuditClientService.getAllAccountEnvironmentMappings();
}
@Operation(summary = "Rest api for fetching account environment mapping record with id")
@RequestMapping(value = "/v3/acctEnvMapping/{id}", method = RequestMethod.GET)
Object getAcctEnvMappingWithId(@PathVariable("id") Integer id) {
return opsmxAuditClientService.getAccountEnvironmentMappingWithId(id);
}
@Operation(summary = "Rest api for fetching all unique environment records")
@RequestMapping(value = "/v3/env", method = RequestMethod.GET)
Object getAllUniqueEnv() {

return opsmxAuditClientService.getAllUniqueEnvironments();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,28 @@

package com.opsmx.spinnaker.gate.controllers

import com.netflix.spinnaker.gate.config.ServiceConfiguration
import com.netflix.spinnaker.gate.exceptions.OesRequestException
import com.netflix.spinnaker.security.AuthenticatedRequest
import com.opsmx.spinnaker.gate.services.OpsmxAuditService
import groovy.util.logging.Slf4j
import io.swagger.v3.oas.annotations.Operation
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okio.Buffer
import okio.BufferedSink
import okio.Okio
import okio.Source
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.multipart.MultipartFile

@Slf4j
@RestController
Expand All @@ -36,6 +48,12 @@ class OpsmxAuditServiceController {
@Autowired
OpsmxAuditService opsmxAuditService

@Autowired
ServiceConfiguration serviceConfiguration

@Autowired
OkHttpClient okHttpClient

@Operation(summary = "Endpoint for audit rest services")
@RequestMapping(value = "/{version}/{type}/{source}/{source1}", method = RequestMethod.POST)
Object postAuditService1(@PathVariable("version") String version,
Expand All @@ -47,4 +65,79 @@ class OpsmxAuditServiceController {
return opsmxAuditService.postAuditService1(version, type, source, source1, data)
}

@Operation(summary = "Rest api for audit save account environment mapping")
@RequestMapping(value = "/v1/acctEnvMapping", method = RequestMethod.POST)
Object saveAcctEnvMapping(@RequestBody Object data) {
return opsmxAuditService.saveAccountEnvironmentMapping(data)
}
@Operation(summary = "Rest api for updating an account environment mapping")
@RequestMapping(value = "/v1/acctEnvMapping/{id}", method = RequestMethod.PUT)
Object updateAcctEnvMapping(@PathVariable("id") Integer id, @RequestBody Object data) {
return opsmxAuditService.updateAccountEnvironmentMapping(id, data)
}

@Operation(summary = "Rest api for deleting account environment mapping record with id")
@RequestMapping(value = "/v1/acctEnvMapping/{id}", method = RequestMethod.DELETE)
Object deleteAcctEnvMapping(@PathVariable("id") Integer id) {
return opsmxAuditService.deleteAccountEnvironmentMappingWithId(id);
}
@Operation(summary = "Rest api for bulk import of account environment mappings")
@RequestMapping(value = "/v1/acctEnvMapping/bulkimport", method = RequestMethod.POST, consumes = "multipart/form-data")
Object bulkImportAcctEnvironmentMappings(@RequestParam("file") MultipartFile data) {
try {
return uploadToAuditService(data)
} catch (Exception e) {
throw new RuntimeException("Failed to process file: ${e.message}", e)
}
}
private String uploadToAuditService(MultipartFile data) {
def obj = AuthenticatedRequest.propagate {
def request = new Request.Builder()
.url(serviceConfiguration.getServiceEndpoint("auditservice").url +"/auditservice/v1/acctEnvMapping/bulkimport")
.post(uploadFileOkHttp(data))
.build()
def response = okHttpClient.newCall(request).execute()
return response
}.call() as okhttp3.Response
if (!obj.isSuccessful()) {
def error = obj.body().string();
log.error("Failed to upload multipart file to audit service : {}", error)
throw new OesRequestException(error)
} else{
return obj.body()?.string() ?: "Unknown reason: " + obj.code() as Object
}
}
private okhttp3.RequestBody uploadFileOkHttp(MultipartFile multiPartfile) throws IOException {
String fileName = multiPartfile.getOriginalFilename();
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
builder.addFormDataPart("file", fileName, new okhttp3.RequestBody() {
@Override
public okhttp3.MediaType contentType() {
return okhttp3.MediaType.parse("application/octet-stream");
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
try {
Source source = Okio.source(multiPartfile.getInputStream());
Buffer buf = new Buffer();
long totalRead = 0;
long totalSize = multiPartfile.getSize();
long remaining = totalSize;
for (long readCount; (readCount = source.read(buf, 32000)) != -1;) {
totalRead += readCount;
remaining -= readCount;
sink.write(buf, readCount);
sink.flush();
}
} catch (Exception e) {
e.printStackTrace();
rahul-chekuri marked this conversation as resolved.
Show resolved Hide resolved
throw new OesRequestException("Failed to upload multipart file to audit service");
}
}
});
return builder.build();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.opsmx.spinnaker.gate.services

import org.springframework.web.bind.annotation.RequestParam
import retrofit.client.Response
import retrofit.http.GET
import retrofit.http.Path
Expand Down Expand Up @@ -133,4 +132,14 @@ interface OpsmxAuditClientService {
@Query('endTime') Long endTime,
@Query('days') Integer days,
@Query('filterBy') String filterBy)

@GET("/auditclientservice/v3/acctEnvMapping")
Object getAllAccountEnvironmentMappings()

@GET("/auditclientservice/v3/acctEnvMapping/{id}")
Object getAccountEnvironmentMappingWithId(@Path('id') Integer id)

@GET("/auditclientservice/v3/env")
Object getAllUniqueEnvironments()

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package com.opsmx.spinnaker.gate.services

import retrofit.http.Body
import retrofit.http.DELETE
import retrofit.http.POST
import retrofit.http.PUT
import retrofit.http.Path

interface OpsmxAuditService {
Expand All @@ -29,4 +31,13 @@ interface OpsmxAuditService {
@Path('source1') String source1,
@Body Object data)

@POST("/auditservice/v1/acctEnvMapping")
Object saveAccountEnvironmentMapping(@Body Object data)

@PUT("/auditservice/v1/acctEnvMapping/{id}")
Object updateAccountEnvironmentMapping(@Path('id') Integer id,
@Body Object data)

@DELETE("/auditservice/v1/acctEnvMapping/{id}")
Object deleteAccountEnvironmentMappingWithId(@Path('id') Integer id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
@Configuration
@ConfigurationProperties("oes")
public class OESSpinnakerVersionProperties {
private String spinnakerVersion="";
private String spinnakerVersion = "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@
@RestController
@RequestMapping(value = "/oes")
public class OESSpinnakerVersionController {
@Autowired(required = false)
private OESSpinnakerVersionProperties oesSpinnakerVersionProperties;

@GetMapping(value = "/spinnakerVersion", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> getOESSpinnakerVersion() {
log.debug("Get OES Spinnaker Version API invoked");
return new ResponseEntity<>(oesSpinnakerVersionProperties.getSpinnakerVersion(), HttpStatus.OK);
}
@Autowired(required = false)
private OESSpinnakerVersionProperties oesSpinnakerVersionProperties;

@GetMapping(value = "/spinnakerVersion", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> getOESSpinnakerVersion() {
log.debug("Get OES Spinnaker Version API invoked");
return new ResponseEntity<>(oesSpinnakerVersionProperties.getSpinnakerVersion(), HttpStatus.OK);
}
}
Loading