Skip to content

Commit

Permalink
Merge pull request #128 from Pranav-b-7/allow-location-header
Browse files Browse the repository at this point in the history
Allow location header
  • Loading branch information
ramyaravi-opsmx authored Feb 4, 2021
2 parents 860217f + 569c23d commit 19b9c40
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

package com.netflix.spinnaker.gate.controllers

import com.google.gson.Gson
import com.netflix.spinnaker.gate.config.ServiceConfiguration
import com.netflix.spinnaker.gate.model.ApprovalGateTriggerResponseModel
import com.netflix.spinnaker.gate.services.internal.OpsmxVisibilityService
import groovy.util.logging.Slf4j
import io.swagger.annotations.ApiOperation
import okhttp3.OkHttpClient
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression
import org.springframework.http.HttpHeaders
Expand All @@ -30,6 +31,8 @@ import retrofit.client.Response
import org.apache.commons.io.IOUtils
import org.springframework.http.HttpStatus

import java.util.stream.Collectors

@RequestMapping("/visibilityservice")
@RestController
@Slf4j
Expand All @@ -54,19 +57,21 @@ class OpsmxVisibilityController {
@Autowired
OpsmxVisibilityService opsmxVisibilityService

Gson gson = new Gson()

@ApiOperation(value = "Endpoint for visibility rest services")
@RequestMapping(value = "/v1/approvalGates/{id}/trigger", method = RequestMethod.POST)
@ResponseBody Object triggerV1ApprovalGate(@PathVariable("id") Integer id,
@RequestBody(required = false) Object data) {
Response response = opsmxVisibilityService.triggerV1ApprovalGate(id, data)
InputStream inputStream = response.getBody().in()
InputStream inputStream = null
try {
HttpHeaders headers = new HttpHeaders()
response.getHeaders().forEach({ header ->
headers.add(header.getName(), header.getValue())
})
headers.add("Location", response.getHeaders().stream().filter({ header -> header.getName().trim().equalsIgnoreCase("Location") }).collect(Collectors.toList()).get(0).value)
inputStream = response.getBody().in()
String responseBody = new String(IOUtils.toByteArray(inputStream))
return new ResponseEntity(responseBody, headers, HttpStatus.valueOf(response.getStatus()))
ApprovalGateTriggerResponseModel approvalGateTriggerResponseModel = gson.fromJson(responseBody, ApprovalGateTriggerResponseModel.class)
return new ResponseEntity(approvalGateTriggerResponseModel, headers, HttpStatus.valueOf(response.getStatus()))
} finally{
if (inputStream!=null){
inputStream.close()
Expand All @@ -77,17 +82,19 @@ class OpsmxVisibilityController {
@ApiOperation(value = "Endpoint for visibility rest services")
@RequestMapping(value = "/v2/approvalGates/{id}/trigger", method = RequestMethod.POST)
@ResponseBody Object triggerV2ApprovalGate(@PathVariable("id") Integer id,
@RequestBody(required = false) Object data) {
@RequestBody(required = false) Object data) throws Exception {

Response response = opsmxVisibilityService.triggerV2ApprovalGate(id, data)
InputStream inputStream = response.getBody().in()
InputStream inputStream = null

try {
HttpHeaders headers = new HttpHeaders()
response.getHeaders().forEach({ header ->
headers.add(header.getName(), header.getValue())
})
headers.add("Location", response.getHeaders().stream().filter({ header -> header.getName().trim().equalsIgnoreCase("Location") }).collect(Collectors.toList()).get(0).value)
inputStream = response.getBody().in()
String responseBody = new String(IOUtils.toByteArray(inputStream))
return new ResponseEntity(responseBody, headers, HttpStatus.valueOf(response.getStatus()))
ApprovalGateTriggerResponseModel approvalGateTriggerResponseModel = gson.fromJson(responseBody, ApprovalGateTriggerResponseModel.class)
return new ResponseEntity(approvalGateTriggerResponseModel, headers, HttpStatus.valueOf(response.getStatus()))

} finally{
if (inputStream!=null){
inputStream.close()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2021 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.netflix.spinnaker.gate.model


class ApprovalGateTriggerResponseModel {
private Integer id
private Integer approvalGateId
private String activatedTime
private String approvalCallbackURL
private String rejectionCallbackURL

static class ApprovalStatus{
private String status

String getStatus() {
return status
}

void setStatus(String status) {
this.status = status
}
}

Integer getId() {
return id
}

void setId(Integer id) {
this.id = id
}

Integer getApprovalGateId() {
return approvalGateId
}

void setApprovalGateId(Integer approvalGateId) {
this.approvalGateId = approvalGateId
}

String getActivatedTime() {
return activatedTime
}

void setActivatedTime(String activatedTime) {
this.activatedTime = activatedTime
}

String getApprovalCallbackURL() {
return approvalCallbackURL
}

void setApprovalCallbackURL(String approvalCallbackURL) {
this.approvalCallbackURL = approvalCallbackURL
}

String getRejectionCallbackURL() {
return rejectionCallbackURL
}

void setRejectionCallbackURL(String rejectionCallbackURL) {
this.rejectionCallbackURL = rejectionCallbackURL
}
}

0 comments on commit 19b9c40

Please sign in to comment.