Skip to content

Commit dd908a8

Browse files
authored
Enable developer extensions (#840)
* Add test package * Define another type * Use: build/packages * Fix * Changelog 0 * Fix * Fix: duplicated fields * Fix: package * Fix: format * Remove replace tag * Add build.yml * Visit external dependencies * Fix: format * WIP * Refactor functions * Fix: go.mod * Fix: go.mod * Comment * Fix: debug
1 parent b95d3db commit dd908a8

File tree

15 files changed

+1275
-31
lines changed

15 files changed

+1275
-31
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/elastic/go-elasticsearch/v7 v7.17.1
1313
github.com/elastic/go-licenser v0.4.0
1414
github.com/elastic/go-ucfg v0.8.5
15-
github.com/elastic/package-spec v1.10.0
15+
github.com/elastic/package-spec v1.11.0
1616
github.com/fatih/color v1.13.0
1717
github.com/go-git/go-billy/v5 v5.3.1
1818
github.com/go-git/go-git/v5 v5.4.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,8 @@ github.com/elastic/go-licenser v0.4.0 h1:jLq6A5SilDS/Iz1ABRkO6BHy91B9jBora8FwGRs
411411
github.com/elastic/go-licenser v0.4.0/go.mod h1:V56wHMpmdURfibNBggaSBfqgPxyT1Tldns1i87iTEvU=
412412
github.com/elastic/go-ucfg v0.8.5 h1:4GB/rMpuh7qTcSFaxJUk97a/JyvFzhi6t+kaskTTLdM=
413413
github.com/elastic/go-ucfg v0.8.5/go.mod h1:4E8mPOLSUV9hQ7sgLEJ4bvt0KhMuDJa8joDT2QGAEKA=
414-
github.com/elastic/package-spec v1.10.0 h1:fkCZRmxN4jesLuylGOEX5g31iITCXZFMbkgX6qvzkZI=
415-
github.com/elastic/package-spec v1.10.0/go.mod h1:KzGTSDqCkdhmL1IFpOH2ZQNSSE9JEhNtndxU3ZrQilA=
414+
github.com/elastic/package-spec v1.11.0 h1:atrhfGqCDVOnVO83Qh+doAjWnk/3Cs+d4C0U1YhvhgI=
415+
github.com/elastic/package-spec v1.11.0/go.mod h1:KzGTSDqCkdhmL1IFpOH2ZQNSSE9JEhNtndxU3ZrQilA=
416416
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153 h1:yUdfgN0XgIJw7foRItutHYUIhlcKzcSf5vDpdhQAKTc=
417417
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
418418
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=

internal/builder/external_fields.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,18 @@ func resolveExternalFields(packageRoot, destinationDir string) error {
3737
return errors.Wrap(err, "can't create field dependency manager")
3838
}
3939

40-
fieldsFile, err := filepath.Glob(filepath.Join(destinationDir, "data_stream", "*", "fields", "*.yml"))
40+
dataStreamFieldsFiles, err := filepath.Glob(filepath.Join(destinationDir, "data_stream", "*", "fields", "*.yml"))
4141
if err != nil {
4242
return err
4343
}
44-
for _, file := range fieldsFile {
44+
45+
packageFieldsFiles, err := filepath.Glob(filepath.Join(destinationDir, "fields", "*.yml"))
46+
if err != nil {
47+
return err
48+
}
49+
50+
var fieldsFiles = append(packageFieldsFiles, dataStreamFieldsFiles...)
51+
for _, file := range fieldsFiles {
4552
data, err := os.ReadFile(file)
4653
if err != nil {
4754
return err

internal/docs/exported_fields.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package docs
66

77
import (
88
"fmt"
9-
"path/filepath"
109
"sort"
1110
"strings"
1211

@@ -25,11 +24,10 @@ type fieldsTableRecord struct {
2524

2625
var escaper = strings.NewReplacer("*", "\\*", "{", "\\{", "}", "\\}", "<", "\\<", ">", "\\>")
2726

28-
func renderExportedFields(packageRoot, dataStreamName string) (string, error) {
29-
dataStreamPath := filepath.Join(packageRoot, "data_stream", dataStreamName)
30-
validator, err := fields.CreateValidatorForDataStream(dataStreamPath)
27+
func renderExportedFields(fieldsParentDir string) (string, error) {
28+
validator, err := fields.CreateValidatorForDirectory(fieldsParentDir)
3129
if err != nil {
32-
return "", errors.Wrapf(err, "can't create fields validator instance (path: %s)", dataStreamPath)
30+
return "", errors.Wrapf(err, "can't create fields validator instance (path: %s)", fieldsParentDir)
3331
}
3432

3533
collected, err := collectFieldsFromDefinitions(validator)

internal/docs/readme.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,12 @@ func renderReadme(fileName, packageRoot, templatePath string) ([]byte, error) {
168168
"event": func(dataStreamName string) (string, error) {
169169
return renderSampleEvent(packageRoot, dataStreamName)
170170
},
171-
"fields": func(dataStreamName string) (string, error) {
172-
return renderExportedFields(packageRoot, dataStreamName)
171+
"fields": func(args ...string) (string, error) {
172+
if len(args) > 0 {
173+
dataStreamPath := filepath.Join(packageRoot, "data_stream", args[0])
174+
return renderExportedFields(dataStreamPath)
175+
}
176+
return renderExportedFields(packageRoot)
173177
},
174178
}).ParseFiles(templatePath)
175179
if err != nil {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type: integration
2+
version: 0.0.1

internal/fields/validate.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import (
1919
"gopkg.in/yaml.v3"
2020

2121
"github.com/elastic/elastic-package/internal/common"
22+
"github.com/elastic/elastic-package/internal/logger"
2223
"github.com/elastic/elastic-package/internal/multierror"
24+
"github.com/elastic/elastic-package/internal/packages"
2325
"github.com/elastic/elastic-package/internal/packages/buildmanifest"
2426
)
2527

@@ -39,7 +41,7 @@ type Validator struct {
3941
allowedCIDRs []*net.IPNet
4042
}
4143

42-
// ValidatorOption represents an optional flag that can be passed to CreateValidatorForDataStream.
44+
// ValidatorOption represents an optional flag that can be passed to CreateValidatorForDirectory.
4345
type ValidatorOption func(*Validator) error
4446

4547
// WithDefaultNumericConversion configures the validator to accept defined keyword (or constant_keyword) fields as numeric-type.
@@ -78,8 +80,8 @@ func WithEnabledAllowedIPCheck() ValidatorOption {
7880
}
7981
}
8082

81-
// CreateValidatorForDataStream function creates a validator for the data stream.
82-
func CreateValidatorForDataStream(dataStreamRootPath string, opts ...ValidatorOption) (v *Validator, err error) {
83+
// CreateValidatorForDirectory function creates a validator for the directory.
84+
func CreateValidatorForDirectory(fieldsParentDir string, opts ...ValidatorOption) (v *Validator, err error) {
8385
v = new(Validator)
8486
for _, opt := range opts {
8587
if err := opt(v); err != nil {
@@ -89,16 +91,28 @@ func CreateValidatorForDataStream(dataStreamRootPath string, opts ...ValidatorOp
8991

9092
v.allowedCIDRs = initializeAllowedCIDRsList()
9193

92-
v.Schema, err = loadFieldsForDataStream(dataStreamRootPath)
94+
fieldsDir := filepath.Join(fieldsParentDir, "fields")
95+
v.Schema, err = loadFieldsFromDir(fieldsDir)
9396
if err != nil {
94-
return nil, errors.Wrapf(err, "can't load fields for data stream (path: %s)", dataStreamRootPath)
97+
return nil, errors.Wrapf(err, "can't load fields from directory (path: %s)", fieldsDir)
9598
}
9699

97100
if v.disabledDependencyManagement {
98101
return v, nil
99102
}
100103

101-
packageRoot := filepath.Dir(filepath.Dir(dataStreamRootPath))
104+
packageRoot, found, err := packages.FindPackageRoot()
105+
if err != nil {
106+
return nil, errors.Wrap(err, "can't find package root")
107+
}
108+
// As every command starts with approximating where is the package root, it isn't required to return an error in case the root is missing.
109+
// This is also useful for testing purposes, where we don't have a real package, but just "fields" directory. The package root is always absent.
110+
if !found {
111+
logger.Debug("Package root not found, dependency management will be disabled.")
112+
v.disabledDependencyManagement = true
113+
return v, nil
114+
}
115+
102116
bm, ok, err := buildmanifest.ReadBuildManifest(packageRoot)
103117
if err != nil {
104118
return nil, errors.Wrap(err, "can't read build manifest")
@@ -132,8 +146,7 @@ func initializeAllowedCIDRsList() (cidrs []*net.IPNet) {
132146
return cidrs
133147
}
134148

135-
func loadFieldsForDataStream(dataStreamRootPath string) ([]FieldDefinition, error) {
136-
fieldsDir := filepath.Join(dataStreamRootPath, "fields")
149+
func loadFieldsFromDir(fieldsDir string) ([]FieldDefinition, error) {
137150
files, err := filepath.Glob(filepath.Join(fieldsDir, "*.yml"))
138151
if err != nil {
139152
return nil, errors.Wrapf(err, "reading directory with fields failed (path: %s)", fieldsDir)

internal/fields/validate_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type results struct {
1818
}
1919

2020
func TestValidate_NoWildcardFields(t *testing.T) {
21-
validator, err := CreateValidatorForDataStream("../../test/packages/parallel/aws/data_stream/elb_logs")
21+
validator, err := CreateValidatorForDirectory("../../test/packages/parallel/aws/data_stream/elb_logs")
2222
require.NoError(t, err)
2323
require.NotNil(t, validator)
2424

@@ -30,7 +30,7 @@ func TestValidate_NoWildcardFields(t *testing.T) {
3030
}
3131

3232
func TestValidate_WithWildcardFields(t *testing.T) {
33-
validator, err := CreateValidatorForDataStream("../../test/packages/parallel/aws/data_stream/sns")
33+
validator, err := CreateValidatorForDirectory("../../test/packages/parallel/aws/data_stream/sns")
3434
require.NoError(t, err)
3535
require.NotNil(t, validator)
3636

@@ -40,7 +40,7 @@ func TestValidate_WithWildcardFields(t *testing.T) {
4040
}
4141

4242
func TestValidate_WithFlattenedFields(t *testing.T) {
43-
validator, err := CreateValidatorForDataStream("testdata",
43+
validator, err := CreateValidatorForDirectory("testdata",
4444
WithDisabledDependencyManagement())
4545
require.NoError(t, err)
4646
require.NotNil(t, validator)
@@ -51,7 +51,7 @@ func TestValidate_WithFlattenedFields(t *testing.T) {
5151
}
5252

5353
func TestValidate_WithNumericKeywordFields(t *testing.T) {
54-
validator, err := CreateValidatorForDataStream("testdata",
54+
validator, err := CreateValidatorForDirectory("testdata",
5555
WithNumericKeywordFields([]string{"foo.code"}),
5656
WithDisabledDependencyManagement())
5757
require.NoError(t, err)
@@ -63,7 +63,7 @@ func TestValidate_WithNumericKeywordFields(t *testing.T) {
6363
}
6464

6565
func TestValidate_constantKeyword(t *testing.T) {
66-
validator, err := CreateValidatorForDataStream("testdata")
66+
validator, err := CreateValidatorForDirectory("testdata")
6767
require.NoError(t, err)
6868
require.NotNil(t, validator)
6969

@@ -77,7 +77,7 @@ func TestValidate_constantKeyword(t *testing.T) {
7777
}
7878

7979
func TestValidate_ipAddress(t *testing.T) {
80-
validator, err := CreateValidatorForDataStream("testdata", WithEnabledAllowedIPCheck())
80+
validator, err := CreateValidatorForDirectory("testdata", WithEnabledAllowedIPCheck())
8181
require.NoError(t, err)
8282
require.NotNil(t, validator)
8383

@@ -491,7 +491,7 @@ func readSampleEvent(t *testing.T, path string) json.RawMessage {
491491
}
492492

493493
func TestValidate_geo_point(t *testing.T) {
494-
validator, err := CreateValidatorForDataStream("../../test/packages/other/fields_tests/data_stream/first")
494+
validator, err := CreateValidatorForDirectory("../../test/packages/other/fields_tests/data_stream/first")
495495

496496
require.NoError(t, err)
497497
require.NotNil(t, validator)

internal/testrunner/runners/pipeline/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (r *runner) run() ([]testrunner.TestResult, error) {
134134
}
135135

136136
tr.TimeElapsed = time.Since(startTime)
137-
fieldsValidator, err := fields.CreateValidatorForDataStream(dataStreamPath,
137+
fieldsValidator, err := fields.CreateValidatorForDirectory(dataStreamPath,
138138
fields.WithNumericKeywordFields(tc.config.NumericKeywordFields),
139139
// explicitly enabled for pipeline tests only
140140
// since system tests can have dynamic public IPs

internal/testrunner/runners/static/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (r runner) verifySampleEvent() []testrunner.TestResult {
8989
return results
9090
}
9191

92-
fieldsValidator, err := fields.CreateValidatorForDataStream(
92+
fieldsValidator, err := fields.CreateValidatorForDirectory(
9393
dataStreamPath,
9494
fields.WithDefaultNumericConversion())
9595
if err != nil {

0 commit comments

Comments
 (0)