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

Commit

Permalink
add support for graphql operations
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed May 23, 2021
1 parent 73b47c9 commit 081c98f
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 184 deletions.
312 changes: 209 additions & 103 deletions client/products/products.go

Large diffs are not rendered by default.

28 changes: 12 additions & 16 deletions cmd/envoy/addbinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,20 @@ var AddBindCmd = &cobra.Command{
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
if legacy {
apiclient.SetPrintOutput(false)
_, err = products.GetAttribute(productName, envoyAttributeName)
apiclient.SetPrintOutput(true)
if err != nil {
attr := make(map[string]string)
attr[string(envoyAttributeName)] = strings.Join(serviceNames, ",")
_, err = products.Update(productName, "", "", "", "", "", "", nil, nil, nil, attr)
return err
} else {
_, err = products.UpdateAttribute(productName, envoyAttributeName, strings.Join(serviceNames, ","))
return err
}

apiclient.SetPrintOutput(false)
_, err = products.GetAttribute(productName, envoyAttributeName)
apiclient.SetPrintOutput(true)
if err != nil {
attr := make(map[string]string)
attr[string(envoyAttributeName)] = strings.Join(serviceNames, ",")
_, err = products.UpdateLegacy(productName, "", "", "", "", "", "", nil, nil, nil, attr)
return err
} else {
_, err = products.UpdateRemoteServiceOperationGroup(productName, serviceNames)
_, err = products.UpdateAttribute(productName, envoyAttributeName, strings.Join(serviceNames, ","))
return err
}

return err
},
}
Expand All @@ -58,8 +56,6 @@ func init() {
"", "Apigee API Product name")
AddBindCmd.Flags().StringArrayVarP(&serviceNames, "remote-svcs", "r",
[]string{}, "Envoy Service names")
AddBindCmd.Flags().BoolVarP(&legacy, "legacy", "l",
false, "Legacy product object")

_ = AddBindCmd.MarkFlagRequired("prod")
_ = AddBindCmd.MarkFlagRequired("remote-svcs")
Expand Down
18 changes: 5 additions & 13 deletions cmd/envoy/createbinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,15 @@ var CreateCmd = &cobra.Command{
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
if legacy {
if attrs == nil {
attrs = make(map[string]string)
}
attrs[envoyAttributeName] = strings.Join(serviceNames, ",")
_, err = products.CreateLegacy(name, description, approval, displayName, quota, quotaInterval, quotaUnit, environments, remoteServiceProxy, scopes, attrs)
} else {
_, err = products.CreateRemoteServiceOperationGroup(name, description, approval, displayName, quota, quotaInterval, quotaUnit, environments, serviceNames, scopes, attrs)
if attrs == nil {
attrs = make(map[string]string)
}
attrs[envoyAttributeName] = strings.Join(serviceNames, ",")
_, err = products.CreateLegacy(name, description, approval, displayName, quota, quotaInterval, quotaUnit, environments, nil, scopes, attrs)
return
},
}

var remoteServiceProxy = []string{"remote-service"}

