Skip to content

Commit c2fc776

Browse files
authored
[FAB-18315] Ch.Part.API: Create Swagger definition for API (hyperledger#2113)
* [FAB-18315] Ch.Part.API: Create Swagger spec for API - Adds swagger specification for Ch.Part.API - Adds swagger annotations to Channel Particiaption endpoints - Adds a script to generate swagger specification Signed-off-by: Tiffany Harris <[email protected]> * [FAB-18315] Add operations endpoint to swagger spec - Updates swagger spec to be generalized for fabric with each server separated into groups (with swagger tags) - Add check-swagger to basic-checks Signed-off-by: Tiffany Harris <[email protected]> Signed-off-by: Brett Logan <[email protected]> * [FAB-18315] Cleanup swagger annotations Signed-off-by: Tiffany Harris <[email protected]>
1 parent 4220751 commit c2fc776

File tree

13 files changed

+1123
-5
lines changed

13 files changed

+1123
-5
lines changed

Makefile

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ all: check-go-version native docker checks
106106
checks: basic-checks unit-test integration-test
107107

108108
.PHONY: basic-checks
109-
basic-checks: check-go-version license spelling references trailing-spaces linter check-metrics-doc filename-spaces
109+
basic-checks: check-go-version license spelling references trailing-spaces linter check-metrics-doc filename-spaces check-swagger
110110

111111
.PHONY: desk-checks
112112
desk-check: checks verify
@@ -193,6 +193,16 @@ generate-metrics-doc:
193193
@echo "Generating metrics reference documentation..."
194194
./scripts/metrics_doc.sh generate
195195

196+
.PHONY: check-swagger
197+
check-swagger: gotool.swagger
198+
@echo "SWAGGER: Checking for outdated swagger..."
199+
./scripts/swagger.sh check
200+
201+
.PHONY: generate-swagger
202+
generate-swagger: gotool.swagger
203+
@echo "Generating swagger..."
204+
./scripts/swagger.sh generate
205+
196206
.PHONY: protos
197207
protos: gotool.protoc-gen-go
198208
@echo "Compiling non-API protos..."

common/flogging/httpadmin/spec.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Logging interface {
2121
Spec() string
2222
}
2323

24+
// swagger:model spec
2425
type LogSpec struct {
2526
Spec string `json:"spec,omitempty"`
2627
}

core/operations/system.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ func (s *System) initializeMetricsProvider() error {
131131
case "prometheus":
132132
s.Provider = &prometheus.Provider{}
133133
s.versionGauge = versionGauge(s.Provider)
134+
// swagger:operation GET /metrics operations metrics
135+
// ---
136+
// responses:
137+
// '200':
138+
// description: Ok.
134139
s.RegisterHandler("/metrics", promhttp.Handler(), s.options.TLS.Enabled)
135140
return nil
136141

@@ -146,11 +151,43 @@ func (s *System) initializeMetricsProvider() error {
146151
}
147152

148153
func (s *System) initializeLoggingHandler() {
154+
// swagger:operation GET /logspec operations logspecget
155+
// ---
156+
// summary: Retrieves the active logging spec for a peer or orderer.
157+
// responses:
158+
// '200':
159+
// description: Ok.
160+
161+
// swagger:operation PUT /logspec operations logspecput
162+
// ---
163+
// summary: Updates the active logging spec for a peer or orderer.
164+
//
165+
// parameters:
166+
// - name: payload
167+
// in: formData
168+
// type: string
169+
// description: The payload must consist of a single attribute named spec.
170+
// required: true
171+
// responses:
172+
// '204':
173+
// description: No content.
174+
// '400':
175+
// description: Bad request.
176+
// consumes:
177+
// - multipart/form-data
149178
s.RegisterHandler("/logspec", httpadmin.NewSpecHandler(), s.options.TLS.Enabled)
150179
}
151180

152181
func (s *System) initializeHealthCheckHandler() {
153182
s.healthHandler = healthz.NewHealthHandler()
183+
// swagger:operation GET /healthz operations healthz
184+
// ---
185+
// summary: Retrieves all registered health checkers for the process.
186+
// responses:
187+
// '200':
188+
// description: Ok.
189+
// '503':
190+
// description: Service unavailable.
154191
s.RegisterHandler("/healthz", s.healthHandler, false)
155192
}
156193

@@ -159,6 +196,12 @@ func (s *System) initializeVersionInfoHandler() {
159196
CommitSHA: metadata.CommitSHA,
160197
Version: metadata.Version,
161198
}
199+
// swagger:operation GET /version operations version
200+
// ---
201+
// summary: Returns the orderer or peer version and the commit SHA on which the release was created.
202+
// responses:
203+
// '200':
204+
// description: Ok.
162205
s.RegisterHandler("/version", versionInfo, false)
163206
}
164207

gotools.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# SPDX-License-Identifier: Apache-2.0
55

6-
GOTOOLS = counterfeiter ginkgo gocov gocov-xml goimports golint misspell mockery protoc-gen-go
6+
GOTOOLS = counterfeiter ginkgo gocov gocov-xml goimports golint misspell mockery protoc-gen-go swagger
77
BUILD_DIR ?= build
88
GOTOOLS_BINDIR ?= $(shell go env GOPATH)/bin
99

@@ -17,6 +17,7 @@ go.fqp.golint := golang.org/x/lint/golint
1717
go.fqp.misspell := github.com/client9/misspell/cmd/misspell
1818
go.fqp.mockery := github.com/vektra/mockery/cmd/mockery
1919
go.fqp.protoc-gen-go := github.com/golang/protobuf/protoc-gen-go
20+
go.fqp.swagger := github.com/go-swagger/go-swagger/cmd/swagger
2021

2122
.PHONY: gotools-install
2223
gotools-install: $(patsubst %,$(GOTOOLS_BINDIR)/%, $(GOTOOLS))

orderer/common/channelparticipation/restapi.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,109 @@ func NewHTTPHandler(config localconfig.ChannelParticipation, registrar ChannelMa
7070
router: mux.NewRouter(),
7171
}
7272

73+
// swagger:operation GET /v1/participation/channels/{channelID} channels listChannel
74+
// ---
75+
// summary: Returns detailed channel information for a specific channel Ordering Service Node (OSN) has joined.
76+
// parameters:
77+
// - name: channelID
78+
// in: path
79+
// description: Channel ID
80+
// required: true
81+
// type: string
82+
// responses:
83+
// '200':
84+
// description: Successfully retrieved channel.
85+
// schema:
86+
// "$ref": "#/definitions/channelInfo"
87+
// headers:
88+
// Content-Type:
89+
// description: The media type of the resource
90+
// type: string
91+
// Cache-Control:
92+
// description: The directives for caching responses
93+
// type: string
94+
7395
handler.router.HandleFunc(urlWithChannelIDKey, handler.serveListOne).Methods(http.MethodGet)
7496

97+
// swagger:operation DELETE /v1/participation/channels/{channelID} channels removeChannel
98+
// ---
99+
// summary: Removes an Ordering Service Node (OSN) from a channel.
100+
// parameters:
101+
// - name: channelID
102+
// in: path
103+
// description: Channel ID
104+
// required: true
105+
// type: string
106+
// responses:
107+
// '204':
108+
// description: Successfully removed channel.
109+
// '400':
110+
// description: Bad request.
111+
// '404':
112+
// description: The channel does not exist.
113+
// '405':
114+
// description: The system channel exists, removal is not allowed.
115+
// '409':
116+
// description: The channel is pending removal.
117+
75118
handler.router.HandleFunc(urlWithChannelIDKey, handler.serveRemove).Methods(http.MethodDelete)
76119
handler.router.HandleFunc(urlWithChannelIDKey, handler.serveNotAllowed)
77120

121+
// swagger:operation GET /v1/participation/channels channels listChannels
122+
// ---
123+
// summary: Returns the complete list of channels an Ordering Service Node (OSN) has joined.
124+
// responses:
125+
// '200':
126+
// description: Successfully retrieved channels.
127+
// schema:
128+
// "$ref": "#/definitions/channelList"
129+
// headers:
130+
// Content-Type:
131+
// description: The media type of the resource
132+
// type: string
133+
// Cache-Control:
134+
// description: The directives for caching responses
135+
// type: string
136+
78137
handler.router.HandleFunc(URLBaseV1Channels, handler.serveListAll).Methods(http.MethodGet)
79138

139+
// swagger:operation POST /v1/participation/channels channels joinChannel
140+
// ---
141+
// summary: Joins an Ordering Service Node (OSN) to a channel.
142+
// description: If a channel does not yet exist, it will be created.
143+
// parameters:
144+
// - name: configBlock
145+
// in: formData
146+
// type: string
147+
// required: true
148+
// responses:
149+
// '201':
150+
// description: Successfully joined channel.
151+
// schema:
152+
// "$ref": "#/definitions/channelInfo"
153+
// headers:
154+
// Content-Type:
155+
// description: The media type of the resource
156+
// type: string
157+
// Location:
158+
// description: The URL to redirect a page to
159+
// type: string
160+
// '400':
161+
// description: Cannot join channel.
162+
// '403':
163+
// description: The client is trying to join the system-channel that does not exist, but application channels exist.
164+
// '405':
165+
// description: |
166+
// The client is trying to join an app-channel, but the system channel exists.
167+
// The client is trying to join an app-channel that exists, but the system channel does not.
168+
// The client is trying to join the system-channel, and it exists.
169+
// '409':
170+
// description: The client is trying to join a channel that is currently being removed.
171+
// '500':
172+
// description: Removal of channel failed.
173+
// consumes:
174+
// - multipart/form-data
175+
80176
handler.router.HandleFunc(URLBaseV1Channels, handler.serveJoin).Methods(http.MethodPost).HeadersRegexp(
81177
"Content-Type", "multipart/form-data*")
82178
handler.router.HandleFunc(URLBaseV1Channels, handler.serveBadContentType).Methods(http.MethodPost)

orderer/common/types/channelinfo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type ErrorResponse struct {
1414

1515
// ChannelList carries the response to an HTTP request to List all the channels.
1616
// This is marshaled into the body of the HTTP response.
17+
// swagger:model channelList
1718
type ChannelList struct {
1819
// The system channel info, nil if it doesn't exist.
1920
SystemChannel *ChannelInfoShort `json:"systemChannel"`
@@ -65,6 +66,7 @@ const (
6566

6667
// ChannelInfo carries the response to an HTTP request to List a single channel.
6768
// This is marshaled into the body of the HTTP response.
69+
// swagger:model channelInfo
6870
type ChannelInfo struct {
6971
// The channel name.
7072
Name string `json:"name"`

scripts/swagger.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash -e
2+
3+
# Copyright IBM Corp All Rights Reserved.
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
fabric_dir="$(cd "$(dirname "$0")/.." && pwd)"
8+
swagger_tags="${fabric_dir}/swagger/tags.json"
9+
swagger_doc="${fabric_dir}/swagger/swagger-fabric.json"
10+
11+
check_spec() {
12+
swagger_doc_check="${fabric_dir}/swagger/swagger-fabric-check.json"
13+
swagger generate spec -o "$swagger_doc_check" --scan-models --exclude-deps --input "$swagger_tags"
14+
if [ -n "$(diff "$swagger_doc_check" "$swagger_doc")" ]; then
15+
echo "The Fabric swagger is out of date."
16+
echo "Please run '$0 generate' to update the swagger."
17+
rm "$swagger_doc_check"
18+
exit 1
19+
fi
20+
rm "$swagger_doc_check"
21+
}
22+
23+
case "$1" in
24+
# check if the swagger is up to date with the swagger
25+
# options in the tree
26+
"check")
27+
check_spec
28+
;;
29+
30+
# generate the swagger
31+
"generate")
32+
swagger generate spec -o "$swagger_doc" --scan-models --exclude-deps --input "$swagger_tags"
33+
;;
34+
35+
*)
36+
echo "Please specify check or generate"
37+
exit 1
38+
;;
39+
esac
40+

swagger/docs.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// Fabric API
8+
//
9+
// Hyperledger Fabric is an enterprise-grade permissioned distributed ledger framework for developing
10+
// solutions and applications. Its modular and versatile design satisfies a broad range of industry use
11+
// cases. It offers a unique approach to consensus that enables performance at scale while preserving privacy.
12+
//
13+
// Schemes: http, https
14+
// Version: 2.3
15+
// License: Copyright IBM Corp. All Rights Reserved.
16+
//
17+
// Consumes:
18+
// - application/json
19+
//
20+
// Produces:
21+
// - application/json
22+
//
23+
// swagger:meta
24+
package docs

0 commit comments

Comments
 (0)