Skip to content

Commit c033d99

Browse files
authored
fix(object): handles nested id (#1690)
1 parent 3ef9362 commit c033d99

File tree

4 files changed

+1826
-179
lines changed

4 files changed

+1826
-179
lines changed

scaleway/helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ func parseLocalizedID(localizedID string) (locality, id string, err error) {
104104
// parseLocalizedNestedID parses a localizedNestedID and extracts the resource locality, the inner and outer id.
105105
func parseLocalizedNestedID(localizedID string) (locality string, innerID, outerID string, err error) {
106106
tab := strings.Split(localizedID, "/")
107-
if len(tab) != 3 {
107+
if len(tab) < 3 {
108108
return "", "", localizedID, fmt.Errorf("cant parse localized id: %s", localizedID)
109109
}
110-
return tab[0], tab[1], tab[2], nil
110+
return tab[0], tab[1], strings.Join(tab[2:], "/"), nil
111111
}
112112

113113
// parseLocalizedNestedID parses a localizedNestedOwnerID and extracts the resource locality, the inner and outer id and owner.

scaleway/helpers_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,61 @@ func TestParseLocalizedID(t *testing.T) {
6161
}
6262
}
6363

64+
func TestParseLocalizedNestedID(t *testing.T) {
65+
testCases := []struct {
66+
name string
67+
localityID string
68+
innerID string
69+
outerID string
70+
locality string
71+
err string
72+
}{
73+
{
74+
name: "id with a sub directory",
75+
localityID: "fr-par/my-id/subdir",
76+
innerID: "my-id",
77+
outerID: "subdir",
78+
locality: "fr-par",
79+
},
80+
{
81+
name: "id with multiple sub directories",
82+
localityID: "fr-par/my-id/subdir/foo/bar",
83+
innerID: "my-id",
84+
outerID: "subdir/foo/bar",
85+
locality: "fr-par",
86+
},
87+
{
88+
name: "simple",
89+
localityID: "fr-par-1/my-id",
90+
err: "cant parse localized id: fr-par-1/my-id",
91+
},
92+
{
93+
name: "empty",
94+
localityID: "",
95+
err: "cant parse localized id: ",
96+
},
97+
{
98+
name: "without locality",
99+
localityID: "my-id",
100+
err: "cant parse localized id: my-id",
101+
},
102+
}
103+
104+
for _, tc := range testCases {
105+
t.Run(tc.name, func(t *testing.T) {
106+
locality, innerID, outerID, err := parseLocalizedNestedID(tc.localityID)
107+
if tc.err != "" {
108+
require.EqualError(t, err, tc.err)
109+
} else {
110+
require.NoError(t, err)
111+
assert.Equal(t, tc.locality, locality)
112+
assert.Equal(t, tc.innerID, innerID)
113+
assert.Equal(t, tc.outerID, outerID)
114+
}
115+
})
116+
}
117+
}
118+
64119
func TestParseZonedID(t *testing.T) {
65120
testCases := []struct {
66121
name string

scaleway/resource_object_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,42 @@ func TestAccScalewayObject_Basic(t *testing.T) {
4242
testAccCheckScalewayObjectExists(tt, "scaleway_object.file"),
4343
),
4444
},
45+
{
46+
Config: fmt.Sprintf(`
47+
resource "scaleway_object_bucket" "base-01" {
48+
name = "%s"
49+
tags = {
50+
foo = "bar"
51+
}
52+
}
53+
54+
resource scaleway_object "file" {
55+
bucket = scaleway_object_bucket.base-01.name
56+
key = "myfile/foo"
57+
}
58+
`, bucketName),
59+
Check: resource.ComposeTestCheckFunc(
60+
testAccCheckScalewayObjectExists(tt, "scaleway_object.file"),
61+
),
62+
},
63+
{
64+
Config: fmt.Sprintf(`
65+
resource "scaleway_object_bucket" "base-01" {
66+
name = "%s"
67+
tags = {
68+
foo = "bar"
69+
}
70+
}
71+
72+
resource scaleway_object "file" {
73+
bucket = scaleway_object_bucket.base-01.name
74+
key = "myfile/foo/bar"
75+
}
76+
`, bucketName),
77+
Check: resource.ComposeTestCheckFunc(
78+
testAccCheckScalewayObjectExists(tt, "scaleway_object.file"),
79+
),
80+
},
4581
},
4682
})
4783
}

0 commit comments

Comments
 (0)