forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity_group_resource.go
80 lines (67 loc) · 2.24 KB
/
security_group_resource.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
package resources
import (
"encoding/json"
"code.cloudfoundry.org/jsonry"
)
type SecurityGroup struct {
Name string `jsonry:"name,omitempty"`
GUID string `jsonry:"guid,omitempty"`
Rules []Rule `jsonry:"rules,omitempty"`
StagingGloballyEnabled *bool `jsonry:"globally_enabled.staging,omitempty"`
RunningGloballyEnabled *bool `jsonry:"globally_enabled.running,omitempty"`
StagingSpaceGUIDs []string `jsonry:"relationships.staging_spaces.data[].guid,omitempty"`
RunningSpaceGUIDs []string `jsonry:"relationships.running_spaces.data[].guid,omitempty"`
}
func (sg SecurityGroup) MarshalJSON() ([]byte, error) {
return jsonry.Marshal(sg)
}
func (sg *SecurityGroup) UnmarshalJSON(data []byte) error {
type alias SecurityGroup
var defaultUnmarshalledSecurityGroup alias
err := json.Unmarshal(data, &defaultUnmarshalledSecurityGroup)
if err != nil {
return err
}
*sg = SecurityGroup(defaultUnmarshalledSecurityGroup)
type RemainingFieldsStruct struct {
GloballyEnabled struct {
Staging *bool
Running *bool
} `json:"globally_enabled"`
Relationships struct {
StagingSpaces struct {
Data []struct {
Guid string
}
} `json:"staging_spaces"`
RunningSpaces struct {
Data []struct {
Guid string
}
} `json:"running_spaces"`
}
}
var remainingFields RemainingFieldsStruct
err = json.Unmarshal(data, &remainingFields)
if err != nil {
return err
}
for _, stagingSpace := range remainingFields.Relationships.StagingSpaces.Data {
sg.StagingSpaceGUIDs = append(sg.StagingSpaceGUIDs, stagingSpace.Guid)
}
for _, runningSpace := range remainingFields.Relationships.RunningSpaces.Data {
sg.RunningSpaceGUIDs = append(sg.RunningSpaceGUIDs, runningSpace.Guid)
}
sg.StagingGloballyEnabled = remainingFields.GloballyEnabled.Staging
sg.RunningGloballyEnabled = remainingFields.GloballyEnabled.Running
return nil
}
type Rule struct {
Protocol string `json:"protocol"`
Destination string `json:"destination"`
Ports *string `json:"ports,omitempty"`
Type *int `json:"type,omitempty"`
Code *int `json:"code,omitempty"`
Description *string `json:"description,omitempty"`
Log *bool `json:"log,omitempty"`
}