Skip to content

Commit 9d80c6c

Browse files
authored
Add serverless provider (#1374)
Add support to create serverless projects and test packages using elastic-package with a new provider (--provider serverless). Currently, supported serverless projects in elastic-package are observability and security. For testing packages in serverless, geoIP fields in documents are skipped when comparing with the expected documents, since it could not be set a specific GeoIP database to make the tests stable.
1 parent ec2bd80 commit 9d80c6c

File tree

17 files changed

+1275
-35
lines changed

17 files changed

+1275
-35
lines changed

internal/kibana/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ func NewClient(opts ...ClientOption) (*Client, error) {
5252
// Allow to initialize version from tests.
5353
var zeroVersion VersionInfo
5454
if c.semver == nil || c.versionInfo == zeroVersion {
55-
v, err := c.requestVersion()
55+
v, err := c.requestStatus()
5656
if err != nil {
5757
return nil, fmt.Errorf("failed to get Kibana version: %w", err)
5858
}
59-
c.versionInfo = v
59+
c.versionInfo = v.Version
6060

6161
c.semver, err = semver.NewVersion(c.versionInfo.Number)
6262
if err != nil {

internal/kibana/fleet.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package kibana
6+
7+
import (
8+
"encoding/json"
9+
"errors"
10+
"fmt"
11+
"net/http"
12+
)
13+
14+
// DefaultFleetServerURL returns the default Fleet server configured in Kibana
15+
func (c *Client) DefaultFleetServerURL() (string, error) {
16+
path := fmt.Sprintf("%s/fleet_server_hosts", FleetAPI)
17+
18+
statusCode, respBody, err := c.get(path)
19+
if err != nil {
20+
return "", fmt.Errorf("could not reach fleet server hosts endpoint: %w", err)
21+
}
22+
23+
if statusCode != http.StatusOK {
24+
return "", fmt.Errorf("could not get status data; API status code = %d; response body = %s", statusCode, respBody)
25+
}
26+
27+
var hosts struct {
28+
Items []struct {
29+
IsDefault bool `json:"is_default"`
30+
HostURLs []string `json:"host_urls"`
31+
} `json:"items"`
32+
}
33+
err = json.Unmarshal(respBody, &hosts)
34+
if err != nil {
35+
return "", fmt.Errorf("failed to decode response: %w", err)
36+
}
37+
38+
for _, server := range hosts.Items {
39+
if server.IsDefault && len(server.HostURLs) > 0 {
40+
return server.HostURLs[0], nil
41+
}
42+
}
43+
44+
return "", errors.New("could not find the fleet server URL for this project")
45+
}

internal/kibana/status.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,46 @@ func (v VersionInfo) IsSnapshot() bool {
3030

3131
type statusType struct {
3232
Version VersionInfo `json:"version"`
33+
Status struct {
34+
Overall struct {
35+
Level string `json:"level"`
36+
} `json:"overall"`
37+
} `json:"status"`
3338
}
3439

3540
// Version method returns the version of Kibana (Elastic stack)
3641
func (c *Client) Version() (VersionInfo, error) {
3742
return c.versionInfo, nil
3843
}
3944

40-
func (c *Client) requestVersion() (VersionInfo, error) {
41-
var version VersionInfo
45+
func (c *Client) requestStatus() (statusType, error) {
46+
var status statusType
4247
statusCode, respBody, err := c.get(StatusAPI)
4348
if err != nil {
44-
return version, fmt.Errorf("could not reach status endpoint: %w", err)
49+
return status, fmt.Errorf("could not reach status endpoint: %w", err)
4550
}
4651

4752
if statusCode != http.StatusOK {
48-
return version, fmt.Errorf("could not get status data; API status code = %d; response body = %s", statusCode, respBody)
53+
return status, fmt.Errorf("could not get status data; API status code = %d; response body = %s", statusCode, respBody)
4954
}
5055

51-
var status statusType
5256
err = json.Unmarshal(respBody, &status)
5357
if err != nil {
54-
return version, fmt.Errorf("unmarshalling response failed (body: \n%s): %w", respBody, err)
58+
return status, fmt.Errorf("unmarshalling response failed (body: \n%s): %w", respBody, err)
59+
}
60+
61+
return status, nil
62+
}
63+
64+
// CheckHealth checks the Kibana health
65+
func (c *Client) CheckHealth() error {
66+
status, err := c.requestStatus()
67+
if err != nil {
68+
return fmt.Errorf("could not reach status endpoint: %w", err)
5569
}
5670

57-
return status.Version, nil
71+
if status.Status.Overall.Level != "available" {
72+
return fmt.Errorf("kibana in unhealthy state: %s", status.Status.Overall.Level)
73+
}
74+
return nil
5875
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
11
# Directory containing GeoIP databases for stacks managed by elastic-agent.
22
# stack.geoip_dir: "/path/to/geoip_dir/"
3+
## Elastic Cloud
4+
# Host URL
5+
# stack.elastic_cloud.host: https://cloud.elastic.co
6+
7+
## Serverless stack provider
8+
# Project type
9+
# stack.serverless.type: observability
10+
# Region where the Serverless project is going to be created
11+
# stack.serverless.region: aws-us-east-1
12+

0 commit comments

Comments
 (0)