Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 4 additions & 4 deletions internal/locality/regional/ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ 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,
Region: region,
}
}

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), "/")
Expand Down
8 changes: 4 additions & 4 deletions internal/locality/zonal/ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ 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,
Zone: zone,
}
}

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), "/")
Expand Down
130 changes: 65 additions & 65 deletions internal/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)

Expand Down
20 changes: 10 additions & 10 deletions internal/services/instance/instancehelpers/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
50 changes: 25 additions & 25 deletions internal/workerpool/workerpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Loading