Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resourceid: parser supports data plane host segment #222

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions resourcemanager/resourceids/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ func descriptionForSpecifiedSegment(segment Segment) (*string, error) {
msg := fmt.Sprintf("should be the user specified value for this %s [for example %q]", name, segment.ExampleValue)
return &msg, nil
}
case DataPlaneHostSegmentType:
{
msg := fmt.Sprintf("should be the data plane host for this %s [for example %q]", segment.Name, segment.ExampleValue)
return &msg, nil
}
}

return nil, fmt.Errorf("internal-error: the Segment Type %q was not implemented for Segment %q", string(segment.Type), segment.Name)
Expand Down
12 changes: 12 additions & 0 deletions resourcemanager/resourceids/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ const (

// UserSpecifiedSegmentType specifies that this Segment is User-Specifiable
UserSpecifiedSegmentType SegmentType = "UserSpecified"

// DataPlaneHostSegmentType sepcifies that this Segment is a Data Plane Host
DataPlaneHostSegmentType SegmentType = "DataPlaneHost"
)

// ConstantSegment is a helper which returns a Segment for a Constant
Expand Down Expand Up @@ -130,3 +133,12 @@ func UserSpecifiedSegment(name, exampleValue string) Segment {
ExampleValue: exampleValue,
}
}

// DataPlaneHostSegment is a helper which returns a Segment for a Data Plane Host
func DataPlaneHostSegment(name, exampleValue string) Segment {
return Segment{
Name: name,
Type: DataPlaneHostSegmentType,
ExampleValue: exampleValue,
}
}
21 changes: 20 additions & 1 deletion resourcemanager/resourceids/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package resourceids

import (
"fmt"
"net/url"
"regexp"
"strings"
)
Expand Down Expand Up @@ -100,6 +101,10 @@ func (p Parser) Parse(input string, insensitively bool) (*ParseResult, error) {
}

switch segment.Type {
case DataPlaneHostSegmentType:
if i != 0 {
return nil, fmt.Errorf("internal error: data plane host segment %q is not at the start of the Resource ID", segment.Name)
}
case ConstantSegmentType:
{
if segment.PossibleValues == nil {
Expand Down Expand Up @@ -155,8 +160,22 @@ func (p Parser) Parse(input string, insensitively bool) (*ParseResult, error) {
uri = fmt.Sprintf("fakestart/%s", uri)
}

var domain string
if p.segments[0].Type == DataPlaneHostSegmentType {
if u, err := url.Parse(uri); err != nil {
return nil, fmt.Errorf("parsing data plane host segment: %+v", err)
} else {
domain = fmt.Sprintf("%s://%s", u.Scheme, u.Host)
uri = u.Path
}
}

uri = strings.TrimPrefix(uri, "/")
split := strings.Split(uri, "/")
if domain != "" {
split = append([]string{domain}, split...)
}

segmentCount := len(split)
if segmentCount < len(p.segments) {
return nil, NewNumberOfSegmentsDidntMatchError(p.resourceId, parseResult)
Expand Down Expand Up @@ -270,7 +289,7 @@ func (p Parser) parseSegment(segment Segment, rawValue string, insensitively boo
return nil, fmt.Errorf("internal error: scope segments aren't supported unless at the start or the end")
}

case ResourceGroupSegmentType, SubscriptionIdSegmentType, UserSpecifiedSegmentType:
case ResourceGroupSegmentType, SubscriptionIdSegmentType, UserSpecifiedSegmentType, DataPlaneHostSegmentType:
{
return &rawValue, nil
}
Expand Down
44 changes: 44 additions & 0 deletions resourcemanager/resourceids/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,50 @@ func TestParseIdContainingJustAScope(t *testing.T) {
}
}

func TestParseIdDataPlane(t *testing.T) {
segments := []resourceids.Segment{
resourceids.DataPlaneHostSegment("domainName", "domainValue"),
resourceids.StaticSegment("staticExample", "example", "example"),
resourceids.UserSpecifiedSegment("fooName", "fooValue"),
resourceids.UserSpecifiedSegment("barName", "barValue"),
}
testData := []struct {
name string
input string
expected *resourceids.ParseResult
insensitive bool
}{
{
name: "empty",
input: "",
insensitive: false,
},
{
name: "with https",
input: "https://example.com/example/foo/bar",
insensitive: false,
expected: &resourceids.ParseResult{
Parsed: map[string]string{
"domainName": "https://example.com",
"staticExample": "example",
"fooName": "foo",
"barName": "bar",
},
RawInput: "https://example.com/example/foo/bar",
},
},
}
for _, test := range testData {
t.Logf("Test %q..", test.name)
rid := fakeIdParser{
segments,
}
parser := resourceids.NewParserFromResourceIdType(rid)
actual, err := parser.Parse(test.input, test.insensitive)
validateResult(t, actual, test.expected, err)
}
}

var _ resourceids.ResourceId = fakeIdParser{}

type fakeIdParser struct {
Expand Down