-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
data_source_policy_definition.go
125 lines (106 loc) · 3.25 KB
/
data_source_policy_definition.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package azurerm
import (
"fmt"
"time"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/policy"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
)
func dataSourceArmPolicyDefinition() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmPolicyDefinitionRead,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"display_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},
"management_group_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: azure.ValidateResourceIDOrEmpty,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"policy_type": {
Type: schema.TypeString,
Computed: true,
},
"policy_rule": {
Type: schema.TypeString,
Computed: true,
},
"parameters": {
Type: schema.TypeString,
Computed: true,
},
"metadata": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceArmPolicyDefinitionRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Policy.DefinitionsClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()
name := d.Get("display_name").(string)
managementGroupID := d.Get("management_group_id").(string)
var policyDefinitions policy.DefinitionListResultIterator
var err error
if managementGroupID != "" {
policyDefinitions, err = client.ListByManagementGroupComplete(ctx, managementGroupID)
} else {
policyDefinitions, err = client.ListComplete(ctx)
}
if err != nil {
return fmt.Errorf("Error loading Policy Definition List: %+v", err)
}
var policyDefinition policy.Definition
for policyDefinitions.NotDone() {
def := policyDefinitions.Value()
if def.DisplayName != nil && *def.DisplayName == name {
policyDefinition = def
break
}
err = policyDefinitions.NextWithContext(ctx)
if err != nil {
return fmt.Errorf("Error loading Policy Definition List: %s", err)
}
}
if policyDefinition.ID == nil {
return fmt.Errorf("Error loading Policy Definition List: could not find policy '%s'", name)
}
d.SetId(*policyDefinition.ID)
d.Set("name", policyDefinition.Name)
d.Set("display_name", policyDefinition.DisplayName)
d.Set("description", policyDefinition.Description)
d.Set("type", policyDefinition.Type)
d.Set("policy_type", policyDefinition.PolicyType)
if policyRuleStr := flattenJSON(policyDefinition.PolicyRule); policyRuleStr != "" {
d.Set("policy_rule", policyRuleStr)
}
if metadataStr := flattenJSON(policyDefinition.Metadata); metadataStr != "" {
d.Set("metadata", metadataStr)
}
if parametersStr := flattenJSON(policyDefinition.Parameters); parametersStr != "" {
d.Set("parameters", parametersStr)
}
return nil
}