Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from rapid7/support-empty-list-fields-kong
Browse files Browse the repository at this point in the history
Support parsing empty object as empty array
  • Loading branch information
erran authored Jan 31, 2018
2 parents 8169946 + 15072fd commit 4478f1d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 20 deletions.
61 changes: 61 additions & 0 deletions kong/api_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package kong

import (
"encoding/json"
)

// APIResponse : Kong API response object structure
type APIResponse struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
UpstreamURL string `json:"upstream_url"`
StripURI bool `json:"strip_uri"`
PreserveHost bool `json:"preserve_host"`
Retries int `json:"retries,omitempty"`
UpstreamConnectTimeout int `json:"upstream_connect_timeout,omitempty"`
UpstreamSendTimeout int `json:"upstream_send_timeout,omitempty"`
UpstreamReadTimeout int `json:"upstream_read_timeout,omitempty"`
HTTPSOnly bool `json:"https_only"`
HTTPIfTerminated bool `json:"http_if_terminated"`

// These will be set in our UnmarshalJSON function.
Hosts []string `json:"-"`
Methods []string `json:"-"`
Uris []string `json:"-"`
}

func (r *APIResponse) UnmarshalJSON(data []byte) error {
type Alias APIResponse
wrapped := &struct {
RawHosts interface{} `json:"hosts,omitempty"`
RawMethods interface{} `json:"methods,omitempty"`
RawUris interface{} `json:"uris,omitempty"`
*Alias
}{
Alias: (*Alias)(r),
}

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

if hosts, ok := wrapped.RawHosts.([]interface{}); ok {
for _, host := range hosts {
r.Hosts = append(r.Hosts, host.(string))
}
}

if methods, ok := wrapped.RawMethods.([]interface{}); ok {
for _, method := range methods {
r.Methods = append(r.Methods, method.(string))
}
}

if uris, ok := wrapped.RawUris.([]interface{}); ok {
for _, uri := range uris {
r.Uris = append(r.Uris, uri.(string))
}
}

return nil
}
22 changes: 2 additions & 20 deletions kong/resource_kong_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,6 @@ type APIRequest struct {
HTTPIfTerminated bool `json:"http_if_terminated"`
}

// APIResponse : Kong API response object structure
type APIResponse struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Hosts []string `json:"hosts,omitempty"`
Uris []string `json:"uris,omitempty"`
Methods []string `json:"methods,omitempty"`
UpstreamURL string `json:"upstream_url"`
StripURI bool `json:"strip_uri"`
PreserveHost bool `json:"preserve_host"`
Retries int `json:"retries,omitempty"`
UpstreamConnectTimeout int `json:"upstream_connect_timeout,omitempty"`
UpstreamSendTimeout int `json:"upstream_send_timeout,omitempty"`
UpstreamReadTimeout int `json:"upstream_read_timeout,omitempty"`
HTTPSOnly bool `json:"https_only"`
HTTPIfTerminated bool `json:"http_if_terminated"`
}

type APICreateError struct {
}

Expand Down Expand Up @@ -273,9 +255,9 @@ func getAPIFromResourceData(d *schema.ResourceData) *APIRequest {
func setAPIToResourceData(d *schema.ResourceData, api *APIResponse) {
d.SetId(api.ID)
d.Set("name", api.Name)
d.Set("hosts", api.Hosts)
d.Set("hosts", strings.Join(api.Hosts, ","))
d.Set("uris", strings.Join(api.Uris, ","))
d.Set("methods", api.Methods)
d.Set("methods", strings.Join(api.Methods, ","))
d.Set("upstream_url", api.UpstreamURL)
d.Set("strip_uri", api.StripURI)
d.Set("preserve_host", api.PreserveHost)
Expand Down

0 comments on commit 4478f1d

Please sign in to comment.