Skip to content

Commit

Permalink
Export function template (#11923)
Browse files Browse the repository at this point in the history
A new subcommand is added under `export` named `function`. This command prints the loadable template to stdout for enabled functions.

```
$ ./functionbeat export function cloudwatch
```

I extracted generating templates from `CLIManager`. Now it has a `TemplateBuilder` which constructs the template for the manager. A template builder can be retrieved from a provider, as generating templates is specific to providers. Thus, `CLIManager` does not need to know about the provider anymore.
  • Loading branch information
kvch authored May 30, 2019
1 parent 8589309 commit e91fd2b
Show file tree
Hide file tree
Showing 14 changed files with 547 additions and 252 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
*Functionbeat*

- New options to configure roles and VPC. {pull}11779[11779]
- Export automation templates used to create functions. {pull}11923[11923]

*Winlogbeat*

Expand Down
12 changes: 12 additions & 0 deletions libbeat/tests/system/beat/beat.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,18 @@ def get_log(self, logfile=None):

return data

def get_log_lines(self, logfile=None):
"""
Returns the log lines as a list of strings
"""
if logfile is None:
logfile = self.beat_name + ".log"

with open(os.path.join(self.working_dir, logfile), 'r') as f:
data = f.readlines()

return data

def wait_log_contains(self, msg, logfile=None,
max_timeout=10, poll_interval=0.1,
name="log_contains",
Expand Down
36 changes: 33 additions & 3 deletions x-pack/functionbeat/cmd/provider_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package cmd

import (
"fmt"
"os"
"path/filepath"

Expand All @@ -18,8 +19,7 @@ import (

var output string

// TODO: Add List() subcommand.
func handler() (*cliHandler, error) {
func initProvider() (provider.Provider, error) {
b, err := instance.NewInitializedBeat(instance.Settings{Name: Name})
if err != nil {
return nil, err
Expand All @@ -35,7 +35,12 @@ func handler() (*cliHandler, error) {
return nil, err
}

provider, err := provider.NewProvider(cfg)
return provider.NewProvider(cfg)
}

// TODO: Add List() subcommand.
func handler() (*cliHandler, error) {
provider, err := initProvider()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -99,3 +104,28 @@ func genPackageCmd() *cobra.Command {
cmd.Flags().StringVarP(&output, "output", "o", "", "full path to the package")
return cmd
}

func genExportFunctionCmd() *cobra.Command {
return &cobra.Command{
Use: "function",
Short: "Export function template",
Run: cli.RunWith(func(_ *cobra.Command, args []string) error {
p, err := initProvider()
if err != nil {
return err
}
builder, err := p.TemplateBuilder()
if err != nil {
return err
}
for _, name := range args {
template, err := builder.RawTemplate(name)
if err != nil {
return fmt.Errorf("error generating raw template for %s: %+v", name, err)
}
fmt.Println(template)
}
return nil
}),
}
}
7 changes: 7 additions & 0 deletions x-pack/functionbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ func init() {
RootCmd.AddCommand(genUpdateCmd())
RootCmd.AddCommand(genRemoveCmd())
RootCmd.AddCommand(genPackageCmd())

addBeatSpecificSubcommands()
}

func addBeatSpecificSubcommands() {
RootCmd.ExportCmd.Short = "Export current config, index template or function"
RootCmd.ExportCmd.AddCommand(genExportFunctionCmd())
}
2 changes: 1 addition & 1 deletion x-pack/functionbeat/provider/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// Bundle exposes the trigger supported by the AWS provider.
var Bundle = provider.MustCreate(
"aws",
provider.NewDefaultProvider("aws", NewCLI),
provider.NewDefaultProvider("aws", NewCLI, NewTemplateBuilder),
feature.NewDetails("AWS Lambda", "listen to events on AWS lambda", feature.Stable),
).MustAddFunction("cloudwatch_logs",
NewCloudwatchLogs,
Expand Down
Loading

0 comments on commit e91fd2b

Please sign in to comment.