Skip to content

Commit

Permalink
cache api product list
Browse files Browse the repository at this point in the history
  • Loading branch information
theganyo committed Apr 28, 2020
2 parents 05f3475 + 1875c5e commit ecb9043
Show file tree
Hide file tree
Showing 19 changed files with 284 additions and 56 deletions.
84 changes: 84 additions & 0 deletions apigee/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2020 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 apigee

import (
"net/url"
"path"
)

const cachePath = "caches"

// CacheService is an interface for interfacing with the Apigee Edge Admin API
// dealing with caches.
type CacheService interface {
Get(cachename string) (*Cache, *Response, error)
Create(cache Cache) (*Response, error)
}

// Cache represents a cache definition
type Cache struct {
Name string `json:"-,omitempty"`
Description string `json:"description,omitempty"`
ExpirySettings *expirySettings `json:"expirySettings,omitempty"`
OverflowToDisk *bool `json:"overflowToDisk,omitempty"`
SkipCacheIfElementSizeInKBExceeds *string `json:"skipCacheIfElementSizeInKBExceeds,omitempty"`
}

type expirySettings struct {
ExpiryDate expiryDate `json:"expiryDate,omitempty"`
ValuesNull string `json:"valuesNull,omitempty"`
}

type expiryDate struct {
Value string `json:"value,omitempty"`
}

// CacheServiceOp represents a cache service operation
type CacheServiceOp struct {
client *EdgeClient
}

var _ CacheService = &CacheServiceOp{}

// Get returns a response given a cache name
func (s *CacheServiceOp) Get(cachename string) (*Cache, *Response, error) {
path := path.Join(cachePath, cachename)
req, e := s.client.NewRequest("GET", path, nil)
if e != nil {
return nil, nil, e
}
returnedCache := Cache{}
resp, e := s.client.Do(req, &returnedCache)
if e != nil {
return nil, resp, e
}
return &returnedCache, resp, e
}

