forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
space.go
92 lines (77 loc) · 2.37 KB
/
space.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
package v2action
import (
"fmt"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
)
// Space represents a CLI Space
type Space ccv2.Space
// SpaceFoundError represents the scenario when the space searched for could
// not be found.
type SpaceNotFoundError struct {
GUID string
Name string
}
func (e SpaceNotFoundError) Error() string {
switch {
case e.Name != "":
return fmt.Sprintf("Space '%s' not found.", e.Name)
case e.GUID != "":
return fmt.Sprintf("Space with GUID '%s' not found.", e.GUID)
default:
return fmt.Sprintf("Space '' not found.")
}
}
// MultipleSpacesFoundError represents the scenario when the cloud
// controller returns multiple spaces when filtering by name. This is a
// far out edge case and should not happen.
type MultipleSpacesFoundError struct {
Name string
OrgGUID string
}
func (e MultipleSpacesFoundError) Error() string {
return fmt.Sprintf("Multiple spaces found matching organization GUID '%s' and name '%s'", e.OrgGUID, e.Name)
}
// GetOrganizationSpaces returns a list of spaces in the specified org
func (actor Actor) GetOrganizationSpaces(orgGUID string) ([]Space, Warnings, error) {
query := []ccv2.Query{
{
Filter: ccv2.OrganizationGUIDFilter,
Operator: ccv2.EqualOperator,
Value: orgGUID,
}}
ccv2Spaces, warnings, err := actor.CloudControllerClient.GetSpaces(query)
if err != nil {
return []Space{}, Warnings(warnings), err
}
spaces := make([]Space, len(ccv2Spaces))
for i, ccv2Space := range ccv2Spaces {
spaces[i] = Space(ccv2Space)
}
return spaces, Warnings(warnings), nil
}
// GetSpaceByOrganizationAndName returns an Space based on the org and name.
func (actor Actor) GetSpaceByOrganizationAndName(orgGUID string, spaceName string) (Space, Warnings, error) {
query := []ccv2.Query{
{
Filter: ccv2.NameFilter,
Operator: ccv2.EqualOperator,
Value: spaceName,
},
{
Filter: ccv2.OrganizationGUIDFilter,
Operator: ccv2.EqualOperator,
Value: orgGUID,
},
}
ccv2Spaces, warnings, err := actor.CloudControllerClient.GetSpaces(query)
if err != nil {
return Space{}, Warnings(warnings), err
}
if len(ccv2Spaces) == 0 {
return Space{}, Warnings(warnings), SpaceNotFoundError{Name: spaceName}
}
if len(ccv2Spaces) > 1 {
return Space{}, Warnings(warnings), MultipleSpacesFoundError{OrgGUID: orgGUID, Name: spaceName}
}
return Space(ccv2Spaces[0]), Warnings(warnings), nil
}