Skip to content
Permalink
Browse files
Make sure the FirewallService remains backwards compatible
  • Loading branch information
Sander van Harmelen committed Apr 19, 2016
1 parent e97322d commit cdf805c97da22e1e8cc31c849e99f2f02e447c0f
Show file tree
Hide file tree
Showing 2 changed files with 260 additions and 4 deletions.
@@ -24,6 +24,56 @@ import (
"strings"
)

// Helper function for maintaining backwards compatibility
func convertFirewallServiceResponse(b []byte) ([]byte, error) {
var raw map[string]interface{}
if err := json.Unmarshal(b, &raw); err != nil {
return nil, err
}

if _, ok := raw["firewallrule"]; ok {
return convertFirewallServiceListResponse(b)
}

for _, k := range []string{"endport", "startport"} {
if sVal, ok := raw[k].(string); ok {
iVal, err := strconv.Atoi(sVal)
if err != nil {
return nil, err
}
raw[k] = iVal
}
}

return json.Marshal(raw)
}

// Helper function for maintaining backwards compatibility
func convertFirewallServiceListResponse(b []byte) ([]byte, error) {
var rawList struct {
Count int `json:"count"`
FirewallRules []map[string]interface{} `json:"firewallrule"`
}

if err := json.Unmarshal(b, &rawList); err != nil {
return nil, err
}

for _, r := range rawList.FirewallRules {
for _, k := range []string{"endport", "startport"} {
if sVal, ok := r[k].(string); ok {
iVal, err := strconv.Atoi(sVal)
if err != nil {
return nil, err
}
r[k] = iVal
}
}
}

return json.Marshal(rawList)
}

type ListPortForwardingRulesParams struct {
p map[string]interface{}
}
@@ -246,6 +296,11 @@ func (s *FirewallService) ListPortForwardingRules(p *ListPortForwardingRulesPara
return nil, err
}

resp, err = convertFirewallServiceResponse(resp)
if err != nil {
return nil, err
}

var r ListPortForwardingRulesResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
@@ -460,6 +515,11 @@ func (s *FirewallService) CreatePortForwardingRule(p *CreatePortForwardingRulePa
return nil, err
}

resp, err = convertFirewallServiceResponse(resp)
if err != nil {
return nil, err
}

var r CreatePortForwardingRuleResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
@@ -480,6 +540,11 @@ func (s *FirewallService) CreatePortForwardingRule(p *CreatePortForwardingRulePa
return nil, err
}

b, err = convertFirewallServiceResponse(b)
if err != nil {
return nil, err
}

if err := json.Unmarshal(b, &r); err != nil {
return nil, err
}
@@ -558,6 +623,11 @@ func (s *FirewallService) DeletePortForwardingRule(p *DeletePortForwardingRulePa
return nil, err
}

resp, err = convertFirewallServiceResponse(resp)
if err != nil {
return nil, err
}

var r DeletePortForwardingRuleResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
@@ -573,6 +643,11 @@ func (s *FirewallService) DeletePortForwardingRule(p *DeletePortForwardingRulePa
return nil, err
}

b, err = convertFirewallServiceResponse(b)
if err != nil {
return nil, err
}

if err := json.Unmarshal(b, &r); err != nil {
return nil, err
}
@@ -682,6 +757,11 @@ func (s *FirewallService) UpdatePortForwardingRule(p *UpdatePortForwardingRulePa
return nil, err
}

resp, err = convertFirewallServiceResponse(resp)
if err != nil {
return nil, err
}

var r UpdatePortForwardingRuleResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
@@ -702,6 +782,11 @@ func (s *FirewallService) UpdatePortForwardingRule(p *UpdatePortForwardingRulePa
return nil, err
}

b, err = convertFirewallServiceResponse(b)
if err != nil {
return nil, err
}

if err := json.Unmarshal(b, &r); err != nil {
return nil, err
}
@@ -875,6 +960,11 @@ func (s *FirewallService) CreateFirewallRule(p *CreateFirewallRuleParams) (*Crea
return nil, err
}

resp, err = convertFirewallServiceResponse(resp)
if err != nil {
return nil, err
}

var r CreateFirewallRuleResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
@@ -895,6 +985,11 @@ func (s *FirewallService) CreateFirewallRule(p *CreateFirewallRuleParams) (*Crea
return nil, err
}

b, err = convertFirewallServiceResponse(b)
if err != nil {
return nil, err
}

if err := json.Unmarshal(b, &r); err != nil {
return nil, err
}
@@ -969,6 +1064,11 @@ func (s *FirewallService) DeleteFirewallRule(p *DeleteFirewallRuleParams) (*Dele
return nil, err
}

resp, err = convertFirewallServiceResponse(resp)
if err != nil {
return nil, err
}

var r DeleteFirewallRuleResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
@@ -984,6 +1084,11 @@ func (s *FirewallService) DeleteFirewallRule(p *DeleteFirewallRuleParams) (*Dele
return nil, err
}

b, err = convertFirewallServiceResponse(b)
if err != nil {
return nil, err
}

if err := json.Unmarshal(b, &r); err != nil {
return nil, err
}
@@ -1219,6 +1324,11 @@ func (s *FirewallService) ListFirewallRules(p *ListFirewallRulesParams) (*ListFi
return nil, err
}

resp, err = convertFirewallServiceResponse(resp)
if err != nil {
return nil, err
}

