From d3b62cfc52a5bd22838e4b07d4e0bcda9b52b838 Mon Sep 17 00:00:00 2001 From: Kaixiang Zhao Date: Fri, 17 Jan 2020 13:52:52 -0800 Subject: [PATCH] feat(codebuild): Add an API to list all AWS CodeBuild accounts --- .../controllers/AwsCodeBuildController.java | 39 ++++++++++ .../gate/services/internal/IgorService.groovy | 3 + .../AwsCodeBuildControllerSpec.groovy | 78 +++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/AwsCodeBuildController.java create mode 100644 gate-web/src/test/groovy/com/netflix/spinnaker/gate/controllers/AwsCodeBuildControllerSpec.groovy diff --git a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/AwsCodeBuildController.java b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/AwsCodeBuildController.java new file mode 100644 index 0000000000..303852847e --- /dev/null +++ b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/AwsCodeBuildController.java @@ -0,0 +1,39 @@ +/* + * Copyright 2020 Amazon.com, 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.controllers; + +import com.netflix.spinnaker.gate.services.internal.IgorService; +import io.swagger.annotations.ApiOperation; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.web.bind.annotation.*; + +@ConditionalOnProperty("services.igor.enabled") +@RestController +@RequestMapping("/codebuild") +@RequiredArgsConstructor +public class AwsCodeBuildController { + + private final IgorService igorService; + + @ApiOperation(value = "Retrieve the list of AWS CodeBuild accounts", response = List.class) + @GetMapping(value = "/accounts") + List getAccounts() { + return igorService.getAwsCodeBuildAccounts(); + } +} diff --git a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/internal/IgorService.groovy b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/internal/IgorService.groovy index 499e9735fb..5b6c69d7b1 100644 --- a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/internal/IgorService.groovy +++ b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/internal/IgorService.groovy @@ -74,6 +74,9 @@ interface IgorService { @GET('/gcb/triggers/{account}') List getGoogleCloudBuildTriggers(@Path("account") String account); + @GET('/codebuild/accounts') + List getAwsCodeBuildAccounts(); + @GET('/artifacts/{provider}/{packageName}') List getArtifactVersions(@Path("provider") String provider, @Path("packageName") String packageName, diff --git a/gate-web/src/test/groovy/com/netflix/spinnaker/gate/controllers/AwsCodeBuildControllerSpec.groovy b/gate-web/src/test/groovy/com/netflix/spinnaker/gate/controllers/AwsCodeBuildControllerSpec.groovy new file mode 100644 index 0000000000..07c983b82b --- /dev/null +++ b/gate-web/src/test/groovy/com/netflix/spinnaker/gate/controllers/AwsCodeBuildControllerSpec.groovy @@ -0,0 +1,78 @@ +/* + * Copyright 2020 Amazon.com, 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.controllers + +import com.fasterxml.jackson.databind.ObjectMapper +import com.netflix.spinnaker.gate.services.internal.IgorService +import com.squareup.okhttp.mockwebserver.MockWebServer +import org.springframework.http.MediaType +import org.springframework.mock.web.MockHttpServletResponse +import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.setup.MockMvcBuilders +import spock.lang.Shared +import spock.lang.Specification + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get + +class AwsCodeBuildControllerSpec extends Specification { + + MockMvc mockMvc + IgorService igorService + + def server = new MockWebServer() + + @Shared def objectMapper = new ObjectMapper() + @Shared def ACCOUNT = 'myAccount' + + void cleanup() { + server.shutdown() + } + + void setup() { + igorService = Mock(IgorService) + server.start() + mockMvc = MockMvcBuilders.standaloneSetup( + new AwsCodeBuildController(igorService)).build() + } + + void 'should get a list of accounts'() { + given: + def accounts = ["account1", "account2"] + 1 * igorService.getAwsCodeBuildAccounts() >> accounts + + when: + MockHttpServletResponse response = mockMvc.perform(get("/codebuild/accounts") + .accept(MediaType.APPLICATION_JSON)).andReturn().response + + then: + response.status == 200 + response.contentAsString == objectMapper.writeValueAsString(accounts) + } + + void 'should get an empty list when no accounts found'() { + given: + 1 * igorService.getAwsCodeBuildAccounts() >> [] + + when: + MockHttpServletResponse response = mockMvc.perform(get("/codebuild/accounts") + .accept(MediaType.APPLICATION_JSON)).andReturn().response + + then: + response.status == 200 + response.contentAsString == '[]' + } +}