From 4bbae24ad107c012a3955ea6a57cb27b50f57063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Wed, 11 Jun 2025 15:20:57 +0200 Subject: [PATCH 1/2] chore: add support for funcorder --- .golangci.yml | 1 + internal/locality/regional/ids.go | 8 +-- internal/locality/zonal/ids.go | 8 +-- .../instance/instancehelpers/block.go | 20 ++++---- internal/workerpool/workerpool.go | 50 +++++++++---------- 5 files changed, 44 insertions(+), 43 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 3f43ca0128..5273a42d4a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -29,6 +29,7 @@ linters: - exptostd # Detects functions from golang.org/x/exp/ that can be replaced by std functions. [auto-fix] - fatcontext # Detects nested contexts in loops and function literals. [auto-fix] - forbidigo # Forbids identifiers [fast: true, auto-fix: false] + - funcorder # Checks the order of functions, methods, and constructors. [fast] - gocheckcompilerdirectives # Checks that go compiler directive comments (//go:) are valid. [fast: true, auto-fix: false] - gochecksumtype # Run exhaustiveness checks on Go "sum types" [fast: false, auto-fix: false] - goconst # Finds repeated strings that could be replaced by a constant [fast: true, auto-fix: false] diff --git a/internal/locality/regional/ids.go b/internal/locality/regional/ids.go index 5690d5ed0e..c7755ce782 100644 --- a/internal/locality/regional/ids.go +++ b/internal/locality/regional/ids.go @@ -14,10 +14,6 @@ type ID struct { Region scw.Region } -func (z ID) String() string { - return fmt.Sprintf("%s/%s", z.Region, z.ID) -} - func NewID(region scw.Region, id string) ID { return ID{ ID: id, @@ -25,6 +21,10 @@ func NewID(region scw.Region, id string) ID { } } +func (z ID) String() string { + return fmt.Sprintf("%s/%s", z.Region, z.ID) +} + func ExpandID(id interface{}) ID { regionalID := ID{} tab := strings.Split(id.(string), "/") diff --git a/internal/locality/zonal/ids.go b/internal/locality/zonal/ids.go index 15462642a4..f38323e3ee 100644 --- a/internal/locality/zonal/ids.go +++ b/internal/locality/zonal/ids.go @@ -14,10 +14,6 @@ type ID struct { Zone scw.Zone } -func (z ID) String() string { - return fmt.Sprintf("%s/%s", z.Zone, z.ID) -} - func NewID(zone scw.Zone, id string) ID { return ID{ ID: id, @@ -25,6 +21,10 @@ func NewID(zone scw.Zone, id string) ID { } } +func (z ID) String() string { + return fmt.Sprintf("%s/%s", z.Zone, z.ID) +} + func ExpandID(id interface{}) ID { zonedID := ID{} tab := strings.Split(id.(string), "/") diff --git a/internal/services/instance/instancehelpers/block.go b/internal/services/instance/instancehelpers/block.go index 4e4b581c69..9103b24afd 100644 --- a/internal/services/instance/instancehelpers/block.go +++ b/internal/services/instance/instancehelpers/block.go @@ -11,16 +11,6 @@ import ( "github.com/scaleway/terraform-provider-scaleway/v2/internal/meta" ) -func NewBlockAndInstanceAPI(client *scw.Client) *BlockAndInstanceAPI { - instanceAPI := instance.NewAPI(client) - blockAPI := block.NewAPI(client) - - return &BlockAndInstanceAPI{ - API: instanceAPI, - BlockAPI: blockAPI, - } -} - // InstanceAndBlockAPIWithZone returns a new instance API and the zone for a Create request func InstanceAndBlockAPIWithZone(d *schema.ResourceData, m interface{}) (*BlockAndInstanceAPI, scw.Zone, error) { zone, err := meta.ExtractZone(d, m) @@ -46,6 +36,16 @@ type BlockAndInstanceAPI struct { BlockAPI *block.API } +func NewBlockAndInstanceAPI(client *scw.Client) *BlockAndInstanceAPI { + instanceAPI := instance.NewAPI(client) + blockAPI := block.NewAPI(client) + + return &BlockAndInstanceAPI{ + API: instanceAPI, + BlockAPI: blockAPI, + } +} + type GetUnknownVolumeRequest struct { VolumeID string Zone scw.Zone diff --git a/internal/workerpool/workerpool.go b/internal/workerpool/workerpool.go index ea26eca145..936b532819 100644 --- a/internal/workerpool/workerpool.go +++ b/internal/workerpool/workerpool.go @@ -27,6 +27,31 @@ func NewWorkerPool(size int) *WorkerPool { return p } +func (p *WorkerPool) AddTask(task Task) { + p.tasksWaitingGroup.Add(1) + p.tasksToDispatch <- task +} + +func (p *WorkerPool) CloseAndWait() []error { + close(p.tasksToDispatch) + p.tasksWaitingGroup.Wait() + + return p.errors +} + +func (p *WorkerPool) worker() { + for task := range p.tasksToRun { + err := task() + if err != nil { + p.errorsMutex.Lock() + p.errors = append(p.errors, err) + p.errorsMutex.Unlock() + } + + p.tasksWaitingGroup.Done() + } +} + func (p *WorkerPool) dispatcher() { var pendingTasks []Task @@ -60,28 +85,3 @@ func (p *WorkerPool) dispatcher() { } } } - -func (p *WorkerPool) worker() { - for task := range p.tasksToRun { - err := task() - if err != nil { - p.errorsMutex.Lock() - p.errors = append(p.errors, err) - p.errorsMutex.Unlock() - } - - p.tasksWaitingGroup.Done() - } -} - -func (p *WorkerPool) AddTask(task Task) { - p.tasksWaitingGroup.Add(1) - p.tasksToDispatch <- task -} - -func (p *WorkerPool) CloseAndWait() []error { - close(p.tasksToDispatch) - p.tasksWaitingGroup.Wait() - - return p.errors -} From 3d12e58118d80ff6d78d239ea9e34be770c783eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Fri, 13 Jun 2025 14:28:02 +0200 Subject: [PATCH 2/2] Fix --- internal/meta/meta.go | 130 +++++++++++++++++++++--------------------- 1 file changed, 65 insertions(+), 65 deletions(-) diff --git a/internal/meta/meta.go b/internal/meta/meta.go index d564f788b9..a3cfd60e3c 100644 --- a/internal/meta/meta.go +++ b/internal/meta/meta.go @@ -49,6 +49,71 @@ type Meta struct { credentialsSource *CredentialsSource } +// NewMeta creates the Meta object containing the SDK client. +func NewMeta(ctx context.Context, config *Config) (*Meta, error) { + //// + // Load Profile + //// + profile, credentialsSource, err := loadProfile(ctx, config.ProviderSchema) + if err != nil { + return nil, err + } + + if config.ForceZone != "" { + region, err := config.ForceZone.Region() + if err != nil { + return nil, err + } + + profile.DefaultRegion = scw.StringPtr(region.String()) + profile.DefaultZone = scw.StringPtr(config.ForceZone.String()) + } + + if config.ForceProjectID != "" { + profile.DefaultProjectID = scw.StringPtr(config.ForceProjectID) + } + + if config.ForceOrganizationID != "" { + profile.DefaultOrganizationID = scw.StringPtr(config.ForceOrganizationID) + } + + if config.ForceAccessKey != "" { + profile.AccessKey = scw.StringPtr(config.ForceAccessKey) + } + + if config.ForceSecretKey != "" { + profile.SecretKey = scw.StringPtr(config.ForceSecretKey) + } + + // TODO validated profile + + //// + // Create scaleway SDK client + //// + opts := []scw.ClientOption{ + scw.WithUserAgent(customizeUserAgent(version.Version, config.TerraformVersion)), + scw.WithProfile(profile), + } + + httpClient := &http.Client{Transport: transport.NewRetryableTransport(http.DefaultTransport)} + if config.HTTPClient != nil { + httpClient = config.HTTPClient + } + + opts = append(opts, scw.WithHTTPClient(httpClient)) + + scwClient, err := scw.NewClient(opts...) + if err != nil { + return nil, err + } + + return &Meta{ + scwClient: scwClient, + httpClient: httpClient, + credentialsSource: credentialsSource, + }, nil +} + func (m Meta) ScwClient() *scw.Client { return m.scwClient } @@ -129,71 +194,6 @@ type Config struct { ForceSecretKey string } -// NewMeta creates the Meta object containing the SDK client. -func NewMeta(ctx context.Context, config *Config) (*Meta, error) { - //// - // Load Profile - //// - profile, credentialsSource, err := loadProfile(ctx, config.ProviderSchema) - if err != nil { - return nil, err - } - - if config.ForceZone != "" { - region, err := config.ForceZone.Region() - if err != nil { - return nil, err - } - - profile.DefaultRegion = scw.StringPtr(region.String()) - profile.DefaultZone = scw.StringPtr(config.ForceZone.String()) - } - - if config.ForceProjectID != "" { - profile.DefaultProjectID = scw.StringPtr(config.ForceProjectID) - } - - if config.ForceOrganizationID != "" { - profile.DefaultOrganizationID = scw.StringPtr(config.ForceOrganizationID) - } - - if config.ForceAccessKey != "" { - profile.AccessKey = scw.StringPtr(config.ForceAccessKey) - } - - if config.ForceSecretKey != "" { - profile.SecretKey = scw.StringPtr(config.ForceSecretKey) - } - - // TODO validated profile - - //// - // Create scaleway SDK client - //// - opts := []scw.ClientOption{ - scw.WithUserAgent(customizeUserAgent(version.Version, config.TerraformVersion)), - scw.WithProfile(profile), - } - - httpClient := &http.Client{Transport: transport.NewRetryableTransport(http.DefaultTransport)} - if config.HTTPClient != nil { - httpClient = config.HTTPClient - } - - opts = append(opts, scw.WithHTTPClient(httpClient)) - - scwClient, err := scw.NewClient(opts...) - if err != nil { - return nil, err - } - - return &Meta{ - scwClient: scwClient, - httpClient: httpClient, - credentialsSource: credentialsSource, - }, nil -} - func customizeUserAgent(providerVersion string, terraformVersion string) string { userAgent := fmt.Sprintf("terraform-provider/%s terraform/%s", providerVersion, terraformVersion)