Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: enable more linting #144

Merged
merged 26 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
df10223
fix: make the return lint happy
SiasMey Nov 26, 2024
46f9d27
style: add empty line before return statements
SiasMey Nov 26, 2024
85430f4
chore: update linter list
SiasMey Nov 26, 2024
0080d3c
fix: many http body left open memory leaks
SiasMey Nov 26, 2024
69bbd7e
fix: ignore some false positives from gosec
SiasMey Nov 26, 2024
9d7e443
style: fix var names to follow style guidelines
SiasMey Nov 26, 2024
02824ce
fix: undo body.Close to find segfault
SiasMey Nov 26, 2024
eaa2b3b
fix: close http response in client tests
SiasMey Nov 26, 2024
1c554e9
fix: close a few more http clients
SiasMey Nov 27, 2024
bb2b5b9
fix: and close some more
SiasMey Nov 27, 2024
6101336
fix: close responses in live tests
SiasMey Nov 27, 2024
44910e4
fix: close responses in agent_group_assignment
SiasMey Nov 27, 2024
16e0418
fix: close responses in agent_group_assignments
SiasMey Nov 27, 2024
d801c89
fix: close response in service_group_assignments
SiasMey Nov 27, 2024
fe44bc0
fix: close responses in wireless_networks
SiasMey Nov 27, 2024
e2d5b22
fix: close response in service_test_groups
SiasMey Nov 27, 2024
b290b45
fix: close response in senso_group_assignments
SiasMey Nov 27, 2024
dc3f099
fix: close most resonpses in resoruce_group
SiasMey Nov 27, 2024
4f16cf7
fix: close responses in network_group_assignments
SiasMey Nov 27, 2024
9718c8f
fix: close the reamining provider http responses
SiasMey Nov 27, 2024
2e8d276
fix: close results in live tests
SiasMey Nov 27, 2024
1786967
refactor: use error instead of *string in response
SiasMey Nov 27, 2024
5ab90b5
fix: move defer to below error check
SiasMey Nov 27, 2024
5f6bc34
fix: move defer to after nil check for http response
SiasMey Nov 27, 2024
c6103e3
chore: update lint config
SiasMey Nov 27, 2024
85cba5b
chore: update linting config to enable ireturn
SiasMey Nov 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ linters:
- cyclop # we have gocyclo, apparently its better
- execinquery # deprecated

- canonicalheader # for checking copyright headers
- bodyclose # we want this
- contextcheck # we want this
- copyloopvar # if we update to later than go 1.22
- depguard # for banning specific dependencies
Expand All @@ -39,18 +37,15 @@ linters:
- funlen # we want this
- goconst # we want this
- gocritic # we want this
- gosec # we want this
- intrange # later version of go
- ireturn # we want this
# - ireturn # we want this
- lll # we want this
- maintidx # we want this
- mnd # we want this todo: also see gomnd
- gomnd # disabled in gl provider todo: also see mnd
- nlreturn # we want this
- noctx # we want this
- nestif # we want this
- revive # we want this
- stylecheck # we want this
- tagliatelle # we want this
- testifylint # we want this
- thelper # we want this
Expand Down Expand Up @@ -93,6 +88,10 @@ issues:
- dupl
path: _test\.go

- linters:
- stylecheck
path: pkg/config-api-client

- linters:
- dupl
path: ".*generated.*go"
Expand All @@ -109,4 +108,12 @@ issues:
- stylecheck
text: "ST1005:"

- linters:
- ireturn
path: internal/provider/resources

- linters:
- ireturn
path: internal/provider/util/retry.go

max-same-issues: 0
3 changes: 2 additions & 1 deletion internal/provider/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "os"

const (
MaxRetriesForTooManyRequests = 10
TokenURL = "https://sso.common.cloud.hpe.com/as/token.oauth2"
TokenURL = "https://sso.common.cloud.hpe.com/as/token.oauth2" // #nosec G101
UXIDefaultHost = "api.capenetworks.com"
)

Expand All @@ -23,6 +23,7 @@ func getEnv(key, fallback string) string {
if value == "" {
return fallback
}

return value
}

Expand Down
4 changes: 2 additions & 2 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (p *uxiConfigurationProvider) Configure(
uxiConfiguration := config_api_client.NewConfiguration()
uxiConfiguration.Host = configuration.Host
uxiConfiguration.Scheme = "https"
uxiConfiguration.HTTPClient = getHttpClient(clientID, clientSecret, configuration.TokenURL)
uxiConfiguration.HTTPClient = getHTTPClient(clientID, clientSecret, configuration.TokenURL)
uxiClient := config_api_client.NewAPIClient(uxiConfiguration)

resp.DataSourceData = uxiClient
Expand Down Expand Up @@ -172,7 +172,7 @@ func (p *uxiConfigurationProvider) Resources(_ context.Context) []func() resourc
}
}

