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_project_v3.go
185 lines (152 loc) · 4.55 KB
/
resource_openstack_identity_project_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package openstack
import (
"fmt"
"log"
"github.com/gophercloud/gophercloud/openstack/identity/v3/projects"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceIdentityProjectV3() *schema.Resource {
return &schema.Resource{
Create: resourceIdentityProjectV3Create,
Read: resourceIdentityProjectV3Read,
Update: resourceIdentityProjectV3Update,
Delete: resourceIdentityProjectV3Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"domain_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"is_domain": {
Type: schema.TypeBool,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"parent_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func resourceIdentityProjectV3Create(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)
}
enabled := d.Get("enabled").(bool)
isDomain := d.Get("is_domain").(bool)
createOpts := projects.CreateOpts{
Description: d.Get("description").(string),
DomainID: d.Get("domain_id").(string),
Enabled: &enabled,
IsDomain: &isDomain,
Name: d.Get("name").(string),
ParentID: d.Get("parent_id").(string),
}
log.Printf("[DEBUG] openstack_identity_project_v3 create options: %#v", createOpts)
project, err := projects.Create(identityClient, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating openstack_identity_project_v3: %s", err)
}
d.SetId(project.ID)
return resourceIdentityProjectV3Read(d, meta)
}
func resourceIdentityProjectV3Read(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)
}
project, err := projects.Get(identityClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "Error retrieving openstack_identity_project_v3")
}
log.Printf("[DEBUG] Retrieved openstack_identity_project_v3 %s: %#v", d.Id(), project)
d.Set("description", project.Description)
d.Set("domain_id", project.DomainID)
d.Set("enabled", project.Enabled)
d.Set("is_domain", project.IsDomain)
d.Set("name", project.Name)
d.Set("parent_id", project.ParentID)
d.Set("region", GetRegion(d, config))
return nil
}
func resourceIdentityProjectV3Update(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 projects.UpdateOpts
if d.HasChange("domain_id") {
hasChange = true
updateOpts.DomainID = d.Get("domain_id").(string)
}
if d.HasChange("enabled") {
hasChange = true
enabled := d.Get("enabled").(bool)
updateOpts.Enabled = &enabled
}
if d.HasChange("is_domain") {
hasChange = true
isDomain := d.Get("is_domain").(bool)
updateOpts.IsDomain = &isDomain
}
if d.HasChange("name") {
hasChange = true
updateOpts.Name = d.Get("name").(string)
}
if d.HasChange("parent_id") {
hasChange = true
updateOpts.ParentID = d.Get("parent_id").(string)
}
if d.HasChange("description") {
hasChange = true
description := d.Get("description").(string)
updateOpts.Description = &description
}
if hasChange {
_, err := projects.Update(identityClient, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating openstack_identity_project_v3 %s: %s", d.Id(), err)
}
}
return resourceIdentityProjectV3Read(d, meta)
}
func resourceIdentityProjectV3Delete(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 = projects.Delete(identityClient, d.Id()).ExtractErr()
if err != nil {
return CheckDeleted(d, err, "Error deleting openstack_identity_project_v3")
}
return nil
}