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

policy_assignment - allow scopes without subscription/<id> #6576

Merged
merged 1 commit into from
Apr 23, 2020
Merged
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
10 changes: 9 additions & 1 deletion azurerm/internal/services/policy/parse/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ type PolicyDefinitionId struct {

// TODO: This parsing function is currently suppressing every case difference due to github issue: https://github.com/Azure/azure-rest-api-specs/issues/8353
func PolicyDefinitionID(input string) (*PolicyDefinitionId, error) {
// in general, the id of a definition should be:
// in general, the id of a definition should be (for custom policy definition):
// {scope}/providers/Microsoft.Authorization/policyDefinitions/{name}
// and for built-in policy-definition:
// /providers/Microsoft.Authorization/policyDefinitions/{name}
regex := regexp.MustCompile(`/providers/[Mm]icrosoft\.[Aa]uthorization/policy[Dd]efinitions/`)
if !regex.MatchString(input) {
return nil, fmt.Errorf("unable to parse Policy Definition ID %q", input)
Expand All @@ -31,6 +33,12 @@ func PolicyDefinitionID(input string) (*PolicyDefinitionId, error) {
return nil, fmt.Errorf("unable to parse Policy Definition ID %q: definition name is empty", input)
}

if scope == "" {
return &PolicyDefinitionId{
Name: name,
}, nil
}

scopeId, err := PolicyScopeID(scope)
if err != nil {
return nil, fmt.Errorf("unable to parse Policy Definition ID %q: %+v", input, err)
Expand Down
7 changes: 7 additions & 0 deletions azurerm/internal/services/policy/parse/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ func TestValidatePolicyDefinitionID(t *testing.T) {
Input: "",
Error: true,
},
{
Name: "built-in policy definition ID",
Input: "/providers/Microsoft.Authorization/policyDefinitions/00000000-0000-0000-0000-000000000000",
Expected: &PolicyDefinitionId{
Name: "00000000-0000-0000-0000-000000000000",
},
},
{
Name: "regular policy definition",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/policyDefinitions/def1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,33 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
)

func TestAccAzureRMPolicyAssignment_basic(t *testing.T) {
func TestAccAzureRMPolicyAssignment_basicCustom(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_policy_assignment", "test")
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMPolicyAssignmentDestroy,
Steps: []resource.TestStep{
{
Config: testAzureRMPolicyAssignment_basic(data),
Config: testAzureRMPolicyAssignment_basicCustom(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMPolicyAssignmentExists(data.ResourceName),
),
},
data.ImportStep(),
},
})
}

func TestAccAzureRMPolicyAssignment_basicBuiltin(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_policy_assignment", "test")
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMPolicyAssignmentDestroy,
Steps: []resource.TestStep{
{
Config: testAzureRMPolicyAssignment_basicBuiltin(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMPolicyAssignmentExists(data.ResourceName),
),
Expand All @@ -43,7 +61,7 @@ func TestAccAzureRMPolicyAssignment_requiresImport(t *testing.T) {
CheckDestroy: testCheckAzureRMPolicyAssignmentDestroy,
Steps: []resource.TestStep{
{
Config: testAzureRMPolicyAssignment_basic(data),
Config: testAzureRMPolicyAssignment_basicCustom(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMPolicyAssignmentExists(data.ResourceName),
),
Expand Down Expand Up @@ -155,24 +173,24 @@ func testCheckAzureRMPolicyAssignmentDestroy(s *terraform.State) error {
return nil
}

func testAzureRMPolicyAssignment_basic(data acceptance.TestData) string {
func testAzureRMPolicyAssignment_basicCustom(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_policy_definition" "test" {
name = "acctestpol-%d"
name = "acctestpol-%[1]d"
policy_type = "Custom"
mode = "All"
display_name = "acctestpol-%d"
display_name = "acctestpol-%[1]d"

policy_rule = <<POLICY_RULE
{
"if": {
"not": {
"field": "location",
"equals": "%s"
"equals": "%[2]s"
}
},
"then": {
Expand All @@ -184,20 +202,50 @@ POLICY_RULE
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
name = "acctestRG-%[1]d"
location = "%[2]s"
}

resource "azurerm_policy_assignment" "test" {
name = "acctestpa-%d"
name = "acctestpa-%[1]d"
scope = azurerm_resource_group.test.id
policy_definition_id = azurerm_policy_definition.test.id
}
`, data.RandomInteger, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
`, data.RandomInteger, data.Locations.Primary)
}

func testAzureRMPolicyAssignment_basicBuiltin(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

data "azurerm_policy_definition" "test" {
display_name = "Allowed locations"
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%[1]d"
location = "%[2]s"
}

resource "azurerm_policy_assignment" "test" {
name = "acctestpa-%[1]d"
scope = azurerm_resource_group.test.id
policy_definition_id = data.azurerm_policy_definition.test.id
parameters = <<PARAMETERS
{
"listOfAllowedLocations": {
"value": [ "%[2]s" ]
}
}
PARAMETERS
}
`, data.RandomInteger, data.Locations.Primary)
}

func testAzureRMPolicyAssignment_requiresImport(data acceptance.TestData) string {
template := testAzureRMPolicyAssignment_basic(data)
template := testAzureRMPolicyAssignment_basicCustom(data)
return fmt.Sprintf(`
%s

Expand Down