diff --git a/bundlegen/generateapi.go b/bundlegen/generateapi.go index 7f987ad06..ce390dba1 100644 --- a/bundlegen/generateapi.go +++ b/bundlegen/generateapi.go @@ -296,10 +296,11 @@ func GetEndpoint(doc *openapi3.T) (u *url.URL, err error) { func GenerateAPIProxyDefFromGQL(name string, gqlDocName string, basePath string, - targetUrlRef string, apiKeyLocation string, skipPolicy bool, - addCORS bool) (err error) { + addCORS bool, + targetUrlRef string, + targetUrl string) (err error) { apiproxy.SetDisplayName(name) apiproxy.SetCreatedAt() @@ -315,8 +316,6 @@ func GenerateAPIProxyDefFromGQL(name string, apiproxy.AddPolicy("Validate-" + name + "-Schema") } - targets.NewTargetEndpoint("https://api.example.com", "", "", "") - proxies.NewProxyEndpoint(basePath) if addCORS { @@ -324,8 +323,22 @@ func GenerateAPIProxyDefFromGQL(name string, apiproxy.AddPolicy("Add-CORS") } - targets.AddStepToPreFlowRequest("Set-Target-1") - apiproxy.AddPolicy("Set-Target-1") + //set a dynamic target url + if targetUrlRef != "" { + targets.AddStepToPreFlowRequest("Set-Target-1") + apiproxy.AddPolicy("Set-Target-1") + generateSetTarget = true + } + + //if target is not set, derive it from the OAS file + if targetUrl == "" { + targets.NewTargetEndpoint("https://api.example.com", "", "", "") + } else { //an explicit target url is set + if _, err = url.Parse(targetUrl); err != nil { + return fmt.Errorf("Invalid target url: ", err) + } + targets.NewTargetEndpoint(targetUrl, "", "", "") + } if !skipPolicy { proxies.AddStepToPreFlowRequest("Validate-" + name + "-Schema") diff --git a/bundlegen/proxybundle/proxybundle.go b/bundlegen/proxybundle/proxybundle.go index 3a1f4f439..9e57a3e14 100644 --- a/bundlegen/proxybundle/proxybundle.go +++ b/bundlegen/proxybundle/proxybundle.go @@ -183,7 +183,8 @@ func GenerateAPIProxyBundleFromGQL(name string, keyName string, skipPolicy bool, addCORS bool, - targetUrlRef string) (err error) { + targetUrlRef string, + targetUrl string) (err error) { var apiProxyData, proxyEndpointData, targetEndpointData string const resourceType = "graphql" @@ -245,9 +246,11 @@ func GenerateAPIProxyBundleFromGQL(name string, return err } - if err = writeXMLData(policiesDirPath+string(os.PathSeparator)+"Set-Target-1.xml", - policies.AddSetTargetEndpoint(targetUrlRef)); err != nil { - return err + if targetUrlRef != "" { + if err = writeXMLData(policiesDirPath+string(os.PathSeparator)+"Set-Target-1.xml", + policies.AddSetTargetEndpoint(targetUrlRef)); err != nil { + return err + } } if !skipPolicy { diff --git a/cmd/apis/gqlcrtapis.go b/cmd/apis/gqlcrtapis.go index a11b49ba3..ab9accf28 100644 --- a/cmd/apis/gqlcrtapis.go +++ b/cmd/apis/gqlcrtapis.go @@ -39,6 +39,9 @@ var GqlCreateCmd = &cobra.Command{ if gqlFile == "" && gqlURI == "" { return fmt.Errorf("Either gqlfile or gqlurl must be passed") } + if targetUrl != "" && targetUrlRef != "" { + return fmt.Errorf("Either target-url or target-url-ref must be passed, not both") + } return apiclient.SetApigeeOrg(org) }, RunE: func(cmd *cobra.Command, args []string) (err error) { @@ -62,10 +65,11 @@ var GqlCreateCmd = &cobra.Command{ err = bundle.GenerateAPIProxyDefFromGQL(name, gqlDocName, basePath, - targetUrlRef, apiKeyLocation, skipPolicy, - addCORS) + addCORS, + targetUrlRef, + targetUrl) if err != nil { return err @@ -80,7 +84,8 @@ var GqlCreateCmd = &cobra.Command{ keyName, skipPolicy, addCORS, - targetUrlRef) + targetUrlRef, + targetUrl) if err != nil { return err @@ -108,8 +113,10 @@ func init() { GqlCreateCmd.Flags().StringVarP(&action, "action", "", "verify", "GraphQL policy action, must be oneOf parse, verify or parse_verify. Default is verify") GqlCreateCmd.Flags().StringVarP(&targetUrlRef, "target-url-ref", "", - "", "Set a reference variable containing the target endpoint") - GqlCreateCmd.Flags().StringVarP(&targetUrlRef, "apikey-location", "", + "", "Set target.url variable from a reference variable containing the target endpoint") + GqlCreateCmd.Flags().StringVarP(&targetUrl, "target-url", "", + "", "Set a target URL for the target endpoint") + GqlCreateCmd.Flags().StringVarP(&apiKeyLocation, "apikey-location", "", "", "Set the location of the API key, ex: request.header.x-api-key") GqlCreateCmd.Flags().BoolVarP(&importProxy, "import", "", true, "Import API Proxy after generation from spec") @@ -119,7 +126,6 @@ func init() { false, "Add a CORS policy") _ = GqlCreateCmd.MarkFlagRequired("name") - _ = GqlCreateCmd.MarkFlagRequired("target-url-ref") _ = GqlCreateCmd.MarkFlagRequired("basepath") }