Skip to content

Commit dce74de

Browse files
authored
Merge pull request #393 from sapcc/enable_revive
enable revive and fix docstring lints
2 parents f5aae6d + 8835711 commit dce74de

16 files changed

Lines changed: 71 additions & 29 deletions

.envrc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
# SPDX-FileCopyrightText: 2019–2020 Target
33
# SPDX-FileCopyrightText: 2021 The Nix Community
44
# SPDX-License-Identifier: Apache-2.0
5-
65
export GO_TESTENV=CASTELLUM_AUDIT_SILENT=true
7-
86
if type -P lorri &>/dev/null; then
97
eval "$(lorri direnv)"
108
elif type -P nix &>/dev/null; then

.golangci.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ linters:
6363
- perfsprint
6464
- predeclared
6565
- rowserrcheck
66+
- revive
6667
- sqlclosecheck
6768
- staticcheck
6869
- unconvert
@@ -161,11 +162,18 @@ linters:
161162
perfsprint:
162163
# modernize generates nicer fix code
163164
concat-loop: false
165+
revive:
166+
rules:
167+
- name: exported
168+
arguments:
169+
- checkPrivateReceivers
170+
- disableChecksOnConstants
164171
staticcheck:
165172
dot-import-whitelist:
166173
- github.com/majewsky/gg/option
167174
- github.com/onsi/ginkgo/v2
168175
- github.com/onsi/gomega
176+
- go.xyrillian.de/gg/option
169177
usestdlibvars:
170178
http-method: true
171179
http-status-code: true

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ prepare-static-check: FORCE install-goimports install-golangci-lint install-shel
6868
# To add additional flags or values (before the default ones), specify the variable in the environment, e.g. `GO_BUILDFLAGS='-tags experimental' make`.
6969
# To override the default flags or values, specify the variable on the command line, e.g. `make GO_BUILDFLAGS='-tags experimental'`.
7070
GO_BUILDFLAGS += -mod vendor
71-
GO_LDFLAGS +=
72-
GO_TESTFLAGS +=
73-
GO_TESTENV += CASTELLUM_AUDIT_SILENT=true
74-
GO_BUILDENV +=
71+
GO_LDFLAGS +=
72+
GO_TESTFLAGS +=
73+
GO_TESTENV += CASTELLUM_AUDIT_SILENT=true
74+
GO_BUILDENV +=
7575

7676
# These definitions are overridable, e.g. to provide fixed version/commit values when
7777
# no .git directory is present or to provide a fixed build date for reproducibility.

Makefile.maker.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ golang:
2626

2727
golangciLint:
2828
createConfig: true
29+
reviveRules:
30+
- name: exported
31+
arguments:
32+
- checkPrivateReceivers
33+
- disableChecksOnConstants
2934

3035
githubWorkflow:
3136
ci:

internal/api/api.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type handler struct {
3939
TimeNow func() time.Time
4040
}
4141

42-
// NewAPI constructs the main httpapi.API for this package.
42+
// NewHandler constructs the main httpapi.API for this package.
4343
func NewHandler(cfg core.Config, dbi *gorp.DbMap, team core.AssetManagerTeam, validator gopherpolicy.Validator, provider core.ProviderClient, auditor audittools.Auditor, timeNow func() time.Time) httpapi.API {
4444
return &handler{Config: cfg, DB: dbi, Team: team, Validator: validator, Provider: provider, Auditor: auditor, TimeNow: timeNow}
4545
}
@@ -114,6 +114,9 @@ func RequireJSON(w http.ResponseWriter, r *http.Request, data any) bool {
114114
return true
115115
}
116116

