forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
organization.go
79 lines (65 loc) · 2.33 KB
/
organization.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
package v3action
import (
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/actor/versioncheck"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
)
// Organization represents a V3 actor organization.
type Organization ccv3.Organization
// GetOrganizationByName returns the organization with the given name.
func (actor Actor) GetOrganizationByName(name string) (Organization, Warnings, error) {
orgs, warnings, err := actor.CloudControllerClient.GetOrganizations(
ccv3.Query{Key: ccv3.NameFilter, Values: []string{name}},
)
if err != nil {
return Organization{}, Warnings(warnings), err
}
if len(orgs) == 0 {
return Organization{}, Warnings(warnings), actionerror.OrganizationNotFoundError{Name: name}
}
return Organization(orgs[0]), Warnings(warnings), nil
}
func (actor Actor) GetOrganizationsByGUIDs(guids ...string) ([]Organization, Warnings, error) {
currentV3Ver := actor.CloudControllerClient.CloudControllerAPIVersion()
guidsSupport, err := versioncheck.IsMinimumAPIVersionMet(currentV3Ver, ccversion.MinVersionSpacesGUIDsParamV3)
if err != nil {
guidsSupport = false
}
queries := []ccv3.Query{}
if guidsSupport {
queries = []ccv3.Query{ccv3.Query{Key: ccv3.GUIDFilter, Values: guids}}
}
orgs, warnings, err := actor.CloudControllerClient.GetOrganizations(queries...)
if err != nil {
return []Organization{}, Warnings(warnings), err
}
guidToOrg := make(map[string]ccv3.Organization)
for _, org := range orgs {
guidToOrg[org.GUID] = org
}
filteredOrgs := make([]ccv3.Organization, 0)
for _, guid := range guids {
filteredOrgs = append(filteredOrgs, guidToOrg[guid])
}
orgs = filteredOrgs
return convertCCToActorOrganizations(orgs), Warnings(warnings), nil
}
func (actor Actor) GetOrganizations() ([]Organization, Warnings, error) {
orderBy := ccv3.Query{
Key: "order_by",
Values: []string{"name"},
}
orgs, warnings, err := actor.CloudControllerClient.GetOrganizations(orderBy)
if err != nil {
return []Organization{}, Warnings(warnings), err
}
return convertCCToActorOrganizations(orgs), Warnings(warnings), nil
}
func convertCCToActorOrganizations(v3orgs []ccv3.Organization) []Organization {
orgs := make([]Organization, len(v3orgs))
for i := range v3orgs {
orgs[i] = Organization(v3orgs[i])
}
return orgs
}