var r ListFirewallRulesResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
@@ -1320,6 +1430,11 @@ func (s *FirewallService) UpdateFirewallRule(p *UpdateFirewallRuleParams) (*Upda
return nil, err
}

resp, err = convertFirewallServiceResponse(resp)
if err != nil {
return nil, err
}

var r UpdateFirewallRuleResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
@@ -1340,6 +1455,11 @@ func (s *FirewallService) UpdateFirewallRule(p *UpdateFirewallRuleParams) (*Upda
return nil, err
}

b, err = convertFirewallServiceResponse(b)
if err != nil {
return nil, err
}

if err := json.Unmarshal(b, &r); err != nil {
return nil, err
}
@@ -1509,6 +1629,11 @@ func (s *FirewallService) CreateEgressFirewallRule(p *CreateEgressFirewallRulePa
return nil, err
}

resp, err = convertFirewallServiceResponse(resp)
if err != nil {
return nil, err
}

var r CreateEgressFirewallRuleResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
@@ -1529,6 +1654,11 @@ func (s *FirewallService) CreateEgressFirewallRule(p *CreateEgressFirewallRulePa
return nil, err
}

b, err = convertFirewallServiceResponse(b)
if err != nil {
return nil, err
}

if err := json.Unmarshal(b, &r); err != nil {
return nil, err
}
@@ -1603,6 +1733,11 @@ func (s *FirewallService) DeleteEgressFirewallRule(p *DeleteEgressFirewallRulePa
return nil, err
}

resp, err = convertFirewallServiceResponse(resp)
if err != nil {
return nil, err
}

var r DeleteEgressFirewallRuleResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
@@ -1618,6 +1753,11 @@ func (s *FirewallService) DeleteEgressFirewallRule(p *DeleteEgressFirewallRulePa
return nil, err
}

b, err = convertFirewallServiceResponse(b)
if err != nil {
return nil, err
}

if err := json.Unmarshal(b, &r); err != nil {
return nil, err
}
@@ -1853,6 +1993,11 @@ func (s *FirewallService) ListEgressFirewallRules(p *ListEgressFirewallRulesPara
return nil, err
}

resp, err = convertFirewallServiceResponse(resp)
if err != nil {
return nil, err
}

var r ListEgressFirewallRulesResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
@@ -1954,6 +2099,11 @@ func (s *FirewallService) UpdateEgressFirewallRule(p *UpdateEgressFirewallRulePa
return nil, err
}

resp, err = convertFirewallServiceResponse(resp)
if err != nil {
return nil, err
}

var r UpdateEgressFirewallRuleResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
@@ -1974,6 +2124,11 @@ func (s *FirewallService) UpdateEgressFirewallRule(p *UpdateEgressFirewallRulePa
return nil, err
}

b, err = convertFirewallServiceResponse(b)
if err != nil {
return nil, err
}

if err := json.Unmarshal(b, &r); err != nil {
return nil, err
}
@@ -2096,6 +2251,11 @@ func (s *FirewallService) AddPaloAltoFirewall(p *AddPaloAltoFirewallParams) (*Ad
return nil, err
}

resp, err = convertFirewallServiceResponse(resp)
if err != nil {
return nil, err
}

var r AddPaloAltoFirewallResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
@@ -2116,6 +2276,11 @@ func (s *FirewallService) AddPaloAltoFirewall(p *AddPaloAltoFirewallParams) (*Ad
return nil, err
}

b, err = convertFirewallServiceResponse(b)
if err != nil {
return nil, err
}

if err := json.Unmarshal(b, &r); err != nil {
return nil, err
}
@@ -2182,6 +2347,11 @@ func (s *FirewallService) DeletePaloAltoFirewall(p *DeletePaloAltoFirewallParams
return nil, err
}

resp, err = convertFirewallServiceResponse(resp)
if err != nil {
return nil, err
}

var r DeletePaloAltoFirewallResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
@@ -2197,6 +2367,11 @@ func (s *FirewallService) DeletePaloAltoFirewall(p *DeletePaloAltoFirewallParams
return nil, err
}

b, err = convertFirewallServiceResponse(b)
if err != nil {
return nil, err
}

if err := json.Unmarshal(b, &r); err != nil {
return nil, err
}
@@ -2261,6 +2436,11 @@ func (s *FirewallService) ConfigurePaloAltoFirewall(p *ConfigurePaloAltoFirewall
return nil, err
}

resp, err = convertFirewallServiceResponse(resp)
if err != nil {
return nil, err
}

var r ConfigurePaloAltoFirewallResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err
@@ -2281,6 +2461,11 @@ func (s *FirewallService) ConfigurePaloAltoFirewall(p *ConfigurePaloAltoFirewall
return nil, err
}

b, err = convertFirewallServiceResponse(b)
if err != nil {
return nil, err
}

if err := json.Unmarshal(b, &r); err != nil {
return nil, err
}
@@ -2392,6 +2577,11 @@ func (s *FirewallService) ListPaloAltoFirewalls(p *ListPaloAltoFirewallsParams)
return nil, err
}

resp, err = convertFirewallServiceResponse(resp)
if err != nil {
return nil, err
}

var r ListPaloAltoFirewallsResponse
if err := json.Unmarshal(resp, &r); err != nil {
return nil, err

0 comments on commit cdf805c

Please sign in to comment.