Skip to content

Commit ba000af

Browse files
authored
chore: add support for funcorder (#3156)
* chore: add support for funcorder * Fix
1 parent e096766 commit ba000af

File tree

6 files changed

+109
-108
lines changed

6 files changed

+109
-108
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ linters:
2929
- exptostd # Detects functions from golang.org/x/exp/ that can be replaced by std functions. [auto-fix]
3030
- fatcontext # Detects nested contexts in loops and function literals. [auto-fix]
3131
- forbidigo # Forbids identifiers [fast: true, auto-fix: false]
32+
- funcorder # Checks the order of functions, methods, and constructors. [fast]
3233
- gocheckcompilerdirectives # Checks that go compiler directive comments (//go:) are valid. [fast: true, auto-fix: false]
3334
- gochecksumtype # Run exhaustiveness checks on Go "sum types" [fast: false, auto-fix: false]
3435
- goconst # Finds repeated strings that could be replaced by a constant [fast: true, auto-fix: false]

internal/locality/regional/ids.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ type ID struct {
1414
Region scw.Region
1515
}
1616

17-
func (z ID) String() string {
18-
return fmt.Sprintf("%s/%s", z.Region, z.ID)
19-
}
20-
2117
func NewID(region scw.Region, id string) ID {
2218
return ID{
2319
ID: id,
2420
Region: region,
2521
}
2622
}
2723

24+
func (z ID) String() string {
25+
return fmt.Sprintf("%s/%s", z.Region, z.ID)
26+
}
27+
2828
func ExpandID(id interface{}) ID {
2929
regionalID := ID{}
3030
tab := strings.Split(id.(string), "/")

internal/locality/zonal/ids.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ type ID struct {
1414
Zone scw.Zone
1515
}
1616

17-
func (z ID) String() string {
18-
return fmt.Sprintf("%s/%s", z.Zone, z.ID)
19-
}
20-
2117
func NewID(zone scw.Zone, id string) ID {
2218
return ID{
2319
ID: id,
2420
Zone: zone,
2521
}
2622
}
2723

24+
func (z ID) String() string {
25+
return fmt.Sprintf("%s/%s", z.Zone, z.ID)
26+
}
27+
2828
func ExpandID(id interface{}) ID {
2929
zonedID := ID{}
3030
tab := strings.Split(id.(string), "/")

internal/meta/meta.go

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,71 @@ type Meta struct {
4949
credentialsSource *CredentialsSource
5050
}
5151

52+
// NewMeta creates the Meta object containing the SDK client.
53+
func NewMeta(ctx context.Context, config *Config) (*Meta, error) {
54+
////
55+
// Load Profile
56+
////
57+
profile, credentialsSource, err := loadProfile(ctx, config.ProviderSchema)
58+
if err != nil {
59+
return nil, err
60+
}
61+
62+
if config.ForceZone != "" {
63+
region, err := config.ForceZone.Region()
64+
if err != nil {
65+
return nil, err
66+
}
67+
68+
profile.DefaultRegion = scw.StringPtr(region.String())
69+
profile.DefaultZone = scw.StringPtr(config.ForceZone.String())
70+
}
71+
72+
if config.ForceProjectID != "" {
73+
profile.DefaultProjectID = scw.StringPtr(config.ForceProjectID)
74+
}
75+
76+
if config.ForceOrganizationID != "" {
77+
profile.DefaultOrganizationID = scw.StringPtr(config.ForceOrganizationID)
78+
}
79+
80+
if config.ForceAccessKey != "" {
81+
profile.AccessKey = scw.StringPtr(config.ForceAccessKey)
82+
}
83+
84+
if config.ForceSecretKey != "" {
85+
profile.SecretKey = scw.StringPtr(config.ForceSecretKey)
86+
}
87+
88+
// TODO validated profile
89+
90+
////
91+
// Create scaleway SDK client
92+
////
93+
opts := []scw.ClientOption{
94+
scw.WithUserAgent(customizeUserAgent(version.Version, config.TerraformVersion)),
95+
scw.WithProfile(profile),
96+
}
97+
98+
httpClient := &http.Client{Transport: transport.NewRetryableTransport(http.DefaultTransport)}
99+
if config.HTTPClient != nil {
100+
httpClient = config.HTTPClient
101+
}
102+
103+
opts = append(opts, scw.WithHTTPClient(httpClient))
104+
105+
scwClient, err := scw.NewClient(opts...)
106+
if err != nil {
107+
return nil, err
108+
}
109+
110+
return &Meta{
111+
scwClient: scwClient,
112+
httpClient: httpClient,
113+
credentialsSource: credentialsSource,
114+
}, nil
115+
}
116+
52117
func (m Meta) ScwClient() *scw.Client {
53118
return m.scwClient
54119
}
@@ -129,71 +194,6 @@ type Config struct {
129194
ForceSecretKey string
130195
}
131196

132-
// NewMeta creates the Meta object containing the SDK client.
133-
func NewMeta(ctx context.Context, config *Config) (*Meta, error) {
134-
////
135-
// Load Profile
136-
////
137-
profile, credentialsSource, err := loadProfile(ctx, config.ProviderSchema)
138-
if err != nil {
139-
return nil, err
140-
}
141-
142-
if config.ForceZone != "" {
143-
region, err := config.ForceZone.Region()
144-
if err != nil {
145-
return nil, err
146-
}
147-
148-
profile.DefaultRegion = scw.StringPtr(region.String())
149-
profile.DefaultZone = scw.StringPtr(config.ForceZone.String())
150-
}
151-
152-
if config.ForceProjectID != "" {
153-
profile.DefaultProjectID = scw.StringPtr(config.ForceProjectID)
154-
}
155-
156-
if config.ForceOrganizationID != "" {
157-
profile.DefaultOrganizationID = scw.StringPtr(config.ForceOrganizationID)
158-
}
159-
160-
if config.ForceAccessKey != "" {
161-
profile.AccessKey = scw.StringPtr(config.ForceAccessKey)
162-
}
163-
164-
if config.ForceSecretKey != "" {
165-
profile.SecretKey = scw.StringPtr(config.ForceSecretKey)
166-
}
167-
168-
// TODO validated profile
169-
170-
////
171-
// Create scaleway SDK client
172-
////
173-
opts := []scw.ClientOption{
174-
scw.WithUserAgent(customizeUserAgent(version.Version, config.TerraformVersion)),
175-
scw.WithProfile(profile),
176-
}
177-
178-
httpClient := &http.Client{Transport: transport.NewRetryableTransport(http.DefaultTransport)}
179-
if config.HTTPClient != nil {
180-
httpClient = config.HTTPClient
181-
}
182-
183-
opts = append(opts, scw.WithHTTPClient(httpClient))
184-
185-
scwClient, err := scw.NewClient(opts...)
186-
if err != nil {
187-
return nil, err
188-
}
189-
190-
return &Meta{
191-
scwClient: scwClient,
192-
httpClient: httpClient,
193-
credentialsSource: credentialsSource,
194-
}, nil
195-
}
196-
197197
func customizeUserAgent(providerVersion string, terraformVersion string) string {
198198
userAgent := fmt.Sprintf("terraform-provider/%s terraform/%s", providerVersion, terraformVersion)
199199

internal/services/instance/instancehelpers/block.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@ import (
1111
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
1212
)
1313

14-
func NewBlockAndInstanceAPI(client *scw.Client) *BlockAndInstanceAPI {
15-
instanceAPI := instance.NewAPI(client)
16-
blockAPI := block.NewAPI(client)
17-
18-
return &BlockAndInstanceAPI{
19-
API: instanceAPI,
20-
BlockAPI: blockAPI,
21-
}
22-
}
23-
2414
// InstanceAndBlockAPIWithZone returns a new instance API and the zone for a Create request
2515
func InstanceAndBlockAPIWithZone(d *schema.ResourceData, m interface{}) (*BlockAndInstanceAPI, scw.Zone, error) {
2616
zone, err := meta.ExtractZone(d, m)
@@ -46,6 +36,16 @@ type BlockAndInstanceAPI struct {
4636
BlockAPI *block.API
4737
}
4838

39+
func NewBlockAndInstanceAPI(client *scw.Client) *BlockAndInstanceAPI {
40+
instanceAPI := instance.NewAPI(client)
41+
blockAPI := block.NewAPI(client)
42+
43+
return &BlockAndInstanceAPI{
44+
API: instanceAPI,
45+
BlockAPI: blockAPI,
46+
}
47+
}
48+
4949
type GetUnknownVolumeRequest struct {
5050
VolumeID string
5151
Zone scw.Zone

internal/workerpool/workerpool.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,31 @@ func NewWorkerPool(size int) *WorkerPool {
2727
return p
2828
}
2929

30+
func (p *WorkerPool) AddTask(task Task) {
31+
p.tasksWaitingGroup.Add(1)
32+
p.tasksToDispatch <- task
33+
}
34+
35+
func (p *WorkerPool) CloseAndWait() []error {
36+
close(p.tasksToDispatch)
37+
p.tasksWaitingGroup.Wait()
38+
39+
return p.errors
40+
}
41+
42+
func (p *WorkerPool) worker() {
43+
for task := range p.tasksToRun {
44+
err := task()
45+
if err != nil {
46+
p.errorsMutex.Lock()
47+
p.errors = append(p.errors, err)
48+
p.errorsMutex.Unlock()
49+
}
50+
51+
p.tasksWaitingGroup.Done()
52+
}
53+
}
54+
3055
func (p *WorkerPool) dispatcher() {
3156
var pendingTasks []Task
3257

@@ -60,28 +85,3 @@ func (p *WorkerPool) dispatcher() {
6085
}
6186
}
6287
}
63-
64-
func (p *WorkerPool) worker() {
65-
for task := range p.tasksToRun {
66-
err := task()
67-
if err != nil {
68-
p.errorsMutex.Lock()
69-
p.errors = append(p.errors, err)
70-
p.errorsMutex.Unlock()
71-
}
72-
73-
p.tasksWaitingGroup.Done()
74-
}
75-
}
76-
77-
func (p *WorkerPool) AddTask(task Task) {
78-
p.tasksWaitingGroup.Add(1)
79-
p.tasksToDispatch <- task
80-
}
81-
82-
func (p *WorkerPool) CloseAndWait() []error {
83-
close(p.tasksToDispatch)
84-
p.tasksWaitingGroup.Wait()
85-
86-
return p.errors
87-
}

0 commit comments

Comments
 (0)