Skip to content

Commit 2a83bc4

Browse files
chore: clean up helpers (#741)
1 parent 43c6f04 commit 2a83bc4

19 files changed

+113
-90
lines changed

docs/resources/baremetal_server.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The following arguments are supported:
3232
- `offer` - (Required) The offer name or UUID of the baremetal server.
3333
Use [this endpoint](https://developers.scaleway.com/en/products/baremetal/api/#get-334154) to find the right offer.
3434

35-
~> **Important:** Updates to `offer_id` will recreate the server.
35+
~> **Important:** Updates to `offer` will recreate the server.
3636

3737
- `os` - (Required) The UUID of the os to install on the server.
3838
Use [this endpoint](https://developers.scaleway.com/en/products/baremetal/api/#get-87598a) to find the right OS ID.

scaleway/helpers.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"net"
77
"net/http"
8-
"regexp"
98
"strings"
109
"time"
1110

@@ -152,14 +151,6 @@ func newRegionalIDString(region scw.Region, id string) string {
152151
return fmt.Sprintf("%s/%s", region, id)
153152
}
154153

155-
// deprecated and should not be used
156-
// newZonedIDStringFromRegion constructs a unique identifier based on resource region and id
157-
// but returns a zoned ID with the first zone in the region, i.e. adding `-1` to the region
158-
// TODO this function is a quick fix
159-
func newZonedIDStringFromRegion(region scw.Region, id string) string {
160-
return fmt.Sprintf("%s-1/%s", region, id)
161-
}
162-
163154
// terraformResourceData is an interface for *schema.ResourceData. (used for mock)
164155
type terraformResourceData interface {
165156
HasChange(string) bool
@@ -188,7 +179,7 @@ func extractZone(d terraformResourceData, meta *Meta) (scw.Zone, error) {
188179
return zone, nil
189180
}
190181

191-
return scw.Zone(""), ErrZoneNotFound
182+
return "", ErrZoneNotFound
192183
}
193184

194185
// ErrRegionNotFound is returned when no region can be detected
@@ -208,7 +199,7 @@ func extractRegion(d terraformResourceData, meta *Meta) (scw.Region, error) {
208199
return region, nil
209200
}
210201

211-
return scw.Region(""), ErrRegionNotFound
202+
return "", ErrRegionNotFound
212203
}
213204

214205
// isHTTPCodeError returns true if err is an http error with code statusCode
@@ -291,13 +282,6 @@ func newRandomName(prefix string) string {
291282

292283
const gb uint64 = 1000 * 1000 * 1000
293284

294-
var UUIDRegex = regexp.MustCompile(`[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}`)
295-
296-
// isUUID returns true if the given string have an UUID format.
297-
func isUUID(s string) bool {
298-
return UUIDRegex.MatchString(s)
299-
}
300-
301285
func flattenTime(date *time.Time) interface{} {
302286
if date != nil {
303287
return date.Format(time.RFC3339)
@@ -428,7 +412,7 @@ func flattenIPNet(ipNet scw.IPNet) string {
428412
// We panic as this should never happen.
429413
panic(err) // lintignore:R009
430414
}
431-
return string(raw[1 : len(raw)-1])
415+
return string(raw[1 : len(raw)-1]) // remove quotes
432416
}
433417

434418
func validateDuration() schema.SchemaValidateFunc {

scaleway/helpers_account.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,5 @@ const (
1313
// accountAPI returns a new account API.
1414
func accountAPI(m interface{}) *account.API {
1515
meta := m.(*Meta)
16-
1716
return account.NewAPI(meta.scwClient)
1817
}

scaleway/helpers_baremetal.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ func baremetalAPIWithZone(d *schema.ResourceData, m interface{}) (*baremetal.API
2020
baremetalAPI := baremetal.NewAPI(meta.scwClient)
2121

2222
zone, err := extractZone(d, meta)
23-
return baremetalAPI, zone, err
23+
if err != nil {
24+
return nil, "", err
25+
}
26+
return baremetalAPI, zone, nil
2427
}
2528

2629
// instanceAPIWithZoneAndID returns an baremetal API with zone and ID extracted from the state
@@ -29,7 +32,10 @@ func baremetalAPIWithZoneAndID(m interface{}, id string) (*baremetal.API, ZonedI
2932
baremetalAPI := baremetal.NewAPI(meta.scwClient)
3033

3134
zone, ID, err := parseZonedID(id)
32-
return baremetalAPI, newZonedID(zone, ID), err
35+
if err != nil {
36+
return nil, ZonedID{}, err
37+
}
38+
return baremetalAPI, newZonedID(zone, ID), nil
3339
}
3440

3541
func flattenBaremetalCPUs(cpus []*baremetal.CPU) interface{} {

scaleway/helpers_instance.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ func instanceAPIWithZone(d *schema.ResourceData, m interface{}) (*instance.API,
3232
instanceAPI := instance.NewAPI(meta.scwClient)
3333

3434
zone, err := extractZone(d, meta)
35-
return instanceAPI, zone, err
35+
if err != nil {
36+
return nil, "", err
37+
}
38+
return instanceAPI, zone, nil
3639
}
3740

3841
// instanceAPIWithZoneAndID returns an instance API with zone and ID extracted from the state
@@ -41,7 +44,10 @@ func instanceAPIWithZoneAndID(m interface{}, zonedID string) (*instance.API, scw
4144
instanceAPI := instance.NewAPI(meta.scwClient)
4245

4346
zone, ID, err := parseZonedID(zonedID)
44-
return instanceAPI, zone, ID, err
47+
if err != nil {
48+
return nil, "", "", err
49+
}
50+
return instanceAPI, zone, ID, nil
4551
}
4652

4753
// instanceAPIWithZoneAndNestedID returns an instance API with zone and inner/outer ID extracted from the state
@@ -50,7 +56,10 @@ func instanceAPIWithZoneAndNestedID(m interface{}, zonedNestedID string) (*insta
5056
instanceAPI := instance.NewAPI(meta.scwClient)
5157

5258
zone, innerID, outerID, err := parseZonedNestedID(zonedNestedID)
53-
return instanceAPI, zone, innerID, outerID, err
59+
if err != nil {
60+
return nil, "", "", "", err
61+
}
62+
return instanceAPI, zone, innerID, outerID, nil
5463
}
5564

5665
// hash hashes a string to a unique hashcode.

scaleway/helpers_k8s.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,21 @@ func k8sAPIWithRegion(d *schema.ResourceData, m interface{}) (*k8s.API, scw.Regi
4949
k8sAPI := k8s.NewAPI(meta.scwClient)
5050

5151
region, err := extractRegion(d, meta)
52-
53-
return k8sAPI, region, err
52+
if err != nil {
53+
return nil, "", err
54+
}
55+
return k8sAPI, region, nil
5456
}
5557

5658
func k8sAPIWithRegionAndID(m interface{}, id string) (*k8s.API, scw.Region, string, error) {
5759
meta := m.(*Meta)
5860
k8sAPI := k8s.NewAPI(meta.scwClient)
5961

6062
region, ID, err := parseRegionalID(id)
61-
return k8sAPI, region, ID, err
63+
if err != nil {
64+
return nil, "", "", err
65+
}
66+
return k8sAPI, region, ID, nil
6267
}
6368

6469
func k8sGetMinorVersionFromFull(version string) (string, error) {

scaleway/helpers_lb.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ func lbAPIWithRegion(d *schema.ResourceData, m interface{}) (*lb.API, scw.Region
2626
lbAPI := lb.NewAPI(meta.scwClient)
2727

2828
region, err := extractRegion(d, meta)
29-
return lbAPI, region, err
29+
if err != nil {
30+
return nil, "", err
31+
}
32+
return lbAPI, region, nil
3033
}
3134

3235
// lbAPIWithRegionAndID returns an lb API with region and ID extracted from the state
@@ -35,7 +38,10 @@ func lbAPIWithRegionAndID(m interface{}, id string) (*lb.API, scw.Region, string
3538
lbAPI := lb.NewAPI(meta.scwClient)
3639

3740
region, ID, err := parseRegionalID(id)
38-
return lbAPI, region, ID, err
41+
if err != nil {
42+
return nil, "", "", err
43+
}
44+
return lbAPI, region, ID, nil
3945
}
4046

4147
func flattenLbBackendMarkdownAction(action lb.OnMarkedDownAction) interface{} {
@@ -88,6 +94,7 @@ func expandLbACLAction(raw interface{}) *lb.ACLAction {
8894
Type: lb.ACLActionType(rawMap["type"].(string)),
8995
}
9096
}
97+
9198
func flattenLbACLMatch(match *lb.ACLMatch) interface{} {
9299
return []map[string]interface{}{
93100
{

scaleway/helpers_marketplace.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ func marketplaceAPIWithZone(d *schema.ResourceData, m interface{}) (*marketplace
1212
marketplaceAPI := marketplace.NewAPI(meta.scwClient)
1313

1414
zone, err := extractZone(d, meta)
15-
return marketplaceAPI, zone, err
15+
if err != nil {
16+
return nil, "", err
17+
}
18+
return marketplaceAPI, zone, nil
1619
}

scaleway/helpers_object.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ func flattenObjectBucketTags(tagsSet []*s3.Tag) map[string]interface{} {
9797
}
9898

9999
func expandObjectBucketTags(tags interface{}) []*s3.Tag {
100-
tagsSet := make([]*s3.Tag, 0)
101-
100+
tagsSet := []*s3.Tag(nil)
102101
for key, value := range tags.(map[string]interface{}) {
103102
tagsSet = append(tagsSet, &s3.Tag{
104103
Key: &key,
@@ -126,30 +125,18 @@ func isS3Err(err error, code string, message string) bool {
126125
}
127126

128127
func flattenObjectBucketVersioning(versioningResponse *s3.GetBucketVersioningOutput) []map[string]interface{} {
129-
vcl := make([]map[string]interface{}, 0, 1)
130-
vc := make(map[string]interface{})
131-
if versioningResponse.Status != nil && aws.StringValue(versioningResponse.Status) == s3.BucketVersioningStatusEnabled {
132-
vc["enabled"] = true
133-
} else {
134-
vc["enabled"] = false
135-
}
136-
vcl = append(vcl, vc)
128+
vcl := []map[string]interface{}{{}}
129+
vcl[0]["enabled"] = versioningResponse.Status != nil && *versioningResponse.Status == s3.BucketVersioningStatusEnabled
137130
return vcl
138131
}
139132

140133
func expandObjectBucketVersioning(v []interface{}) *s3.VersioningConfiguration {
141134
vc := &s3.VersioningConfiguration{}
142-
135+
vc.Status = scw.StringPtr(s3.BucketVersioningStatusSuspended)
143136
if len(v) > 0 {
144-
c := v[0].(map[string]interface{})
145-
146-
if c["enabled"].(bool) {
137+
if c := v[0].(map[string]interface{}); c["enabled"].(bool) {
147138
vc.Status = scw.StringPtr(s3.BucketVersioningStatusEnabled)
148-
} else {
149-
vc.Status = scw.StringPtr(s3.BucketVersioningStatusSuspended)
150139
}
151-
} else {
152-
vc.Status = scw.StringPtr(s3.BucketVersioningStatusSuspended)
153140
}
154141
return vc
155142
}

scaleway/helpers_rdb.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ func rdbAPIWithRegion(d *schema.ResourceData, m interface{}) (*rdb.API, scw.Regi
1818
rdbAPI := rdb.NewAPI(meta.scwClient)
1919

2020
region, err := extractRegion(d, meta)
21-
return rdbAPI, region, err
21+
if err != nil {
22+
return nil, "", err
23+
}
24+
return rdbAPI, region, nil
2225
}
2326

2427
// rdbAPIWithRegionAndID returns an lb API with region and ID extracted from the state
@@ -27,7 +30,10 @@ func rdbAPIWithRegionAndID(m interface{}, id string) (*rdb.API, scw.Region, stri
2730
rdbAPI := rdb.NewAPI(meta.scwClient)
2831

2932
region, ID, err := parseRegionalID(id)
30-
return rdbAPI, region, ID, err
33+
if err != nil {
34+
return nil, "", "", err
35+
}
36+
return rdbAPI, region, ID, nil
3137
}
3238

3339
func flattenRdbInstanceReadReplicas(readReplicas []*rdb.Endpoint) interface{} {

scaleway/helpers_registry.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ func registryAPIWithRegion(d *schema.ResourceData, m interface{}) (*registry.API
1818
api := registry.NewAPI(meta.scwClient)
1919

2020
region, err := extractRegion(d, meta)
21-
return api, region, err
21+
if err != nil {
22+
return nil, "", err
23+
}
24+
return api, region, nil
2225
}
2326

2427
// registryAPIWithRegionAndID returns a new container registry API, region and ID.
@@ -27,5 +30,8 @@ func registryAPIWithRegionAndID(m interface{}, id string) (*registry.API, scw.Re
2730
api := registry.NewAPI(meta.scwClient)
2831

2932
region, id, err := parseRegionalID(id)
30-
return api, region, id, err
33+
if err != nil {
34+
return nil, "", "", err
35+
}
36+
return api, region, id, nil
3137
}

scaleway/helpers_test.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net"
66
"net/http"
7+
"regexp"
78
"strings"
89
"testing"
910

@@ -46,15 +47,15 @@ func TestParseLocalizedID(t *testing.T) {
4647
},
4748
}
4849

49-
for _, testCase := range testCases {
50-
t.Run(testCase.name, func(t *testing.T) {
51-
locality, id, err := parseLocalizedID(testCase.localityID)
52-
if testCase.err != "" {
53-
require.EqualError(t, err, testCase.err)
50+
for _, tc := range testCases {
51+
t.Run(tc.name, func(t *testing.T) {
52+
locality, id, err := parseLocalizedID(tc.localityID)
53+
if tc.err != "" {
54+
require.EqualError(t, err, tc.err)
5455
} else {
5556
require.NoError(t, err)
56-
assert.Equal(t, testCase.locality, locality)
57-
assert.Equal(t, testCase.id, id)
57+
assert.Equal(t, tc.locality, locality)
58+
assert.Equal(t, tc.id, id)
5859
}
5960
})
6061
}
@@ -86,15 +87,15 @@ func TestParseZonedID(t *testing.T) {
8687
},
8788
}
8889

89-
for _, testCase := range testCases {
90-
t.Run(testCase.name, func(t *testing.T) {
91-
zone, id, err := parseZonedID(testCase.localityID)
92-
if testCase.err != "" {
93-
require.EqualError(t, err, testCase.err)
90+
for _, tc := range testCases {
91+
t.Run(tc.name, func(t *testing.T) {
92+
zone, id, err := parseZonedID(tc.localityID)
93+
if tc.err != "" {
94+
require.EqualError(t, err, tc.err)
9495
} else {
9596
require.NoError(t, err)
96-
assert.Equal(t, testCase.zone, zone)
97-
assert.Equal(t, testCase.id, id)
97+
assert.Equal(t, tc.zone, zone)
98+
assert.Equal(t, tc.id, id)
9899
}
99100
})
100101
}
@@ -126,15 +127,15 @@ func TestParseRegionID(t *testing.T) {
126127
},
127128
}
128129

129-
for _, testCase := range testCases {
130-
t.Run(testCase.name, func(t *testing.T) {
131-
region, id, err := parseRegionalID(testCase.localityID)
132-
if testCase.err != "" {
133-
require.EqualError(t, err, testCase.err)
130+
for _, tc := range testCases {
131+
t.Run(tc.name, func(t *testing.T) {
132+
region, id, err := parseRegionalID(tc.localityID)
133+
if tc.err != "" {
134+
require.EqualError(t, err, tc.err)
134135
} else {
135136
require.NoError(t, err)
136-
assert.Equal(t, testCase.region, region)
137-
assert.Equal(t, testCase.id, id)
137+
assert.Equal(t, tc.region, region)
138+
assert.Equal(t, tc.id, id)
138139
}
139140
})
140141
}
@@ -190,6 +191,8 @@ func testCheckResourceAttrFunc(name string, key string, test func(string) error)
190191
}
191192
}
192193

194+
var UUIDRegex = regexp.MustCompile(`[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}`)
195+
193196
func testCheckResourceAttrUUID(name string, key string) resource.TestCheckFunc {
194197
return resource.TestMatchResourceAttr(name, key, UUIDRegex)
195198
}

0 commit comments

Comments
 (0)