Skip to content

Commit 831ac4f

Browse files
committed
MAJOR: makefile: add command to generate swagger generated files
with `make swagger` its now possible to generate swagger read only files. swagger version is determined and pulled from client-native project that also holds models for dataplaneapi. this directive also automaticly handle replace directives in go.mod file. if different client-native is used, it uses that repository for downloading correct specification, both for github and gitlab repositories. also if correct swagger and jq is installed, there is an option to run script directly on machine
1 parent 562fc00 commit 831ac4f

File tree

4 files changed

+132
-44
lines changed

4 files changed

+132
-44
lines changed

CONTRIBUTING.md

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,11 @@ This document is a short guide for contributing to this project.
99
Data Plane API is generated using [go-swagger](https://github.com/go-swagger/go-swagger) from the swagger spec found [here](https://github.com/haproxytech/client-native/blob/master/specification/build/haproxy_spec.yaml) using the following command.
1010

1111
```
12-
./swagger generate server -f haproxy_spec.yaml \
13-
-A "Data Plane" \
14-
-t $GOPATH/src/github.com/haproxytech/ \
15-
--existing-models github.com/haproxytech/client-native/v2/models \
16-
--exclude-main \
17-
--skip-models \
18-
-s dataplaneapi \
19-
--tags=Discovery \
20-
--tags=ServiceDiscovery \
21-
--tags=Information \
22-
--tags=Specification \
23-
--tags=SpecificationOpenapiv3 \
24-
--tags=Transactions \
25-
--tags=Sites \
26-
--tags=Stats \
27-
--tags=Global \
28-
--tags=Frontend \
29-
--tags=Backend \
30-
--tags=Bind \
31-
--tags=Server \
32-
--tags=Configuration \
33-
--tags=HTTPRequestRule \
34-
--tags=HTTPResponseRule \
35-
--tags=BackendSwitchingRule \
36-
--tags=ServerSwitchingRule \
37-
--tags=TCPResponseRule \
38-
--tags=TCPRequestRule \
39-
--tags=Filter \
40-
--tags=StickRule \
41-
--tags=LogTarget \
42-
--tags=Reloads \
43-
--tags=ACL \
44-
--tags=Defaults \
45-
--tags=StickTable \
46-
--tags=Maps \
47-
--tags=Nameserver \
48-
--tags=Cluster \
49-
--tags=Peer \
50-
--tags=PeerEntry \
51-
--tags=Resolver \
52-
--tags=Spoe \
53-
--tags=SpoeTransactions \
54-
--tags=Storage \
55-
-r ~/dataplaneapi-specification/copyright.txt
12+
make swagger
13+
```
14+
or if you prefer to run it directly (not in docker)
15+
```
16+
make swagger-native
5617
```
5718

5819
This command generates some of the files in this project, which are marked with // Code generated by go-swagger; DO NOT EDIT.

Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ GIT_TAG_COMMIT=$(shell git rev-parse --short ${GIT_LAST_TAG})
66
GIT_MODIFIED1=$(shell git diff "${GIT_HEAD_COMMIT}" "${GIT_TAG_COMMIT}" --quiet || echo .dev)
77
GIT_MODIFIED2=$(shell git diff --quiet || echo .dirty)
88
GIT_MODIFIED=${GIT_MODIFIED1}${GIT_MODIFIED2}
9+
SWAGGER_VERSION=${shell curl -s https://raw.githubusercontent.com/haproxytech/client-native/master/Makefile | grep SWAGGER_VERSION -m 1 | awk -F"=" '{print $$2}'}
910
BUILD_DATE=$(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
1011
CGO_ENABLED?=0
1112

@@ -25,3 +26,16 @@ build:
2526
.PHONY: e2e
2627
e2e: build
2728
TESTNAME=$(TESTNAME) TESTNUMBER=$(TESTNUMBER) TESTDESCRIPTION="$(TESTDESCRIPTION)" SKIP_CLEANUP=$(SKIP_CLEANUP) PREWIPE=$(PREWIPE) HAPROXY_VERSION=$(HAPROXY_VERSION) ./e2e/run.bash
29+
30+
.PHONY: generate
31+
generate:
32+
cd generate/swagger;docker build \
33+
--build-arg SWAGGER_VERSION=${SWAGGER_VERSION} \
34+
--build-arg UID=$(shell id -u) \
35+
--build-arg GID=$(shell id -g) \
36+
-t dataplaneapi-swagger-gen .
37+
docker run --rm -it -v "$(PWD)":/data dataplaneapi-swagger-gen
38+
39+
.PHONY: generate-native
40+
generate-native:
41+
generate/swagger/script.sh

generate/swagger/Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ARG SWAGGER_VERSION
2+
FROM quay.io/goswagger/swagger:$SWAGGER_VERSION
3+
4+
WORKDIR /data
5+
ARG UID
6+
ARG GID
7+
COPY script.sh /generate/swagger/script.sh
8+
VOLUME ["/data"]
9+
10+
RUN apk add bash jq && addgroup -g "$GID" -S docker && adduser -u "$UID" -S user -G docker && \
11+
chmod +x /generate/swagger/script.sh && \
12+
chown -R "${UID}:${GID}" /data
13+
14+
USER user
15+
ENTRYPOINT ["/generate/swagger/script.sh"]

generate/swagger/script.sh

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/bin/bash
2+
3+
set -e
4+
SPEC_DIR=$(mktemp -d)
5+
echo " ---> source folder: $SPEC_DIR"
6+
DST_DIR=$(mktemp -d)
7+
echo " ---> generate folder: $DST_DIR"
8+
# see if we have a replace directive
9+
CN_VERSION=$(go mod edit -json | jq -c -r '.Replace | .[] | select(.Old.Path | contains("github.com/haproxytech/client-native/v2")) | .New.Version' 2>/dev/null | awk -F"-" '{print $NF}') || ""
10+
# if hash is to short take all of it (example v1.0.0-dev1)
11+
[ "${#CN_VERSION}" -gt 0 ] && [ "${#CN_VERSION}" -lt 4 ] && CN_VERSION=$(go mod edit -json | jq -c -r '.Replace | .[] | select(.Old.Path | contains("github.com/haproxytech/client-native/v2")) | .New.Version')
12+
# check if version is ther, if not, use one from require
13+
[ -z "$CN_VERSION" ] && CN_VERSION=$(go mod edit -json | jq -c -r '.Require | .[] | select(.Path | contains("github.com/haproxytech/client-native/v2")) | .Version' 2>/dev/null | awk -F"-" '{print $NF}')
14+
echo " ---> version of client native used: $CN_VERSION"
15+
16+
# extract repository
17+
REPO_PATH=$(go mod edit -json | jq -r '.Replace | .[] | select(.Old.Path | contains("github.com/haproxytech/client-native/v2")) | .New.Path' 2>/dev/null | awk -F"/" '{print $2 "/" $3}') || ""
18+
[ -z "$REPO_PATH" ] && REPO_PATH=haproxytech/client-native
19+
20+
# extract url, gitlab and github have different urls to raw content
21+
URL_PATH=$(go mod edit -json | jq -r '.Replace | .[] | select(.Old.Path | contains("github.com/haproxytech/client-native/v2")) | .New.Path' 2>/dev/null | awk -F"/" '{print $1}') || ""
22+
EXTRA_PATH=""
23+
if [[ $URL_PATH =~ "gitlab" ]]; then
24+
EXTRA_PATH="-/raw/"
25+
else
26+
URL_PATH=raw.githubusercontent.com
27+
fi
28+
29+
echo " ---> URL path: $URL_PATH"
30+
echo " ---> repository path: $REPO_PATH"
31+
SPEC_URL=https://$URL_PATH/$REPO_PATH/$EXTRA_PATH$CN_VERSION/specification
32+
echo " ---> fetching specification: $SPEC_URL/build/haproxy_spec.yaml"
33+
wget -q -O $SPEC_DIR/haproxy_spec.yaml $SPEC_URL/build/haproxy_spec.yaml
34+
echo " ---> fetching copyright: $SPEC_URL/copyright.txt"
35+
wget -q -O $SPEC_DIR/copyright.txt $SPEC_URL/copyright.txt
36+
37+
echo "module github.com/haproxytech" > $DST_DIR/go.mod
38+
mkdir -p $DST_DIR/dataplaneapi/operations
39+
cp configure_data_plane.go $DST_DIR/dataplaneapi/configure_data_plane.go
40+
41+
swagger generate server -f $SPEC_DIR/haproxy_spec.yaml \
42+
-A "Data Plane" \
43+
-t $DST_DIR \
44+
--existing-models github.com/haproxytech/client-native/v2/models \
45+
--exclude-main \
46+
--skip-models \
47+
-s dataplaneapi \
48+
--tags=Discovery \
49+
--tags=ServiceDiscovery \
50+
--tags=Information \
51+
--tags=Specification \
52+
--tags=SpecificationOpenapiv3 \
53+
--tags=Transactions \
54+
--tags=Sites \
55+
--tags=Stats \
56+
--tags=Global \
57+
--tags=Frontend \
58+
--tags=Backend \
59+
--tags=Bind \
60+
--tags=Server \
61+
--tags=Configuration \
62+
--tags=HTTPRequestRule \
63+
--tags=HTTPResponseRule \
64+
--tags=BackendSwitchingRule \
65+
--tags=ServerSwitchingRule \
66+
--tags=TCPResponseRule \
67+
--tags=TCPRequestRule \
68+
--tags=Filter \
69+
--tags=StickRule \
70+
--tags=LogTarget \
71+
--tags=Reloads \
72+
--tags=ACL \
73+
--tags=Defaults \
74+
--tags=StickTable \
75+
--tags=Maps \
76+
--tags=Nameserver \
77+
--tags=Cluster \
78+
--tags=Peer \
79+
--tags=PeerEntry \
80+
--tags=Resolver \
81+
--tags=Spoe \
82+
--tags=SpoeTransactions \
83+
--tags=Storage \
84+
--tags="ACL Runtime" \
85+
--tags=ServerTemplate \
86+
-r $SPEC_DIR/copyright.txt
87+
88+
echo " ---> removing doc.go"
89+
rm doc.go
90+
echo " ---> removing embedded_spec.go"
91+
rm embedded_spec.go
92+
echo " ---> removing server.go"
93+
rm server.go
94+
echo " ---> removing operations/*"
95+
rm -rf operations/*
96+
97+
echo " ---> copy generated files to destination"
98+
cp -a $DST_DIR/dataplaneapi/. .

0 commit comments

Comments
 (0)