Skip to content

Commit

Permalink
policy: fixing and superseding azurerm_policy_assignment (#12349)
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff authored Jul 1, 2021
1 parent e95c568 commit 16f936f
Show file tree
Hide file tree
Showing 53 changed files with 3,964 additions and 265 deletions.
1 change: 1 addition & 0 deletions azurerm/internal/provider/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func SupportedTypedServices() []sdk.TypedServiceRegistration {
return []sdk.TypedServiceRegistration{
eventhub.Registration{},
loadbalancer.Registration{},
policy.Registration{},
resource.Registration{},
web.Registration{},
}
Expand Down
3 changes: 0 additions & 3 deletions azurerm/internal/sdk/service_registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ type TypedServiceRegistration interface {
// Name is the name of this Service
Name() string

// PackagePath is the relative path to this package
PackagePath() string

// DataSources returns a list of Data Sources supported by this Service
DataSources() []DataSource

Expand Down
6 changes: 4 additions & 2 deletions azurerm/internal/sdk/wrapper_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ func (dw *DataSourceWrapper) DataSource() (*schema.Resource, error) {
}

modelObj := dw.dataSource.ModelObject()
if err := ValidateModelObject(&modelObj); err != nil {
return nil, fmt.Errorf("validating model for %q: %+v", dw.dataSource.ResourceType(), err)
if modelObj != nil {
if err := ValidateModelObject(&modelObj); err != nil {
return nil, fmt.Errorf("validating model for %q: %+v", dw.dataSource.ResourceType(), err)
}
}

d := func(duration time.Duration) *time.Duration {
Expand Down
6 changes: 4 additions & 2 deletions azurerm/internal/sdk/wrapper_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ func (rw *ResourceWrapper) Resource() (*schema.Resource, error) {
}

modelObj := rw.resource.ModelObject()
if err := ValidateModelObject(&modelObj); err != nil {
return nil, fmt.Errorf("validating model for %q: %+v", rw.resource.ResourceType(), err)
if modelObj != nil {
if err := ValidateModelObject(&modelObj); err != nil {
return nil, fmt.Errorf("validating model for %q: %+v", rw.resource.ResourceType(), err)
}
}

d := func(duration time.Duration) *time.Duration {
Expand Down
5 changes: 0 additions & 5 deletions azurerm/internal/services/eventhub/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ func (r Registration) SupportedResources() map[string]*pluginsdk.Resource {
}
}

// PackagePath is the relative path to this package
func (r Registration) PackagePath() string {
return "TODO"
}

// DataSources returns a list of Data Sources supported by this Service
func (r Registration) DataSources() []sdk.DataSource {
return []sdk.DataSource{}
Expand Down
5 changes: 0 additions & 5 deletions azurerm/internal/services/loadbalancer/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ func (r Registration) SupportedResources() map[string]*pluginsdk.Resource {
}
}

// PackagePath is the relative path to this package
func (r Registration) PackagePath() string {
return "TODO: do we need this?"
}

// Resources returns a list of Resources supported by this Service
func (r Registration) Resources() []sdk.Resource {
return []sdk.Resource{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func resourceLogicAppWorkflow() *pluginsdk.Resource {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
Default: "https://pluginsdk.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
Default: "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
},

"workflow_version": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ resource "azurerm_managed_application_definition" "test" {
create_ui_definition = <<CREATE_UI_DEFINITION
{
"$schema": "https://pluginsdk.management.azure.com/schemas/0.1.2-preview/CreateUIDefinition.MultiVm.json#",
"$schema": "https://schema.management.azure.com/schemas/0.1.2-preview/CreateUIDefinition.MultiVm.json#",
"handler": "Microsoft.Azure.CreateUIDef",
"version": "0.1.2-preview",
"parameters": {
Expand Down Expand Up @@ -216,7 +216,7 @@ resource "azurerm_managed_application_definition" "test" {
main_template = <<MAIN_TEMPLATE
{
"$schema": "https://pluginsdk.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageAccountNamePrefix": {
Expand Down
58 changes: 58 additions & 0 deletions azurerm/internal/services/policy/assignment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package policy

import (
"context"
"fmt"
"strconv"
"time"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-09-01/policy"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/policy/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func convertEnforcementMode(mode bool) policy.EnforcementMode {
if mode {
return policy.Default
} else {
return policy.DoNotEnforce
}
}

func waitForPolicyAssignmentToStabilize(ctx context.Context, client *policy.AssignmentsClient, id parse.PolicyAssignmentId, shouldExist bool) error {
deadline, ok := ctx.Deadline()
if !ok {
return fmt.Errorf("context was missing a deadline")
}
stateConf := &pluginsdk.StateChangeConf{
Pending: []string{"404"},
Target: []string{"200"},
Refresh: func() (interface{}, string, error) {
resp, err := client.Get(ctx, id.Scope, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return resp, strconv.Itoa(resp.StatusCode), nil
}

return nil, strconv.Itoa(resp.StatusCode), fmt.Errorf("polling for %s: %+v", id, err)
}

return resp, strconv.Itoa(resp.StatusCode), nil
},
MinTimeout: 10 * time.Second,
ContinuousTargetOccurence: 20,
PollInterval: 5 * time.Second,
Timeout: time.Until(deadline),
}
if !shouldExist {
stateConf.Pending = []string{"200"}
stateConf.Target = []string{"404"}
}

if _, err := stateConf.WaitForStateContext(ctx); err != nil {
return err
}

return nil
}
Loading

0 comments on commit 16f936f

Please sign in to comment.