Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
GGabriele committed Feb 11, 2022
1 parent 64a2881 commit 8ea7c1d
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 17 deletions.
36 changes: 19 additions & 17 deletions file/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,34 +633,36 @@ func populateConsumers(kongState *state.KongState, file *Content,
return nil
}

func setPluginSharedConfig(fp *FPlugin, sharedPlugins map[string]utils.SharedPlugin,
consumerID, serviceID, routeID string) {
func getSharedPlugin(sharedPlugins map[string]utils.SharedPlugin,
consumerID, serviceID, routeID string) string {
for name, content := range sharedPlugins {
for _, consumer := range content.Consumers {
if consumer != consumerID {
continue
if consumer == consumerID {
return name
}
fp.Plugin.Config = nil
fp.ConfigSource = kong.String(name)
break
}
for _, service := range content.Services {
if service != serviceID {
continue
if service == serviceID {
return name
}
fp.Plugin.Config = nil
fp.ConfigSource = kong.String(name)
break
}
for _, route := range content.Routes {
if route != routeID {
continue
if route == routeID {
return name
}
fp.Plugin.Config = nil
fp.ConfigSource = kong.String(name)
break
}
}
return ""
}

func setPluginSharedConfig(fp *FPlugin, sharedPlugins map[string]utils.SharedPlugin,
consumerID, serviceID, routeID string) {
if sharedPlugin := getSharedPlugin(
sharedPlugins, consumerID, serviceID, routeID,
); sharedPlugin != "" {
fp.Plugin.Config = nil
fp.ConfigSource = kong.String(sharedPlugin)
}
}

func WriteContentToFile(content *Content, filename string, format Format) error {
Expand Down
69 changes: 69 additions & 0 deletions file/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/kong/deck/state"
"github.com/kong/deck/utils"
"github.com/kong/go-kong/kong"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -341,3 +342,71 @@ services:
}`
assert.Equal(expected, output)
}

func Test_getSharedPlugin(t *testing.T) {
sharedPlugins := map[string]utils.SharedPlugin{
"prometheus-0": {
Config: kong.Configuration{
"per_consumer": false,
},
Consumers: []string{"consumer1", "consumer2"},
Services: []string{"service1"},
},
"rate-limiting-0": {
Config: kong.Configuration{
"key": "value",
},
Routes: []string{"route1", "route2", "route3"},
},
}
tests := []struct {
name string
consumerID string
serviceID string
routeID string
expected string
}{
{
consumerID: "consumer1",
expected: "prometheus-0",
},
{
consumerID: "consumer2",
expected: "prometheus-0",
},
{
serviceID: "service1",
expected: "prometheus-0",
},
{
routeID: "route1",
expected: "rate-limiting-0",
},
{
routeID: "route2",
expected: "rate-limiting-0",
},
{
routeID: "route3",
expected: "rate-limiting-0",
},
{
routeID: "not-existing",
expected: "",
},
{
serviceID: "not-existing",
expected: "",
},
{
consumerID: "not-existing",
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := getSharedPlugin(sharedPlugins, tt.consumerID, tt.serviceID, tt.routeID)
assert.Equal(t, tt.expected, got)
})
}
}

0 comments on commit 8ea7c1d

Please sign in to comment.