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

Commit

Permalink
import oas doc to proxy and validate policy
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Apr 12, 2020
1 parent d2b02f0 commit 5d3468d
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 88 deletions.
64 changes: 37 additions & 27 deletions cmd/apis/apiproxydef/apiproxydef.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,50 @@ import (
"time"
)

type policiesType struct {
XMLName xml.Name `xml:"Policies"`
Policy []string `xml:"Policy,omitempty"`
type policiesDef struct {
Policy []string `xml:"Policy,omitempty"`
}

type proxyEndpointsType struct {
type proxyEndpointsDef struct {
XMLName xml.Name `xml:"ProxyEndpoints"`
ProxyEndpoint []string `xml:"ProxyEndpoint,omitempty"`
}

type targetEndpointsType struct {
type targetEndpointsDef struct {
XMLName xml.Name `xml:"TargetEndpoints"`
TargetEndpoint []string `xml:"TargetEndpoint,omitempty"`
}

type configurationVersionType struct {
type configurationVersionDef struct {
XMLName xml.Name `xml:"ConfigurationVersion,omitempty"`
MajorVersion string `xml:"majorVersion,attr"`
MinorVersion string `xml:"minorVersion,attr"`
}

type apiProxyType struct {
XMLName xml.Name `xml:"APIProxy"`
Name string `xml:"name,attr"`
Revision string `xml:"revision,attr"`
BasePaths string `xml:"Basepaths,omitempty"`
ConfigurationVersion configurationVersionType `xml:"ConfigurationVersion,omitempty"`
CreatedAt string `xml:"CreatedAt,omitempty"`
Description string `xml:"Description,omitempty"`
DisplayName string `xml:"DisplayName,omitempty"`
LastModifiedAt string `xml:"LastModifiedAt,omitempty"`
Policies policiesType `xml:"Policies,omitempty"`
ProxyEndpoints proxyEndpointsType `xml:"ProxyEndpoints,omitempty"`
Resources string `xml:"Resources,omitempty"`
Spec string `xml:"Spec,omitempty"`
TargetServers string `xml:"TargetServers,omitempty"`
TargetEndpoints targetEndpointsType `xml:"TargetEndpoints,omitempty"`
Validate string `xml:"validate,omitempty"`
type resourcesDef struct {
Resource []string `xml:"Resource,omitempty"`
}

var apiProxy apiProxyType
type apiProxyDef struct {
XMLName xml.Name `xml:"APIProxy"`
Name string `xml:"name,attr"`
Revision string `xml:"revision,attr"`
BasePaths string `xml:"Basepaths,omitempty"`
ConfigurationVersion configurationVersionDef `xml:"ConfigurationVersion,omitempty"`
CreatedAt string `xml:"CreatedAt,omitempty"`
Description string `xml:"Description,omitempty"`
DisplayName string `xml:"DisplayName,omitempty"`
LastModifiedAt string `xml:"LastModifiedAt,omitempty"`
Policies policiesDef `xml:"Policies,omitempty"`
ProxyEndpoints proxyEndpointsDef `xml:"ProxyEndpoints,omitempty"`
Resources resourcesDef `xml:"Resources,omitempty"`
Spec string `xml:"Spec,omitempty"`
TargetServers string `xml:"TargetServers,omitempty"`
TargetEndpoints targetEndpointsDef `xml:"TargetEndpoints,omitempty"`
Validate string `xml:"validate,omitempty"`
}

var apiProxy apiProxyDef

func SetDisplayName(name string) {
apiProxy.DisplayName = name
Expand Down Expand Up @@ -99,12 +102,19 @@ func SetDescription(description string) {
apiProxy.Description = description
}

func GetAPIProxy() string {
proxyBody, _ := xml.MarshalIndent(apiProxy, "", " ")
return string(proxyBody)
func GetAPIProxy() (string, error) {
proxyBody, err := xml.MarshalIndent(apiProxy, "", " ")
if err != nil {
return "", err
}
return string(proxyBody), nil
}

func SetConfigurationVersion() {
apiProxy.ConfigurationVersion.MajorVersion = "4"
apiProxy.ConfigurationVersion.MinorVersion = "0"
}

func AddResource(name string) {
apiProxy.Resources.Resource = append(apiProxy.Resources.Resource, "oas://"+name)
}
23 changes: 18 additions & 5 deletions cmd/apis/crtapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,24 @@ var CreateCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) (err error) {
if proxy != "" {
_, err = apis.CreateProxy(name, proxy)
} else if oasDoc != "" {
err = GenerateAPIProxyDefFromOAS(name, oasDoc)
} else if oasFile != "" || oasURI != "" {
var content []byte
var oasDocName string
if oasFile != "" {
oasDocName, content, err = LoadDocumentFromFile(oasFile)
} else {
oasDocName, content, err = LoadDocumentFromURI(oasURI)
}
if err != nil {
return err
}

err = GenerateAPIProxyDefFromOAS(name, oasDocName)
if err != nil {
return err
}

err = proxybundle.GenerateAPIProxyBundle(name)
err = proxybundle.GenerateAPIProxyBundle(name, string(content), oasDocName)
if err != nil {
return err
}
Expand All @@ -56,7 +67,7 @@ var CreateCmd = &cobra.Command{
},
}

var proxy, oasDoc string
var proxy, oasFile, oasURI string
var importProxy bool

func init() {
Expand All @@ -65,8 +76,10 @@ func init() {
"", "API Proxy name")
CreateCmd.Flags().StringVarP(&proxy, "proxy", "p",
"", "API Proxy Bundle path")
CreateCmd.Flags().StringVarP(&oasDoc, "oas", "f",
CreateCmd.Flags().StringVarP(&oasFile, "oasfile", "f",
"", "Open API 3.0 Specification file")
CreateCmd.Flags().StringVarP(&oasURI, "oasuri", "u",
"", "Open API 3.0 Specification URI location")
CreateCmd.Flags().BoolVarP(&importProxy, "import", "",
true, "Import API Proxy after generation from spec")

Expand Down
Loading

0 comments on commit 5d3468d

Please sign in to comment.