Skip to content

Commit 59c1e7c

Browse files
kevinawooGanesh Sapkal
authored andcommitted
refact(gateapi): swagger 2.4.15 query params are typed now (spinnaker#292)
* chore(gateapi): update gateapi from 5c625da5 using swagger 2.4.14 * feat(contribution): add instructions on how to generate gateapi using 2.4.1 * refact(gateapi): swagger 2.4.14 query params are typed now * tests(gateapi): gateapi generated code expects valid JSON and headers This also removes "*malformed" test cases since 2.14.4 doesn't return parsing errors swagger-api/swagger-codegen#10434 reports the issue. hopefully swagger-api/swagger-codegen#10429 be merged will add that ability back in. * refactor(imports): fixed import styling * chore(gateapi): use swagger-cli 2.4.15
1 parent 0c0a86e commit 59c1e7c

File tree

188 files changed

+20163
-16265
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

188 files changed

+20163
-16265
lines changed

CONTRIBUTING.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,14 @@ from the root `spin/` directory.
5252

5353
Spin CLI uses [Swagger](https://swagger.io/) to generate the API client library for [Gate](https://github.com/spinnaker/gate). To update the client library:
5454

55-
- From the root of the Gate directory, execute `swagger/generate_swagger.sh` to create the `swagger.json` API spec.
56-
- Get the [Swagger Codegen CLI](https://github.com/swagger-api/swagger-codegen). Use the version specified [here](https://github.com/spinnaker/spin/blob/master/gateapi/.swagger-codegen/VERSION).
57-
- Remove the existing generated code from the spin directory `rm -r ~/spin/gateapi`
58-
- Use the Swagger Codegen CLI to generate the new library and drop it into the spin project `java -jar ~/swagger-codegen-cli.jar generate -i ~/gate/swagger/swagger.json -l go -o ~/spin/gateapi`
55+
- Use the Swagger Codegen to generate the new library and drop it into the spin project
56+
```bash
57+
GATE_REPO_PATH=PATH_TO_YOUR_GATE_REPO
58+
SWAGGER_CODEGEN_VERSION=$(cat gateapi/.swagger-codegen/VERSION)
59+
rm -rf gateapi/ \
60+
&& docker run -it \
61+
-v "${GATE_REPO_PATH}/swagger/:/tmp/gate" \
62+
-v "$PWD/gateapi/:/tmp/go/" \
63+
"swaggerapi/swagger-codegen-cli:${SWAGGER_CODEGEN_VERSION}" generate -i /tmp/gate/swagger.json -l go -o /tmp/go/
64+
```
5965
- Commit the changes and open a PR.

cmd/account/get.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"fmt"
1919
"net/http"
2020

21+
gate "github.com/spinnaker/spin/gateapi"
22+
2123
"github.com/spf13/cobra"
2224

2325
"github.com/spinnaker/spin/util"
@@ -57,7 +59,7 @@ func getAccount(cmd *cobra.Command, options *getOptions, args []string) error {
5759
return err
5860
}
5961

60-
account, resp, err := options.GateClient.CredentialsControllerApi.GetAccountUsingGET(options.GateClient.Context, accountName, map[string]interface{}{})
62+
account, resp, err := options.GateClient.CredentialsControllerApi.GetAccountUsingGET(options.GateClient.Context, accountName, &gate.CredentialsControllerApiGetAccountUsingGETOpts{})
6163
if resp != nil {
6264
if resp.StatusCode == http.StatusNotFound {
6365
return fmt.Errorf("Account '%s' not found\n", accountName)

cmd/account/get_test.go

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,6 @@ func TestAccountGet_flags(t *testing.T) {
107107
}
108108
}
109109

110-
func TestAccountGet_malformed(t *testing.T) {
111-
ts := testGateAccountGetMalformed()
112-
defer ts.Close()
113-
114-
rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
115-
rootCmd.AddCommand(NewAccountCmd(rootOpts))
116-
117-
args := []string{"account", "get", ACCOUNT, "--gate-endpoint=" + ts.URL}
118-
rootCmd.SetArgs(args)
119-
err := rootCmd.Execute()
120-
if err == nil { // Success is actually failure here, return payload is malformed.
121-
t.Fatalf("Command failed with: %d", err)
122-
}
123-
}
124-
125110
func TestAccountGet_fail(t *testing.T) {
126111
ts := testGateFail()
127112
defer ts.Close()
@@ -142,31 +127,12 @@ func TestAccountGet_fail(t *testing.T) {
142127
func testGateAccountGetSuccess() *httptest.Server {
143128
mux := util.TestGateMuxWithVersionHandler()
144129
mux.Handle("/credentials/"+ACCOUNT, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
130+
w.Header().Add("content-type", "application/json")
145131
fmt.Fprintln(w, strings.TrimSpace(accountJson))
146132
}))
147133
return httptest.NewServer(mux)
148134
}
149135

150-
// testGateAccountGetMalformed returns a malformed list of pipeline configs.
151-
func testGateAccountGetMalformed() *httptest.Server {
152-
mux := util.TestGateMuxWithVersionHandler()
153-
mux.Handle("/credentials/"+ACCOUNT, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
154-
fmt.Fprintln(w, strings.TrimSpace(malformedAccountGetJson))
155-
}))
156-
return httptest.NewServer(mux)
157-
}
158-
159-
const malformedAccountGetJson = `
160-
"type": "kubernetes",
161-
"providerVersion": "v2",
162-
"environment": "self",
163-
"skin": "v2",
164-
"name": "self",
165-
"cloudProvider": "kubernetes",
166-
"accountType": "self"
167-
}
168-
`
169-
170136
const accountJson = `
171137
{
172138
"type": "kubernetes",

cmd/account/list.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import (
1818
"fmt"
1919
"net/http"
2020

21+
"github.com/antihax/optional"
2122
"github.com/spf13/cobra"
23+
24+
gate "github.com/spinnaker/spin/gateapi"
2225
)
2326

2427
type listOptions struct {
@@ -52,7 +55,7 @@ func NewListCmd(accOptions *accountOptions) *cobra.Command {
5255
}
5356

5457
func listAccount(cmd *cobra.Command, options *listOptions, args []string) error {
55-
accountList, resp, err := options.GateClient.CredentialsControllerApi.GetAccountsUsingGET(options.GateClient.Context, map[string]interface{}{"expand": options.expand})
58+
accountList, resp, err := options.GateClient.CredentialsControllerApi.GetAccountsUsingGET(options.GateClient.Context, &gate.CredentialsControllerApiGetAccountsUsingGETOpts{Expand: optional.NewBool(options.expand)})
5659
if err != nil {
5760
return err
5861
}

cmd/account/list_test.go

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,6 @@ func TestAccountList_basic(t *testing.T) {
4141
}
4242
}
4343

44-
func TestAccountList_malformed(t *testing.T) {
45-
ts := testGateAccountListMalformed()
46-
defer ts.Close()
47-
48-
rootCmd, options := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
49-
rootCmd.AddCommand(NewAccountCmd(options))
50-
51-
args := []string{"account", "list", "--gate-endpoint=" + ts.URL}
52-
rootCmd.SetArgs(args)
53-
err := rootCmd.Execute()
54-
if err == nil {
55-
t.Fatalf("Command failed with: %s", err)
56-
}
57-
}
58-
5944
func TestAccountList_fail(t *testing.T) {
6045
ts := testGateFail()
6146
defer ts.Close()
@@ -74,35 +59,12 @@ func TestAccountList_fail(t *testing.T) {
7459
func testGateAccountListSuccess() *httptest.Server {
7560
mux := util.TestGateMuxWithVersionHandler()
7661
mux.Handle("/credentials/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
62+
w.Header().Add("content-type", "application/json")
7763
fmt.Fprintln(w, strings.TrimSpace(accountListJson))
7864
}))
7965
return httptest.NewServer(mux)
8066
}
8167

82-
func testGateAccountListMalformed() *httptest.Server {
83-
mux := util.TestGateMuxWithVersionHandler()
84-
mux.Handle("/credentials/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
85-
fmt.Fprintln(w, strings.TrimSpace(malformedAccountListJson))
86-
}))
87-
return httptest.NewServer(mux)
88-
}
89-
90-
const malformedAccountListJson = `
91-
{
92-
"type": "kubernetes",
93-
"skin": "v2",
94-
"providerVersion": "v2",
95-
"name": "foobar"
96-
},
97-
{
98-
"type": "dockerRegistry",
99-
"skin": "v1",
100-
"providerVersion": "v1",
101-
"name": "dockerhub"
102-
}
103-
]
104-
`
105-
10668
const accountListJson = `[
10769
{
10870
"type": "kubernetes",

cmd/application/delete.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ import (
1818
"fmt"
1919
"net/http"
2020

21-
orca_tasks "github.com/spinnaker/spin/cmd/orca-tasks"
22-
21+
"github.com/antihax/optional"
2322
"github.com/spf13/cobra"
2423

24+
orca_tasks "github.com/spinnaker/spin/cmd/orca-tasks"
25+
gate "github.com/spinnaker/spin/gateapi"
2526
"github.com/spinnaker/spin/util"
2627
)
2728

@@ -65,7 +66,7 @@ func deleteApplication(cmd *cobra.Command, options *deleteOptions, args []string
6566
},
6667
}
6768

68-
_, resp, err := options.GateClient.ApplicationControllerApi.GetApplicationUsingGET(options.GateClient.Context, applicationName, map[string]interface{}{"expand": false})
69+
_, resp, err := options.GateClient.ApplicationControllerApi.GetApplicationUsingGET(options.GateClient.Context, applicationName, &gate.ApplicationControllerApiGetApplicationUsingGETOpts{Expand: optional.NewBool(false)})
6970

7071
if resp != nil && resp.StatusCode == http.StatusNotFound {
7172
return fmt.Errorf("Attempting to delete application '%s' which does not exist, exiting...", applicationName)

cmd/application/delete_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,23 @@ func testGateApplicationDeleteSuccess() *httptest.Server {
7878
mux.Handle("/applications/"+APP, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
7979
payload := map[string]string{} // We don't use the payload, we are just checking if the target app exists.
8080
b, _ := json.Marshal(&payload)
81+
w.Header().Add("content-type", "application/json")
8182
fmt.Fprintln(w, string(b))
8283
}))
8384
mux.Handle("/tasks", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
8485
payload := map[string]string{
8586
"ref": "/tasks/id",
8687
}
8788
b, _ := json.Marshal(&payload)
89+
w.Header().Add("content-type", "application/json")
8890
fmt.Fprintln(w, string(b))
8991
}))
9092
mux.Handle("/tasks/id", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
9193
payload := map[string]string{
9294
"status": "SUCCEEDED",
9395
}
9496
b, _ := json.Marshal(&payload)
97+
w.Header().Add("content-type", "application/json")
9598
fmt.Fprintln(w, string(b))
9699
}))
97100
return httptest.NewServer(mux)

