Skip to content

Commit

Permalink
Some changes to conform the changes in 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcturusZhang committed Feb 25, 2020
1 parent bed8110 commit 496c912
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
28 changes: 19 additions & 9 deletions azurerm/internal/services/policyinsights/parse/remediation.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,25 @@ type ManagementGroupId struct {
}

func ManagementGroupID(input string) (*ManagementGroupId, error) {
// a management group id should be like /providers/Microsoft.Management/managementGroups/00000000-0000-0000-0000-000000000000
regex := regexp.MustCompile(`/providers/[Mm]icrosoft\.[Mm]anagement/management[Gg]roups/([0-9abcdef]{8}-[0-9abcdef]{4}-[0-9abcdef]{4}-[0-9abcdef]{4}-[0-9abcdef]{12})`)
if regex.MatchString(input) {
groups := regex.FindStringSubmatch(input)
id := ManagementGroupId{
GroupId: groups[1],
}
return &id, nil
regex := regexp.MustCompile(`^/providers/[Mm]icrosoft\.[Mm]anagement/management[Gg]roups/`)
if !regex.MatchString(input) {
return nil, fmt.Errorf("Unable to parse Management Group ID %q", input)
}

// Split the input ID by the regex
segments := regex.Split(input, -1)
if len(segments) != 2 {
return nil, fmt.Errorf("Unable to parse Management Group ID %q: expected id to have two segments after splitting", input)
}
groupID := segments[1]
// portal says: The name can only be an ASCII letter, digit, -, _, (, ), . and have a maximum length constraint of 90
if matched := regexp.MustCompile(`^[a-zA-Z0-9_().-]{1,90}$`).Match([]byte(groupID)); !matched {
return nil, fmt.Errorf("Unable to parse Management Group ID %q: group id can only consist of ASCII letters, digits, -, _, (, ), . , and cannot exceed the maximum length of 90", input)
}

return nil, fmt.Errorf("Cannot parse %q as management group id", input)
id := ManagementGroupId{
GroupId: groupID,
}

return &id, nil
}
28 changes: 28 additions & 0 deletions azurerm/internal/services/policyinsights/parse/remediation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ func TestRemediationID(t *testing.T) {
},
},
},
{
Name: "Policy Remediation ID at Management Group with readable id",
Input: "/providers/Microsoft.Management/managementGroups/group1/providers/Microsoft.PolicyInsights/remediations/test",
Expected: &RemediationId{
Name: "test",
RemediationScopeId: RemediationScopeId{
Type: AtManagementGroup,
ScopeId: "/providers/Microsoft.Management/managementGroups/group1",
ManagementGroupId: "group1",
},
},
},
{
Name: "Policy Remediation ID at Management Group with wrong casing",
Input: "/providers/microsoft.management/managementgroups/00000000-0000-0000-0000-000000000000/providers/microsoft.policyinsights/remediations/test",
Expand Down Expand Up @@ -259,6 +271,15 @@ func TestRemediationScopeID(t *testing.T) {
ManagementGroupId: "00000000-0000-0000-0000-000000000000",
},
},
{
Name: "Management Group ID with readable id",
Input: "/providers/Microsoft.Management/managementGroups/group1",
Expected: &RemediationScopeId{
Type: AtManagementGroup,
ScopeId: "/providers/Microsoft.Management/managementGroups/group1",
ManagementGroupId: "group1",
},
},
}

for _, v := range testData {
Expand Down Expand Up @@ -344,6 +365,13 @@ func TestManagementGroupID(t *testing.T) {
GroupId: "00000000-0000-0000-0000-000000000000",
},
},
{
Name: "Management Group ID with readable name",
Input: "/providers/Microsoft.Management/managementGroups/group1",
Expected: &ManagementGroupId{
GroupId: "group1",
},
},
{
Name: "Management Group ID with wrong casing in provider",
Input: "/providers/microsoft.management/managementGroups/00000000-0000-0000-0000-000000000000",
Expand Down
6 changes: 6 additions & 0 deletions azurerm/internal/services/policyinsights/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ func (r Registration) Name() string {
return "PolicyInsights"
}

func (r Registration) WebsiteCategories() []string {
return []string{
"Policy",
}
}

// SupportedDataSources returns the supported Data Sources supported by this Service
func (r Registration) SupportedDataSources() map[string]*schema.Resource {
return map[string]*schema.Resource{
Expand Down

0 comments on commit 496c912

Please sign in to comment.