Skip to content

Commit

Permalink
Merge pull request #397 from apigee/genapitests
Browse files Browse the repository at this point in the history
feat: Adds support for target server
  • Loading branch information
ssvaidyanathan authored Feb 11, 2024
2 parents 55b8d69 + f2128ac commit d7536c4
Show file tree
Hide file tree
Showing 16 changed files with 380 additions and 101 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ require (
github.com/lestrrat-go/jwx/v2 v2.0.19 // indirect
github.com/lestrrat-go/option v1.0.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/pb33f/libopenapi v0.15.1 // indirect
github.com/pb33f/libopenapi-validator v0.0.40 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
Expand Down
16 changes: 10 additions & 6 deletions internal/bundlegen/generateapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ func GenerateAPIProxyDefFromOAS(name string,
apiproxy.SetLastModifiedAt()
apiproxy.SetConfigurationVersion()
if integrationEndpoint {
apiproxy.AddIntegrationEndpoint("default")
apiproxy.AddIntegrationEndpoint(DEFAULT)
} else {
apiproxy.AddTargetEndpoint(NoAuthTargetName)
apiproxy.AddTargetEndpoint(DEFAULT)
}
apiproxy.AddProxyEndpoint("default")
apiproxy.AddProxyEndpoint(DEFAULT)

if !skipPolicy {
apiproxy.AddResource(oasDocName, "oas")
Expand All @@ -263,25 +263,29 @@ func GenerateAPIProxyDefFromOAS(name string,
} else {
// if target is not set, derive it from the OAS file
if targetURL == "" {
targets.NewTargetEndpoint(NoAuthTargetName,
targets.NewTargetEndpoint(DEFAULT,
u.Scheme+"://"+u.Hostname()+u.Path,
"",
"",
oasGoogleAcessTokenScopeLiteral,
oasGoogleIDTokenAudLiteral,
oasGoogleIDTokenAudRef)
} else { // an explicit target url is set
if _, err = url.Parse(targetURL); err != nil {
return fmt.Errorf("invalid target url: %v", err)
}
targets.NewTargetEndpoint(NoAuthTargetName,
targets.NewTargetEndpoint(DEFAULT,
targetURL,
"",
"",
oasGoogleAcessTokenScopeLiteral,
oasGoogleIDTokenAudLiteral,
oasGoogleIDTokenAudRef)
}

// set a dynamic target url
if oasTargetURLRef != "" {
targets.AddStepToPreFlowRequest("Set-Target-1", NoAuthTargetName)
targets.AddStepToPreFlowRequest("Set-Target-1", DEFAULT)
apiproxy.AddPolicy("Set-Target-1")
generateSetTarget = true
}
Expand Down
57 changes: 32 additions & 25 deletions internal/bundlegen/generateapiv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,7 @@ func GenerateAPIProxyDefFromOASv2(name string,
oasDocName string,
skipPolicy bool,
addCORS bool,
integrationEndpoint bool,
oasGoogleAcessTokenScopeLiteral string,
oasGoogleIDTokenAudLiteral string,
oasGoogleIDTokenAudRef string,
oasTargetURLRef string,
targetURL string,
targetOptions TargetOptions,
) (err error) {
if docModel == nil {
return fmt.Errorf("the Open API document not loaded")
Expand All @@ -147,13 +142,13 @@ func GenerateAPIProxyDefFromOASv2(name string,
apiproxy.SetCreatedAt()
apiproxy.SetLastModifiedAt()
apiproxy.SetConfigurationVersion()
if integrationEndpoint {
apiproxy.AddIntegrationEndpoint("default")
if targetOptions.IntegrationBackend.IntegrationName != "" {
apiproxy.AddIntegrationEndpoint(DEFAULT)
} else {
apiproxy.AddTargetEndpoint(NoAuthTargetName)
apiproxy.AddTargetEndpoint(DEFAULT)
}

apiproxy.AddProxyEndpoint("default")
apiproxy.AddProxyEndpoint(DEFAULT)

if !skipPolicy {
apiproxy.AddResource(oasDocName, "oas")
Expand All @@ -177,32 +172,44 @@ func GenerateAPIProxyDefFromOASv2(name string,
}

// decide on the type of target
if integrationEndpoint { // assume an integration endpoint
if targetOptions.IntegrationBackend.IntegrationName != "" { // assume an integration endpoint
proxies.AddStepToPreFlowRequest("set-integration-request")
apiproxy.AddPolicy("set-integration-request")
proxies.NewProxyEndpoint(u.Path, false)
} else {
// if target is not set, derive it from the OAS file
if targetURL == "" {
targets.NewTargetEndpoint(NoAuthTargetName,
// step 1. check for target server
if targetOptions.HttpBackend.TargetServerName != "" {
targets.NewTargetEndpoint(DEFAULT,
"",
u.Path,
targetOptions.HttpBackend.TargetServerName,
targetOptions.HttpBackend.OasGoogleAcessTokenScopeLiteral,
targetOptions.HttpBackend.OasGoogleIDTokenAudLiteral,
targetOptions.HttpBackend.OasGoogleIDTokenAudRef)
} else if targetOptions.HttpBackend.TargetURL == "" { // step 2. check for target url
targets.NewTargetEndpoint(DEFAULT,
u.Scheme+"://"+u.Hostname()+u.Path,
oasGoogleAcessTokenScopeLiteral,
oasGoogleIDTokenAudLiteral,
oasGoogleIDTokenAudRef)
"",
"",
targetOptions.HttpBackend.OasGoogleAcessTokenScopeLiteral,
targetOptions.HttpBackend.OasGoogleIDTokenAudLiteral,
targetOptions.HttpBackend.OasGoogleIDTokenAudRef)
} else { // an explicit target url is set
if _, err = url.Parse(targetURL); err != nil {
if _, err = url.Parse(targetOptions.HttpBackend.TargetURL); err != nil {
return fmt.Errorf("invalid target url: %v", err)
}
targets.NewTargetEndpoint(NoAuthTargetName,
targetURL,
oasGoogleAcessTokenScopeLiteral,
oasGoogleIDTokenAudLiteral,
oasGoogleIDTokenAudRef)
targets.NewTargetEndpoint(DEFAULT,
targetOptions.HttpBackend.TargetURL,
"",
"",
targetOptions.HttpBackend.OasGoogleAcessTokenScopeLiteral,
targetOptions.HttpBackend.OasGoogleIDTokenAudLiteral,
targetOptions.HttpBackend.OasGoogleIDTokenAudRef)
}

// set a dynamic target url
if oasTargetURLRef != "" {
targets.AddStepToPreFlowRequest("Set-Target-1", NoAuthTargetName)
if targetOptions.HttpBackend.OasTargetURLRef != "" {
targets.AddStepToPreFlowRequest("Set-Target-1", DEFAULT)
apiproxy.AddPolicy("Set-Target-1")
generateSetTarget = true
}
Expand Down
4 changes: 2 additions & 2 deletions internal/bundlegen/generateapiv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestSpecs(t *testing.T) {
for _, specName := range specNames {
fmt.Println("Testing " + specName + " ...")
testLoadDocument(specName, t)
testGenerateAPIProxyDefFromOASv2(specName, t)
TestGenerateAPIProxyDefFromOASv2(specName, t)
}
}

Expand All @@ -51,7 +51,7 @@ func testLoadDocument(specName string, t *testing.T) {
}
}

func testGenerateAPIProxyDefFromOASv2(specName string, t *testing.T) {
func TestGenerateAPIProxyDefFromOASv2(specName string, t *testing.T) {
skipPolicy := false
name := "test"
addCORS := true
Expand Down
10 changes: 5 additions & 5 deletions internal/bundlegen/generategql.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func GenerateAPIProxyDefFromGQL(name string,
apiproxy.SetCreatedAt()
apiproxy.SetLastModifiedAt()
apiproxy.SetConfigurationVersion()
apiproxy.AddTargetEndpoint(NoAuthTargetName)
apiproxy.AddProxyEndpoint("default")
apiproxy.AddTargetEndpoint(DEFAULT)
apiproxy.AddProxyEndpoint(DEFAULT)

apiproxy.SetDescription("Generated API Proxy from " + gqlDocName)

Expand All @@ -55,17 +55,17 @@ func GenerateAPIProxyDefFromGQL(name string,

// if target is not set, add a default/fake endpoint
if targetUrl == "" {
targets.NewTargetEndpoint(NoAuthTargetName, "https://api.example.com", "", "", "")
targets.NewTargetEndpoint(DEFAULT, "https://api.example.com", "", "", "", "", "")
} else { // an explicit target url is set
if _, err = url.Parse(targetUrl); err != nil {
return fmt.Errorf("invalid target url: %v", err)
}
targets.NewTargetEndpoint(NoAuthTargetName, targetUrl, "", "", "")
targets.NewTargetEndpoint(DEFAULT, targetUrl, "", "", "", "", "")
}

// set a dynamic target url
if targetUrlRef != "" {
targets.AddStepToPreFlowRequest("Set-Target-1", NoAuthTargetName)
targets.AddStepToPreFlowRequest("Set-Target-1", DEFAULT)
apiproxy.AddPolicy("Set-Target-1")
generateSetTarget = true
}
Expand Down
40 changes: 40 additions & 0 deletions internal/bundlegen/generategql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bundlegen

import (
"testing"
)

func TestGenerateAPIProxyDefFromGQL(t *testing.T) {
skipPolicy := false
name := "test"
gqlDocName := "schema"
apiKeyLocation := "request.header.x-api-key"
addCORS := true
basePath := ""
targetURL := "http://api.example.com"
targetURLRef := ""
if err := GenerateAPIProxyDefFromGQL(name,
gqlDocName,
basePath,
apiKeyLocation,
skipPolicy,
addCORS,
targetURLRef,
targetURL); err != nil {
t.Fatalf("%v", err)
}
}
25 changes: 25 additions & 0 deletions internal/bundlegen/generateint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bundlegen

import "testing"

func TestGenerateIntegrationAPIProxy(t *testing.T) {
name := "test"
apiTrigger := "test"
if err := GenerateIntegrationAPIProxy(name, apiTrigger); err != nil {
t.Fatalf("%v", err)
}
}
Loading

0 comments on commit d7536c4

Please sign in to comment.