Trigger new snapshot if scopes are different from before.
diff --git a/changes.go b/changes.go index 0b77cba..e7a9289 100644 --- a/changes.go +++ b/changes.go
@@ -2,14 +2,14 @@ import ( "encoding/json" + "github.com/apigee-labs/transicator/common" "io/ioutil" "net/http" "net/url" "path" - "time" - - "github.com/apigee-labs/transicator/common" + "sort" "sync/atomic" + "time" ) var lastSequence string @@ -251,6 +251,17 @@ updateSequence(resp.LastSequence) + /* + * Check to see if there was any change in scope. If found, handle it + * by getting a new snapshot + */ + newScopes := findScopesForId(apidInfo.ClusterID) + cs := scopeChanged(newScopes, scopes) + if cs != nil { + log.Debugf("Detected scope changes, going to fetch a new snapshot to sync...") + return cs + } + return nil } @@ -316,3 +327,25 @@ } } + +/* + * Returns nil if the two arrays have matching contents + */ +func scopeChanged(a, b []string) error { + + if len(a) != len(b) { + return changeServerError{ + Code: "Scope changes detected; must get new snapshot", + } + } + sort.Strings(a) + sort.Strings(b) + for i, v := range a { + if v != b[i] { + return changeServerError{ + Code: "Scope changes detected; must get new snapshot", + } + } + } + return nil +}
diff --git a/listener_test.go b/listener_test.go index ee0b4cc..b5fea9b 100644 --- a/listener_test.go +++ b/listener_test.go
@@ -50,6 +50,22 @@ Expect(func() { handler.Handle(&event) }).To(Panic()) }, 3) + It("should fail if more than one apid_cluster rows", func() { + newScopes := []string{"foo"} + scopes := []string{"bar"} + Expect(scopeChanged(newScopes, scopes)).To(Equal(changeServerError{Code: "Scope changes detected; must get new snapshot"})) + newScopes = []string{"foo", "bar"} + scopes = []string{"bar"} + Expect(scopeChanged(newScopes, scopes)).To(Equal(changeServerError{Code: "Scope changes detected; must get new snapshot"})) + newScopes = []string{"foo"} + scopes = []string{"bar", "foo"} + Expect(scopeChanged(newScopes, scopes)).To(Equal(changeServerError{Code: "Scope changes detected; must get new snapshot"})) + newScopes = []string{"foo", "bar"} + scopes = []string{"bar", "foo"} + Expect(scopeChanged(newScopes, scopes)).To(BeNil()) + + }, 3) + It("should process a valid Snapshot", func() { event := common.Snapshot{ @@ -329,6 +345,7 @@ Expect(len(scopes)).To(Equal(2)) Expect(scopes[0]).To(Equal("s1")) Expect(scopes[1]).To(Equal("s2")) + }, 3) It("delete event should delete", func() {