Skip to content

Commit f047481

Browse files
Sh4d1jerome-quere
andauthored
feat: custom URL and custom regions/zones (#542)
Signed-off-by: Patrik Cyvoct <[email protected]> Co-authored-by: Jerome Quere <[email protected]>
1 parent 2fe2901 commit f047481

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

scaleway/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"io/ioutil"
1010
"net/http"
1111
"os"
12+
"strings"
1213
"time"
1314

1415
"github.com/aws/aws-sdk-go/aws"
@@ -31,6 +32,7 @@ type Meta struct {
3132
DefaultOrganizationID string
3233
DefaultRegion scw.Region
3334
DefaultZone scw.Zone
35+
APIURL string
3436
TerraformVersion string
3537

3638
// scwClient is the Scaleway SDK client.
@@ -91,6 +93,10 @@ func (m *Meta) bootstrapScwClient() error {
9193
options = append(options, scw.WithDefaultZone(m.DefaultZone))
9294
}
9395

96+
if m.APIURL != "" {
97+
options = append(options, scw.WithAPIURL(m.APIURL))
98+
}
99+
94100
client, err := scw.NewClient(options...)
95101
if err != nil {
96102
return fmt.Errorf("cannot create SDK client: %s", err)
@@ -167,6 +173,11 @@ func (m *Meta) bootstrapDeprecatedClient() error {
167173
options,
168174
)
169175
if err != nil {
176+
// Ugly fix bug this should be removed when we remove support for older resources.
177+
if strings.HasSuffix(err.Error(), "isn't a valid region") {
178+
// will panic if using the deprecated client
179+
return nil
180+
}
170181
return fmt.Errorf("cannot create deprecated SDK client: %s", err)
171182
}
172183

scaleway/provider.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
1010
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1111
"github.com/mitchellh/go-homedir"
12+
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
1213
"github.com/scaleway/scaleway-sdk-go/scw"
1314
)
1415

@@ -168,7 +169,20 @@ func Provider() terraform.ResourceProvider {
168169
},
169170
ValidateFunc: validationZone(),
170171
},
171-
172+
"api_url": {
173+
Type: schema.TypeString,
174+
Optional: true,
175+
Description: "The Scaleway API URL to use.",
176+
DefaultFunc: func() (interface{}, error) {
177+
if envProfile.APIURL != nil {
178+
return *envProfile.APIURL, nil
179+
}
180+
if activeProfile != nil && activeProfile.APIURL != nil {
181+
return *activeProfile.APIURL, nil
182+
}
183+
return nil, nil
184+
},
185+
},
172186
// Deprecated values
173187
"token": {
174188
Type: schema.TypeString,
@@ -294,6 +308,7 @@ func providerConfigure(d *schema.ResourceData, terraformVersion string) (interfa
294308
DefaultRegion: region,
295309
DefaultZone: zone,
296310
TerraformVersion: terraformVersion,
311+
APIURL: d.Get("api_url").(string),
297312
}
298313

299314
err = meta.bootstrap()
@@ -303,10 +318,15 @@ func providerConfigure(d *schema.ResourceData, terraformVersion string) (interfa
303318

304319
// fetch known scaleway server types to support validation in r/server
305320
if len(commercialServerTypes) == 0 {
306-
if availability, err := meta.deprecatedClient.GetServerAvailabilities(); err == nil {
307-
commercialServerTypes = availability.CommercialTypes()
321+
instanceAPI := instance.NewAPI(meta.scwClient)
322+
availabilityResp, err := instanceAPI.GetServerTypesAvailability(&instance.GetServerTypesAvailabilityRequest{}, scw.WithAllPages())
323+
if err == nil {
324+
for k := range availabilityResp.Servers {
325+
commercialServerTypes = append(commercialServerTypes, k)
326+
}
308327
sort.StringSlice(commercialServerTypes).Sort()
309328
}
329+
310330
if os.Getenv("DISABLE_SCALEWAY_SERVER_TYPE_VALIDATION") != "" {
311331
commercialServerTypes = commercialServerTypes[:0]
312332
}

0 commit comments

Comments
 (0)