Skip to content

Commit

Permalink
Backport of chore(lint): use Go stdlib variables for HTTP methods and…
Browse files Browse the repository at this point in the history
… status codes into release/1.5.x (#18073)

Co-authored-by: Ville Vesilehto <[email protected]>
Co-authored-by: James Rasell <[email protected]>
  • Loading branch information
3 people authored Jul 26, 2023
1 parent 98bffe7 commit 54a9acd
Show file tree
Hide file tree
Showing 48 changed files with 387 additions and 386 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,6 @@ linters:
# - errchkjson (todo)
# - errorlint (todo)
- exportloopref
- usestdlibvars
fast: false

4 changes: 2 additions & 2 deletions client/allocrunner/checks_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ func allocWithDifferentNomadChecks(id, addr, port string) *structs.Allocation {
var checkHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/fail":
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
_, _ = io.WriteString(w, "500 problem")
case "/hang":
time.Sleep(2 * time.Second)
_, _ = io.WriteString(w, "too slow")
default:
w.WriteHeader(200)
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(w, "200 ok")
}
})
Expand Down
4 changes: 2 additions & 2 deletions client/fingerprint/env_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (f *EnvAzureFingerprint) Get(attribute string, format string) (string, erro
}

req := &http.Request{
Method: "GET",
Method: http.MethodGet,
URL: parsedURL,
Header: http.Header{
"Metadata": []string{"true"},
Expand All @@ -103,7 +103,7 @@ func (f *EnvAzureFingerprint) Get(attribute string, format string) (string, erro
return "", err
}

if res.StatusCode >= 400 {
if res.StatusCode >= http.StatusBadRequest {
return "", ReqError{res.StatusCode}
}

Expand Down
2 changes: 1 addition & 1 deletion client/fingerprint/env_azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func testFingerprint_Azure(t *testing.T, withExternalIp bool) {
}

if !found {
w.WriteHeader(404)
w.WriteHeader(http.StatusNotFound)
}
}))
defer ts.Close()
Expand Down
2 changes: 1 addition & 1 deletion client/fingerprint/env_digitalocean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestFingerprint_DigitalOcean(t *testing.T) {
}

if !found {
w.WriteHeader(404)
w.WriteHeader(http.StatusNotFound)
}
}))
defer ts.Close()
Expand Down
4 changes: 2 additions & 2 deletions client/fingerprint/env_gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (f *EnvGCEFingerprint) Get(attribute string, recursive bool) (string, error
}

req := &http.Request{
Method: "GET",
Method: http.MethodGet,
URL: parsedUrl,
Header: http.Header{
"Metadata-Flavor": []string{"Google"},
Expand All @@ -118,7 +118,7 @@ func (f *EnvGCEFingerprint) Get(attribute string, recursive bool) (string, error
return "", err
}

if res.StatusCode >= 400 {
if res.StatusCode >= http.StatusBadRequest {
return "", ReqError{res.StatusCode}
}

Expand Down
2 changes: 1 addition & 1 deletion client/fingerprint/env_gce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func testFingerprint_GCE(t *testing.T, withExternalIp bool) {
}

if !found {
w.WriteHeader(404)
w.WriteHeader(http.StatusNotFound)
}
}))
defer ts.Close()
Expand Down
4 changes: 2 additions & 2 deletions client/serviceregistration/checks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ func (c *checker) checkHTTP(ctx context.Context, qc *QueryContext, q *Query) *st
qr.StatusCode = result.StatusCode