func getHttpClient(clientID string, clientSecret string, tokenURL string) *http.Client {
func getHTTPClient(clientID string, clientSecret string, tokenURL string) *http.Client {
config := &clientcredentials.Config{
ClientID: clientID,
ClientSecret: clientSecret,
Expand Down
6 changes: 5 additions & 1 deletion internal/provider/resources/data_source_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,20 @@ func (d *agentDataSource) Read(

agentResponse, response, err := util.RetryForTooManyRequests(request.Execute)
errorPresent, errorDetail := util.RaiseForStatus(response, err)

errorSummary := util.GenerateErrorSummary("read", "uxi_agent")

if errorPresent {
resp.Diagnostics.AddError(errorSummary, errorDetail)

return
}

defer response.Body.Close()

if len(agentResponse.Items) != 1 {
resp.Diagnostics.AddError(errorSummary, "Could not find specified data source")
resp.State.RemoveResource(ctx)

return
}

Expand Down Expand Up @@ -171,6 +174,7 @@ func (d *agentDataSource) Configure(
"Unexpected Data Source Configure Type",
"Data Source type: Agent. Please report this issue to the provider developers.",
)

return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,20 @@ func (d *agentGroupAssignmentDataSource) Read(
Id(state.Filter.ID)
agentGroupAssignmentResponse, response, err := util.RetryForTooManyRequests(request.Execute)
errorPresent, errorDetail := util.RaiseForStatus(response, err)

errorSummary := util.GenerateErrorSummary("read", "uxi_agent_group_assignment")

if errorPresent {
resp.Diagnostics.AddError(errorSummary, errorDetail)

return
}

defer response.Body.Close()

if len(agentGroupAssignmentResponse.Items) != 1 {
resp.Diagnostics.AddError(errorSummary, "Could not find specified data source")
resp.State.RemoveResource(ctx)

return
}

Expand Down Expand Up @@ -139,6 +142,7 @@ func (d *agentGroupAssignmentDataSource) Configure(
"Unexpected Data Source Configure Type",
"Data Source type: Agent Group Assignment. Please report this issue to the provider developers.",
)

return
}

Expand Down
7 changes: 6 additions & 1 deletion internal/provider/resources/data_source_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,27 @@ func (d *groupDataSource) Read(

groupResponse, response, err := util.RetryForTooManyRequests(request.Execute)
errorPresent, errorDetail := util.RaiseForStatus(response, err)

errorSummary := util.GenerateErrorSummary("read", "uxi_group")

if errorPresent {
resp.Diagnostics.AddError(errorSummary, errorDetail)

return
}

defer response.Body.Close()

if len(groupResponse.Items) != 1 {
resp.Diagnostics.AddError(errorSummary, "Could not find specified data source")
resp.State.RemoveResource(ctx)

return
}

group := groupResponse.Items[0]
if util.IsRoot(group) {
resp.Diagnostics.AddError(errorSummary, "The root group cannot be used as a data source")

return
}

Expand Down Expand Up @@ -151,6 +155,7 @@ func (d *groupDataSource) Configure(
"Unexpected Data Source Configure Type",
"Data Source type: Group. Please report this issue to the provider developers.",
)

return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,20 @@ func (d *networkGroupAssignmentDataSource) Read(
Id(state.Filter.ID)
networkGroupAssignmentResponse, response, err := util.RetryForTooManyRequests(request.Execute)
errorPresent, errorDetail := util.RaiseForStatus(response, err)

errorSummary := util.GenerateErrorSummary("read", "uxi_network_group_assignment")

if errorPresent {
resp.Diagnostics.AddError(errorSummary, errorDetail)

return
}

defer response.Body.Close()

if len(networkGroupAssignmentResponse.Items) != 1 {
resp.Diagnostics.AddError(errorSummary, "Could not find specified data source")
resp.State.RemoveResource(ctx)

return
}

Expand Down Expand Up @@ -139,6 +142,7 @@ func (d *networkGroupAssignmentDataSource) Configure(
"Unexpected Data Source Configure Type",
"Data Source type: Network Group Assignment. Please report this issue to the provider developers.",
)

return
}

Expand Down
6 changes: 5 additions & 1 deletion internal/provider/resources/data_source_sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,20 @@ func (d *sensorDataSource) Read(

sensorResponse, response, err := util.RetryForTooManyRequests(request.Execute)
errorPresent, errorDetail := util.RaiseForStatus(response, err)

errorSummary := util.GenerateErrorSummary("read", "uxi_sensor")

if errorPresent {
resp.Diagnostics.AddError(errorSummary, errorDetail)

return
}

defer response.Body.Close()

if len(sensorResponse.Items) != 1 {
resp.Diagnostics.AddError(errorSummary, "Could not find specified data source")
resp.State.RemoveResource(ctx)

return
}

Expand Down Expand Up @@ -189,6 +192,7 @@ func (d *sensorDataSource) Configure(
"Unexpected Data Source Configure Type",
"Data Source type: Sensor. Please report this issue to the provider developers.",
)

return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,20 @@ func (d *sensorGroupAssignmentDataSource) Read(
Id(state.Filter.ID)
sensorGroupAssignmentResponse, response, err := util.RetryForTooManyRequests(request.Execute)
errorPresent, errorDetail := util.RaiseForStatus(response, err)

errorSummary := util.GenerateErrorSummary("read", "uxi_sensor_group_assignment")

if errorPresent {
resp.Diagnostics.AddError(errorSummary, errorDetail)

return
}

defer response.Body.Close()

if len(sensorGroupAssignmentResponse.Items) != 1 {
resp.Diagnostics.AddError(errorSummary, "Could not find specified data source")
resp.State.RemoveResource(ctx)

return
}

Expand Down Expand Up @@ -139,6 +142,7 @@ func (d *sensorGroupAssignmentDataSource) Configure(
"Unexpected Data Source Configure Type",
"Data Source type: Sensor Group Assignment. Please report this issue to the provider developers.",
)

return
}

Expand Down
6 changes: 5 additions & 1 deletion internal/provider/resources/data_source_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,20 @@ func (d *serviceTestDataSource) Read(

serviceTestResponse, response, err := util.RetryForTooManyRequests(request.Execute)
errorPresent, errorDetail := util.RaiseForStatus(response, err)

errorSummary := util.GenerateErrorSummary("read", "uxi_service_test")

if errorPresent {
resp.Diagnostics.AddError(errorSummary, errorDetail)

return
}

defer response.Body.Close()

if len(serviceTestResponse.Items) != 1 {
resp.Diagnostics.AddError(errorSummary, "Could not find specified data source")
resp.State.RemoveResource(ctx)

return
}

Expand Down Expand Up @@ -162,6 +165,7 @@ func (d *serviceTestDataSource) Configure(
"Unexpected Data Source Configure Type",
"Data Source type: ServiceTest. Please report this issue to the provider developers.",
)

return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,20 @@ func (d *serviceTestGroupAssignmentDataSource) Read(
request.Execute,
)
errorPresent, errorDetail := util.RaiseForStatus(response, err)

errorSummary := util.GenerateErrorSummary("read", "uxi_service_test_group_assignment")

if errorPresent {
resp.Diagnostics.AddError(errorSummary, errorDetail)

return
}

defer response.Body.Close()

if len(serviceTestGroupAssignmentResponse.Items) != 1 {
resp.Diagnostics.AddError(errorSummary, "Could not find specified data source")
resp.State.RemoveResource(ctx)

return
}

Expand Down Expand Up @@ -141,6 +144,7 @@ func (d *serviceTestGroupAssignmentDataSource) Configure(
"Unexpected Data Source Configure Type",
"Data Source type: ServiceTest Group Assignment. Please report this issue to the provider developers.",
)

return
}

Expand Down
6 changes: 5 additions & 1 deletion internal/provider/resources/data_source_wired_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,20 @@ func (d *wiredNetworkDataSource) Read(
Id(state.Filter.ID)
networkResponse, response, err := util.RetryForTooManyRequests(request.Execute)
errorPresent, errorDetail := util.RaiseForStatus(response, err)

errorSummary := util.GenerateErrorSummary("read", "uxi_wired_network")

if errorPresent {
resp.Diagnostics.AddError(errorSummary, errorDetail)

return
}

defer response.Body.Close()

if len(networkResponse.Items) != 1 {
resp.Diagnostics.AddError(errorSummary, "Could not find specified data source")
resp.State.RemoveResource(ctx)

return
}

Expand Down Expand Up @@ -175,6 +178,7 @@ func (d *wiredNetworkDataSource) Configure(
"Unexpected Data Source Configure Type",
"Data Source type: Wired Network. Please report this issue to the provider developers.",
)

return
}

Expand Down
6 changes: 5 additions & 1 deletion internal/provider/resources/data_source_wireless_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,20 @@ func (d *wirelessNetworkDataSource) Read(
Id(state.Filter.ID)
networkResponse, response, err := util.RetryForTooManyRequests(request.Execute)
errorPresent, errorDetail := util.RaiseForStatus(response, err)

errorSummary := util.GenerateErrorSummary("read", "uxi_wireless_network")

if errorPresent {
resp.Diagnostics.AddError(errorSummary, errorDetail)

return
}

defer response.Body.Close()

if len(networkResponse.Items) != 1 {
resp.Diagnostics.AddError(errorSummary, "Could not find specified data source")
resp.State.RemoveResource(ctx)

return
}

Expand Down Expand Up @@ -187,6 +190,7 @@ func (d *wirelessNetworkDataSource) Configure(
"Unexpected Data Source Configure Type",
"Data Source type: Wireless Network. Please report this issue to the provider developers.",
)

return
}

Expand Down
Loading
Loading