Skip to content

Commit

Permalink
kibana API: add parsed response as CreateDownloadSource return parame…
Browse files Browse the repository at this point in the history
…ter (#162)
  • Loading branch information
AndersonQ authored Nov 15, 2023
1 parent 69d2263 commit dc9719a
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions kibana/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,21 @@ type DownloadSource struct {
ProxyID interface{} `json:"proxy_id"`
}

func (client *Client) CreateDownloadSource(ctx context.Context, source DownloadSource) error {
type DownloadSourceResponse struct {
Item struct {
ID string `json:"id"`
Name string `json:"name"`
Host string `json:"host"`
IsDefault bool `json:"is_default"`
ProxyID string `json:"proxy_id"`
} `json:"item"`
}

func (client *Client) CreateDownloadSource(ctx context.Context, source DownloadSource) (DownloadSourceResponse, error) {
reqBody, err := json.Marshal(source)
if err != nil {
return fmt.Errorf("unable to marshal DownloadSource into JSON: %w", err)
return DownloadSourceResponse{},
fmt.Errorf("unable to marshal DownloadSource into JSON: %w", err)
}

resp, err := client.Connection.SendWithContext(
Expand All @@ -162,7 +173,8 @@ func (client *Client) CreateDownloadSource(ctx context.Context, source DownloadS
nil,
bytes.NewReader(reqBody))
if err != nil {
return fmt.Errorf("error calling Agent Binary Download Sources API: %w", err)
return DownloadSourceResponse{},
fmt.Errorf("error calling Agent Binary Download Sources API: %w", err)
}
defer resp.Body.Close()

Expand All @@ -177,11 +189,18 @@ func (client *Client) CreateDownloadSource(ctx context.Context, source DownloadS
client.log.Errorw(
"could not create download source, kibana returned "+resp.Status,
"http.response.body.content", respBody)
return fmt.Errorf("could not create download source, kibana returned %s. response body: %s: %w",
resp.Status, respBody, err)
return DownloadSourceResponse{},
fmt.Errorf("could not create download source, kibana returned %s. response body: %s: %w",
resp.Status, respBody, err)
}

return nil
body := DownloadSourceResponse{}
if err = json.NewDecoder(resp.Body).Decode(&body); err != nil {
return DownloadSourceResponse{},
fmt.Errorf("failed parsing download source response: %w", err)
}

return body, nil
}

// GetPolicy returns the requested ID
Expand Down

0 comments on commit dc9719a

Please sign in to comment.