switch {
case result.StatusCode == 200:
case result.StatusCode == http.StatusOK:
qr.Status = structs.CheckSuccess
qr.Output = "nomad: http ok"
return qr
case result.StatusCode < 400:
case result.StatusCode < http.StatusBadRequest:
qr.Status = structs.CheckSuccess
default:
qr.Status = structs.CheckFailure
Expand Down
8 changes: 4 additions & 4 deletions client/serviceregistration/checks/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ func TestChecker_Do_HTTP(t *testing.T) {

switch r.URL.Path {
case "/fail":
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
_, _ = io.WriteString(w, "500 problem")
case "/hang":
time.Sleep(1 * time.Second)
_, _ = io.WriteString(w, "too slow")
case "/long-fail":
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
_, _ = io.WriteString(w, tooLong)
case "/long-not-fail":
w.WriteHeader(201)
w.WriteHeader(http.StatusCreated)
_, _ = io.WriteString(w, tooLong)
default:
w.WriteHeader(200)
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(w, "200 ok")
}
}))
Expand Down
18 changes: 9 additions & 9 deletions command/agent/acl_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func (s *HTTPServer) ACLPoliciesRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
if req.Method != "GET" {
if req.Method != http.MethodGet {
return nil, CodedError(405, ErrInvalidMethod)
}

Expand All @@ -36,11 +36,11 @@ func (s *HTTPServer) ACLPolicySpecificRequest(resp http.ResponseWriter, req *htt
return nil, CodedError(400, "Missing Policy Name")
}
switch req.Method {
case "GET":
case http.MethodGet:
return s.aclPolicyQuery(resp, req, name)
case "PUT", "POST":
case http.MethodPut, http.MethodPost:
return s.aclPolicyUpdate(resp, req, name)
case "DELETE":
case http.MethodDelete:
return s.aclPolicyDelete(resp, req, name)
default:
return nil, CodedError(405, ErrInvalidMethod)
Expand Down Expand Up @@ -112,7 +112,7 @@ func (s *HTTPServer) aclPolicyDelete(resp http.ResponseWriter, req *http.Request
}

func (s *HTTPServer) ACLTokensRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
if req.Method != "GET" {
if req.Method != http.MethodGet {
return nil, CodedError(405, ErrInvalidMethod)
}

Expand Down Expand Up @@ -184,11 +184,11 @@ func (s *HTTPServer) aclTokenCrud(resp http.ResponseWriter, req *http.Request,
}

switch req.Method {
case "GET":
case http.MethodGet:
return s.aclTokenQuery(resp, req, tokenAccessor)
case "PUT", "POST":
case http.MethodPut, http.MethodPost:
return s.aclTokenUpdate(resp, req, tokenAccessor)
case "DELETE":
case http.MethodDelete:
return s.aclTokenDelete(resp, req, tokenAccessor)
default:
return nil, CodedError(405, ErrInvalidMethod)
Expand Down Expand Up @@ -217,7 +217,7 @@ func (s *HTTPServer) aclTokenQuery(resp http.ResponseWriter, req *http.Request,
}

func (s *HTTPServer) aclTokenSelf(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
if req.Method != "GET" {
if req.Method != http.MethodGet {
return nil, CodedError(405, ErrInvalidMethod)
}
args := structs.ResolveACLTokenRequest{}
Expand Down
30 changes: 15 additions & 15 deletions command/agent/acl_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestHTTP_ACLPolicyList(t *testing.T) {
}

// Make the HTTP request
req, err := http.NewRequest("GET", "/v1/acl/policies", nil)
req, err := http.NewRequest(http.MethodGet, "/v1/acl/policies", nil)
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestHTTP_ACLPolicyQuery(t *testing.T) {
}

// Make the HTTP request
req, err := http.NewRequest("GET", "/v1/acl/policy/"+p1.Name, nil)
req, err := http.NewRequest(http.MethodGet, "/v1/acl/policy/"+p1.Name, nil)
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestHTTP_ACLPolicyCreate(t *testing.T) {
// Make the HTTP request
p1 := mock.ACLPolicy()
buf := encodeReq(p1)
req, err := http.NewRequest("PUT", "/v1/acl/policy/"+p1.Name, buf)
req, err := http.NewRequest(http.MethodPut, "/v1/acl/policy/"+p1.Name, buf)
must.NoError(t, err)

respW := httptest.NewRecorder()
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestHTTP_ACLPolicyDelete(t *testing.T) {
}

// Make the HTTP request
req, err := http.NewRequest("DELETE", "/v1/acl/policy/"+p1.Name, nil)
req, err := http.NewRequest(http.MethodDelete, "/v1/acl/policy/"+p1.Name, nil)
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -220,7 +220,7 @@ func TestHTTP_ACLTokenBootstrap(t *testing.T) {
}
httpTest(t, conf, func(s *TestAgent) {
// Make the HTTP request
req, err := http.NewRequest("PUT", "/v1/acl/bootstrap", nil)
req, err := http.NewRequest(http.MethodPut, "/v1/acl/bootstrap", nil)
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -259,7 +259,7 @@ func TestHTTP_ACLTokenBootstrapOperator(t *testing.T) {
buf := encodeReq(args)

// Make the HTTP request
req, err := http.NewRequest("PUT", "/v1/acl/bootstrap", buf)
req, err := http.NewRequest(http.MethodPut, "/v1/acl/bootstrap", buf)
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -309,7 +309,7 @@ func TestHTTP_ACLTokenList(t *testing.T) {
}

// Make the HTTP request
req, err := http.NewRequest("GET", "/v1/acl/tokens", nil)
req, err := http.NewRequest(http.MethodGet, "/v1/acl/tokens", nil)
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -360,7 +360,7 @@ func TestHTTP_ACLTokenQuery(t *testing.T) {
out := resp.Tokens[0]

// Make the HTTP request
req, err := http.NewRequest("GET", "/v1/acl/token/"+out.AccessorID, nil)
req, err := http.NewRequest(http.MethodGet, "/v1/acl/token/"+out.AccessorID, nil)
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -409,7 +409,7 @@ func TestHTTP_ACLTokenSelf(t *testing.T) {
out := resp.Tokens[0]

// Make the HTTP request
req, err := http.NewRequest("GET", "/v1/acl/token/self", nil)
req, err := http.NewRequest(http.MethodGet, "/v1/acl/token/self", nil)
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -446,7 +446,7 @@ func TestHTTP_ACLTokenCreate(t *testing.T) {
p1 := mock.ACLToken()
p1.AccessorID = ""
buf := encodeReq(p1)
req, err := http.NewRequest("PUT", "/v1/acl/token", buf)
req, err := http.NewRequest(http.MethodPut, "/v1/acl/token", buf)
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -488,7 +488,7 @@ func TestHTTP_ACLTokenCreateExpirationTTL(t *testing.T) {
"Global": false
}`

req, err := http.NewRequest("PUT", "/v1/acl/token", bytes.NewReader([]byte(aclToken)))
req, err := http.NewRequest(http.MethodPut, "/v1/acl/token", bytes.NewReader([]byte(aclToken)))
must.NoError(t, err)

respW := httptest.NewRecorder()
Expand Down Expand Up @@ -534,7 +534,7 @@ func TestHTTP_ACLTokenDelete(t *testing.T) {
ID := resp.Tokens[0].AccessorID

// Make the HTTP request
req, err := http.NewRequest("DELETE", "/v1/acl/token/"+ID, nil)
req, err := http.NewRequest(http.MethodDelete, "/v1/acl/token/"+ID, nil)
if err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -582,7 +582,7 @@ func TestHTTP_OneTimeToken(t *testing.T) {

// Make a HTTP request to get a one-time token

req, err := http.NewRequest("POST", "/v1/acl/token/onetime", nil)
req, err := http.NewRequest(http.MethodPost, "/v1/acl/token/onetime", nil)
require.NoError(t, err)
req.Header.Set("X-Nomad-Token", aclSecret)
respW := httptest.NewRecorder()
Expand All @@ -599,7 +599,7 @@ func TestHTTP_OneTimeToken(t *testing.T) {

buf := encodeReq(structs.OneTimeTokenExchangeRequest{
OneTimeSecretID: ott.OneTimeToken.OneTimeSecretID})
req, err = http.NewRequest("POST", "/v1/acl/token/onetime/exchange", buf)
req, err = http.NewRequest(http.MethodPost, "/v1/acl/token/onetime/exchange", buf)
require.NoError(t, err)
respW = httptest.NewRecorder()

Expand All @@ -615,7 +615,7 @@ func TestHTTP_OneTimeToken(t *testing.T) {

buf = encodeReq(structs.OneTimeTokenExchangeRequest{
OneTimeSecretID: ott.OneTimeToken.OneTimeSecretID})
req, err = http.NewRequest("POST", "/v1/acl/token/onetime/exchange", buf)
req, err = http.NewRequest(http.MethodPost, "/v1/acl/token/onetime/exchange", buf)
require.NoError(t, err)
respW = httptest.NewRecorder()

Expand Down
10 changes: 5 additions & 5 deletions command/agent/agent_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func nomadMember(m serf.Member) Member {
}

func (s *HTTPServer) AgentSelfRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
if req.Method != "GET" {
if req.Method != http.MethodGet {
return nil, CodedError(405, ErrInvalidMethod)
}

Expand Down Expand Up @@ -128,7 +128,7 @@ func (s *HTTPServer) AgentJoinRequest(resp http.ResponseWriter, req *http.Reques
}

func (s *HTTPServer) AgentMembersRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
if req.Method != "GET" {
if req.Method != http.MethodGet {
return nil, CodedError(405, ErrInvalidMethod)
}

Expand Down Expand Up @@ -415,9 +415,9 @@ func (s *HTTPServer) agentPprof(reqType pprof.ReqType, resp http.ResponseWriter,
// servers for a given agent.
func (s *HTTPServer) AgentServersRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
switch req.Method {
case "PUT", "POST":
case http.MethodPut, http.MethodPost:
return s.updateServers(resp, req)
case "GET":
case http.MethodGet:
return s.listServers(resp, req)
default:
return nil, CodedError(405, ErrInvalidMethod)
Expand Down Expand Up @@ -549,7 +549,7 @@ type joinResult struct {
}

func (s *HTTPServer) HealthRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
if req.Method != "GET" {
if req.Method != http.MethodGet {
return nil, CodedError(405, ErrInvalidMethod)
}

Expand Down
Loading

0 comments on commit 54a9acd

Please sign in to comment.