This repository has been archived by the owner on Nov 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
resource_openstack_identity_role_v3.go
125 lines (101 loc) · 3.07 KB
/
resource_openstack_identity_role_v3.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 openstack
import (
"fmt"
"log"
"github.com/gophercloud/gophercloud/openstack/identity/v3/roles"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceIdentityRoleV3() *schema.Resource {
return &schema.Resource{
Create: resourceIdentityRoleV3Create,
Read: resourceIdentityRoleV3Read,
Update: resourceIdentityRoleV3Update,
Delete: resourceIdentityRoleV3Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"domain_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}
func resourceIdentityRoleV3Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
identityClient, err := config.identityV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack identity client: %s", err)
}
createOpts := roles.CreateOpts{
DomainID: d.Get("domain_id").(string),
Name: d.Get("name").(string),
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
role, err := roles.Create(identityClient, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating OpenStack role: %s", err)
}
d.SetId(role.ID)
return resourceIdentityRoleV3Read(d, meta)
}
func resourceIdentityRoleV3Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
identityClient, err := config.identityV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack identity client: %s", err)
}
role, err := roles.Get(identityClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "role")
}
log.Printf("[DEBUG] Retrieved OpenStack role: %#v", role)
d.Set("domain_id", role.DomainID)
d.Set("name", role.Name)
d.Set("region", GetRegion(d, config))
return nil
}
func resourceIdentityRoleV3Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
identityClient, err := config.identityV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack identity client: %s", err)
}
var hasChange bool
var updateOpts roles.UpdateOpts
if d.HasChange("name") {
hasChange = true
updateOpts.Name = d.Get("name").(string)
}
if hasChange {
_, err := roles.Update(identityClient, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating OpenStack role: %s", err)
}
}
return resourceIdentityRoleV3Read(d, meta)
}
func resourceIdentityRoleV3Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
identityClient, err := config.identityV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack identity client: %s", err)
}
err = roles.Delete(identityClient, d.Id()).ExtractErr()
if err != nil {
return fmt.Errorf("Error deleting OpenStack role: %s", err)
}
return nil
}