diff --git a/internal/pkg/openapi2apisix/openapi2apisix.go b/internal/pkg/openapi2apisix/openapi2apisix.go index de29b0b..c94c843 100644 --- a/internal/pkg/openapi2apisix/openapi2apisix.go +++ b/internal/pkg/openapi2apisix/openapi2apisix.go @@ -66,7 +66,7 @@ func Convert(ctx context.Context, oas []byte) (*apitypes.Configuration, error) { ups.ID = common.GenID(ups.Name) result.Services = []*apitypes.Service{ - &apitypes.Service{ + { ID: common.GenID(doc.Info.Title), Name: doc.Info.Title, Description: doc.Info.Description, @@ -76,13 +76,13 @@ func Convert(ctx context.Context, oas []byte) (*apitypes.Configuration, error) { } // handle routes - routes := createRoutes(doc, doc.Info.Title) + routes := createRoutes(doc, result.Services[0].ID) result.Routes = routes return &result, nil } -func createRoutes(doc *openapi3.T, serviceName string) []*apitypes.Route { +func createRoutes(doc *openapi3.T, serviceID string) []*apitypes.Route { // create a sorted array of paths, to be deterministic in our output order sortedPaths := make([]string, len(doc.Paths)) i := 0 @@ -108,7 +108,7 @@ func createRoutes(doc *openapi3.T, serviceName string) []*apitypes.Route { // route name routeName := operation.OperationID if routeName == "" { - routeName = Slugify(serviceName, method, path) + routeName = Slugify(doc.Info.Title, method, path) } routeDescription := operation.Summary if routeDescription == "" { @@ -124,8 +124,9 @@ func createRoutes(doc *openapi3.T, serviceName string) []*apitypes.Route { Name: routeName, Description: routeDescription, // Labels: tags, - Methods: []string{method}, - Uris: []string{routePath}, + Methods: []string{method}, + Uris: []string{routePath}, + ServiceID: serviceID, } routes = append(routes, &route) } diff --git a/internal/pkg/openapi2apisix/openapi2apisix_test.go b/internal/pkg/openapi2apisix/openapi2apisix_test.go index 6113ae7..d4401a2 100644 --- a/internal/pkg/openapi2apisix/openapi2apisix_test.go +++ b/internal/pkg/openapi2apisix/openapi2apisix_test.go @@ -93,7 +93,7 @@ func TestConvert(t *testing.T) { }, want: &apitypes.Configuration{ Services: []*apitypes.Service{ - &apitypes.Service{ + { Name: "API 101", Description: `API 101 template for learning API request basics. Follow along with the webinar / video or just open the first request and hit **Send**!`, Upstream: &apitypes.Upstream{ @@ -121,35 +121,35 @@ func TestConvert(t *testing.T) { }, }, Routes: []*apitypes.Route{ - &apitypes.Route{ + { Name: "api-101_get_customer", Description: "Get one customer", // Labels: []string{"default"}, Methods: []string{"GET"}, Uris: []string{"/customer"}, }, - &apitypes.Route{ + { Name: "api-101_post_customer", Description: "Add new customer", // Labels: []string{"default"}, Methods: []string{"POST"}, Uris: []string{"/customer"}, }, - &apitypes.Route{ + { Name: "api-101_delete_customer-customer-id", Description: "Remove customer", // Labels: []string{"default"}, Methods: []string{"DELETE"}, Uris: []string{"/customer/*"}, }, - &apitypes.Route{ + { Name: "api-101_put_customer-customer-id", Description: "Update customer", // Labels: []string{"default"}, Methods: []string{"PUT"}, Uris: []string{"/customer/*"}, }, - &apitypes.Route{ + { Name: "api-101_get_customers", Description: "Get all customers", // Labels: []string{"default"}, @@ -167,7 +167,7 @@ func TestConvert(t *testing.T) { }, want: &apitypes.Configuration{ Services: []*apitypes.Service{ - &apitypes.Service{ + { Name: "API 101", Description: "modify operationId", Upstream: &apitypes.Upstream{ @@ -190,14 +190,14 @@ func TestConvert(t *testing.T) { }, }, Routes: []*apitypes.Route{ - &apitypes.Route{ + { Name: "update Customer", Description: "Update customer", // Labels: []string{"default"}, Methods: []string{"PUT"}, Uris: []string{"/customer/*"}, }, - &apitypes.Route{ + { Name: "getCustomers", Description: "Get all customers", // Labels: []string{"default"}, @@ -215,7 +215,7 @@ func TestConvert(t *testing.T) { }, want: &apitypes.Configuration{ Services: []*apitypes.Service{ - &apitypes.Service{ + { Name: "API 101", Description: "modify operationId", Upstream: &apitypes.Upstream{ @@ -240,7 +240,7 @@ func TestConvert(t *testing.T) { }, }, Routes: []*apitypes.Route{ - &apitypes.Route{ + { Name: "getCustomers", Description: "Get all customers", // Labels: []string{"default", "customer"}, @@ -258,7 +258,7 @@ func TestConvert(t *testing.T) { }, want: &apitypes.Configuration{ Services: []*apitypes.Service{ - &apitypes.Service{ + { Name: "API 101", Description: "modify operationId", Upstream: &apitypes.Upstream{ @@ -283,7 +283,7 @@ func TestConvert(t *testing.T) { }, }, Routes: []*apitypes.Route{ - &apitypes.Route{ + { Name: "getCustomers", Description: "Get all customers", // Labels: []string{"default", "customer"}, @@ -303,21 +303,24 @@ func TestConvert(t *testing.T) { return } if got != nil { - for _, route := range tt.want.Routes { - route.ID = common.GenID(route.Name) - } - for _, svc := range tt.want.Services { - svc.ID = common.GenID(svc.Name) - if svc.Upstream != nil { - if svc.Upstream.Nodes == nil { - svc.Upstream.Nodes = []apitypes.UpstreamNode{ - apitypes.UpstreamNode{}, - } + // normally the converter should only generate one service + svc := tt.want.Services[0] + svc.ID = common.GenID(svc.Name) + if svc.Upstream != nil { + if svc.Upstream.Nodes == nil { + svc.Upstream.Nodes = []apitypes.UpstreamNode{ + {}, } - svc.Upstream.Name = Slugify("Upstream for", svc.Name) - svc.Upstream.ID = common.GenID(svc.Upstream.Name) } + svc.Upstream.Name = Slugify("Upstream for", svc.Name) + svc.Upstream.ID = common.GenID(svc.Upstream.Name) + } + + for _, route := range tt.want.Routes { + route.ID = common.GenID(route.Name) + route.ServiceID = svc.ID } + assert.Equal(t, tt.want.Services, got.Services) assert.Equal(t, tt.want.Routes, got.Routes) } diff --git a/test/cli/suites-consumer/sync.go b/test/cli/suites-consumer/sync.go index 6f02958..63903ed 100644 --- a/test/cli/suites-consumer/sync.go +++ b/test/cli/suites-consumer/sync.go @@ -29,6 +29,7 @@ var _ = ginkgo.Describe("`adc sync` tests", func() { "time_window": 60, "rejected_code": 503, "key": "remote_addr", + "_vid": "suites-consumer", }, }, }