cmd/application/get.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import (
1818
"fmt"
1919
"net/http"
2020

21-
"github.com/spinnaker/spin/util"
22-
21+
"github.com/antihax/optional"
2322
"github.com/spf13/cobra"
23+
24+
gate "github.com/spinnaker/spin/gateapi"
25+
"github.com/spinnaker/spin/util"
2426
)
2527

2628
type getOptions struct {
@@ -63,7 +65,7 @@ func getApplication(cmd *cobra.Command, options *getOptions, args []string) erro
6365
return err
6466
}
6567

66-
app, resp, err := options.GateClient.ApplicationControllerApi.GetApplicationUsingGET(options.GateClient.Context, applicationName, map[string]interface{}{"expand": options.expand})
68+
app, resp, err := options.GateClient.ApplicationControllerApi.GetApplicationUsingGET(options.GateClient.Context, applicationName, &gate.ApplicationControllerApiGetApplicationUsingGETOpts{Expand: optional.NewBool(options.expand)})
6769
if resp != nil {
6870
if resp.StatusCode == http.StatusNotFound {
6971
return fmt.Errorf("Application '%s' not found\n", applicationName)

cmd/application/get_test.go

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,6 @@ func TestApplicationGet_flags(t *testing.T) {
114114
}
115115
}
116116

117-
func TestApplicationGet_malformed(t *testing.T) {
118-
ts := testGateApplicationGetMalformed()
119-
defer ts.Close()
120-
121-
rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
122-
rootCmd.AddCommand(NewApplicationCmd(rootOpts))
123-
124-
args := []string{"application", "get", APP, "--gate-endpoint=" + ts.URL}
125-
rootCmd.SetArgs(args)
126-
err := rootCmd.Execute()
127-
if err == nil { // Success is actually failure here, return payload is malformed.
128-
t.Fatalf("Command failed with: %d", err)
129-
}
130-
}
131-
132117
func TestApplicationGet_fail(t *testing.T) {
133118
ts := testGateFail()
134119
defer ts.Close()
@@ -149,48 +134,12 @@ func TestApplicationGet_fail(t *testing.T) {
149134
func testGateApplicationGetSuccess() *httptest.Server {
150135
mux := util.TestGateMuxWithVersionHandler()
151136
mux.Handle("/applications/"+APP, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
137+
w.Header().Add("content-type", "application/json")
152138
fmt.Fprintln(w, strings.TrimSpace(applicationJsonExpanded))
153139
}))
154140
return httptest.NewServer(mux)
155141
}
156142

157-
// testGateApplicationGetMalformed returns a malformed list of pipeline configs.
158-
func testGateApplicationGetMalformed() *httptest.Server {
159-
mux := util.TestGateMuxWithVersionHandler()
160-
mux.Handle("/applications/"+APP, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
161-
fmt.Fprintln(w, strings.TrimSpace(malformedApplicationGetJson))
162-
}))
163-
return httptest.NewServer(mux)
164-
}
165-
166-
const malformedApplicationGetJson = `
167-
"accounts": "account1",
168-
"cloudproviders": [
169-
"gce",
170-
"kubernetes"
171-
],
172-
"createTs": "1527261941734",
173-
"email": "app",
174-
"instancePort": 80,
175-
"lastModifiedBy": "anonymous",
176-
"name": "app",
177-
"permissions": {
178-
"EXECUTE": [
179-
"admin-group"
180-
],
181-
"READ": [
182-
"admin-group",
183-
"user-group"
184-
],
185-
"WRITE": [
186-
"admin-group"
187-
]
188-
},
189-
"updateTs": "1527261941735",
190-
"user": "anonymous"
191-
}
192-
`
193-
194143
// GET /applications/{app} returns an envelope with 'attributes' and 'clusters'.
195144
const applicationJsonExpanded = `
196145
{

cmd/application/list.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"fmt"
1919
"net/http"
2020

21+
gate "github.com/spinnaker/spin/gateapi"
22+
2123
"github.com/spf13/cobra"
2224
)
2325

@@ -49,7 +51,7 @@ func NewListCmd(appOptions *applicationOptions) *cobra.Command {
4951
}
5052

5153
func listApplication(cmd *cobra.Command, options *listOptions, args []string) error {
52-
appList, resp, err := options.GateClient.ApplicationControllerApi.GetAllApplicationsUsingGET(options.GateClient.Context, map[string]interface{}{})
54+
appList, resp, err := options.GateClient.ApplicationControllerApi.GetAllApplicationsUsingGET(options.GateClient.Context, &gate.ApplicationControllerApiGetAllApplicationsUsingGETOpts{})
5355
if err != nil {
5456
return err
5557
}

0 commit comments

Comments
 (0)