-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Returns every possible parent, including collection segments. Useful for resolving IAM permissions without further knowledge of patterns for specific resource names.
- Loading branch information
Showing
5 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package resourcename | ||
|
||
// RangeParents iterates over all parents of the provided resource name. | ||
// The iteration order is from root ancestor down to the closest parent. | ||
// Collection segments are included in the iteration, so as to not require knowing the pattern. | ||
// For full resource names, the service is omitted. | ||
func RangeParents(name string, fn func(parent string) bool) { | ||
var sc Scanner | ||
sc.Init(name) | ||
// First segment: special-case to handle full resource names. | ||
if !sc.Scan() { | ||
return | ||
} | ||
start := sc.Start() | ||
if sc.End() != len(name) && !fn(name[start:sc.End()]) { | ||
return | ||
} | ||
// Scan remaining segments. | ||
for sc.Scan() { | ||
if sc.End() != len(name) && !fn(name[start:sc.End()]) { | ||
return | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package resourcename | ||
|
||
import ( | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestRangeParents(t *testing.T) { | ||
t.Parallel() | ||
for _, tt := range []struct { | ||
name string | ||
input string | ||
expected []string | ||
}{ | ||
{ | ||
name: "empty", | ||
input: "", | ||
}, | ||
|
||
{ | ||
name: "singleton", | ||
input: "foo", | ||
}, | ||
|
||
{ | ||
name: "single", | ||
input: "foo/bar", | ||
expected: []string{ | ||
"foo", | ||
}, | ||
}, | ||
|
||
{ | ||
name: "multiple", | ||
input: "foo/bar/baz/123", | ||
expected: []string{ | ||
"foo", | ||
"foo/bar", | ||
"foo/bar/baz", | ||
}, | ||
}, | ||
|
||
{ | ||
name: "full", | ||
input: "//test.example.com/foo/bar/baz/123", | ||
expected: []string{ | ||
"foo", | ||
"foo/bar", | ||
"foo/bar/baz", | ||
}, | ||
}, | ||
} { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
var actual []string | ||
RangeParents(tt.input, func(parent string) bool { | ||
actual = append(actual, parent) | ||
return true | ||
}) | ||
assert.DeepEqual(t, tt.expected, actual) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters