Skip to content
This repository has been archived by the owner on May 6, 2022. It is now read-only.

Commit

Permalink
FR: generate bundle from oas
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Jan 28, 2022
1 parent 8ce9a29 commit a40f1bb
Show file tree
Hide file tree
Showing 231 changed files with 498 additions and 371 deletions.
63 changes: 63 additions & 0 deletions bundlegen/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2021 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 common

import (
"encoding/xml"
)

type PreFlowDef struct {
XMLName xml.Name `xml:"PreFlow"`
Name string `xml:"name,attr"`
Request RequestFlowDef `xml:"Request"`
Response ResponseFlowDef `xml:"Response"`
}

type PostFlowDef struct {
XMLName xml.Name `xml:"PostFlow"`
Name string `xml:"name,attr"`
Request RequestFlowDef `xml:"Request"`
Response ResponseFlowDef `xml:"Response"`
}

type RequestFlowDef struct {
Step []*StepDef `xml:"Step"`
}

type ResponseFlowDef struct {
Step []*StepDef `xml:"Step"`
}

type StepDef struct {
Name string `xml:"Name"`
}

type FlowsDef struct {
XMLName xml.Name `xml:"Flows"`
Flow []FlowDef `xml:"Flow"`
}

type FlowDef struct {
XMLName xml.Name `xml:"Flow"`
Name string `xml:"name,attr"`
Description string `xml:"Description,omitempty"`
Request RequestFlowDef `xml:"Request"`
Response ResponseFlowDef `xml:"Response"`
Condition ConditionDef `xml:"Condition"`
}

type ConditionDef struct {
ConditionData string `xml:",innerxml"`
}
19 changes: 15 additions & 4 deletions bundlegen/generateapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/ghodss/yaml"
apiproxy "github.com/srinandan/apigeecli/bundlegen/apiproxydef"
proxies "github.com/srinandan/apigeecli/bundlegen/proxies"
target "github.com/srinandan/apigeecli/bundlegen/targetendpoint"
targets "github.com/srinandan/apigeecli/bundlegen/targets"
)

type pathDetailDef struct {
Expand All @@ -36,7 +36,7 @@ type pathDetailDef struct {
APIKeyPoicy bool
}

var generateOAuthPolicy, generateAPIKeyPolicy bool
var generateOAuthPolicy, generateAPIKeyPolicy, generateSetTarget bool

var doc *openapi3.T

Expand Down Expand Up @@ -120,7 +120,7 @@ func isFileYaml(name string) bool {
return false
}

func GenerateAPIProxyDefFromOAS(name string, oasDocName string, skipPolicy bool, addCORS bool) (err error) {
func GenerateAPIProxyDefFromOAS(name string, oasDocName string, skipPolicy bool, addCORS bool, oasGoogleAcessTokenScopeLiteral string, oasGoogleIdTokenAudLiteral string, oasGoogleIdTokenAudRef string, oasTargetUrlRef string) (err error) {

if doc == nil {
return fmt.Errorf("Open API document not loaded")
Expand Down Expand Up @@ -151,7 +151,14 @@ func GenerateAPIProxyDefFromOAS(name string, oasDocName string, skipPolicy bool,

apiproxy.SetBasePath(u.Path)

target.NewTargetEndpoint(u.Scheme + "://" + u.Hostname())
//set a dynamic target url
if oasTargetUrlRef != "" {
targets.AddStepToPreFlowRequest("Set-Target-1")
apiproxy.AddPolicy("Set-Target-1")
generateSetTarget = true
}

targets.NewTargetEndpoint(u.Scheme+"://"+u.Hostname(), oasGoogleAcessTokenScopeLiteral, oasGoogleIdTokenAudLiteral, oasGoogleIdTokenAudRef)

proxies.NewProxyEndpoint(u.Path)

Expand Down Expand Up @@ -347,6 +354,10 @@ func GenerateAPIKeyPolicy() bool {
return generateAPIKeyPolicy
}

func GenerateSetTargetPolicy() bool {
return generateSetTarget
}

func replacePathWithWildCard(keyPath string) string {
re := regexp.MustCompile(`{(.*?)}`)
if strings.ContainsAny(keyPath, "{") {
Expand Down
14 changes: 14 additions & 0 deletions bundlegen/policies/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package policies

import (
"regexp"
"strings"
)

var oasPolicyTemplate = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
Expand Down Expand Up @@ -58,6 +59,15 @@ var corsPolicy = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</CORS>`

var setTargetEndpointPolicy = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Set-Target-1">
<AssignVariable>
<Name>target.url</Name>
<Ref>dynamic.target.url</Ref>
</AssignVariable>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>`

func AddOpenAPIValidatePolicy(name string) string {
return replaceTemplateWithPolicy(name)
}
Expand All @@ -74,6 +84,10 @@ func AddCORSPolicy() string {
return corsPolicy
}

func AddSetTargetEndpoint(ref string) string {
return strings.Replace(setTargetEndpointPolicy, "dynamic.target.url", ref, -1)
}

func replaceTemplateWithPolicy(name string) string {
re := regexp.MustCompile(`{(.*?)}`)
return re.ReplaceAllLiteralString(oasPolicyTemplate, name)
Expand Down
58 changes: 8 additions & 50 deletions bundlegen/proxies/proxies.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,70 +17,28 @@ package proxies
import (
"encoding/xml"
"fmt"

proxytypes "github.com/srinandan/apigeecli/bundlegen/common"
)

type proxyEndpointDef struct {
XMLName xml.Name `xml:"ProxyEndpoint"`
Name string `xml:"name,attr"`
Description string `xml:"Description,omitempty"`
FaultRules string `xml:"FaultRules,omitempty"`
PreFlow preFlowDef `xml:"PreFlow,omitempty"`
PostFlow postFlowDef `xml:"PostFlow,omitempty"`
Flows flowsDef `xml:"Flows,omitempty"`
PreFlow proxytypes.PreFlowDef `xml:"PreFlow,omitempty"`
PostFlow proxytypes.PostFlowDef `xml:"PostFlow,omitempty"`
Flows proxytypes.FlowsDef `xml:"Flows,omitempty"`
HTTPProxyConnection httpProxyConnectionDef `xml:"HTTPProxyConnection,omitempty"`
RouteRule routeRuleDef `xml:"RouteRule,omitempty"`
}

type preFlowDef struct {
XMLName xml.Name `xml:"PreFlow"`
Name string `xml:"name,attr"`
Request requestFlowDef `xml:"Request"`
Response responseFlowDef `xml:"Response"`
}

type postFlowDef struct {
XMLName xml.Name `xml:"PostFlow"`
Name string `xml:"name,attr"`
Request requestFlowDef `xml:"Request"`
Response responseFlowDef `xml:"Response"`
}

type requestFlowDef struct {
Step []*stepDef `xml:"Step"`
}

type responseFlowDef struct {
Step []*stepDef `xml:"Step"`
}

type stepDef struct {
Name string `xml:"Name"`
}

type routeRuleDef struct {
XMLName xml.Name `xml:"RouteRule"`
Name string `xml:"name,attr"`
TargetEndpoint string `xml:"TargetEndpoint"`
}

type flowsDef struct {
XMLName xml.Name `xml:"Flows"`
Flow []flowDef `xml:"Flow"`
}

type flowDef struct {
XMLName xml.Name `xml:"Flow"`
Name string `xml:"name,attr"`
Description string `xml:"Description,omitempty"`
Request requestFlowDef `xml:"Request"`
Response responseFlowDef `xml:"Response"`
Condition conditionDef `xml:"Condition"`
}

type conditionDef struct {
ConditionData string `xml:",innerxml"`
}

type httpProxyConnectionDef struct {
XMLName xml.Name `xml:"HTTPProxyConnection"`
BasePath string `xml:"BasePath"`
Expand Down Expand Up @@ -108,23 +66,23 @@ func NewProxyEndpoint(basePath string) {
}

func AddFlow(operationId string, keyPath string, method string, description string) {
flow := flowDef{}
flow := proxytypes.FlowDef{}
flow.Name = operationId
flow.Description = description
flow.Condition.ConditionData = "(proxy.pathsuffix MatchesPath \"" + keyPath + "\") and (request.verb = \"" + method + "\")"
proxyEndpoint.Flows.Flow = append(proxyEndpoint.Flows.Flow, flow)
}

func AddStepToPreFlowRequest(name string) {
step := stepDef{}
step := proxytypes.StepDef{}
step.Name = name
proxyEndpoint.PreFlow.Request.Step = append(proxyEndpoint.PreFlow.Request.Step, &step)
}

func AddStepToFlowRequest(name string, flowName string) error {
for flowKey, flow := range proxyEndpoint.Flows.Flow {
if flow.Name == flowName {
step := stepDef{}
step := proxytypes.StepDef{}
step.Name = name
proxyEndpoint.Flows.Flow[flowKey].Request.Step = append(proxyEndpoint.Flows.Flow[flowKey].Request.Step, &step)
return nil
Expand Down
11 changes: 9 additions & 2 deletions bundlegen/proxybundle/proxybundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ import (
apiproxy "github.com/srinandan/apigeecli/bundlegen/apiproxydef"
policies "github.com/srinandan/apigeecli/bundlegen/policies"
proxies "github.com/srinandan/apigeecli/bundlegen/proxies"
target "github.com/srinandan/apigeecli/bundlegen/targetendpoint"
target "github.com/srinandan/apigeecli/bundlegen/targets"
"github.com/srinandan/apigeecli/clilog"
"golang.org/x/oauth2"
)

const rootDir = "apiproxy"

func GenerateAPIProxyBundle(name string, content string, fileName string, resourceType string, skipPolicy bool, addCORS bool) (err error) {
func GenerateAPIProxyBundle(name string, content string, fileName string, resourceType string, skipPolicy bool, addCORS bool, oasGoogleAcessTokenScopeLiteral string, oasGoogleIdTokenAudLiteral string, oasGoogleIdTokenAudRef string, oasTargetUrlRef string) (err error) {

var apiProxyData, proxyEndpointData, targetEndpointData string

Expand Down Expand Up @@ -100,6 +100,13 @@ func GenerateAPIProxyBundle(name string, content string, fileName string, resour
return err
}

//add set target url
if genapi.GenerateSetTargetPolicy() {
if err = writeXMLData(policiesDirPath+string(os.PathSeparator)+"Set-Target-1.xml", policies.AddSetTargetEndpoint(oasTargetUrlRef)); err != nil {
return err
}
}

//add oauth policy
if genapi.GenerateOAuthPolicy() {
if err = writeXMLData(policiesDirPath+string(os.PathSeparator)+"OAuth-v20-1.xml", policies.AddOAuth2Policy()); err != nil {
Expand Down
74 changes: 0 additions & 74 deletions bundlegen/targetendpoint/targetendpoint.go

This file was deleted.

Loading

0 comments on commit a40f1bb

Please sign in to comment.