-
Notifications
You must be signed in to change notification settings - Fork 927
/
organization.go
88 lines (75 loc) · 2.77 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
80
81
82
83
84
85
86
87
88
package ccv2
import (
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// Organization represents a Cloud Controller Organization.
type Organization struct {
GUID string
Name string
QuotaDefinitionGUID string
DefaultIsolationSegmentGUID string
}
// UnmarshalJSON helps unmarshal a Cloud Controller Organization response.
func (org *Organization) UnmarshalJSON(data []byte) error {
var ccOrg struct {
Metadata internal.Metadata `json:"metadata"`
Entity struct {
Name string `json:"name"`
QuotaDefinitionGUID string `json:"quota_definition_guid"`
DefaultIsolationSegmentGUID string `json:"default_isolation_segment_guid"`
} `json:"entity"`
}
if err := json.Unmarshal(data, &ccOrg); err != nil {
return err
}
org.GUID = ccOrg.Metadata.GUID
org.Name = ccOrg.Entity.Name
org.QuotaDefinitionGUID = ccOrg.Entity.QuotaDefinitionGUID
org.DefaultIsolationSegmentGUID = ccOrg.Entity.DefaultIsolationSegmentGUID
return nil
}
//go:generate go run $GOPATH/src/code.cloudfoundry.org/cli/util/codegen/generate.go Organization codetemplates/delete_async_by_guid.go.template delete_organization.go
//go:generate go run $GOPATH/src/code.cloudfoundry.org/cli/util/codegen/generate.go Organization codetemplates/delete_async_by_guid_test.go.template delete_organization_test.go
// GetOrganization returns an Organization associated with the provided guid.
func (client *Client) GetOrganization(guid string) (Organization, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetOrganizationRequest,
URIParams: Params{"organization_guid": guid},
})
if err != nil {
return Organization{}, nil, err
}
var org Organization
response := cloudcontroller.Response{
Result: &org,
}
err = client.connection.Make(request, &response)
return org, response.Warnings, err
}
// GetOrganizations returns back a list of Organizations based off of the
// provided queries.
func (client *Client) GetOrganizations(queries []Query) ([]Organization, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetOrganizationsRequest,
Query: FormatQueryParameters(queries),
})
if err != nil {
return nil, nil, err
}
var fullOrgsList []Organization
warnings, err := client.paginate(request, Organization{}, func(item interface{}) error {
if org, ok := item.(Organization); ok {
fullOrgsList = append(fullOrgsList, org)
} else {
return ccerror.UnknownObjectInListError{
Expected: Organization{},
Unexpected: item,
}
}
return nil
})
return fullOrgsList, warnings, err
}