forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_builtin_role_definition.go
104 lines (92 loc) · 2.71 KB
/
data_source_builtin_role_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
package azurerm
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func dataSourceArmBuiltInRoleDefinition() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmBuiltInRoleDefinitionRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"Contributor",
"Reader",
"Owner",
"VirtualMachineContributor",
}, false),
},
// Computed
"description": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"permissions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"actions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"not_actions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"assignable_scopes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func dataSourceArmBuiltInRoleDefinitionRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).roleDefinitionsClient
ctx := meta.(*ArmClient).StopContext
name := d.Get("name").(string)
roleDefinitionIds := map[string]string{
"Contributor": "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c",
"Owner": "/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635",
"Reader": "/providers/Microsoft.Authorization/roleDefinitions/acdd72a7-3385-48ef-bd42-f606fba81ae7",
"VirtualMachineContributor": "/providers/Microsoft.Authorization/roleDefinitions/d73bb868-a0df-4d4d-bd69-98a00b01fccb",
}
roleDefinitionId := roleDefinitionIds[name]
d.SetId(roleDefinitionId)
role, err := client.GetByID(ctx, roleDefinitionId)
if err != nil {
return fmt.Errorf("Error loadng Role Definition: %+v", err)
}
if props := role.Properties; props != nil {
d.Set("name", props.RoleName)
d.Set("description", props.Description)
d.Set("type", props.Type)
permissions := flattenRoleDefinitionPermissions(props.Permissions)
if err := d.Set("permissions", permissions); err != nil {
return err
}
assignableScopes := flattenRoleDefinitionAssignableScopes(props.AssignableScopes)
if err := d.Set("assignable_scopes", assignableScopes); err != nil {
return err
}
}
return nil
}