This repository has been archived by the owner on Aug 6, 2021. It is now read-only.
/
organizations.go
218 lines (209 loc) · 5.75 KB
/
organizations.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package resources
import (
"code.cloudfoundry.org/cli/cf/errors"
"code.cloudfoundry.org/cli/cf/models"
"github.com/hashicorp/terraform/helper/schema"
"github.com/orange-cloudfoundry/terraform-provider-cloudfoundry/cf_client"
"log"
)
const DEFAULT_ORG_QUOTA_NAME = "default"
type CfOrganizationResource struct{}
func (c CfOrganizationResource) resourceObject(d *schema.ResourceData) models.Organization {
quotaDef := models.QuotaFields{
GUID: d.Get("quota_id").(string),
}
orgField := models.OrganizationFields{
GUID: d.Id(),
Name: d.Get("name").(string),
QuotaDefinition: quotaDef,
}
repo := models.Organization{
OrganizationFields: orgField,
}
return repo
}
func (c CfOrganizationResource) Create(d *schema.ResourceData, meta interface{}) error {
client := meta.(cf_client.Client)
org := c.resourceObject(d)
if ok, _ := c.Exists(d, meta); ok {
log.Printf(
"[INFO] skipping creation of organization %s/%s because it already exists on your Cloud Foundry",
client.Config().ApiEndpoint,
org.Name,
)
} else {
err := client.Organizations().Create(org)
if err != nil {
return err
}
c.Exists(d, meta)
}
orgCf, err := c.getOrgFromCf(client, d.Id())
if err != nil {
return err
}
err = c.bindQuota(client, org.QuotaDefinition.GUID, orgCf.GUID)
if err != nil {
return err
}
d.SetId(orgCf.GUID)
return nil
}
func (c CfOrganizationResource) bindQuota(client cf_client.Client, quotaId, orgId string) error {
if quotaId == "" {
return nil
}
return client.Quotas().AssignQuotaToOrg(orgId, quotaId)
}
func (c CfOrganizationResource) Read(d *schema.ResourceData, meta interface{}) error {
client := meta.(cf_client.Client)
orgName := d.Get("name").(string)
org, err := c.getOrgFromCf(client, d.Id())
if err != nil {
return err
}
if org.GUID == "" {
log.Printf(
"[WARN] removing organization %s/%s from state because it no longer exists in your Cloud Foundry",
client.Config().ApiEndpoint,
orgName,
)
d.SetId("")
return nil
}
quota, _ := client.Quotas().FindByName(DEFAULT_ORG_QUOTA_NAME)
if quota.GUID == "" || org.QuotaDefinition.GUID != quota.GUID {
d.Set("quota_id", org.QuotaDefinition.GUID)
}
d.Set("name", org.Name)
return nil
}
func (c CfOrganizationResource) getOrgFromCf(client cf_client.Client, orgGuid string) (models.Organization, error) {
orgs, err := client.Organizations().GetManyOrgsByGUID([]string{orgGuid})
if err != nil {
if _, ok := err.(*errors.HTTPNotFoundError); ok {
return models.Organization{}, nil
}
return models.Organization{}, err
}
return client.Organizations().FindByName(orgs[0].Name)
}
func (c CfOrganizationResource) Exists(d *schema.ResourceData, meta interface{}) (bool, error) {
client := meta.(cf_client.Client)
if d.Id() != "" {
d, err := c.getOrgFromCf(client, d.Id())
if err != nil {
return false, err
}
return d.GUID != "", nil
}
orgName := d.Get("name").(string)
org, err := client.Organizations().FindByName(orgName)
if err != nil {
if _, ok := err.(*errors.ModelNotFoundError); ok {
return false, nil
}
return false, err
}
d.SetId(org.GUID)
return true, nil
}
func (c CfOrganizationResource) Update(d *schema.ResourceData, meta interface{}) error {
client := meta.(cf_client.Client)
quotaId := d.Get("quota_id").(string)
orgName := d.Get("name").(string)
org, err := c.getOrgFromCf(client, d.Id())
if err != nil {
return err
}
if org.GUID == "" {
log.Printf(
"[WARN] removing organization %s/%s from state because it no longer exists in your Cloud Foundry",
client.Config().ApiEndpoint,
orgName,
)
d.SetId("")
return nil
}
if org.QuotaDefinition.GUID != quotaId {
err = c.bindQuota(client, quotaId, d.Id())
if err != nil {
return err
}
}
if org.Name != orgName {
err = client.Organizations().Rename(d.Id(), d.Get("name").(string))
if err != nil {
return err
}
}
return nil
}
func (c CfOrganizationResource) Delete(d *schema.ResourceData, meta interface{}) error {
client := meta.(cf_client.Client)
isSystemDomain := d.Get("is_system_domain").(bool)
orgName := d.Get("name").(string)
if isSystemDomain {
log.Printf(
"[WARN] removing organization %s/%s isn't possible because it's the system_domain organization of your Cloud Foundry",
client.Config().ApiEndpoint,
orgName,
)
return nil
}
return client.Organizations().Delete(d.Id())
}
func (c CfOrganizationResource) Schema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"quota_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"is_system_domain": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
}
}
func (c CfOrganizationResource) DataSourceSchema() map[string]*schema.Schema {
return CreateDataSourceSchema(c, "name")
}
func (c CfOrganizationResource) DataSourceRead(d *schema.ResourceData, meta interface{}) error {
fn := CreateDataSourceReadFuncWithReq(c, "name")
return fn(d, meta)
}
type CfOrganizationsDataSource struct{}
func (c CfOrganizationsDataSource) DataSourceSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"names": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
}
}
func (c CfOrganizationsDataSource) DataSourceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(cf_client.Client)
orgs, err := client.Organizations().ListOrgs(0)
if err != nil {
return err
}
names := make([]string, len(orgs))
ids := make([]string, len(orgs))
for i, org := range orgs {
names[i] = org.Name
ids[i] = org.GUID
}
d.Set("names", names)
d.Set("ids", ids)
return nil
}