Skip to content

Commit 89e5f2e

Browse files
committed
fix: many golangci-lint issues, remove unused lookUpEndpoints in client
1 parent ccec91a commit 89e5f2e

18 files changed

+145
-134
lines changed

client/buildingblock.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ func (c *MeshStackProviderClient) ReadBuildingBlock(uuid string) (*MeshBuildingB
9494
return nil, err
9595
}
9696

97-
defer res.Body.Close()
97+
defer func() {
98+
_ = res.Body.Close()
99+
}()
98100

99101
data, err := io.ReadAll(res.Body)
100102
if err != nil {
@@ -135,8 +137,9 @@ func (c *MeshStackProviderClient) CreateBuildingBlock(bb *MeshBuildingBlockCreat
135137
if err != nil {
136138
return nil, err
137139
}
138-
139-
defer res.Body.Close()
140+
defer func() {
141+
_ = res.Body.Close()
142+
}()
140143

141144
data, err := io.ReadAll(res.Body)
142145
if err != nil {

client/buildingblock_v2.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
const (
1616
CONTENT_TYPE_BUILDING_BLOCK_V2 = "application/vnd.meshcloud.api.meshbuildingblock.v2-preview.hal+json"
1717

18-
// Building Block Status Constants
18+
// Building Block Status Constants.
1919
BUILDING_BLOCK_STATUS_WAITING_FOR_DEPENDENT_INPUT = "WAITING_FOR_DEPENDENT_INPUT"
2020
BUILDING_BLOCK_STATUS_WAITING_FOR_OPERATOR_INPUT = "WAITING_FOR_OPERATOR_INPUT"
2121
BUILDING_BLOCK_STATUS_PENDING = "PENDING"
@@ -85,7 +85,9 @@ func (c *MeshStackProviderClient) ReadBuildingBlockV2(uuid string) (*MeshBuildin
8585
return nil, err
8686
}
8787

88-
defer res.Body.Close()
88+
defer func() {
89+
_ = res.Body.Close()
90+
}()
8991

9092
data, err := io.ReadAll(res.Body)
9193
if err != nil {
@@ -127,7 +129,9 @@ func (c *MeshStackProviderClient) CreateBuildingBlockV2(bb *MeshBuildingBlockV2C
127129
return nil, err
128130
}
129131

130-
defer res.Body.Close()
132+
defer func() {
133+
_ = res.Body.Close()
134+
}()
131135

132136
data, err := io.ReadAll(res.Body)
133137
if err != nil {
@@ -153,15 +157,15 @@ func (c *MeshStackProviderClient) DeleteBuildingBlockV2(uuid string) error {
153157
}
154158

155159
// PollBuildingBlockV2UntilCompletion polls a building block until it reaches a terminal state (SUCCEEDED or FAILED)
156-
// Returns the final building block state or an error if polling fails or times out
160+
// Returns the final building block state or an error if polling fails or times out.
157161
func (c *MeshStackProviderClient) PollBuildingBlockV2UntilCompletion(ctx context.Context, uuid string) (*MeshBuildingBlockV2, error) {
158162
var result *MeshBuildingBlockV2
159163

160164
err := retry.RetryContext(ctx, 30*time.Minute, c.waitForBuildingBlockV2CompletionFunc(uuid, &result))
161165
return result, err
162166
}
163167

164-
// waitForBuildingBlockV2CompletionFunc returns a RetryFunc that checks building block completion status
168+
// waitForBuildingBlockV2CompletionFunc returns a RetryFunc that checks building block completion status.
165169
func (c *MeshStackProviderClient) waitForBuildingBlockV2CompletionFunc(uuid string, result **MeshBuildingBlockV2) retry.RetryFunc {
166170
return func() *retry.RetryError {
167171
current, err := c.ReadBuildingBlockV2(uuid)
@@ -189,12 +193,12 @@ func (c *MeshStackProviderClient) waitForBuildingBlockV2CompletionFunc(uuid stri
189193
}
190194

191195
// PollBuildingBlockV2UntilDeletion polls a building block until it is deleted (not found)
192-
// Returns nil on successful deletion or an error if polling fails or times out
196+
// Returns nil on successful deletion or an error if polling fails or times out.
193197
func (c *MeshStackProviderClient) PollBuildingBlockV2UntilDeletion(ctx context.Context, uuid string) error {
194198
return retry.RetryContext(ctx, 30*time.Minute, c.waitForBuildingBlockV2DeletionFunc(uuid))
195199
}
196200

197-
// waitForBuildingBlockV2DeletionFunc returns a RetryFunc that checks building block deletion status
201+
// waitForBuildingBlockV2DeletionFunc returns a RetryFunc that checks building block deletion status.
198202
func (c *MeshStackProviderClient) waitForBuildingBlockV2DeletionFunc(uuid string) retry.RetryFunc {
199203
return func() *retry.RetryError {
200204
current, err := c.ReadBuildingBlockV2(uuid)

client/client.go

Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package client
33
import (
44
"bytes"
55
"encoding/json"
6-
"errors"
76
"fmt"
87
"io"
98
"log"
@@ -15,11 +14,6 @@ import (
1514
const (
1615
apiMeshObjectsRoot = "/api/meshobjects"
1716
loginEndpoint = "/api/login"
18-
19-
ERROR_GENERIC_CLIENT_ERROR = "client error"
20-
ERROR_GENERIC_API_ERROR = "api error"
21-
ERROR_AUTHENTICATION_FAILURE = "Not authorized. Check api key and secret."
22-
ERROR_ENDPOINT_LOOKUP = "Could not fetch endpoints for meshStack."
2317
)
2418

2519
type MeshStackProviderClient struct {
@@ -109,14 +103,16 @@ func (c *MeshStackProviderClient) login() error {
109103
req.Header.Add("Content-Type", "application/json")
110104

111105
res, err := c.httpClient.Do(req)
112-
113106
if err != nil {
114107
return err
115-
} else if res.StatusCode != 200 {
116-
return fmt.Errorf("Status %d: %s", res.StatusCode, ERROR_AUTHENTICATION_FAILURE)
117108
}
109+
defer func() {
110+
_ = res.Body.Close()
111+
}()
118112

119-
defer res.Body.Close()
113+
if res.StatusCode != 200 {
114+
return fmt.Errorf("login failed with status %d, check api key and secret", res.StatusCode)
115+
}
120116

121117
data, err := io.ReadAll(res.Body)
122118
if err != nil {
@@ -142,53 +138,6 @@ func (c *MeshStackProviderClient) ensureValidToken() error {
142138
return nil
143139
}
144140

145-
// nolint: unused
146-
func (c *MeshStackProviderClient) lookUpEndpoints() error {
147-
if c.ensureValidToken() != nil {
148-
return errors.New(ERROR_AUTHENTICATION_FAILURE)
149-
}
150-
151-
meshObjectsPath, err := url.JoinPath(c.url.String(), apiMeshObjectsRoot)
152-
if err != nil {
153-
return err
154-
}
155-
meshObjects, _ := url.Parse(meshObjectsPath)
156-
157-
res, err := c.httpClient.Do(
158-
&http.Request{
159-
URL: meshObjects,
160-
Method: "GET",
161-
Header: http.Header{
162-
"Authorization": {c.token},
163-
},
164-
},
165-
)
166-
167-
if err != nil {
168-
return errors.New(ERROR_GENERIC_CLIENT_ERROR)
169-
}
170-
171-
defer res.Body.Close()
172-
173-
if res.StatusCode != 200 {
174-
return errors.New(ERROR_AUTHENTICATION_FAILURE)
175-
}
176-
177-
data, err := io.ReadAll(res.Body)
178-
if err != nil {
179-
return err
180-
}
181-
182-
var endpoints endpoints
183-
err = json.Unmarshal(data, &endpoints)
184-
if err != nil {
185-
return err
186-
}
187-
188-
c.endpoints = endpoints
189-
return nil
190-
}
191-
192141
func (c *MeshStackProviderClient) doAuthenticatedRequest(req *http.Request) (*http.Response, error) {
193142
// ensure that headeres are initialized
194143
if req.Header == nil {
@@ -224,18 +173,20 @@ func (c *MeshStackProviderClient) deleteMeshObject(targetUrl url.URL, expectedSt
224173
res, err := c.doAuthenticatedRequest(req)
225174

226175
if err != nil {
227-
return errors.New(ERROR_GENERIC_CLIENT_ERROR)
176+
return fmt.Errorf("cannot authenticate for delete request: %w ", err)
228177
}
229178

230-
defer res.Body.Close()
179+
defer func() {
180+
_ = res.Body.Close()
181+
}()
231182

232183
data, err := io.ReadAll(res.Body)
233184
if err != nil {
234185
return err
235186
}
236187

237188
if res.StatusCode != expectedStatus {
238-
return fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
189+
return fmt.Errorf("expected status code %d, but got %d, body: '%s'", expectedStatus, res.StatusCode, string(data))
239190
}
240191

241192
return nil

client/integrations.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ type MeshIntegrationStatus struct {
3434
WorkloadIdentityFederation *MeshWorkloadIdentityFederation `json:"workloadIdentityFederation,omitempty" tfsdk:"workload_identity_federation"`
3535
}
3636

37-
// Integration Config wrapper with type discrimination
37+
// Integration Config wrapper with type discrimination.
3838
type MeshIntegrationConfig struct {
3939
Type string `json:"type" tfsdk:"type"`
4040
Github *MeshIntegrationGithubConfig `json:"github,omitempty" tfsdk:"github"`
4141
Gitlab *MeshIntegrationGitlabConfig `json:"gitlab,omitempty" tfsdk:"gitlab"`
4242
AzureDevops *MeshIntegrationAzureDevopsConfig `json:"azuredevops,omitempty" tfsdk:"azuredevops"`
4343
}
4444

45-
// GitHub Integration
45+
// GitHub Integration.
4646
type MeshIntegrationGithubConfig struct {
4747
Owner string `json:"owner" tfsdk:"owner"`
4848
BaseUrl string `json:"baseUrl" tfsdk:"base_url"`
@@ -51,27 +51,27 @@ type MeshIntegrationGithubConfig struct {
5151
RunnerRef BuildingBlockRunnerRef `json:"runnerRef" tfsdk:"runner_ref"`
5252
}
5353

54-
// GitLab Integration
54+
// GitLab Integration.
5555
type MeshIntegrationGitlabConfig struct {
5656
BaseUrl string `json:"baseUrl" tfsdk:"base_url"`
5757
RunnerRef BuildingBlockRunnerRef `json:"runnerRef" tfsdk:"runner_ref"`
5858
}
5959

60-
// Azure DevOps Integration
60+
// Azure DevOps Integration.
6161
type MeshIntegrationAzureDevopsConfig struct {
6262
BaseUrl string `json:"baseUrl" tfsdk:"base_url"`
6363
Organization string `json:"organization" tfsdk:"organization"`
6464
PersonalAccessToken string `json:"personalAccessToken" tfsdk:"personal_access_token"`
6565
RunnerRef BuildingBlockRunnerRef `json:"runnerRef" tfsdk:"runner_ref"`
6666
}
6767

68-
// Building Block Runner Reference
68+
// Building Block Runner Reference.
6969
type BuildingBlockRunnerRef struct {
7070
Uuid string `json:"uuid" tfsdk:"uuid"`
7171
Kind string `json:"kind" tfsdk:"kind"`
7272
}
7373

74-
// Workload Identity Federation
74+
// Workload Identity Federation.
7575
type MeshWorkloadIdentityFederation struct {
7676
Issuer string `json:"issuer" tfsdk:"issuer"`
7777
Subject string `json:"subject" tfsdk:"subject"`
@@ -106,7 +106,7 @@ func (c *MeshStackProviderClient) ReadIntegration(workspace string, uuid string)
106106
return nil, err
107107
}
108108

109-
defer res.Body.Close()
109+
defer func() { _ = res.Body.Close() }()
110110

111111
data, err := io.ReadAll(res.Body)
112112
if err != nil {
@@ -153,7 +153,9 @@ func (c *MeshStackProviderClient) ReadIntegrations() (*[]MeshIntegration, error)
153153
return nil, err
154154
}
155155

156-
defer res.Body.Close()
156+
defer func() {
157+
_ = res.Body.Close()
158+
}()
157159

158160
data, err := io.ReadAll(res.Body)
159161
if err != nil {

client/landingzone.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"net/url"
1010
)
1111

12-
const CONTENT_TYPE_LANDINGZONE = "application/vnd.meshcloud.api.meshlandingzone.v1-preview.hal+json"
12+
const contentTypeLandingzone = "application/vnd.meshcloud.api.meshlandingzone.v1-preview.hal+json"
1313

1414
type MeshLandingZone struct {
1515
ApiVersion string `json:"apiVersion" tfsdk:"api_version"`
@@ -77,14 +77,14 @@ func (c *MeshStackProviderClient) ReadLandingZone(name string) (*MeshLandingZone
7777
if err != nil {
7878
return nil, err
7979
}
80-
req.Header.Set("Accept", CONTENT_TYPE_LANDINGZONE)
80+
req.Header.Set("Accept", contentTypeLandingzone)
8181

8282
res, err := c.doAuthenticatedRequest(req)
8383
if err != nil {
8484
return nil, err
8585
}
8686

87-
defer res.Body.Close()
87+
defer func() { _ = res.Body.Close() }()
8888

8989
if res.StatusCode == http.StatusNotFound {
9090
return nil, nil // Not found is not an error
@@ -117,14 +117,16 @@ func (c *MeshStackProviderClient) CreateLandingZone(landingZone *MeshLandingZone
117117
if err != nil {
118118
return nil, err
119119
}
120-
req.Header.Set("Content-Type", CONTENT_TYPE_LANDINGZONE)
121-
req.Header.Set("Accept", CONTENT_TYPE_LANDINGZONE)
120+
req.Header.Set("Content-Type", contentTypeLandingzone)
121+
req.Header.Set("Accept", contentTypeLandingzone)
122122

123123
res, err := c.doAuthenticatedRequest(req)
124124
if err != nil {
125125
return nil, err
126126
}
127-
defer res.Body.Close()
127+
defer func() {
128+
_ = res.Body.Close()
129+
}()
128130

129131
data, err := io.ReadAll(res.Body)
130132
if err != nil {
@@ -155,14 +157,16 @@ func (c *MeshStackProviderClient) UpdateLandingZone(name string, landingZone *Me
155157
if err != nil {
156158
return nil, err
157159
}
158-
req.Header.Set("Content-Type", CONTENT_TYPE_LANDINGZONE)
159-
req.Header.Set("Accept", CONTENT_TYPE_LANDINGZONE)
160+
req.Header.Set("Content-Type", contentTypeLandingzone)
161+
req.Header.Set("Accept", contentTypeLandingzone)
160162

161163
res, err := c.doAuthenticatedRequest(req)
162164
if err != nil {
163165
return nil, err
164166
}
165-
defer res.Body.Close()
167+
defer func() {
168+
_ = res.Body.Close()
169+
}()
166170

167171
data, err := io.ReadAll(res.Body)
168172
if err != nil {

client/payment_method.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ func (c *MeshStackProviderClient) ReadPaymentMethod(workspace string, identifier
6161
return nil, err
6262
}
6363

64-
defer res.Body.Close()
64+
defer func() {
65+
_ = res.Body.Close()
66+
}()
6567

6668
if res.StatusCode == http.StatusNotFound {
6769
return nil, nil
@@ -103,7 +105,9 @@ func (c *MeshStackProviderClient) CreatePaymentMethod(paymentMethod *MeshPayment
103105
return nil, err
104106
}
105107

106-
defer res.Body.Close()
108+
defer func() {
109+
_ = res.Body.Close()
110+
}()
107111

108112
data, err := io.ReadAll(res.Body)
109113
if err != nil {
@@ -143,7 +147,9 @@ func (c *MeshStackProviderClient) UpdatePaymentMethod(identifier string, payment
143147
return nil, err
144148
}
145149

146-
defer res.Body.Close()
150+
defer func() {
151+
_ = res.Body.Close()
152+
}()
147153

148154
data, err := io.ReadAll(res.Body)
149155
if err != nil {

0 commit comments

Comments
 (0)