forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorgs.go
832 lines (736 loc) · 24.8 KB
/
orgs.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
package cfclient
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/pkg/errors"
)
type OrgResponse struct {
Count int `json:"total_results"`
Pages int `json:"total_pages"`
NextUrl string `json:"next_url"`
Resources []OrgResource `json:"resources"`
}
type OrgResource struct {
Meta Meta `json:"metadata"`
Entity Org `json:"entity"`
}
type OrgUserResponse struct {
Count int `json:"total_results"`
Pages int `json:"total_pages"`
NextURL string `json:"next_url"`
Resources []UserResource `json:"resources"`
}
type Org struct {
Guid string `json:"guid"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Name string `json:"name"`
Status string `json:"status"`
QuotaDefinitionGuid string `json:"quota_definition_guid"`
DefaultIsolationSegmentGuid string `json:"default_isolation_segment_guid"`
c *Client
}
type OrgSummary struct {
Guid string `json:"guid"`
Name string `json:"name"`
Status string `json:"status"`
Spaces []OrgSummarySpaces `json:"spaces"`
}
type OrgSummarySpaces struct {
Guid string `json:"guid"`
Name string `json:"name"`
ServiceCount int `json:"service_count"`
AppCount int `json:"app_count"`
MemDevTotal int `json:"mem_dev_total"`
MemProdTotal int `json:"mem_prod_total"`
}
type OrgRequest struct {
Name string `json:"name"`
Status string `json:"status,omitempty"`
QuotaDefinitionGuid string `json:"quota_definition_guid,omitempty"`
DefaultIsolationSegmentGuid string `json:"default_isolation_segment_guid,omitempty"`
}
func (c *Client) ListOrgsByQuery(query url.Values) ([]Org, error) {
var orgs []Org
requestURL := "/v2/organizations?" + query.Encode()
for {
orgResp, err := c.getOrgResponse(requestURL)
if err != nil {
return []Org{}, err
}
for _, org := range orgResp.Resources {
orgs = append(orgs, c.mergeOrgResource(org))
}
requestURL = orgResp.NextUrl
if requestURL == "" {
break
}
}
return orgs, nil
}
func (c *Client) ListOrgs() ([]Org, error) {
return c.ListOrgsByQuery(nil)
}
func (c *Client) GetOrgByName(name string) (Org, error) {
var org Org
q := url.Values{}
q.Set("q", "name:"+name)
orgs, err := c.ListOrgsByQuery(q)
if err != nil {
return org, err
}
if len(orgs) == 0 {
return org, fmt.Errorf("Unable to find org %s", name)
}
return orgs[0], nil
}
func (c *Client) GetOrgByGuid(guid string) (Org, error) {
var orgRes OrgResource
r := c.NewRequest("GET", "/v2/organizations/"+guid)
resp, err := c.DoRequest(r)
if err != nil {
return Org{}, err
}
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return Org{}, err
}
err = json.Unmarshal(body, &orgRes)
if err != nil {
return Org{}, err
}
return c.mergeOrgResource(orgRes), nil
}
func (c *Client) OrgSpaces(guid string) ([]Space, error) {
return c.fetchSpaces(fmt.Sprintf("/v2/organizations/%s/spaces", guid))
}
func (o *Org) Summary() (OrgSummary, error) {
var orgSummary OrgSummary
requestURL := fmt.Sprintf("/v2/organizations/%s/summary", o.Guid)
r := o.c.NewRequest("GET", requestURL)
resp, err := o.c.DoRequest(r)
if err != nil {
return OrgSummary{}, errors.Wrap(err, "Error requesting org summary")
}
resBody, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return OrgSummary{}, errors.Wrap(err, "Error reading org summary body")
}
err = json.Unmarshal(resBody, &orgSummary)
if err != nil {
return OrgSummary{}, errors.Wrap(err, "Error unmarshalling org summary")
}
return orgSummary, nil
}
func (o *Org) Quota() (*OrgQuota, error) {
var orgQuota *OrgQuota
var orgQuotaResource OrgQuotasResource
if o.QuotaDefinitionGuid == "" {
return nil, nil
}
requestURL := fmt.Sprintf("/v2/quota_definitions/%s", o.QuotaDefinitionGuid)
r := o.c.NewRequest("GET", requestURL)
resp, err := o.c.DoRequest(r)
if err != nil {
return &OrgQuota{}, errors.Wrap(err, "Error requesting org quota")
}
resBody, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return &OrgQuota{}, errors.Wrap(err, "Error reading org quota body")
}
err = json.Unmarshal(resBody, &orgQuotaResource)
if err != nil {
return &OrgQuota{}, errors.Wrap(err, "Error unmarshalling org quota")
}
orgQuota = &orgQuotaResource.Entity
orgQuota.Guid = orgQuotaResource.Meta.Guid
orgQuota.c = o.c
return orgQuota, nil
}
func (c *Client) ListOrgUsersByQuery(orgGUID string, query url.Values) ([]User, error) {
var users []User
requestURL := fmt.Sprintf("/v2/organizations/%s/users?%s", orgGUID, query.Encode())
for {
omResp, err := c.getOrgUserResponse(requestURL)
if err != nil {
return []User{}, err
}
for _, u := range omResp.Resources {
users = append(users, c.mergeUserResource(u))
}
requestURL = omResp.NextURL
if requestURL == "" {
break
}
}
return users, nil
}
func (c *Client) ListOrgUsers(orgGUID string) ([]User, error) {
return c.ListOrgUsersByQuery(orgGUID, nil)
}
func (c *Client) listOrgRolesByQuery(orgGUID, role string, query url.Values) ([]User, error) {
var users []User
requestURL := fmt.Sprintf("/v2/organizations/%s/%s?%s", orgGUID, role, query.Encode())
for {
omResp, err := c.getOrgUserResponse(requestURL)
if err != nil {
return []User{}, err
}
for _, u := range omResp.Resources {
users = append(users, c.mergeUserResource(u))
}
requestURL = omResp.NextURL
if requestURL == "" {
break
}
}
return users, nil
}
func (c *Client) ListOrgManagersByQuery(orgGUID string, query url.Values) ([]User, error) {
return c.listOrgRolesByQuery(orgGUID, "managers", query)
}
func (c *Client) ListOrgManagers(orgGUID string) ([]User, error) {
return c.ListOrgManagersByQuery(orgGUID, nil)
}
func (c *Client) ListOrgAuditorsByQuery(orgGUID string, query url.Values) ([]User, error) {
return c.listOrgRolesByQuery(orgGUID, "auditors", query)
}
func (c *Client) ListOrgAuditors(orgGUID string) ([]User, error) {
return c.ListOrgAuditorsByQuery(orgGUID, nil)
}
func (c *Client) ListOrgBillingManagersByQuery(orgGUID string, query url.Values) ([]User, error) {
return c.listOrgRolesByQuery(orgGUID, "billing_managers", query)
}
func (c *Client) ListOrgBillingManagers(orgGUID string) ([]User, error) {
return c.ListOrgBillingManagersByQuery(orgGUID, nil)
}
func (c *Client) AssociateOrgManager(orgGUID, userGUID string) (Org, error) {
org := Org{Guid: orgGUID, c: c}
return org.AssociateManager(userGUID)
}
func (c *Client) AssociateOrgManagerByUsername(orgGUID, name string) (Org, error) {
org := Org{Guid: orgGUID, c: c}
return org.AssociateManagerByUsername(name)
}
func (c *Client) AssociateOrgManagerByUsernameAndOrigin(orgGUID, name, origin string) (Org, error) {
org := Org{Guid: orgGUID, c: c}
return org.AssociateManagerByUsernameAndOrigin(name, origin)
}
func (c *Client) AssociateOrgUser(orgGUID, userGUID string) (Org, error) {
org := Org{Guid: orgGUID, c: c}
return org.AssociateUser(userGUID)
}
func (c *Client) AssociateOrgAuditor(orgGUID, userGUID string) (Org, error) {
org := Org{Guid: orgGUID, c: c}
return org.AssociateAuditor(userGUID)
}
func (c *Client) AssociateOrgUserByUsername(orgGUID, name string) (Org, error) {
org := Org{Guid: orgGUID, c: c}
return org.AssociateUserByUsername(name)
}
func (c *Client) AssociateOrgUserByUsernameAndOrigin(orgGUID, name, origin string) (Org, error) {
org := Org{Guid: orgGUID, c: c}
return org.AssociateUserByUsernameAndOrigin(name, origin)
}
func (c *Client) AssociateOrgAuditorByUsername(orgGUID, name string) (Org, error) {
org := Org{Guid: orgGUID, c: c}
return org.AssociateAuditorByUsername(name)
}
func (c *Client) AssociateOrgAuditorByUsernameAndOrigin(orgGUID, name, origin string) (Org, error) {
org := Org{Guid: orgGUID, c: c}
return org.AssociateAuditorByUsernameAndOrigin(name, origin)
}
func (c *Client) AssociateOrgBillingManager(orgGUID, userGUID string) (Org, error) {
org := Org{Guid: orgGUID, c: c}
return org.AssociateBillingManager(userGUID)
}
func (c *Client) AssociateOrgBillingManagerByUsername(orgGUID, name string) (Org, error) {
org := Org{Guid: orgGUID, c: c}
return org.AssociateBillingManagerByUsername(name)
}
func (c *Client) AssociateOrgBillingManagerByUsernameAndOrigin(orgGUID, name, origin string) (Org, error) {
org := Org{Guid: orgGUID, c: c}
return org.AssociateBillingManagerByUsernameAndOrigin(name, origin)
}
func (c *Client) RemoveOrgManager(orgGUID, userGUID string) error {
org := Org{Guid: orgGUID, c: c}
return org.RemoveManager(userGUID)
}
func (c *Client) RemoveOrgManagerByUsername(orgGUID, name string) error {
org := Org{Guid: orgGUID, c: c}
return org.RemoveManagerByUsername(name)
}
func (c *Client) RemoveOrgManagerByUsernameAndOrigin(orgGUID, name, origin string) error {
org := Org{Guid: orgGUID, c: c}
return org.RemoveManagerByUsernameAndOrigin(name, origin)
}
func (c *Client) RemoveOrgUser(orgGUID, userGUID string) error {
org := Org{Guid: orgGUID, c: c}
return org.RemoveUser(userGUID)
}
func (c *Client) RemoveOrgAuditor(orgGUID, userGUID string) error {
org := Org{Guid: orgGUID, c: c}
return org.RemoveAuditor(userGUID)
}
func (c *Client) RemoveOrgUserByUsername(orgGUID, name string) error {
org := Org{Guid: orgGUID, c: c}
return org.RemoveUserByUsername(name)
}
func (c *Client) RemoveOrgUserByUsernameAndOrigin(orgGUID, name, origin string) error {
org := Org{Guid: orgGUID, c: c}
return org.RemoveUserByUsernameAndOrigin(name, origin)
}
func (c *Client) RemoveOrgAuditorByUsername(orgGUID, name string) error {
org := Org{Guid: orgGUID, c: c}
return org.RemoveAuditorByUsername(name)
}
func (c *Client) RemoveOrgAuditorByUsernameAndOrigin(orgGUID, name, origin string) error {
org := Org{Guid: orgGUID, c: c}
return org.RemoveAuditorByUsernameAndOrigin(name, origin)
}
func (c *Client) RemoveOrgBillingManager(orgGUID, userGUID string) error {
org := Org{Guid: orgGUID, c: c}
return org.RemoveBillingManager(userGUID)
}
func (c *Client) RemoveOrgBillingManagerByUsername(orgGUID, name string) error {
org := Org{Guid: orgGUID, c: c}
return org.RemoveBillingManagerByUsername(name)
}
func (c *Client) RemoveOrgBillingManagerByUsernameAndOrigin(orgGUID, name, origin string) error {
org := Org{Guid: orgGUID, c: c}
return org.RemoveBillingManagerByUsernameAndOrigin(name, origin)
}
func (c *Client) ListOrgSpaceQuotas(orgGUID string) ([]SpaceQuota, error) {
org := Org{Guid: orgGUID, c: c}
return org.ListSpaceQuotas()
}
func (c *Client) ListOrgPrivateDomains(orgGUID string) ([]Domain, error) {
org := Org{Guid: orgGUID, c: c}
return org.ListPrivateDomains()
}
func (c *Client) ShareOrgPrivateDomain(orgGUID, privateDomainGUID string) (*Domain, error) {
org := Org{Guid: orgGUID, c: c}
return org.SharePrivateDomain(privateDomainGUID)
}
func (c *Client) UnshareOrgPrivateDomain(orgGUID, privateDomainGUID string) error {
org := Org{Guid: orgGUID, c: c}
return org.UnsharePrivateDomain(privateDomainGUID)
}
func (o *Org) ListSpaceQuotas() ([]SpaceQuota, error) {
var spaceQuotas []SpaceQuota
requestURL := fmt.Sprintf("/v2/organizations/%s/space_quota_definitions", o.Guid)
for {
spaceQuotasResp, err := o.c.getSpaceQuotasResponse(requestURL)
if err != nil {
return []SpaceQuota{}, err
}
for _, resource := range spaceQuotasResp.Resources {
spaceQuotas = append(spaceQuotas, *o.c.mergeSpaceQuotaResource(resource))
}
requestURL = spaceQuotasResp.NextUrl
if requestURL == "" {
break
}
}
return spaceQuotas, nil
}
func (o *Org) ListPrivateDomains() ([]Domain, error) {
var domains []Domain
requestURL := fmt.Sprintf("/v2/organizations/%s/private_domains", o.Guid)
for {
domainsResp, err := o.c.getDomainsResponse(requestURL)
if err != nil {
return []Domain{}, err
}
for _, resource := range domainsResp.Resources {
domains = append(domains, *o.c.mergeDomainResource(resource))
}
requestURL = domainsResp.NextUrl
if requestURL == "" {
break
}
}
return domains, nil
}
func (o *Org) SharePrivateDomain(privateDomainGUID string) (*Domain, error) {
requestURL := fmt.Sprintf("/v2/organizations/%s/private_domains/%s", o.Guid, privateDomainGUID)
r := o.c.NewRequest("PUT", requestURL)
resp, err := o.c.DoRequest(r)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusCreated {
return nil, errors.Wrapf(err, "Error sharing domain %s for org %s, response code: %d", privateDomainGUID, o.Guid, resp.StatusCode)
}
return o.c.handleDomainResp(resp)
}
func (o *Org) UnsharePrivateDomain(privateDomainGUID string) error {
requestURL := fmt.Sprintf("/v2/organizations/%s/private_domains/%s", o.Guid, privateDomainGUID)
r := o.c.NewRequest("DELETE", requestURL)
resp, err := o.c.DoRequest(r)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return errors.Wrapf(err, "Error unsharing domain %s for org %s, response code: %d", privateDomainGUID, o.Guid, resp.StatusCode)
}
return nil
}
func (o *Org) associateRole(userGUID, role string) (Org, error) {
requestURL := fmt.Sprintf("/v2/organizations/%s/%s/%s", o.Guid, role, userGUID)
r := o.c.NewRequest("PUT", requestURL)
resp, err := o.c.DoRequest(r)
if err != nil {
return Org{}, err
}
if resp.StatusCode != http.StatusCreated {
return Org{}, errors.Wrapf(err, "Error associating %s %s, response code: %d", role, userGUID, resp.StatusCode)
}
return o.c.handleOrgResp(resp)
}
func (o *Org) associateRoleByUsernameAndOrigin(name, role, origin string) (Org, error) {
requestURL := fmt.Sprintf("/v2/organizations/%s/%s", o.Guid, role)
buf := bytes.NewBuffer(nil)
payload := make(map[string]string)
payload["username"] = name
if origin != "" {
payload["origin"] = origin
}
err := json.NewEncoder(buf).Encode(payload)
if err != nil {
return Org{}, err
}
r := o.c.NewRequestWithBody("PUT", requestURL, buf)
resp, err := o.c.DoRequest(r)
if err != nil {
return Org{}, err
}
if resp.StatusCode != http.StatusCreated {
return Org{}, errors.Wrapf(err, "Error associating %s %s, response code: %d", role, name, resp.StatusCode)
}
return o.c.handleOrgResp(resp)
}
func (o *Org) AssociateManager(userGUID string) (Org, error) {
return o.associateRole(userGUID, "managers")
}
func (o *Org) AssociateManagerByUsername(name string) (Org, error) {
return o.associateRoleByUsernameAndOrigin(name, "managers", "")
}
func (o *Org) AssociateManagerByUsernameAndOrigin(name, origin string) (Org, error) {
return o.associateRoleByUsernameAndOrigin(name, "managers", origin)
}
func (o *Org) AssociateUser(userGUID string) (Org, error) {
requestURL := fmt.Sprintf("/v2/organizations/%s/users/%s", o.Guid, userGUID)
r := o.c.NewRequest("PUT", requestURL)
resp, err := o.c.DoRequest(r)
if err != nil {
return Org{}, err
}
if resp.StatusCode != http.StatusCreated {
return Org{}, errors.Wrapf(err, "Error associating user %s, response code: %d", userGUID, resp.StatusCode)
}
return o.c.handleOrgResp(resp)
}
func (o *Org) AssociateAuditor(userGUID string) (Org, error) {
return o.associateRole(userGUID, "auditors")
}
func (o *Org) AssociateAuditorByUsername(name string) (Org, error) {
return o.associateRoleByUsernameAndOrigin(name, "auditors", "")
}
func (o *Org) AssociateAuditorByUsernameAndOrigin(name, origin string) (Org, error) {
return o.associateRoleByUsernameAndOrigin(name, "auditors", origin)
}
func (o *Org) AssociateBillingManager(userGUID string) (Org, error) {
return o.associateRole(userGUID, "billing_managers")
}
func (o *Org) AssociateBillingManagerByUsername(name string) (Org, error) {
return o.associateRoleByUsernameAndOrigin(name, "billing_managers", "")
}
func (o *Org) AssociateBillingManagerByUsernameAndOrigin(name, origin string) (Org, error) {
return o.associateRoleByUsernameAndOrigin(name, "billing_managers", origin)
}
func (o *Org) AssociateUserByUsername(name string) (Org, error) {
return o.associateUserByUsernameAndOrigin(name, "")
}
func (o *Org) AssociateUserByUsernameAndOrigin(name, origin string) (Org, error) {
return o.associateUserByUsernameAndOrigin(name, origin)
}
func (o *Org) associateUserByUsernameAndOrigin(name, origin string) (Org, error) {
requestURL := fmt.Sprintf("/v2/organizations/%s/users", o.Guid)
buf := bytes.NewBuffer(nil)
payload := make(map[string]string)
payload["username"] = name
if origin != "" {
payload["origin"] = origin
}
err := json.NewEncoder(buf).Encode(payload)
if err != nil {
return Org{}, err
}
r := o.c.NewRequestWithBody("PUT", requestURL, buf)
resp, err := o.c.DoRequest(r)
if err != nil {
return Org{}, err
}
if resp.StatusCode != http.StatusCreated {
return Org{}, errors.Wrapf(err, "Error associating user %s, response code: %d", name, resp.StatusCode)
}
return o.c.handleOrgResp(resp)
}
func (o *Org) removeRole(userGUID, role string) error {
requestURL := fmt.Sprintf("/v2/organizations/%s/%s/%s", o.Guid, role, userGUID)
r := o.c.NewRequest("DELETE", requestURL)
resp, err := o.c.DoRequest(r)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return errors.Wrapf(err, "Error removing %s %s, response code: %d", role, userGUID, resp.StatusCode)
}
return nil
}
func (o *Org) removeRoleByUsernameAndOrigin(name, role, origin string) error {
var requestURL string
var method string
buf := bytes.NewBuffer(nil)
payload := make(map[string]string)
payload["username"] = name
if origin != "" {
requestURL = fmt.Sprintf("/v2/organizations/%s/%s/remove", o.Guid, role)
method = "POST"
payload["origin"] = origin
} else {
requestURL = fmt.Sprintf("/v2/organizations/%s/%s", o.Guid, role)
method = "DELETE"
}
err := json.NewEncoder(buf).Encode(payload)
if err != nil {
return err
}
r := o.c.NewRequestWithBody(method, requestURL, buf)
resp, err := o.c.DoRequest(r)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return errors.Wrapf(err, "Error removing manager %s, response code: %d", name, resp.StatusCode)
}
return nil
}
func (o *Org) RemoveManager(userGUID string) error {
return o.removeRole(userGUID, "managers")
}
func (o *Org) RemoveManagerByUsername(name string) error {
return o.removeRoleByUsernameAndOrigin(name, "managers", "")
}
func (o *Org) RemoveManagerByUsernameAndOrigin(name, origin string) error {
return o.removeRoleByUsernameAndOrigin(name, "managers", origin)
}
func (o *Org) RemoveAuditor(userGUID string) error {
return o.removeRole(userGUID, "auditors")
}
func (o *Org) RemoveAuditorByUsername(name string) error {
return o.removeRoleByUsernameAndOrigin(name, "auditors", "")
}
func (o *Org) RemoveAuditorByUsernameAndOrigin(name, origin string) error {
return o.removeRoleByUsernameAndOrigin(name, "auditors", origin)
}
func (o *Org) RemoveBillingManager(userGUID string) error {
return o.removeRole(userGUID, "billing_managers")
}
func (o *Org) RemoveBillingManagerByUsername(name string) error {
return o.removeRoleByUsernameAndOrigin(name, "billing_managers", "")
}
func (o *Org) RemoveBillingManagerByUsernameAndOrigin(name, origin string) error {
return o.removeRoleByUsernameAndOrigin(name, "billing_managers", origin)
}
func (o *Org) RemoveUser(userGUID string) error {
requestURL := fmt.Sprintf("/v2/organizations/%s/users/%s", o.Guid, userGUID)
r := o.c.NewRequest("DELETE", requestURL)
resp, err := o.c.DoRequest(r)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return errors.Wrapf(err, "Error removing user %s, response code: %d", userGUID, resp.StatusCode)
}
return nil
}
func (o *Org) RemoveUserByUsername(name string) error {
return o.removeUserByUsernameAndOrigin(name, "")
}
func (o *Org) RemoveUserByUsernameAndOrigin(name, origin string) error {
return o.removeUserByUsernameAndOrigin(name, origin)
}
func (o *Org) removeUserByUsernameAndOrigin(name, origin string) error {
var requestURL string
var method string
buf := bytes.NewBuffer(nil)
payload := make(map[string]string)
payload["username"] = name
if origin != "" {
payload["origin"] = origin
requestURL = fmt.Sprintf("/v2/organizations/%s/users/remove", o.Guid)
method = "POST"
} else {
requestURL = fmt.Sprintf("/v2/organizations/%s/users", o.Guid)
method = "DELETE"
}
err := json.NewEncoder(buf).Encode(payload)
if err != nil {
return err
}
r := o.c.NewRequestWithBody(method, requestURL, buf)
resp, err := o.c.DoRequest(r)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return errors.Wrapf(err, "Error removing user %s, response code: %d", name, resp.StatusCode)
}
return nil
}
func (c *Client) CreateOrg(req OrgRequest) (Org, error) {
buf := bytes.NewBuffer(nil)
err := json.NewEncoder(buf).Encode(req)
if err != nil {
return Org{}, err
}
r := c.NewRequestWithBody("POST", "/v2/organizations", buf)
resp, err := c.DoRequest(r)
if err != nil {
return Org{}, err
}
if resp.StatusCode != http.StatusCreated {
return Org{}, errors.Wrapf(err, "Error creating organization, response code: %d", resp.StatusCode)
}
return c.handleOrgResp(resp)
}
func (c *Client) UpdateOrg(orgGUID string, orgRequest OrgRequest) (Org, error) {
buf := bytes.NewBuffer(nil)
err := json.NewEncoder(buf).Encode(orgRequest)
if err != nil {
return Org{}, err
}
r := c.NewRequestWithBody("PUT", fmt.Sprintf("/v2/organizations/%s", orgGUID), buf)
resp, err := c.DoRequest(r)
if err != nil {
return Org{}, err
}
if resp.StatusCode != http.StatusCreated {
return Org{}, errors.Wrapf(err, "Error updating organization, response code: %d", resp.StatusCode)
}
return c.handleOrgResp(resp)
}
func (c *Client) DeleteOrg(guid string, recursive, async bool) error {
resp, err := c.DoRequest(c.NewRequest("DELETE", fmt.Sprintf("/v2/organizations/%s?recursive=%t&async=%t", guid, recursive, async)))
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return errors.Wrapf(err, "Error deleting organization %s, response code: %d", guid, resp.StatusCode)
}
return nil
}
func (c *Client) getOrgResponse(requestURL string) (OrgResponse, error) {
var orgResp OrgResponse
r := c.NewRequest("GET", requestURL)
resp, err := c.DoRequest(r)
if err != nil {
return OrgResponse{}, errors.Wrap(err, "Error requesting orgs")
}
resBody, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return OrgResponse{}, errors.Wrap(err, "Error reading org request")
}
err = json.Unmarshal(resBody, &orgResp)
if err != nil {
return OrgResponse{}, errors.Wrap(err, "Error unmarshalling org")
}
return orgResp, nil
}
func (c *Client) fetchOrgs(requestURL string) ([]Org, error) {
var orgs []Org
for {
orgResp, err := c.getOrgResponse(requestURL)
if err != nil {
return []Org{}, err
}
for _, org := range orgResp.Resources {
orgs = append(orgs, c.mergeOrgResource(org))
}
requestURL = orgResp.NextUrl
if requestURL == "" {
break
}
}
return orgs, nil
}
func (c *Client) handleOrgResp(resp *http.Response) (Org, error) {
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return Org{}, err
}
var orgResource OrgResource
err = json.Unmarshal(body, &orgResource)
if err != nil {
return Org{}, err
}
return c.mergeOrgResource(orgResource), nil
}
func (c *Client) getOrgUserResponse(requestURL string) (OrgUserResponse, error) {
var omResp OrgUserResponse
r := c.NewRequest("GET", requestURL)
resp, err := c.DoRequest(r)
if err != nil {
return OrgUserResponse{}, errors.Wrap(err, "error requesting org managers")
}
defer resp.Body.Close()
resBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return OrgUserResponse{}, errors.Wrap(err, "error reading org managers response body")
}
if err := json.Unmarshal(resBody, &omResp); err != nil {
return OrgUserResponse{}, errors.Wrap(err, "error unmarshaling org managers")
}
return omResp, nil
}
func (c *Client) mergeOrgResource(org OrgResource) Org {
org.Entity.Guid = org.Meta.Guid
org.Entity.CreatedAt = org.Meta.CreatedAt
org.Entity.UpdatedAt = org.Meta.UpdatedAt
org.Entity.c = c
return org.Entity
}
func (c *Client) DefaultIsolationSegmentForOrg(orgGUID, isolationSegmentGUID string) error {
return c.updateOrgDefaultIsolationSegment(orgGUID, map[string]interface{}{"guid": isolationSegmentGUID})
}
func (c *Client) ResetDefaultIsolationSegmentForOrg(orgGUID string) error {
return c.updateOrgDefaultIsolationSegment(orgGUID, nil)
}
func (c *Client) updateOrgDefaultIsolationSegment(orgGUID string, data interface{}) error {
requestURL := fmt.Sprintf("/v3/organizations/%s/relationships/default_isolation_segment", orgGUID)
buf := bytes.NewBuffer(nil)
err := json.NewEncoder(buf).Encode(map[string]interface{}{"data": data})
if err != nil {
return err
}
r := c.NewRequestWithBody("PATCH", requestURL, buf)
resp, err := c.DoRequest(r)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.Wrapf(err, "Error setting default isolation segment for org %s, response code: %d", orgGUID, resp.StatusCode)
}
return nil
}