Skip to content

Commit

Permalink
feat(apiops): integrate go-apiops
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Jul 14, 2023
1 parent 5bbd6a8 commit 74bf5c5
Show file tree
Hide file tree
Showing 12 changed files with 900 additions and 5 deletions.
37 changes: 37 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Table of Contents

- [v1.24.0](#v1240)
- [v1.23.0](#v1230)
- [v1.22.1](#v1221)
- [v1.22.0](#v1220)
Expand Down Expand Up @@ -60,6 +61,41 @@
- [v0.2.0](#v020)
- [v0.1.0](#v010)

## [v1.24.0]

> Release date: to-be-set
### Added

- Added a new command `file openapi2kong` that will generate a deck file from an OpenAPI
3.0 spec. This is the replacement for the similar `inso` functionality.
The functionality is imported from the [go-apiops library](https://github.com/Kong/go-apiops).
[#939](https://github.com/Kong/deck/pull/939)
- Added a new command `file merge` that will merge multiple deck files. The files will not be
validated, which allows for working with incomplete or even invalid files in a pipeline.
The functionality is imported from the [go-apiops library](https://github.com/Kong/go-apiops).
[#939](https://github.com/Kong/deck/pull/939)
- Added a new command `file patch` for applying patches on top of a decK file. The patches can be
provided on the commandline, or via patch files. The deck file will not be
validated, which allows for working with incomplete or even invalid files in a pipeline.
The functionality is imported from the [go-apiops library](https://github.com/Kong/go-apiops).
[#939](https://github.com/Kong/deck/pull/939)
- Added a new commands `file add-tags/list-tags/remove-tags` to manage tags in a decK file. The deck file will not be
validated, which allows for working with incomplete or even invalid files in a pipeline.
The functionality is imported from the [go-apiops library](https://github.com/Kong/go-apiops).
[#939](https://github.com/Kong/deck/pull/939)
- Added a new command `file add-plugins` for adding plugins to a decK file. The plugins can be
provided on the commandline, or via config files. The deck file will not be
validated, which allows for working with incomplete or even invalid files in a pipeline.
The functionality is imported from the [go-apiops library](https://github.com/Kong/go-apiops).
[#939](https://github.com/Kong/deck/pull/939)

### Fixes


### Misc


## [v1.23.0]

> Release date: 2023/07/03
Expand Down Expand Up @@ -1264,6 +1300,7 @@ No breaking changes have been introduced in this release.

Debut release of decK

[v1.24.0]: https://github.com/kong/deck/compare/v1.23.0...v1.24.0
[v1.23.0]: https://github.com/kong/deck/compare/v1.22.1...v1.23.0
[v1.22.1]: https://github.com/kong/deck/compare/v1.22.0...v1.22.1
[v1.22.0]: https://github.com/kong/deck/compare/v1.21.0...v1.22.0
Expand Down
24 changes: 24 additions & 0 deletions cmd/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"github.com/spf13/cobra"
)

//
//
// Define the CLI data for the file sub-command
//
//

func newAddFileCmd() *cobra.Command {
addFileCmd := &cobra.Command{
Use: "file [sub-command]...",
Short: "Sub-command to host the decK file manipulation operations",
Long: `Sub-command to host the decK file manipulation operations`,
}

return addFileCmd
}
158 changes: 158 additions & 0 deletions cmd/file_addplugins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"
"log"
"strings"

"github.com/kong/go-apiops/deckformat"
"github.com/kong/go-apiops/filebasics"
"github.com/kong/go-apiops/jsonbasics"
"github.com/kong/go-apiops/logbasics"
"github.com/kong/go-apiops/plugins"
"github.com/spf13/cobra"
)

var (
cmdAddPluginsOverwrite bool
cmdAddPluginsInputFilename string
cmdAddPluginOutputFilename string
cmdAddPluginOutputFormat string
cmdAddPluginsSelectors []string
cmdAddPluginsStrConfigs []string
)

// Executes the CLI command "add-plugins"
func executeAddPlugins(cmd *cobra.Command, cfgFiles []string) error {
verbosity, _ := cmd.Flags().GetInt("verbose")
logbasics.Initialize(log.LstdFlags, verbosity)

cmdAddPluginOutputFormat = strings.ToUpper(cmdAddPluginOutputFormat)

var pluginConfigs []map[string]interface{}
{
for _, strConfig := range cmdAddPluginsStrConfigs {
pluginConfig, err := filebasics.Deserialize([]byte(strConfig))
if err != nil {
return fmt.Errorf("failed to deserialize plugin config '%s'; %w", strConfig, err)
}
pluginConfigs = append(pluginConfigs, pluginConfig)
}
}

var pluginFiles []plugins.DeckPluginFile
{
for _, filename := range cfgFiles {
var file plugins.DeckPluginFile
if err := file.ParseFile(filename); err != nil {
return fmt.Errorf("failed to parse plugin file '%s'; %w", filename, err)
}
pluginFiles = append(pluginFiles, file)
}
}

// do the work: read/add-plugins/write
jsondata, err := filebasics.DeserializeFile(cmdAddPluginsInputFilename)
if err != nil {
return fmt.Errorf("failed to read input file '%s'; %w", cmdAddPluginsInputFilename, err)
}
yamlNode := jsonbasics.ConvertToYamlNode(jsondata)

// apply CLI flags
plugger := plugins.Plugger{}
plugger.SetYamlData(yamlNode)
err = plugger.SetSelectors(cmdAddPluginsSelectors)
if err != nil {
return fmt.Errorf("failed to set selectors; %w", err)
}
err = plugger.AddPlugins(pluginConfigs, cmdAddPluginsOverwrite)
if err != nil {
return fmt.Errorf("failed to add plugins; %w", err)
}
yamlNode = plugger.GetYamlData()

// apply plugin-files
for i, pluginFile := range pluginFiles {
err = pluginFile.Apply(yamlNode)
if err != nil {
return fmt.Errorf("failed to apply plugin file '%s'; %w", cfgFiles[i], err)
}
}
jsondata = plugger.GetData()

trackInfo := deckformat.HistoryNewEntry("add-plugins")
trackInfo["input"] = cmdAddPluginsInputFilename
trackInfo["output"] = cmdAddPluginOutputFilename
trackInfo["overwrite"] = cmdAddPluginsOverwrite
if len(pluginConfigs) > 0 {
trackInfo["configs"] = pluginConfigs
}
if len(cfgFiles) > 0 {
trackInfo["pluginfiles"] = cfgFiles
}
trackInfo["selectors"] = cmdAddPluginsSelectors
deckformat.HistoryAppend(jsondata, trackInfo)

return filebasics.WriteSerializedFile(cmdAddPluginOutputFilename, jsondata, cmdAddPluginOutputFormat)
}

//
//
// Define the CLI data for the add-plugins command
//
//

func newAddPluginsCmd() *cobra.Command {
addPluginsCmd := &cobra.Command{
Use: "add-plugins [flags] [...plugin-files]",
Short: "Add plugins to objects in a decK file",
Long: `Add plugins to objects in a decK file.
The plugins are added to all objects that match the selector expressions. If no
selectors are given, they will be added to the top-level 'plugins' array.
The plugin-files have the following format (JSON or YAML) and are applied in the
order they are given;
{ "_format_version": "1.0",
"add-plugins": [
{ "selectors": [
"$..services[*]"
],
"overwrite": false,
"plugins": [
{ "name": "my-plugin",
"config": {
"my-property": "value"
}
}
],
}
]
}
`,
RunE: executeAddPlugins,
Args: cobra.MinimumNArgs(0),
}

addPluginsCmd.Flags().StringVarP(&cmdAddPluginsInputFilename, "state", "s", "-",
"decK file to process. Use - to read from stdin")
addPluginsCmd.Flags().StringArrayVar(&cmdAddPluginsSelectors, "selector", []string{},
"JSON path expression to select plugin-owning objects to add plugins to,\n"+
"defaults to the top-level (selector '$'). Repeat for multiple selectors.")
addPluginsCmd.Flags().StringArrayVar(&cmdAddPluginsStrConfigs, "config", []string{},
"JSON snippet containing the plugin configuration to add. Repeat to add\n"+
"multiple plugins.")
addPluginsCmd.Flags().BoolVar(&cmdAddPluginsOverwrite, "overwrite", false,
"specifying this flag will overwrite plugins by the same name if they already\n"+
"exist in an array. The default is to skip existing plugins.")
addPluginsCmd.Flags().StringVarP(&cmdAddPluginOutputFilename, "output-file", "o", "-",
"output file to write. Use - to write to stdout")
addPluginsCmd.Flags().StringVarP(&cmdAddPluginOutputFormat, "format", "", filebasics.OutputFormatYaml,
"output format: "+filebasics.OutputFormatJSON+" or "+filebasics.OutputFormatYaml)

return addPluginsCmd
}
89 changes: 89 additions & 0 deletions cmd/file_addtags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"
"log"
"strings"

"github.com/kong/go-apiops/deckformat"
"github.com/kong/go-apiops/filebasics"
"github.com/kong/go-apiops/logbasics"
"github.com/kong/go-apiops/tags"
"github.com/spf13/cobra"
)

var (
cmdAddTagsInputFilename string
cmdAddTagsOutputFilename string
cmdAddTagsOutputFormat string
cmdAddTagsSelectors []string
)

// Executes the CLI command "add-tags"
func executeAddTags(cmd *cobra.Command, tagsToAdd []string) error {
verbosity, _ := cmd.Flags().GetInt("verbose")
logbasics.Initialize(log.LstdFlags, verbosity)

cmdAddTagsOutputFormat = strings.ToUpper(cmdAddTagsOutputFormat)

// do the work: read/add-tags/write
data, err := filebasics.DeserializeFile(cmdAddTagsInputFilename)
if err != nil {
return fmt.Errorf("failed to read input file '%s'; %w", cmdAddTagsInputFilename, err)
}

tagger := tags.Tagger{}
tagger.SetData(data)
err = tagger.SetSelectors(cmdAddTagsSelectors)
if err != nil {
return fmt.Errorf("failed to set selectors; %w", err)
}
err = tagger.AddTags(tagsToAdd)
if err != nil {
return fmt.Errorf("failed to add tags; %w", err)
}
data = tagger.GetData()

trackInfo := deckformat.HistoryNewEntry("add-tags")
trackInfo["input"] = cmdAddTagsInputFilename
trackInfo["output"] = cmdAddTagsOutputFilename
trackInfo["tags"] = tagsToAdd
trackInfo["selectors"] = cmdAddTagsSelectors
deckformat.HistoryAppend(data, trackInfo)

return filebasics.WriteSerializedFile(cmdAddTagsOutputFilename, data, cmdAddTagsOutputFormat)
}

//
//
// Define the CLI data for the add-tags command
//
//

func newAddTagsCmd() *cobra.Command {
addTagsCmd := &cobra.Command{
Use: "add-tags [flags] tag [...tag]",
Short: "Add tags to objects in a decK file",
Long: `Add tags to objects in a decK file.
The tags are added to all objects that match the selector expressions. If no
selectors are given, all Kong entities are tagged.`,
RunE: executeAddTags,
Args: cobra.MinimumNArgs(1),
}

addTagsCmd.Flags().StringVarP(&cmdAddTagsInputFilename, "state", "s", "-",
"decK file to process. Use - to read from stdin")
addTagsCmd.Flags().StringArrayVar(&cmdAddTagsSelectors, "selector", []string{},
"JSON path expression to select objects to add tags to,\n"+
"defaults to all Kong entities (repeat for multiple selectors)")
addTagsCmd.Flags().StringVarP(&cmdAddTagsOutputFilename, "output-file", "o", "-",
"output file to write. Use - to write to stdout")
addTagsCmd.Flags().StringVarP(&cmdAddTagsOutputFormat, "format", "", filebasics.OutputFormatYaml,
"output format: "+filebasics.OutputFormatJSON+" or "+filebasics.OutputFormatYaml)

return addTagsCmd
}
Loading

0 comments on commit 74bf5c5

Please sign in to comment.