Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: changed dql routes format to show dict for service #187

Merged
merged 6 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions pkg/file/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1748,7 +1748,7 @@ func (b *stateBuilder) ingestCustomEntities(customEntities []FCustomEntity) {
}

func (b *stateBuilder) ingestDeGraphqlRoute(degraphqlRouteEntity FCustomEntity) {
degraphqlRoute, err := copyToDegraphqlRoute(degraphqlRouteEntity)
degraphqlRoute, err := b.copyToDegraphqlRoute(degraphqlRouteEntity)
if err != nil {
b.err = err
return
Expand All @@ -1771,7 +1771,7 @@ func (b *stateBuilder) ingestDeGraphqlRoute(degraphqlRouteEntity FCustomEntity)
b.rawState.DegraphqlRoutes = append(b.rawState.DegraphqlRoutes, &degraphqlRoute.DegraphqlRoute)
}

func copyToDegraphqlRoute(fcEntity FCustomEntity) (DegraphqlRoute, error) {
func (b *stateBuilder) copyToDegraphqlRoute(fcEntity FCustomEntity) (DegraphqlRoute, error) {
degraphqlRoute := DegraphqlRoute{}
if fcEntity.ID != nil {
degraphqlRoute.ID = fcEntity.ID
Expand All @@ -1782,9 +1782,28 @@ func copyToDegraphqlRoute(fcEntity FCustomEntity) (DegraphqlRoute, error) {
}

if fcEntity.Fields["service"] != nil {
if service, ok := fcEntity.Fields["service"].(*string); ok {
if service, ok := fcEntity.Fields["service"].(map[string]interface{}); ok {
var serviceID string
var serviceName string
s, ok := service["id"].(string)
if ok {
serviceID = s
}
s, ok = service["name"].(string)
if ok {
serviceName = s
}

if serviceID == "" && serviceName != "" {
s, err := b.intermediate.Services.Get(serviceName)
if err != nil {
return DegraphqlRoute{}, fmt.Errorf("service %v not found", serviceName)
}
serviceID = *s.ID
}

degraphqlRoute.Service = &kong.Service{
ID: service,
ID: kong.String(serviceID),
}
}
}
Expand Down
144 changes: 118 additions & 26 deletions pkg/file/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func existingDegraphqlRouteState(t *testing.T) *state.KongState {
DegraphqlRoute: kong.DegraphqlRoute{
ID: kong.String("4bfcb11f-c962-4817-83e5-9433cf20b663"),
Service: &kong.Service{
ID: kong.String("ba54b737-38aa-49d1-87c4-64e756b0c6f9"),
ID: kong.String("fdfd14cc-cd69-49a0-9e23-cd3375b6c0cd"),
},
Methods: kong.StringSlice("GET"),
URI: kong.String("/example"),
Expand Down Expand Up @@ -3884,10 +3884,11 @@ func Test_stateBuilder_ingestCustomEntities(t *testing.T) {
targetContent *Content
}
tests := []struct {
name string
fields fields
want *utils.KongRawState
wantErr bool
name string
fields fields
want *utils.KongRawState
wantErr bool
errString string
}{
{
name: "generates a new degraphql route from valid config passed",
Expand All @@ -3897,9 +3898,11 @@ func Test_stateBuilder_ingestCustomEntities(t *testing.T) {
{
Type: kong.String("degraphql_routes"),
Fields: CustomEntityConfiguration{
"uri": kong.String("/foo"),
"query": kong.String("query { foo { bar }}"),
"service": kong.String("fdfd14cc-cd69-49a0-9e23-cd3375b6c0cd"),
"uri": kong.String("/foo"),
"query": kong.String("query { foo { bar }}"),
"service": map[string]interface{}{
"id": "fdfd14cc-cd69-49a0-9e23-cd3375b6c0cd",
},
},
},
},
Expand Down Expand Up @@ -3928,9 +3931,11 @@ func Test_stateBuilder_ingestCustomEntities(t *testing.T) {
{
Type: kong.String("degraphql_routes"),
Fields: CustomEntityConfiguration{
"uri": kong.String("/example"),
"query": kong.String("query{ example { foo } }"),
"service": kong.String("ba54b737-38aa-49d1-87c4-64e756b0c6f9"),
"uri": kong.String("/example"),
"query": kong.String("query{ example { foo } }"),
"service": map[string]interface{}{
"id": "fdfd14cc-cd69-49a0-9e23-cd3375b6c0cd",
},
},
},
},
Expand All @@ -3942,7 +3947,7 @@ func Test_stateBuilder_ingestCustomEntities(t *testing.T) {
{
ID: kong.String("4bfcb11f-c962-4817-83e5-9433cf20b663"),
Service: &kong.Service{
ID: kong.String("ba54b737-38aa-49d1-87c4-64e756b0c6f9"),
ID: kong.String("fdfd14cc-cd69-49a0-9e23-cd3375b6c0cd"),
},
Methods: kong.StringSlice("GET"),
URI: kong.String("/example"),
Expand Down Expand Up @@ -3974,7 +3979,9 @@ func Test_stateBuilder_ingestCustomEntities(t *testing.T) {
author
}
}`),
"service": kong.String("foo"),
"service": map[string]interface{}{
"name": "foo",
},
"methods": kong.StringSlice("GET", "POST"),
},
},
Expand All @@ -3987,7 +3994,7 @@ func Test_stateBuilder_ingestCustomEntities(t *testing.T) {
{
ID: kong.String("dfd79b4d-7642-4b61-ba0c-9f9f0d3ba55b"),
Service: &kong.Service{
ID: kong.String("foo"),
ID: kong.String("5b1484f2-5209-49d9-b43e-92ba09dd9d52"),
},
Methods: kong.StringSlice("GET", "POST"),
URI: kong.String("/foo"),
Expand Down Expand Up @@ -4028,21 +4035,37 @@ func Test_stateBuilder_ingestCustomEntities(t *testing.T) {
name: "handles multiple degraphql routes",
fields: fields{
targetContent: &Content{
Services: []FService{
{
Service: kong.Service{
Name: kong.String("service1"),
},
},
{
Service: kong.Service{
Name: kong.String("service2"),
},
},
},
CustomEntities: []FCustomEntity{
{
Type: kong.String("degraphql_routes"),
Fields: CustomEntityConfiguration{
"uri": kong.String("/foo"),
"query": kong.String("query { foo }"),
"service": kong.String("service1"),
"uri": kong.String("/foo"),
"query": kong.String("query { foo }"),
"service": map[string]interface{}{
"name": "service1",
},
},
},
{
Type: kong.String("degraphql_routes"),
Fields: CustomEntityConfiguration{
"uri": kong.String("/bar"),
"query": kong.String("query { bar }"),
"service": kong.String("service2"),
"uri": kong.String("/bar"),
"query": kong.String("query { bar }"),
"service": map[string]interface{}{
"name": "service2",
},
"methods": kong.StringSlice("POST", "PUT"),
},
},
Expand All @@ -4053,35 +4076,102 @@ func Test_stateBuilder_ingestCustomEntities(t *testing.T) {
want: &utils.KongRawState{
DegraphqlRoutes: []*kong.DegraphqlRoute{
{
ID: kong.String("0cc0d614-4c88-4535-841a-cbe0709b0758"),
ID: kong.String("9e6f82e5-4e74-4e81-a79e-4bbd6fe34cdc"),
URI: kong.String("/foo"),
Query: kong.String("query { foo }"),
Service: &kong.Service{
ID: kong.String("service1"),
ID: kong.String("0cc0d614-4c88-4535-841a-cbe0709b0758"),
},
Methods: kong.StringSlice("GET"),
},
{
ID: kong.String("083f61d3-75bc-42b4-9df4-f91929e18fda"),
ID: kong.String("ba843ee8-d63e-4c4f-be1c-ebea546d8fac"),
URI: kong.String("/bar"),
Query: kong.String("query { bar }"),
Service: &kong.Service{
ID: kong.String("service2"),
ID: kong.String("083f61d3-75bc-42b4-9df4-f91929e18fda"),
},
Methods: kong.StringSlice("POST", "PUT"),
},
},
Services: []*kong.Service{
{
ID: kong.String("0cc0d614-4c88-4535-841a-cbe0709b0758"),
Name: kong.String("service1"),
Protocol: kong.String("http"),
ConnectTimeout: kong.Int(60000),
WriteTimeout: kong.Int(60000),
ReadTimeout: kong.Int(60000),
},
{
ID: kong.String("083f61d3-75bc-42b4-9df4-f91929e18fda"),
Name: kong.String("service2"),
Protocol: kong.String("http"),
ConnectTimeout: kong.Int(60000),
WriteTimeout: kong.Int(60000),
ReadTimeout: kong.Int(60000),
},
},
},
},
{
name: "handles missing required fields - service",
fields: fields{
targetContent: &Content{
CustomEntities: []FCustomEntity{
{
Type: kong.String("degraphql_routes"),
Fields: CustomEntityConfiguration{
"uri": kong.String("/foo"),
"query": kong.String("query{ example { foo } }"),
},
},
},
},
currentState: emptyState(),
},
want: &utils.KongRawState{
DegraphqlRoutes: nil,
},
wantErr: true,
errString: "service is required for degraphql_routes",
},
{
name: "handles missing required fields",
name: "handles missing required fields - uri",
fields: fields{
targetContent: &Content{
CustomEntities: []FCustomEntity{
{
Type: kong.String("degraphql_routes"),
Fields: CustomEntityConfiguration{
"query": kong.String("query{ example { foo } }"),
"service": map[string]interface{}{
"id": "fdfd14cc-cd69-49a0-9e23-cd3375b6c0cd",
},
},
},
},
},
currentState: emptyState(),
},
want: &utils.KongRawState{
DegraphqlRoutes: nil,
},
wantErr: true,
errString: "uri and query are required for degraphql_routes",
},
{
name: "handles missing required fields - uri",
fields: fields{
targetContent: &Content{
CustomEntities: []FCustomEntity{
{
Type: kong.String("degraphql_routes"),
Fields: CustomEntityConfiguration{
"uri": kong.String("/foo"),
"service": map[string]interface{}{
"id": "fdfd14cc-cd69-49a0-9e23-cd3375b6c0cd",
},
},
},
},
Expand All @@ -4091,7 +4181,8 @@ func Test_stateBuilder_ingestCustomEntities(t *testing.T) {
want: &utils.KongRawState{
DegraphqlRoutes: nil,
},
wantErr: true,
wantErr: true,
errString: "uri and query are required for degraphql_routes",
},
}

Expand All @@ -4104,6 +4195,7 @@ func Test_stateBuilder_ingestCustomEntities(t *testing.T) {
_, _, err := b.build()
if tt.wantErr {
require.Error(t, err, "build error was expected")
assert.ErrorContains(t, err, tt.errString)
assert.Equal(t, tt.want, b.rawState)
return
}
Expand Down
39 changes: 32 additions & 7 deletions pkg/file/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,10 @@ func copyFromDegraphqlRoute(dRoute DegraphqlRoute, fcEntity *FCustomEntity) erro
fcEntity.Fields = make(map[string]interface{})

if dRoute.Service != nil && dRoute.Service.ID != nil {
fcEntity.Fields["service"] = *dRoute.Service.ID
serviceMap := make(map[string]interface{})
serviceMap["id"] = *dRoute.Service.ID
serviceMap["name"] = *dRoute.Service.Name
fcEntity.Fields["service"] = serviceMap
}

if dRoute.URI != nil {
Expand Down Expand Up @@ -1021,23 +1024,45 @@ func copyToFCustomEntity(dRoute map[string]interface{}, fcEntity *FCustomEntity)
}

fcEntity.Fields = make(map[string]interface{})
dRouteFields := dRoute["fields"].(map[string]interface{})

f, ok := dRoute["fields"].(map[string]interface{})
if !ok {
return fmt.Errorf("fields field should be a map")
}
dRouteFields := f

if dRouteFields["service"] != nil {
fcEntity.Fields["service"] = kong.String(dRouteFields["service"].(string))
service, ok := dRouteFields["service"].(map[string]interface{})
if !ok {
return fmt.Errorf("service field should be a map")
}
fcEntity.Fields["service"] = service
}

if dRouteFields["uri"] != nil {
fcEntity.Fields["uri"] = kong.String(dRouteFields["uri"].(string))
uri, ok := dRouteFields["uri"].(string)
if !ok {
return fmt.Errorf("uri field should be a string")
}
fcEntity.Fields["uri"] = kong.String(uri)
}

if dRouteFields["query"] != nil {
fcEntity.Fields["query"] = kong.String(dRouteFields["query"].(string))
query, ok := dRouteFields["query"].(string)
if !ok {
return fmt.Errorf("query field should be a string")
}
fcEntity.Fields["query"] = kong.String(query)
}

if dRouteFields["methods"] != nil {
methods := make([]string, len(dRouteFields["methods"].([]interface{})))
for i, method := range dRouteFields["methods"].([]interface{}) {
methodsArray, ok := dRouteFields["methods"].([]interface{})
if !ok {
return fmt.Errorf("methods field should be an array")
}
methods := make([]string, len(methodsArray))

for i, method := range methodsArray {
methods[i] = method.(string)
}
fcEntity.Fields["methods"] = kong.StringSlice(methods...)
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7653,6 +7653,18 @@ func Test_Sync_DegraphqlRoutes(t *testing.T) {
expectedMethods := kong.StringSlice("POST")
assert.Equal(t, expectedMethods, degraphqlRoutes[0].Methods)
})

t.Run("create degraphql route - fails if service reference is not an object", func(t *testing.T) {
err := sync("testdata/sync/037-degraphql-routes/kong-wrong-svc-config.yaml")
require.Error(t, err)
assert.ErrorContains(t, err, "service field should be a map")
})

t.Run("create degraphql route - fails if service reference is not an object", func(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Prashansa-K this test is repeated? 👀

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad! I will clean this up.

err := sync("testdata/sync/037-degraphql-routes/kong-wrong-svc-config.yaml")
require.Error(t, err)
assert.ErrorContains(t, err, "service field should be a map")
})
}

func Test_Sync_CustomEntities_Fake(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ custom_entities:
author
}
}
service: svc1
service:
name: svc1
Loading
Loading