forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
security_groups.go
132 lines (112 loc) · 3.58 KB
/
security_groups.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
package security_groups
import (
"fmt"
"net/url"
"github.com/cloudfoundry/cli/cf/api/resources"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/net"
)
type SecurityGroupRepo interface {
Create(name string, rules []map[string]interface{}) error
Update(guid string, rules []map[string]interface{}) error
Read(string) (models.SecurityGroup, error)
Delete(string) error
FindAll() ([]models.SecurityGroup, error)
}
type cloudControllerSecurityGroupRepo struct {
gateway net.Gateway
config core_config.Reader
}
func NewSecurityGroupRepo(config core_config.Reader, gateway net.Gateway) SecurityGroupRepo {
return cloudControllerSecurityGroupRepo{
config: config,
gateway: gateway,
}
}
func (repo cloudControllerSecurityGroupRepo) Create(name string, rules []map[string]interface{}) error {
path := "/v2/security_groups"
params := models.SecurityGroupParams{
Name: name,
Rules: rules,
}
return repo.gateway.CreateResourceFromStruct(repo.config.ApiEndpoint(), path, params)
}
func (repo cloudControllerSecurityGroupRepo) Read(name string) (models.SecurityGroup, error) {
path := fmt.Sprintf("/v2/security_groups?q=%s", url.QueryEscape("name:"+name))
group := models.SecurityGroup{}
foundGroup := false
err := repo.gateway.ListPaginatedResources(
repo.config.ApiEndpoint(),
path,
resources.SecurityGroupResource{},
func(resource interface{}) bool {
if asgr, ok := resource.(resources.SecurityGroupResource); ok {
group = asgr.ToModel()
foundGroup = true
}
return false
},
)
if err != nil {
return group, err
}
if !foundGroup {
return group, errors.NewModelNotFoundError("security group", name)
}
err = repo.gateway.ListPaginatedResources(
repo.config.ApiEndpoint(),
group.SpaceUrl+"?inline-relations-depth=1",
resources.SpaceResource{},
func(resource interface{}) bool {
if asgr, ok := resource.(resources.SpaceResource); ok {
group.Spaces = append(group.Spaces, asgr.ToModel())
return true
}
return false
},
)
return group, err
}
func (repo cloudControllerSecurityGroupRepo) Update(guid string, rules []map[string]interface{}) error {
url := fmt.Sprintf("/v2/security_groups/%s", guid)
return repo.gateway.UpdateResourceFromStruct(repo.config.ApiEndpoint(), url, models.SecurityGroupParams{Rules: rules})
}
func (repo cloudControllerSecurityGroupRepo) FindAll() ([]models.SecurityGroup, error) {
path := "/v2/security_groups"
securityGroups := []models.SecurityGroup{}
err := repo.gateway.ListPaginatedResources(
repo.config.ApiEndpoint(),
path,
resources.SecurityGroupResource{},
func(resource interface{}) bool {
if securityGroupResource, ok := resource.(resources.SecurityGroupResource); ok {
securityGroups = append(securityGroups, securityGroupResource.ToModel())
}
return true
},
)
if err != nil {
return nil, err
}
for i := range securityGroups {
err = repo.gateway.ListPaginatedResources(
repo.config.ApiEndpoint(),
securityGroups[i].SpaceUrl+"?inline-relations-depth=1",
resources.SpaceResource{},
func(resource interface{}) bool {
if asgr, ok := resource.(resources.SpaceResource); ok {
securityGroups[i].Spaces = append(securityGroups[i].Spaces, asgr.ToModel())
return true
}
return false
},
)
}
return securityGroups, err
}
func (repo cloudControllerSecurityGroupRepo) Delete(securityGroupGuid string) error {
path := fmt.Sprintf("/v2/security_groups/%s", securityGroupGuid)
return repo.gateway.DeleteResource(repo.config.ApiEndpoint(), path)
}