117+
// CheckToken evaluates whether the Openstack access token fits to the scope specified
118+
// in the requests' variables or query arguments and returns the projectUUID and a token
119+
// when the validation was successful.
117120
func (h handler) CheckToken(w http.ResponseWriter, r *http.Request) (string, *gopherpolicy.Token) {
118121
// for endpoints requiring the `project_id` variable, check that it's not empty
119122
projectUUID, projectScoped := mux.Vars(r)["project_id"]
@@ -149,6 +152,8 @@ func (h handler) CheckToken(w http.ResponseWriter, r *http.Request) (string, *go
149152
return projectUUID, token
150153
}
151154

155+
// SetTokenToProjectScope sets the tokens' request context to the specific projects
156+
// information when the project exists and if the project exists and any possible errors.
152157
func (h handler) SetTokenToProjectScope(ctx context.Context, token *gopherpolicy.Token, projectUUID string) (projectExists bool, err error) {
153158
objectAttrs := map[string]string{
154159
"project_id": projectUUID,
@@ -180,6 +185,9 @@ func (h handler) SetTokenToProjectScope(ctx context.Context, token *gopherpolicy
180185
return projectExists, nil
181186
}
182187

188+
// LoadResource loads the requested db.Resource and returns it.
189+
// If the process fails, an error is written to the response and nil is returned.
190+
// If createIfMissing is true, a new db.Resource will be created.a
183191
func (h handler) LoadResource(w http.ResponseWriter, r *http.Request, projectUUID string, token *gopherpolicy.Token, createIfMissing bool) *db.Resource {
184192
assetType := db.AssetType(mux.Vars(r)["asset_type"])
185193
if assetType == "" {

internal/api/assets.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func FinishedOperationFromDB(dbOp db.FinishedOperation, assetID string, res *db.
112112
////////////////////////////////////////////////////////////////////////////////
113113
// HTTP handlers
114114

115+
// GetAssets handles GET /v1/projects/:id/assets/:type.
115116
func (h handler) GetAssets(w http.ResponseWriter, r *http.Request) {
116117
httpapi.IdentifyEndpoint(r, "/v1/projects/:id/assets/:type")
117118
projectUUID, token := h.CheckToken(w, r)
@@ -142,6 +143,7 @@ func (h handler) GetAssets(w http.ResponseWriter, r *http.Request) {
142143
respondwith.JSON(w, http.StatusOK, result)
143144
}
144145

146+
// GetAsset handles GET /v1/projects/:id/assets/:type/:uuid.
145147
func (h handler) GetAsset(w http.ResponseWriter, r *http.Request) {
146148
httpapi.IdentifyEndpoint(r, "/v1/projects/:id/assets/:type/:uuid")
147149
projectUUID, token := h.CheckToken(w, r)
@@ -209,6 +211,7 @@ var (
209211
`)
210212
)
211213

214+
// PostAssetErrorResolved handles POST /v1/projects/:id/assets/:type/:uuid/error-resolved.
212215
func (h handler) PostAssetErrorResolved(w http.ResponseWriter, r *http.Request) {
213216
httpapi.IdentifyEndpoint(r, "/v1/projects/:id/assets/:type/:uuid/error-resolved")
214217

internal/api/audit.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type scalingEventTarget struct {
1515
resource *castellum.Resource // only used for enable/update action events
1616
}
1717

18+
// Render implements the audittools.Target interface.
1819
func (t scalingEventTarget) Render() cadf.Resource {
1920
result := cadf.Resource{
2021
TypeURI: "data/security/project",

internal/api/errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/sapcc/castellum/internal/db"
1414
)
1515

16+
// GetResourceScrapeErrors handles GET /v1/admin/resource-scrape-errors.
1617
func (h handler) GetResourceScrapeErrors(w http.ResponseWriter, r *http.Request) {
1718
httpapi.IdentifyEndpoint(r, "/v1/admin/resource-scrape-errors")
1819
_, token := h.CheckToken(w, r)
@@ -54,6 +55,7 @@ func (h handler) GetResourceScrapeErrors(w http.ResponseWriter, r *http.Request)
5455
}{resScrapeErrs})
5556
}
5657

58+
// GetAssetScrapeErrors handles GET /v1/admin/asset-scrape-errors.
5759
func (h handler) GetAssetScrapeErrors(w http.ResponseWriter, r *http.Request) {
5860
httpapi.IdentifyEndpoint(r, "/v1/admin/asset-scrape-errors")
5961
_, token := h.CheckToken(w, r)
@@ -108,6 +110,7 @@ func (h handler) GetAssetScrapeErrors(w http.ResponseWriter, r *http.Request) {
108110
}{assetScrapeErrs})
109111
}
110112

113+
// GetAssetResizeErrors handles GET /v1/admin/asset-resize-errors.
111114
func (h handler) GetAssetResizeErrors(w http.ResponseWriter, r *http.Request) {
112115
httpapi.IdentifyEndpoint(r, "/v1/admin/asset-resize-errors")
113116
_, token := h.CheckToken(w, r)

internal/api/operations.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/sapcc/castellum/internal/db"
2222
)
2323

24-
func (h handler) LoadMatchingResources(w http.ResponseWriter, r *http.Request) (map[int64]db.Resource, bool) {
24+
func (h handler) loadMatchingResources(w http.ResponseWriter, r *http.Request) (map[int64]db.Resource, bool) {
2525
// CheckToken discovers project ID in both URL path and query
2626
var token *gopherpolicy.Token
2727
projectUUID, token := h.CheckToken(w, r)
@@ -109,9 +109,10 @@ func (h handler) LoadMatchingResources(w http.ResponseWriter, r *http.Request) (
109109
return allowedResources, true
110110
}
111111

112+
// GetPendingOperations handles GET /v1/operations/pending.
112113
func (h handler) GetPendingOperations(w http.ResponseWriter, r *http.Request) {
113114
httpapi.IdentifyEndpoint(r, "/v1/operations/pending")
114-
dbResources, ok := h.LoadMatchingResources(w, r)
115+
dbResources, ok := h.loadMatchingResources(w, r)
115116
if !ok {
116117
return
117118
}
@@ -163,9 +164,10 @@ func (h handler) getAssetUUIDMap(res db.Resource) (map[int64]string, error) {
163164
return assetUUIDs, err
164165
}
165166

167+
// GetRecentlyFailedOperations handles GET /v1/operations/recently-failed.
166168
func (h handler) GetRecentlyFailedOperations(w http.ResponseWriter, r *http.Request) {
167169
httpapi.IdentifyEndpoint(r, "/v1/operations/recently-failed")
168-
dbResources, ok := h.LoadMatchingResources(w, r)
170+
dbResources, ok := h.loadMatchingResources(w, r)
169171
if !ok {
170172
return
171173
}
@@ -179,7 +181,7 @@ func (h handler) GetRecentlyFailedOperations(w http.ResponseWriter, r *http.Requ
179181
ResourceID: dbResource.ID,
180182
Outcomes: []castellum.OperationOutcome{castellum.OperationOutcomeFailed, castellum.OperationOutcomeErrored},
181183
OverriddenBy: `TRUE`,
182-
}.Execute()
184+
}.execute()
183185
if respondwith.ObfuscatedErrorText(w, err) {
184186
return
185187
}
@@ -207,9 +209,10 @@ func (h handler) GetRecentlyFailedOperations(w http.ResponseWriter, r *http.Requ
207209
}{relevantOps})
208210
}
209211

212+
// GetRecentlySucceededOperations handles GET /v1/operations/recently-succeeded.
210213
func (h handler) GetRecentlySucceededOperations(w http.ResponseWriter, r *http.Request) {
211214
httpapi.IdentifyEndpoint(r, "/v1/operations/recently-succeeded")
212-
dbResources, ok := h.LoadMatchingResources(w, r)
215+
dbResources, ok := h.loadMatchingResources(w, r)
213216
if !ok {
214217
return
215218
}
@@ -228,7 +231,7 @@ func (h handler) GetRecentlySucceededOperations(w http.ResponseWriter, r *http.R
228231
ResourceID: dbResource.ID,
229232
Outcomes: []castellum.OperationOutcome{castellum.OperationOutcomeSucceeded},
230233
OverriddenBy: fmt.Sprintf(`outcome != '%s'`, castellum.OperationOutcomeCancelled),
231-
}.Execute()
234+
}.execute()
232235
if respondwith.ObfuscatedErrorText(w, err) {
233236
return
234237
}
@@ -277,7 +280,7 @@ var recentOperationQueryStr = sqlext.SimplifyWhitespace(`
277280
WHERE a.resource_id = $1 AND o.outcome IN ('%s')
278281
`)
279282

280-
func (q recentOperationQuery) Execute() (map[int64]db.FinishedOperation, error) {
283+
func (q recentOperationQuery) execute() (map[int64]db.FinishedOperation, error) {
281284
outcomes := make([]string, len(q.Outcomes))
282285
for idx, o := range q.Outcomes {
283286
outcomes[idx] = string(o)

internal/api/resources.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func (h handler) ResourceFromDB(res db.Resource) (castellum.Resource, error) {
7676
////////////////////////////////////////////////////////////////////////////////
7777
// HTTP handlers
7878

79+
// GetProject handles GET /v1/projects/:id.
7980
func (h handler) GetProject(w http.ResponseWriter, r *http.Request) {
8081
httpapi.IdentifyEndpoint(r, "/v1/projects/:id")
8182
projectUUID, token := h.CheckToken(w, r)
@@ -112,6 +113,7 @@ func (h handler) GetProject(w http.ResponseWriter, r *http.Request) {
112113
respondwith.JSON(w, http.StatusOK, result)
113114
}
114115

116+
// GetResource handles GET /v1/projects/:id/resources/:type.
115117
func (h handler) GetResource(w http.ResponseWriter, r *http.Request) {
116118
httpapi.IdentifyEndpoint(r, "/v1/projects/:id/resources/:type")
117119
projectUUID, token := h.CheckToken(w, r)
@@ -130,6 +132,7 @@ func (h handler) GetResource(w http.ResponseWriter, r *http.Request) {
130132
respondwith.JSON(w, http.StatusOK, resource)
131133
}
132134

135+
// PutResource handles PUT /v1/projects/:id/resources/:type.
133136
func (h handler) PutResource(w http.ResponseWriter, r *http.Request) {
134137
httpapi.IdentifyEndpoint(r, "/v1/projects/:id/resources/:type")
135138
requestTime := time.Now()
@@ -211,6 +214,7 @@ func (h handler) PutResource(w http.ResponseWriter, r *http.Request) {
211214
w.WriteHeader(http.StatusAccepted)
212215
}
213216

217+
// DeleteResource handles DELETE /v1/projects/:id/resources/:type.
214218
func (h handler) DeleteResource(w http.ResponseWriter, r *http.Request) {
215219
httpapi.IdentifyEndpoint(r, "/v1/projects/:id/resources/:type")
216220
requestTime := time.Now()

0 commit comments

Comments
 (0)