func init() {
CreateCmd.Flags().StringVarP(&name, "name", "n",
"", "Name of the API Product")
Expand All @@ -56,7 +50,7 @@ func init() {
CreateCmd.Flags().StringArrayVarP(&environments, "envs", "e",
[]string{}, "Environments to enable")
CreateCmd.Flags().StringArrayVarP(&serviceNames, "remote-svcs", "r",
[]string{}, "Envoy Service names. Ex: -s service1 -s service2")
[]string{}, "Envoy Service names. Ex: -s service1:port1 -s service2:port2")
CreateCmd.Flags().StringArrayVarP(&scopes, "scopes", "s",
[]string{}, "OAuth scopes")
CreateCmd.Flags().StringVarP(&quota, "quota", "q",
Expand All @@ -69,8 +63,6 @@ func init() {
"", "Approval type")
CreateCmd.Flags().StringToStringVar(&attrs, "attrs",
nil, "Custom attributes")
CreateCmd.Flags().BoolVarP(&legacy, "legacy", "l",
false, "Legacy product object")

_ = CreateCmd.MarkFlagRequired("prod")
_ = CreateCmd.MarkFlagRequired("remote-svcs")
Expand Down
59 changes: 27 additions & 32 deletions cmd/envoy/removebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,40 @@ var RemoveCmd = &cobra.Command{
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
if legacy {
fmt.Println("Current Values of Attribute: ")
respBody, err := products.GetAttribute(productName, envoyAttributeName)
if err != nil {
return err
}

var attributeValue map[string]string
err = json.Unmarshal(respBody, &attributeValue)
if err != nil {
return err
}
fmt.Println("Current Values of Attribute: ")
respBody, err := products.GetAttribute(productName, envoyAttributeName)
if err != nil {
return err
}

values := strings.Split(attributeValue["value"], ",")
var newValues []string
var found bool
var attributeValue map[string]string
err = json.Unmarshal(respBody, &attributeValue)
if err != nil {
return err
}

for _, value := range values {
if strings.TrimSpace(value) != serviceName {
newValues = append(newValues, strings.TrimSpace(value))
} else {
found = true
fmt.Println(("Found service name, removing binding"))
}
}
values := strings.Split(attributeValue["value"], ",")
var newValues []string
var found bool

if !found {
return fmt.Errorf("did not find value matching service name")
for _, value := range values {
if strings.TrimSpace(value) != serviceName {
newValues = append(newValues, strings.TrimSpace(value))
} else {
found = true
fmt.Println(("Found service name, removing binding"))
}
}

fmt.Println("New Values of Attribute: ")
newAttributeValues := strings.Join(newValues, ",")
_, err = products.UpdateAttribute(productName, envoyAttributeName, newAttributeValues)
return err
} else {
_, err = products.UpdateRemoteServiceOperationGroup(productName, serviceNames)
if !found {
return fmt.Errorf("did not find value matching service name")
}

fmt.Println("New Values of Attribute: ")
newAttributeValues := strings.Join(newValues, ",")
_, err = products.UpdateAttribute(productName, envoyAttributeName, newAttributeValues)

return
},
}
Expand All @@ -83,8 +80,6 @@ func init() {
"", "Apigee API Product name")
RemoveCmd.Flags().StringVarP(&serviceName, "svc", "s",
"", "Envoy Service name")
RemoveCmd.Flags().BoolVarP(&legacy, "legacy", "l",
false, "Legacy product object")

_ = RemoveCmd.MarkFlagRequired("org")
_ = RemoveCmd.MarkFlagRequired("prod")
Expand Down
23 changes: 15 additions & 8 deletions cmd/products/crtprod.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,38 @@ var CreateCmd = &cobra.Command{
Short: "Create an API product",
Long: "Create an API product",
Args: func(cmd *cobra.Command, args []string) (err error) {
if legacy && operationGroupFile != "" {
return fmt.Errorf("operationGroupFile cannot be used with legacy mode")
if legacy && (operationGroupFile != "" || gqlOperationGroupFile != "") {
return fmt.Errorf("operationGroupFile/gqlOperationGroupFile cannot be used with legacy mode")
}
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
if legacy {
_, err = products.CreateLegacy(name, description, approval, displayName, quota, quotaInterval, quotaUnit, environments, proxies, scopes, attrs)
} else {
var operationGrp, gqlOperationGrp []byte
if operationGroupFile != "" {
var operationGrp []byte
operationGrp, err = ioutil.ReadFile(operationGroupFile)
if err != nil {
clilog.Info.Println(err)
return err
}
_, err = products.CreateProxyOperationGroup(name, description, approval, displayName, quota, quotaInterval, quotaUnit, environments, scopes, operationGrp, attrs)
} else {
_, err = products.Create(name, description, approval, displayName, quota, quotaInterval, quotaUnit, environments, proxies, scopes, attrs)
}
if gqlOperationGroupFile != "" {
gqlOperationGrp, err = ioutil.ReadFile(gqlOperationGroupFile)
if err != nil {
clilog.Info.Println(err)
return err
}
}
_, err = products.CreateProxyOperationGroup(name, description, approval, displayName, quota, quotaInterval, quotaUnit, environments, scopes, operationGrp, gqlOperationGrp, attrs)
}
return
},
}

var legacy bool
var operationGroupFile string
var operationGroupFile, gqlOperationGroupFile string

func init() {

Expand All @@ -82,8 +87,10 @@ func init() {
"", "Approval type")
CreateCmd.Flags().StringToStringVar(&attrs, "attrs",
nil, "Custom attributes")
CreateCmd.Flags().StringVarP(&operationGroupFile, "opgrp", "g",
CreateCmd.Flags().StringVarP(&operationGroupFile, "opgrp", "",
"", "File containing Operation Group JSON. See samples for how to create the file")
CreateCmd.Flags().StringVarP(&gqlOperationGroupFile, "gqlopgrp", "",
"", "File containing GraphQL Operation Group JSON. See samples for how to create the file")
CreateCmd.Flags().BoolVarP(&legacy, "legacy", "l",
false, "Legacy product object")

Expand Down
24 changes: 16 additions & 8 deletions cmd/products/updprod.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package products

import (
"fmt"
"io/ioutil"

"github.com/spf13/cobra"
Expand All @@ -29,23 +30,31 @@ var UpdateCmd = &cobra.Command{
Short: "Update an API product",
Long: "Update an API product",
Args: func(cmd *cobra.Command, args []string) (err error) {
if legacy && (operationGroupFile != "" || gqlOperationGroupFile != "") {
return fmt.Errorf("operationGroupFile/gqlOperationGroupFile cannot be used with legacy mode")
}
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
if legacy {
_, err = products.UpdateLegacy(name, description, approval, displayName, quota, quotaInterval, quotaUnit, environments, proxies, scopes, attrs)
} else {
var operationGrp, gqlOperationGrp []byte
if operationGroupFile != "" {
var operationGrp []byte
operationGrp, err = ioutil.ReadFile(operationGroupFile)
if err != nil {
clilog.Info.Println(err)
return err
}
_, err = products.UpdateProxyOperationGroup(name, description, approval, displayName, quota, quotaInterval, quotaUnit, environments, scopes, operationGrp, attrs)
} else {
_, err = products.Update(name, description, approval, displayName, quota, quotaInterval, quotaUnit, environments, proxies, scopes, attrs)
}
if gqlOperationGroupFile != "" {
gqlOperationGrp, err = ioutil.ReadFile(gqlOperationGroupFile)
if err != nil {
clilog.Info.Println(err)
return err
}
}
_, err = products.UpdateProxyOperationGroup(name, description, approval, displayName, quota, quotaInterval, quotaUnit, environments, scopes, operationGrp, gqlOperationGrp, attrs)
}
return
},
Expand Down Expand Up @@ -75,14 +84,13 @@ func init() {
"", "Approval type")
UpdateCmd.Flags().StringToStringVar(&attrs, "attrs",
nil, "Custom attributes")
UpdateCmd.Flags().StringVarP(&operationGroupFile, "opgrp", "g",
UpdateCmd.Flags().StringVarP(&operationGroupFile, "opgrp", "",
"", "File containing Operation Group JSON. See samples for how to create the file")
UpdateCmd.Flags().StringVarP(&gqlOperationGroupFile, "gqlopgrp", "",
"", "File containing GraphQL Operation Group JSON. See samples for how to create the file")
UpdateCmd.Flags().BoolVarP(&legacy, "legacy", "l",
false, "Legacy product object")
//TODO: apiresource -r later

_ = UpdateCmd.MarkFlagRequired("name")
_ = UpdateCmd.MarkFlagRequired("envs")
_ = UpdateCmd.MarkFlagRequired("proxies")
_ = UpdateCmd.MarkFlagRequired("approval")
}
20 changes: 20 additions & 0 deletions samples/apiproduct-gqlgroup.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"operationConfigs": [
{
"apiSource": "orders-gql",
"operations": [{
"operationTypes": [
"query"
],
"operation": "getOrder"
}
],
"quota": {
"limit": "1",
"interval": "1",
"timeUnit": "second"
}
}
],
"operationConfigType": "proxy"
}
8 changes: 4 additions & 4 deletions samples/apiproduct-op-group.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"operationConfigs": [
{
"apiSource": "httpbin",
"apiSource": "orders",
"operations": [
{
"resource": "/httpbin",
"resource": "/",
"methods": [
"GET"
"GET", "POST"
]
}
],
Expand All @@ -18,4 +18,4 @@
}
],
"operationConfigType": "proxy"
}
}

0 comments on commit 081c98f

Please sign in to comment.