This commit is contained in:
Divyansh Manchanda 2019-09-26 12:16:39 +05:30
Родитель 199fc43c7c
Коммит 5e223b8f9e
3 изменённых файлов: 45 добавлений и 22 удалений

Просмотреть файл

@ -0,0 +1,11 @@
apiVersion: v1
data:
.agent: agentFileContent
.credentials: credentialFileContent
.url: downloadAgentUrl
type: Opaque
kind: Secret
metadata:
creationTimestamp: null
name: agent-secret
namespace: azuredevops

15
constants.go Normal file
Просмотреть файл

@ -0,0 +1,15 @@
package main
const (
NoAgentIdError = "No AgentId sent in request body."
NoValidSignatureError = "Endpoint can only be invoked with AzureDevOps with the correct Shared Signature."
InvalidRequestError = "Invalid request Method."
)
type ErrorMessage struct {
Error string
}
func GetError(message string) ErrorMessage {
return ErrorMessage{Error: message}
}

41
main.go
Просмотреть файл

@ -24,7 +24,7 @@ func main() {
func AcquireAgentHandler(resp http.ResponseWriter, req *http.Request) {
// HTTP method should be POST and the HMAC header should be valid
if req.Method == "POST" {
if req.Method == http.MethodPost {
if isRequestHmacValid(req) {
var agentRequest AgentRequest
requestBody, err := ioutil.ReadAll(req.Body)
@ -32,54 +32,51 @@ func AcquireAgentHandler(resp http.ResponseWriter, req *http.Request) {
json.Unmarshal(requestBody, &agentRequest)
if err != nil {
http.Error(resp, err.Error(), http.StatusCreated)
writeJsonResponse(resp, http.StatusBadRequest, err.Error())
} else if agentRequest.AgentId == "" {
writeJsonResponse(resp, http.StatusBadRequest, GetError(NoAgentIdError))
} else {
var pods = CreatePod(agentRequest.AgentId, agentRequest.AuthenticationToken)
writeJsonResponse(resp, http.StatusCreated, pods)
}
if agentRequest.AgentId == "" {
http.Error(resp, "No AgentId sent in request body.", http.StatusCreated)
}
var pods = CreatePod(agentRequest.AgentId, agentRequest.AuthenticationToken)
writeJsonResponse(resp, pods)
} else {
http.Error(resp, "Endpoint can only be invoked with AzureDevOps with the correct Shared Signature.", http.StatusForbidden)
writeJsonResponse(resp, http.StatusForbidden, GetError(NoValidSignatureError))
}
} else {
http.Error(resp, "Invalid request Method.", http.StatusMethodNotAllowed)
writeJsonResponse(resp, http.StatusMethodNotAllowed, GetError(InvalidRequestError))
}
}
func ReleaseAgentHandler(resp http.ResponseWriter, req *http.Request) {
// HTTP method should be POST and the HMAC header should be valid
if req.Method == "POST" {
if req.Method == http.MethodPost {
if isRequestHmacValid(req) {
var agentRequest ReleaseAgentRequest
requestBody, _ := ioutil.ReadAll(req.Body)
json.Unmarshal(requestBody, &agentRequest)
if agentRequest.AgentId == "" {
http.Error(resp, "No AgentId sent in request body.", http.StatusCreated)
writeJsonResponse(resp, http.StatusBadRequest, GetError(NoAgentIdError))
} else {
var pods = DeletePodWithAgentId(agentRequest.AgentId)
writeJsonResponse(resp, http.StatusCreated, pods)
}
var pods = DeletePodWithAgentId(agentRequest.AgentId)
writeJsonResponse(resp, pods)
} else {
http.Error(resp, "Endpoint can only be invoked with AzureDevOps with the correct Shared Signature.", http.StatusForbidden)
writeJsonResponse(resp, http.StatusForbidden, GetError(NoValidSignatureError))
}
} else {
http.Error(resp, "Invalid request Method.", http.StatusMethodNotAllowed)
writeJsonResponse(resp, http.StatusMethodNotAllowed, GetError(InvalidRequestError))
}
}
func EmptyResponeHandler(resp http.ResponseWriter, req *http.Request) {
var emptyResponse PodResponse
writeJsonResponse(resp, emptyResponse)
writeJsonResponse(resp, http.StatusCreated, emptyResponse)
}
func writeJsonResponse(resp http.ResponseWriter, podResponse interface{}) {
func writeJsonResponse(resp http.ResponseWriter, httpStatus int, podResponse interface{}) {
jsonData, _ := json.Marshal(podResponse)
resp.Header().Set("Content-Type", "application/json")
resp.WriteHeader(http.StatusCreated)
resp.WriteHeader(httpStatus)
resp.Write(jsonData)
}