// Create creates a cache and returns a response
func (s *CacheServiceOp) Create(cache Cache) (*Response, error) {
path := path.Join(cachePath)
u, _ := url.Parse(path)
q := u.Query()
q.Set("name", cache.Name)
u.RawQuery = q.Encode()
req, e := s.client.NewRequest("POST", u.String(), cache)
if e != nil {
return nil, e
}
resp, e := s.client.Do(req, &cache)
return resp, e
}
3 changes: 3 additions & 0 deletions apigee/edge_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type EdgeClient struct {
Proxies ProxiesService

KVMService KVMService

CacheService CacheService
// Account AccountService
// Actions ActionsService
// Domains DomainsService
Expand Down Expand Up @@ -246,6 +248,7 @@ func NewEdgeClient(o *EdgeClientOptions) (*EdgeClient, error) {
}
c.Proxies = &ProxiesServiceOp{client: c}
c.KVMService = &KVMServiceOp{client: c}
c.CacheService = &CacheServiceOp{client: c}

if !o.Auth.SkipAuth {
var e error
Expand Down
2 changes: 1 addition & 1 deletion apigee/proxies_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ func (s *ProxiesServiceOp) GetDeployedRevision(proxy string) (*Revision, error)
if err != nil && (resp == nil || resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden) {
return nil, err
}
if resp.StatusCode == http.StatusNotFound {
if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusBadRequest {
return nil, nil
}
for _, rev := range deployment.Revision {
Expand Down
23 changes: 21 additions & 2 deletions cmd/provision/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const ( // legacy

const ( // modern
kvmName = "remote-service"
cacheName = "remote-service"
encryptKVM = true
authProxyName = "remote-service"

Expand Down Expand Up @@ -110,8 +111,8 @@ func Cmd(rootArgs *shared.RootArgs, printf, fatalf shared.FormatFn) *cobra.Comma
c := &cobra.Command{
Use: "provision",
Short: "Provision your Apigee environment for remote services",
Long: `The provision command will set up your Apigee environment for remote services. This includes creating
and installing a remote-service kvm with certificates, creating credentials, and deploying a remote-service proxy
Long: `The provision command will set up your Apigee environment for remote services. This includes creating
and installing a remote-service kvm with certificates, creating credentials, and deploying a remote-service proxy
to your organization and environment.`,
Args: cobra.NoArgs,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -814,6 +815,24 @@ func (p *provision) importAndDeployProxy(name string, proxy *apigee.Proxy, oldRe
}
}

if !p.IsGCPManaged {
cache := apigee.Cache{
Name: cacheName,
}
resp, err = p.Client.CacheService.Create(cache)
if err != nil && (resp == nil || resp.StatusCode != http.StatusConflict) { // http.StatusConflict == already exists
return err
}
if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusConflict {
return fmt.Errorf("error creating cache %s, status code: %v", cacheName, resp.StatusCode)
}
if resp.StatusCode == http.StatusConflict {
printf("cache %s already exists", cacheName)
} else {
printf("cache %s created", cacheName)
}
}

printf("deploying proxy %s revision %d to env %s...", name, newRev, p.Env)
_, resp, err = p.Client.Proxies.Deploy(name, p.Env, newRev)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.2.0 h1:reN85Pxc5larApoH1keMBiu2GWtPqXQ1nc9gx+jOU+E=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down
23 changes: 12 additions & 11 deletions proxies/proxies.go

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions proxies/remote-proxy-gcp/apiproxy/policies/Lookup-Products.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LookupCache async="false" continueOnError="false" enabled="true" name="Lookup-Products">
<DisplayName>Lookup Products</DisplayName>
<CacheKey>
<KeyFragment ref="request.uri"/>
</CacheKey>
<Scope>Exclusive</Scope>
<AssignTo>productlist</AssignTo>
<CacheResource>remote-service</CacheResource>
</LookupCache>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PopulateCache async="false" continueOnError="false" enabled="true" name="Populate-Product-List">
<DisplayName>Populate Product List</DisplayName>
<Properties/>
<CacheKey>
<Prefix/>
<KeyFragment ref="request.uri"/>
</CacheKey>
<CacheResource>remote-service</CacheResource>
<Scope>Exclusive</Scope>
<ExpirySettings>
<TimeoutInSec>120</TimeoutInSec>
</ExpirySettings>
<Source>response.content</Source>
</PopulateCache>
14 changes: 14 additions & 0 deletions proxies/remote-proxy-gcp/apiproxy/policies/Send-Product-List.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Send-Product-List">
<DisplayName>Send Product List</DisplayName>
<Properties/>
<Set>
<Headers>
<Header name="Cache-Control">no-store</Header>
<Header name="Pragma">no-cache</Header>
</Headers>
<Payload contentType="application/json" variablePrefix="@" variableSuffix="#">@productlist#</Payload>
</Set>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>
12 changes: 12 additions & 0 deletions proxies/remote-proxy-gcp/apiproxy/proxies/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,22 @@
<Step>
<Name>Verify-API-Key</Name>
</Step>
<Step>
<Name>Lookup-Products</Name>
</Step>
</Request>
<Response>
<Step>
<Name>JavaCallout</Name>
<Condition>lookupcache.Lookup-Products.cachehit = false</Condition>
</Step>
<Step>
<Name>Populate-Product-List</Name>
<Condition>lookupcache.Lookup-Products.cachehit = false</Condition>
</Step>
<Step>
<Name>Send-Product-List</Name>
<Condition>lookupcache.Lookup-Products.cachehit = true</Condition>
</Step>
</Response>
<Condition>(proxy.pathsuffix MatchesPath "/products") and (request.verb = "GET")</Condition>
Expand Down
53 changes: 28 additions & 25 deletions proxies/remote-proxy-gcp/apiproxy/remote-service.xml
Original file line number Diff line number Diff line change
@@ -1,49 +1,52 @@
<APIProxy revision="1" name="remote-service">
<DisplayName>remote-service</DisplayName>
<Description>remote-service</Description>
<CreatedAt>1586561070679</CreatedAt>
<LastModifiedAt>1586561070679</LastModifiedAt>
<CreatedAt>1587832719075</CreatedAt>
<LastModifiedAt>1587832876047</LastModifiedAt>
<BasePaths>/remote-service</BasePaths>
<Policies>
<Policy>Clear-API-Key</Policy>
<Policy>Access-App-Info</Policy>
<Policy>JavaCallout</Policy>
<Policy>Set-Quota-Response</Policy>
<Policy>Create-Refresh-Request</Policy>
<Policy>Set-Quota-Variables</Policy>
<Policy>Generate-VerifyKey-Token</Policy>
<Policy>DistributedQuota</Policy>
<Policy>RevokeRefreshToken</Policy>
<Policy>Extract-API-Key</Policy>
<Policy>Create-Refresh-Request</Policy>
<Policy>Set-JWT-Variables</Policy>
<Policy>Clear-API-Key</Policy>
<Policy>Set-Response</Policy>
<Policy>AccessTokenRequest</Policy>
<Policy>Raise-Fault-Unknown-Request</Policy>
<Policy>Raise-Fault-Missing-Secret</Policy>
<Policy>RefreshAccessToken</Policy>
<Policy>JavaCallout</Policy>
<Policy>Extract-Refresh-Params</Policy>
<Policy>Generate-VerifyKey-Token</Policy>
<Policy>Eval-Quota-Result</Policy>
<Policy>Extract-API-Key</Policy>
<Policy>Verify-API-Key</Policy>
<Policy>Products-to-JSON</Policy>
<Policy>Create-OAuth-Request</Policy>
<Policy>Raise-Fault-Unknown-Request</Policy>
<Policy>Extract-Revoke-Params</Policy>
<Policy>Set-Quota-Variables</Policy>
<Policy>Generate-Access-Token</Policy>
<Policy>Extract-Refresh-Params</Policy>
<Policy>Extract-OAuth-Params</Policy>
<Policy>Send-Version</Policy>
<Policy>Set-Response</Policy>
<Policy>Extract-Revoke-Params</Policy>
<Policy>Set-JWT-Variables</Policy>
<Policy>Decode-Basic-Authentication</Policy>
<Policy>Products-to-JSON</Policy>
<Policy>Verify-API-Key</Policy>
<Policy>AccessTokenRequest</Policy>
<Policy>Access-App-Info</Policy>
<Policy>Eval-Quota-Result</Policy>
<Policy>DistributedQuota</Policy>
<Policy>Send-JWKs-Message</Policy>
<Policy>Raise-Fault-Missing-Secret</Policy>
<Policy>Assign-Debug</Policy>
<Policy>Send-JWKs-Message</Policy>
<Policy>Decode-Basic-Authentication</Policy>
<Policy>Send-Version</Policy>
<Policy>Lookup-Products</Policy>
<Policy>Populate-Product-List</Policy>
<Policy>Send-Product-List</Policy>
</Policies>
<ProxyEndpoints>
<ProxyEndpoint>default</ProxyEndpoint>
</ProxyEndpoints>
<Resources>
<Resource>java://products-javacallout-2.0.0.jar</Resource>
<Resource>jsc://set-quota-variables.js</Resource>
<Resource>jsc://set-jwt-variables.js</Resource>
<Resource>jsc://eval-quota-result.js</Resource>
<Resource>jsc://set-response.js</Resource>
<Resource>jsc://set-jwt-variables.js</Resource>
</Resources>
<TargetServers></TargetServers>
<TargetEndpoints></TargetEndpoints>
</APIProxy>
</APIProxy>
Loading

0 comments on commit ecb9043

Please sign in to comment.