Files
grafana/pkg/services/ngalert/api/tooling/Makefile
Joe Blubaugh 689ae96a0e Alerting: Refactor API types generation with different names. (#51785)
This changes the API codegen template (controller-api.mustache) to simplify some names. When this package was created, most APIs "forked" to either a Grafana backend implementation or a "Lotex" remote implementation. As we have added APIs it's no longer the case. Provisioning, configuration, and testing APIs do not fork, and we are likely to add additional APIs that don't fork.

This change replaces {{classname}}ForkingService with {{classname}} for interface names, and names the concrete implementation {{classname}}Handler. It changes the implied implementation of a route handler from fork{{nickname}} to handle{{nickname}}. So PrometheusApiForkingService becomes PrometheusApi, ForkedPrometheusApi becomes PrometheusApiHandler and forkRouteGetGrafanaAlertStatuses becomes handleRouteGetGrafanaAlertStatuses

It also renames some files - APIs that do no forking go from forked_{{name}}.go to {{name}}.go and APIs that still fork go from forked_{{name}}.go to forking_{{name}}.go to capture the idea that those files a "doing forking" rather than "are a fork of something."

Signed-off-by: Joe Blubaugh <joe.blubaugh@grafana.com>
2022-07-18 03:08:08 -04:00

71 lines
2.1 KiB
Makefile

include ../../../../../.bingo/Variables.mk
.DEFAULT_GOAL := openapi
API_DIR = definitions
GO_PKG_FILES = $(shell find $(API_DIR) -name *.go -print)
PATH_DOWN = pkg/services/ngalert/api/tooling
PATH_UP = ../../../../..
.DEFAULT_GOAL := all
GENERATED_GO_MATCHERS = ./go/*.go
SED_INPLACE := -i
ifneq ($(OS),Windows_NT)
UNAME := $(shell uname)
ifeq ($(UNAME),Darwin)
SED_INPLACE = -i ''
endif
endif
spec.json spec-stable.json: $(GO_PKG_FILES) $(SWAGGER)
SWAGGER_GENERATE_EXTENSION=false $(SWAGGER) generate spec -m -w $(API_DIR) -o spec.json && SWAGGER_GENERATE_EXTENSION=false $(SWAGGER) generate spec -m --include-tag=stable -o spec-stable.json
post.json: spec.json
go run cmd/clean-swagger/main.go -if $(<) -of $@
api.json: spec-stable.json
go run cmd/clean-swagger/main.go -if $(<) -of $@
validate-stable: spec-stable.json $(SWAGGER)
$(SWAGGER) validate $(<)
validate: spec.json $(SWAGGER)
$(SWAGGER) validate $(<)
swagger-codegen-api:
docker run --rm -v $$(pwd):/local --user $$(id -u):$$(id -g) parsertongue/swagger-codegen-cli:3.0.32 generate \
-i /local/post.json \
-l go-server \
-Dapis \
-o /local \
--additional-properties packageName=api \
-t /local/swagger-codegen/templates \
# --import-mappings eval.RelativeTimeRange="github.com/grafana/grafana/pkg/services/ngalert/eval" \
# --type-mappings RelativeTimeRange=eval.RelativeTimeRange
copy-files:
ls -1 go | xargs -n 1 -I {} mv go/{} ../generated_base_{}
fix:
sed $(SED_INPLACE) -e 's/apimodels\.\[\]PostableAlert/apimodels.PostableAlerts/' $(GENERATED_GO_MATCHERS)
sed $(SED_INPLACE) -e 's/apimodels\.\[\]UpdateDashboardAclCommand/apimodels.Permissions/' $(GENERATED_GO_MATCHERS)
sed $(SED_INPLACE) -e 's/apimodels\.\[\]PostableApiReceiver/apimodels.TestReceiversConfigParams/' $(GENERATED_GO_MATCHERS)
goimports -w -v $(GENERATED_GO_MATCHERS)
clean:
rm -rf ./go
serve: post.json
docker run --rm -p 80:8080 -v $$(pwd):/tmp -e SWAGGER_FILE=/tmp/$(<) swaggerapi/swagger-editor
serve-stable: api.json
docker run --rm -p 80:8080 -v $$(pwd):/tmp -e SWAGGER_FILE=/tmp/$(<) swaggerapi/swagger-editor
gen: swagger-codegen-api fix copy-files clean
all: post.json api.json gen