Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kibana API: add parsed response as CreateDownloadSource return parameter #162

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading