Skip to content

Commit 1399ab5

Browse files
authored
API: Universal swagger generation (grafana#51033)
1 parent cb17f91 commit 1399ab5

File tree

11 files changed

+1204
-2370
lines changed

11 files changed

+1204
-2370
lines changed

.bingo/Variables.mk

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ $(DRONE): $(BINGO_DIR)/drone.mod
2929
@echo "(re)installing $(GOBIN)/drone-v1.5.0"
3030
@cd $(BINGO_DIR) && $(GO) build -mod=mod -modfile=drone.mod -o=$(GOBIN)/drone-v1.5.0 "github.com/drone/drone-cli/drone"
3131

32+
SWAGGER := $(GOBIN)/swagger-v0.29.0
33+
$(SWAGGER): $(BINGO_DIR)/swagger.mod
34+
@# Install binary/ries using Go 1.14+ build command. This is using bwplotka/bingo-controlled, separate go module with pinned dependencies.
35+
@echo "(re)installing $(GOBIN)/swagger-v0.29.0"
36+
@cd $(BINGO_DIR) && $(GO) build -mod=mod -modfile=swagger.mod -o=$(GOBIN)/swagger-v0.29.0 "github.com/go-swagger/go-swagger/cmd/swagger"
37+
3238
GOLANGCI_LINT := $(GOBIN)/golangci-lint-v1.40.1
3339
$(GOLANGCI_LINT): $(BINGO_DIR)/golangci-lint.mod
3440
@# Install binary/ries using Go 1.14+ build command. This is using bwplotka/bingo-controlled, separate go module with pinned dependencies.

.bingo/swagger.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT
2+
3+
go 1.18
4+
5+
require github.com/go-swagger/go-swagger v0.29.0 // cmd/swagger

.bingo/swagger.sum

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
2+
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
3+
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ=
4+
github.com/felixge/httpsnoop v1.0.2 h1:+nS9g82KMXccJ/wp0zyRW9ZBHFETmMGtkk+2CTTrW4o=
5+
github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=
6+
github.com/go-openapi/analysis v0.21.2 h1:hXFrOYFHUAMQdu6zwAiKKJHJQ8kqZs1ux/ru1P1wLJU=
7+
github.com/go-openapi/errors v0.20.2 h1:dxy7PGTqEh94zj2E3h1cUmQQWiM1+aeCROfAr02EmK8=
8+
github.com/go-openapi/inflect v0.19.0 h1:9jCH9scKIbHeV9m12SmPilScz6krDxKRasNNSNPXu/4=
9+
github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY=
10+
github.com/go-openapi/jsonreference v0.19.6 h1:UBIxjkht+AWIgYzCDSv2GN+E/togfwXUJFRTWhl2Jjs=
11+
github.com/go-openapi/loads v0.21.0 h1:jYtUO4wwP7psAweisP/MDoOpdzsYEESdoPcsWjHDR68=
12+
github.com/go-openapi/runtime v0.21.1 h1:/KIG00BzA2x2HRStX2tnhbqbQdPcFlkgsYCiNY20FZs=
13+
github.com/go-openapi/spec v0.20.4 h1:O8hJrt0UMnhHcluhIdUgCLRWyM2x7QkBXRvOs7m+O1M=
14+
github.com/go-openapi/strfmt v0.21.1 h1:G6s2t5V5kGCHLVbSdZ/6lI8Wm4OzoPFkc3/cjAsKQrM=
15+
github.com/go-openapi/swag v0.19.15 h1:D2NRCBzS9/pEY3gP9Nl8aDqGUcPFrwG2p+CNFrLyrCM=
16+
github.com/go-openapi/validate v0.20.3 h1:GZPPhhKSZrE8HjB4eEkoYAZmoWA4+tCemSgINH1/vKw=
17+
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
18+
github.com/go-swagger/go-swagger v0.29.0 h1:z3YoZtLvS1Y8TE/PCat1VypcZxM0IgKLt0NvZxQyNl8=
19+
github.com/go-swagger/go-swagger v0.29.0/go.mod h1:Z4GJzI+bHKKkGB2Ji1rawpi3/ldXX8CkzGIa9HAC5EE=
20+
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
21+
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
22+
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
23+
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
24+
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
25+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
26+
github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=
27+
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
28+
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
29+
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
30+
github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=
31+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
32+
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
33+
github.com/spf13/afero v1.8.0 h1:5MmtuhAgYeU6qpa7w7bP0dv6MBYuup0vekhSpSkoq60=
34+
github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA=
35+
github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=
36+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
37+
github.com/spf13/viper v1.10.1 h1:nuJZuYpG7gTj/XqiUwg8bA0cp1+M2mC3J4g5luUYBKk=
38+
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
39+
github.com/toqueteos/webbrowser v1.2.0 h1:tVP/gpK69Fx+qMJKsLE7TD8LuGWPnEV71wBN9rrstGQ=
40+
go.mongodb.org/mongo-driver v1.8.2 h1:8ssUXufb90ujcIvR6MyE1SchaNj0SFxsakiZgxIyrMk=
41+
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
42+
golang.org/x/net v0.0.0-20220121210141-e204ce36a2ba h1:6u6sik+bn/y7vILcYkK3iwTBWN7WtBvB0+SZswQnbf8=
43+
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0=
44+
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
45+
golang.org/x/tools v0.1.8 h1:P1HhGGuLW4aAclzjtmJdf0mJOjVUZUzOTqkAkWL+l6w=
46+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
47+
gopkg.in/ini.v1 v1.66.3 h1:jRskFVxYaMGAMUbN0UZ7niA9gzL9B49DOqE78vg0k3w=
48+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=

.bingo/variables.env

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ BRA="${GOBIN}/bra-v0.0.0-20200517080246-1e3013ecaff8"
1212

1313
DRONE="${GOBIN}/drone-v1.5.0"
1414

15+
SWAGGER="${GOBIN}/swagger-v0.29.0"
16+
1517
GOLANGCI_LINT="${GOBIN}/golangci-lint-v1.40.1"
1618

1719
WIRE="${GOBIN}/wire-v0.5.0"

Makefile

+5-34
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ GO = go
1313
GO_FILES ?= ./pkg/...
1414
SH_FILES ?= $(shell find ./scripts -name *.sh)
1515
API_DEFINITION_FILES = $(shell find ./pkg/api/docs/definitions -name '*.go' -print)
16-
SWAGGER_TAG ?= latest
1716
GO_BUILD_FLAGS += $(if $(GO_BUILD_DEV),-dev)
1817
GO_BUILD_FLAGS += $(if $(GO_BUILD_TAGS),-build-tags=$(GO_BUILD_TAGS))
1918

@@ -37,50 +36,22 @@ SPEC_TARGET = public/api-spec.json
3736
MERGED_SPEC_TARGET := public/api-merged.json
3837
NGALERT_SPEC_TARGET = pkg/services/ngalert/api/tooling/api.json
3938

40-
$(SPEC_TARGET): $(API_DEFINITION_FILES) ## Generate API spec
41-
docker run --rm -it \
42-
-e GOPATH=${HOME}/go:/go \
43-
-e SWAGGER_GENERATE_EXTENSION=false \
44-
-v ${HOME}/go:/go \
45-
-v $$(pwd):/grafana \
46-
-v $$(pwd)/../grafana-enterprise:$$(pwd)/../grafana-enterprise \
47-
-w $$(pwd)/pkg/api/docs quay.io/goswagger/swagger:$(SWAGGER_TAG) \
48-
generate spec -m -o /grafana/public/api-spec.json \
49-
-w /grafana/pkg/server \
50-
-x "grafana/grafana/pkg/services/ngalert/api/tooling/definitions" \
51-
-x "github.com/prometheus/alertmanager" \
52-
-i /grafana/pkg/api/docs/tags.json
53-
54-
swagger-api-spec: gen-go $(SPEC_TARGET) $(MERGED_SPEC_TARGET) validate-api-spec
55-
5639
$(NGALERT_SPEC_TARGET):
5740
+$(MAKE) -C pkg/services/ngalert/api/tooling api.json
5841

5942
$(MERGED_SPEC_TARGET): $(SPEC_TARGET) $(NGALERT_SPEC_TARGET) ## Merge generated and ngalert API specs
60-
go run pkg/api/docs/merge/merge_specs.go -o=public/api-merged.json $(<) pkg/services/ngalert/api/tooling/api.json
61-
62-
ensure_go-swagger_mac:
63-
@hash swagger &>/dev/null || (brew tap go-swagger/go-swagger && brew install go-swagger)
43+
go run pkg/api/docs/merge/merge_specs.go -o=$(MERGED_SPEC_TARGET) $(<) $(NGALERT_SPEC_TARGET)
6444

65-
--swagger-api-spec-mac: ensure_go-swagger_mac $(API_DEFINITION_FILES) ## Generate API spec (for M1 Mac)
66-
SWAGGER_GENERATE_EXTENSION=false swagger generate spec -m -w pkg/server -o public/api-spec.json \
45+
--swagger-api-spec: $(API_DEFINITION_FILES) ## Generate API Swagger specification
46+
SWAGGER_GENERATE_EXTENSION=false $(SWAGGER) generate spec -m -w pkg/server -o public/api-spec.json \
6747
-x "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" \
6848
-x "github.com/prometheus/alertmanager" \
6949
-i pkg/api/docs/tags.json
7050

71-
swagger-api-spec-mac: gen-go --swagger-api-spec-mac $(MERGED_SPEC_TARGET) validate-api-spec-mac
51+
swagger-api-spec: gen-go --swagger-api-spec $(MERGED_SPEC_TARGET) validate-api-spec
7252

7353
validate-api-spec: $(MERGED_SPEC_TARGET) ## Validate API spec
74-
docker run --rm -it \
75-
-e GOPATH=${HOME}/go:/go \
76-
-e SWAGGER_GENERATE_EXTENSION=false \
77-
-v ${HOME}/go:/go \
78-
-v $$(pwd):/grafana \
79-
-w $$(pwd)/pkg/api/docs quay.io/goswagger/swagger:$(SWAGGER_TAG) \
80-
validate /grafana/$(<)
81-
82-
validate-api-spec-mac: $(MERGED_SPEC_TARGET) ## Validate API spec
83-
swagger validate $(<)
54+
$(SWAGGER) validate $(<)
8455

8556
clean-api-spec:
8657
rm $(SPEC_TARGET) $(MERGED_SPEC_TARGET)

pkg/services/ngalert/api/tooling/Makefile

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
include ../../../../../.bingo/Variables.mk
2+
13
.DEFAULT_GOAL := openapi
24

35
API_DIR = definitions
@@ -21,21 +23,7 @@ ifneq ($(OS),Windows_NT)
2123
endif
2224

2325
spec.json spec-stable.json: $(GO_PKG_FILES)
24-
# this is slow because this image does not use the cache
25-
# https://github.com/go-swagger/go-swagger/blob/v0.27.0/Dockerfile#L5
26-
docker run --rm -it \
27-
-w /src/$(PATH_DOWN) \
28-
-v $$(pwd)/$(PATH_UP):/src \
29-
--entrypoint sh \
30-
quay.io/goswagger/swagger:$(SWAGGER_TAG) \
31-
-c \
32-
"swagger generate spec -m -o $@; swagger generate spec -m --include-tag=stable -o spec-stable.json"
33-
34-
ensure_go-swagger_mac:
35-
@hash swagger &>/dev/null || (brew tap go-swagger/go-swagger && brew install go-swagger)
36-
37-
spec.json-mac: ensure_go-swagger_mac $(GO_PKG_FILES)
38-
swagger generate spec -m -w $(API_DIR) -o spec.json && swagger generate spec -m --include-tag=stable -o ngalert.json
26+
SWAGGER_GENERATE_EXTENSION=false $(SWAGGER) generate spec -m -w $(API_DIR) -o spec.json && $(SWAGGER) generate spec -m --include-tag=stable -o spec-stable.json
3927

4028
post.json: spec.json
4129
go run cmd/clean-swagger/main.go -if $(<) -of $@

pkg/services/ngalert/api/tooling/api.json

+13-17
Original file line numberDiff line numberDiff line change
@@ -3141,6 +3141,7 @@
31413141
"x-go-package": "github.com/prometheus/alertmanager/timeinterval"
31423142
},
31433143
"URL": {
3144+
"description": "The general form represented is:\n\n[scheme:][//[userinfo@]host][/]path[?query][#fragment]\n\nURLs that do not start with a slash after the scheme are interpreted as:\n\nscheme:opaque[?query][#fragment]\n\nNote that the Path field is stored in decoded form: /%47%6f%2f becomes /Go/.\nA consequence is that it is impossible to tell which slashes in the Path were\nslashes in the raw URL and which were %2f. This distinction is rarely important,\nbut when it is, the code should use RawPath, an optional field which only gets\nset if the default encoding is different from Path.\n\nURL's String method uses the EscapedPath method to obtain the path. See the\nEscapedPath method for more details.",
31443145
"properties": {
31453146
"ForceQuery": {
31463147
"type": "boolean"
@@ -3173,9 +3174,9 @@
31733174
"$ref": "#/definitions/Userinfo"
31743175
}
31753176
},
3176-
"title": "URL is a custom URL type that allows validation at configuration load time.",
3177+
"title": "A URL represents a parsed URL (technically, a URI reference).",
31773178
"type": "object",
3178-
"x-go-package": "github.com/prometheus/common/config"
3179+
"x-go-package": "net/url"
31793180
},
31803181
"Userinfo": {
31813182
"description": "The Userinfo type is an immutable encapsulation of username and\npassword details for a URL. An existing Userinfo value is guaranteed\nto have a username set (potentially empty, as allowed by RFC 2396),\nand optionally a password.",
@@ -3373,6 +3374,7 @@
33733374
"x-go-package": "github.com/prometheus/alertmanager/api/v2/models"
33743375
},
33753376
"alertGroup": {
3377+
"description": "AlertGroup alert group",
33763378
"properties": {
33773379
"alerts": {
33783380
"description": "alerts",
@@ -3394,9 +3396,7 @@
33943396
"labels",
33953397
"receiver"
33963398
],
3397-
"type": "object",
3398-
"x-go-name": "AlertGroup",
3399-
"x-go-package": "github.com/prometheus/alertmanager/api/v2/models"
3399+
"type": "object"
34003400
},
34013401
"alertGroups": {
34023402
"items": {
@@ -3524,6 +3524,7 @@
35243524
"$ref": "#/definitions/Duration"
35253525
},
35263526
"gettableAlert": {
3527+
"description": "GettableAlert gettable alert",
35273528
"properties": {
35283529
"annotations": {
35293530
"$ref": "#/definitions/labelSet"
@@ -3582,17 +3583,14 @@
35823583
"status",
35833584
"updatedAt"
35843585
],
3585-
"type": "object",
3586-
"x-go-name": "GettableAlert",
3587-
"x-go-package": "github.com/prometheus/alertmanager/api/v2/models"
3586+
"type": "object"
35883587
},
35893588
"gettableAlerts": {
3589+
"description": "GettableAlerts gettable alerts",
35903590
"items": {
35913591
"$ref": "#/definitions/gettableAlert"
35923592
},
3593-
"type": "array",
3594-
"x-go-name": "GettableAlerts",
3595-
"x-go-package": "github.com/prometheus/alertmanager/api/v2/models"
3593+
"type": "array"
35963594
},
35973595
"gettableSilence": {
35983596
"description": "GettableSilence gettable silence",
@@ -3783,6 +3781,7 @@
37833781
"x-go-package": "github.com/prometheus/alertmanager/api/v2/models"
37843782
},
37853783
"postableSilence": {
3784+
"description": "PostableSilence postable silence",
37863785
"properties": {
37873786
"comment": {
37883787
"description": "comment",
@@ -3822,11 +3821,10 @@
38223821
"matchers",
38233822
"startsAt"
38243823
],
3825-
"type": "object",
3826-
"x-go-name": "PostableSilence",
3827-
"x-go-package": "github.com/prometheus/alertmanager/api/v2/models"
3824+
"type": "object"
38283825
},
38293826
"receiver": {
3827+
"description": "Receiver receiver",
38303828
"properties": {
38313829
"name": {
38323830
"description": "name",
@@ -3837,9 +3835,7 @@
38373835
"required": [
38383836
"name"
38393837
],
3840-
"type": "object",
3841-
"x-go-name": "Receiver",
3842-
"x-go-package": "github.com/prometheus/alertmanager/api/v2/models"
3838+
"type": "object"
38433839
},
38443840
"silence": {
38453841
"description": "Silence silence",

0 commit comments

Comments
 (0)