github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/cloudcontroller/ccv2/security_group.go (about) 1 package ccv2 2 3 import ( 4 "encoding/json" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller" 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal" 9 ) 10 11 // SecurityGroupRule represents a Cloud Controller Security Group Role. 12 type SecurityGroupRule struct { 13 // Description is a short message discribing the rule. 14 Description string 15 16 // Destination is the destination CIDR or range of IPs. 17 Destination string 18 19 // Ports is the port or port range. 20 Ports string 21 22 // Protocol can be tcp, icmp, udp, all. 23 Protocol string 24 } 25 26 // SecurityGroup represents a Cloud Controller Security Group. 27 type SecurityGroup struct { 28 // GUID is the unique Security Group identifier. 29 GUID string 30 // Name is the Security Group's name. 31 Name string 32 // Rules are the Security Group Rules associated with this Security Group. 33 Rules []SecurityGroupRule 34 // RunningDefault is true when this Security Group is applied to all running 35 // apps in the CF instance. 36 RunningDefault bool 37 // StagingDefault is true when this Security Group is applied to all staging 38 // apps in the CF instance. 39 StagingDefault bool 40 } 41 42 // UnmarshalJSON helps unmarshal a Cloud Controller Security Group response 43 func (securityGroup *SecurityGroup) UnmarshalJSON(data []byte) error { 44 var ccSecurityGroup struct { 45 Metadata internal.Metadata `json:"metadata"` 46 Entity struct { 47 GUID string `json:"guid"` 48 Name string `json:"name"` 49 Rules []struct { 50 Description string `json:"description"` 51 Destination string `json:"destination"` 52 Ports string `json:"ports"` 53 Protocol string `json:"protocol"` 54 } `json:"rules"` 55 RunningDefault bool `json:"running_default"` 56 StagingDefault bool `json:"staging_default"` 57 } `json:"entity"` 58 } 59 60 if err := json.Unmarshal(data, &ccSecurityGroup); err != nil { 61 return err 62 } 63 64 securityGroup.GUID = ccSecurityGroup.Metadata.GUID 65 securityGroup.Name = ccSecurityGroup.Entity.Name 66 securityGroup.Rules = make([]SecurityGroupRule, len(ccSecurityGroup.Entity.Rules)) 67 for i, ccRule := range ccSecurityGroup.Entity.Rules { 68 securityGroup.Rules[i].Description = ccRule.Description 69 securityGroup.Rules[i].Destination = ccRule.Destination 70 securityGroup.Rules[i].Ports = ccRule.Ports 71 securityGroup.Rules[i].Protocol = ccRule.Protocol 72 } 73 securityGroup.RunningDefault = ccSecurityGroup.Entity.RunningDefault 74 securityGroup.StagingDefault = ccSecurityGroup.Entity.StagingDefault 75 return nil 76 } 77 78 // UpdateSecurityGroupSpace associates a security group in the running phase 79 // for the lifecycle, specified by its GUID, from a space, which is also 80 // specified by its GUID. 81 func (client *Client) UpdateSecurityGroupSpace(securityGroupGUID string, spaceGUID string) (Warnings, error) { 82 request, err := client.newHTTPRequest(requestOptions{ 83 RequestName: internal.PutSecurityGroupSpaceRequest, 84 URIParams: Params{ 85 "security_group_guid": securityGroupGUID, 86 "space_guid": spaceGUID, 87 }, 88 }) 89 90 if err != nil { 91 return nil, err 92 } 93 94 response := cloudcontroller.Response{} 95 96 err = client.connection.Make(request, &response) 97 return response.Warnings, err 98 } 99 100 // UpdateSecurityGroupStagingSpace associates a security group in the staging 101 // phase for the lifecycle, specified by its GUID, from a space, which is also 102 // specified by its GUID. 103 func (client *Client) UpdateSecurityGroupStagingSpace(securityGroupGUID string, spaceGUID string) (Warnings, error) { 104 request, err := client.newHTTPRequest(requestOptions{ 105 RequestName: internal.PutSecurityGroupStagingSpaceRequest, 106 URIParams: Params{ 107 "security_group_guid": securityGroupGUID, 108 "space_guid": spaceGUID, 109 }, 110 }) 111 112 if err != nil { 113 return nil, err 114 } 115 116 response := cloudcontroller.Response{} 117 118 err = client.connection.Make(request, &response) 119 return response.Warnings, err 120 } 121 122 // GetSecurityGroups returns a list of Security Groups based off the provided 123 // filters. 124 func (client *Client) GetSecurityGroups(filters ...Filter) ([]SecurityGroup, Warnings, error) { 125 request, err := client.newHTTPRequest(requestOptions{ 126 RequestName: internal.GetSecurityGroupsRequest, 127 Query: ConvertFilterParameters(filters), 128 }) 129 130 if err != nil { 131 return nil, nil, err 132 } 133 134 var securityGroupsList []SecurityGroup 135 warnings, err := client.paginate(request, SecurityGroup{}, func(item interface{}) error { 136 if securityGroup, ok := item.(SecurityGroup); ok { 137 securityGroupsList = append(securityGroupsList, securityGroup) 138 } else { 139 return ccerror.UnknownObjectInListError{ 140 Expected: SecurityGroup{}, 141 Unexpected: item, 142 } 143 } 144 return nil 145 }) 146 147 return securityGroupsList, warnings, err 148 } 149 150 // GetSpaceSecurityGroups returns the running Security Groups associated with 151 // the provided Space GUID. 152 func (client *Client) GetSpaceSecurityGroups(spaceGUID string, filters ...Filter) ([]SecurityGroup, Warnings, error) { 153 return client.getSpaceSecurityGroupsBySpaceAndLifecycle(spaceGUID, internal.GetSpaceSecurityGroupsRequest, filters) 154 } 155 156 // GetSpaceStagingSecurityGroups returns the staging Security Groups 157 // associated with the provided Space GUID. 158 func (client *Client) GetSpaceStagingSecurityGroups(spaceGUID string, filters ...Filter) ([]SecurityGroup, Warnings, error) { 159 return client.getSpaceSecurityGroupsBySpaceAndLifecycle(spaceGUID, internal.GetSpaceStagingSecurityGroupsRequest, filters) 160 } 161 162 func (client *Client) getSpaceSecurityGroupsBySpaceAndLifecycle(spaceGUID string, lifecycle string, filters []Filter) ([]SecurityGroup, Warnings, error) { 163 request, err := client.newHTTPRequest(requestOptions{ 164 RequestName: lifecycle, 165 URIParams: map[string]string{"space_guid": spaceGUID}, 166 Query: ConvertFilterParameters(filters), 167 }) 168 if err != nil { 169 return nil, nil, err 170 } 171 172 var securityGroupsList []SecurityGroup 173 warnings, err := client.paginate(request, SecurityGroup{}, func(item interface{}) error { 174 if securityGroup, ok := item.(SecurityGroup); ok { 175 securityGroupsList = append(securityGroupsList, securityGroup) 176 } else { 177 return ccerror.UnknownObjectInListError{ 178 Expected: SecurityGroup{}, 179 Unexpected: item, 180 } 181 } 182 return err 183 }) 184 185 return securityGroupsList, warnings, err 186 } 187 188 // DeleteSecurityGroupSpace disassociates a security group in the running phase 189 // for the lifecycle, specified by its GUID, from a space, which is also 190 // specified by its GUID. 191 func (client *Client) DeleteSecurityGroupSpace(securityGroupGUID string, spaceGUID string) (Warnings, error) { 192 request, err := client.newHTTPRequest(requestOptions{ 193 RequestName: internal.DeleteSecurityGroupSpaceRequest, 194 URIParams: Params{ 195 "security_group_guid": securityGroupGUID, 196 "space_guid": spaceGUID, 197 }, 198 }) 199 200 if err != nil { 201 return nil, err 202 } 203 204 response := cloudcontroller.Response{} 205 206 err = client.connection.Make(request, &response) 207 return response.Warnings, err 208 } 209 210 // DeleteSecurityGroupStagingSpace disassociates a security group in the 211 // staging phase fo the lifecycle, specified by its GUID, from a space, which 212 // is also specified by its GUID. 213 func (client *Client) DeleteSecurityGroupStagingSpace(securityGroupGUID string, spaceGUID string) (Warnings, error) { 214 request, err := client.newHTTPRequest(requestOptions{ 215 RequestName: internal.DeleteSecurityGroupStagingSpaceRequest, 216 URIParams: Params{ 217 "security_group_guid": securityGroupGUID, 218 "space_guid": spaceGUID, 219 }, 220 }) 221 222 if err != nil { 223 return nil, err 224 } 225 226 response := cloudcontroller.Response{} 227 228 err = client.connection.Make(request, &response) 229 return response.Warnings, err 230 }