-
Notifications
You must be signed in to change notification settings - Fork 46
/
resource_role.go
134 lines (114 loc) · 3.14 KB
/
resource_role.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
126
127
128
129
130
131
132
133
134
package nexus
import (
"strings"
nexus "github.com/datadrivers/go-nexus-client"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceRole() *schema.Resource {
return &schema.Resource{
Create: resourceRoleCreate,
Read: resourceRoleRead,
Update: resourceRoleUpdate,
Delete: resourceRoleDelete,
Exists: resourceRoleExists,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"roleid": {
Description: "The id of the role.",
Required: true,
Type: schema.TypeString,
},
"name": {
Description: "The name of the role.",
Required: true,
Type: schema.TypeString,
},
"description": {
Description: "The description of this role.",
Optional: true,
Type: schema.TypeString,
},
"privileges": {
Description: "The privileges of this role.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Set: func(v interface{}) int {
return schema.HashString(strings.ToLower(v.(string)))
},
Type: schema.TypeSet,
},
"roles": {
Description: "The roles of this role.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Set: func(v interface{}) int {
return schema.HashString(strings.ToLower(v.(string)))
},
Type: schema.TypeSet,
},
},
}
}
func getRoleFromResourceData(d *schema.ResourceData) nexus.Role {
return nexus.Role{
ID: d.Get("roleid").(string),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Privileges: interfaceSliceToStringSlice(d.Get("privileges").(*schema.Set).List()),
Roles: interfaceSliceToStringSlice(d.Get("roles").(*schema.Set).List()),
}
}
func resourceRoleCreate(d *schema.ResourceData, m interface{}) error {
nexusClient := m.(nexus.Client)
role := getRoleFromResourceData(d)
if err := nexusClient.RoleCreate(role); err != nil {
return err
}
d.SetId(role.ID)
return resourceRoleRead(d, m)
}
func resourceRoleRead(d *schema.ResourceData, m interface{}) error {
nexusClient := m.(nexus.Client)
role, err := nexusClient.RoleRead(d.Id())
if err != nil {
return err
}
if role == nil {
d.SetId("")
return nil
}
d.Set("description", role.Description)
d.Set("name", role.Name)
d.Set("privileges", stringSliceToInterfaceSlice(role.Privileges))
d.Set("roleid", role.ID)
d.Set("roles", stringSliceToInterfaceSlice(role.Roles))
return nil
}
func resourceRoleUpdate(d *schema.ResourceData, m interface{}) error {
nexusClient := m.(nexus.Client)
roleID := d.Get("roleid").(string)
role := getRoleFromResourceData(d)
if err := nexusClient.RoleUpdate(roleID, role); err != nil {
return err
}
return resourceRoleRead(d, m)
}
func resourceRoleDelete(d *schema.ResourceData, m interface{}) error {
nexusClient := m.(nexus.Client)
if err := nexusClient.RoleDelete(d.Id()); err != nil {
return err
}
d.SetId("")
return nil
}
func resourceRoleExists(d *schema.ResourceData, m interface{}) (bool, error) {
nexusClient := m.(nexus.Client)
role, err := nexusClient.RoleRead(d.Id())
return role != nil, err
}