forked from huaweicloud/terraform-provider-huaweicloud
/
resource_huaweicloud_identity_group_v3.go
147 lines (118 loc) · 3.59 KB
/
resource_huaweicloud_identity_group_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package huaweicloud
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/identity/v3/groups"
)
func resourceIdentityGroupV3() *schema.Resource {
return &schema.Resource{
Create: resourceIdentityGroupV3Create,
Read: resourceIdentityGroupV3Read,
Update: resourceIdentityGroupV3Update,
Delete: resourceIdentityGroupV3Delete,
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,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}
func resourceIdentityGroupV3Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
identityClient, err := config.identityV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud identity client: %s", err)
}
createOpts := groups.CreateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
DomainID: d.Get("domain_id").(string),
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
group, err := groups.Create(identityClient, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud Group: %s", err)
}
d.SetId(group.ID)
return resourceIdentityGroupV3Read(d, meta)
}
func resourceIdentityGroupV3Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
identityClient, err := config.identityV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud identity client: %s", err)
}
group, err := groups.Get(identityClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "group")
}
log.Printf("[DEBUG] Retrieved HuaweiCloud Group: %#v", group)
d.Set("domain_id", group.DomainID)
d.Set("description", group.Description)
d.Set("name", group.Name)
d.Set("region", GetRegion(d, config))
return nil
}
func resourceIdentityGroupV3Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
identityClient, err := config.identityV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud identity client: %s", err)
}
var hasChange bool
var updateOpts groups.UpdateOpts
if d.HasChange("description") {
hasChange = true
updateOpts.Description = d.Get("description").(string)
}
if d.HasChange("domain_id") {
hasChange = true
updateOpts.DomainID = d.Get("domain_id").(string)
}
if d.HasChange("name") {
hasChange = true
updateOpts.Name = d.Get("name").(string)
}
if hasChange {
log.Printf("[DEBUG] Update Options: %#v", updateOpts)
}
if hasChange {
_, err := groups.Update(identityClient, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating HuaweiCloud group: %s", err)
}
}
return resourceIdentityGroupV3Read(d, meta)
}
func resourceIdentityGroupV3Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
identityClient, err := config.identityV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud identity client: %s", err)
}
err = groups.Delete(identityClient, d.Id()).ExtractErr()
if err != nil {
return fmt.Errorf("Error deleting HuaweiCloud group: %s", err)
}
return nil
}