-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for scopes and collections (#2)
Motivation ------------ Allow usage with Couchbase Server 7.0 scopes and collections. Modifications --------------- Upgrade to couchbase-index-manager 2.0.0-beta001. Add scope name and collection name to the CRD for indexes. Change index matching to use scope, collection, and name instead of just the name. Results -------- Indexes may be managed on non-default collections.
- Loading branch information
1 parent
812a096
commit 8f7adf5
Showing
13 changed files
with
276 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package cbim | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
couchbasev1beta1 "github.com/brantburnett/couchbase-index-operator/api/v1beta1" | ||
) | ||
|
||
const ( | ||
defaultScopeName = "_default" | ||
defaultCollectionName = "_default" | ||
) | ||
|
||
// Uniquely defines a global secondary index. | ||
type GlobalSecondaryIndexIdentifier struct { | ||
// Name of the index | ||
Name string | ||
// Name of the index's scope | ||
ScopeName string | ||
// Name of the index's collection | ||
CollectionName string | ||
} | ||
|
||
func defaultedName(name *string) string { | ||
if name == nil { | ||
return defaultScopeName | ||
} | ||
|
||
return *name | ||
} | ||
|
||
func GetIndexIdentifier(index couchbasev1beta1.GlobalSecondaryIndex) GlobalSecondaryIndexIdentifier { | ||
return GlobalSecondaryIndexIdentifier{ | ||
Name: index.Name, | ||
ScopeName: defaultedName(index.ScopeName), | ||
CollectionName: defaultedName(index.CollectionName), | ||
} | ||
} | ||
|
||
func (identifier GlobalSecondaryIndexIdentifier) IsDefaultCollection() bool { | ||
return identifier.ScopeName == defaultScopeName && identifier.CollectionName == defaultCollectionName | ||
} | ||
|
||
func (identifier GlobalSecondaryIndexIdentifier) ToString() string { | ||
if identifier.IsDefaultCollection() { | ||
return identifier.Name | ||
} | ||
|
||
return fmt.Sprintf("%s.%s.%s", identifier.ScopeName, identifier.CollectionName, identifier.Name) | ||
} | ||
|
||
func ParseIndexIdentifierString(identifier string) (GlobalSecondaryIndexIdentifier, error) { | ||
if identifier == "" { | ||
return GlobalSecondaryIndexIdentifier{}, errors.New("invalid index identifier") | ||
} | ||
|
||
split := strings.Split(identifier, ".") | ||
if len(split) == 1 { | ||
return GlobalSecondaryIndexIdentifier{ | ||
ScopeName: defaultScopeName, | ||
CollectionName: defaultCollectionName, | ||
Name: split[0], | ||
}, nil | ||
} | ||
|
||
if len(split) != 3 || split[0] == "" || split[1] == "" || split[2] == "" { | ||
return GlobalSecondaryIndexIdentifier{}, errors.New("invalid index identifier") | ||
} | ||
|
||
return GlobalSecondaryIndexIdentifier{ | ||
ScopeName: split[0], | ||
CollectionName: split[1], | ||
Name: split[2], | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package cbim | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest/printer" | ||
//+kubebuilder:scaffold:imports | ||
) | ||
|
||
// These tests use Ginkgo (BDD-style Go testing framework). Refer to | ||
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo. | ||
|
||
func TestAPIs(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
|
||
RunSpecsWithDefaultAndCustomReporters(t, | ||
"V1Beta1 Suite", | ||
[]Reporter{printer.NewlineReporter{}}) | ||
} | ||
|
||
var _ = Describe("GlobalSecondaryIndexIdentifier.ToString", func() { | ||
|
||
It("should exclude the default collection name", func() { | ||
// Arrange | ||
|
||
identifier := GlobalSecondaryIndexIdentifier{ | ||
ScopeName: "_default", | ||
CollectionName: "_default", | ||
Name: "my_index", | ||
} | ||
|
||
// Act | ||
|
||
result := identifier.ToString() | ||
|
||
// Assert | ||
|
||
Expect(result).To(Equal("my_index")) | ||
}) | ||
|
||
It("should return a dotted name", func() { | ||
// Arrange | ||
|
||
identifier := GlobalSecondaryIndexIdentifier{ | ||
ScopeName: "scope", | ||
CollectionName: "_default", | ||
Name: "my_index", | ||
} | ||
|
||
// Act | ||
|
||
result := identifier.ToString() | ||
|
||
// Assert | ||
|
||
Expect(result).To(Equal("scope._default.my_index")) | ||
}) | ||
}) | ||
|
||
var _ = Describe("ParseIndexIdentifierString", func() { | ||
|
||
It("should error on empty string", func() { | ||
// Act | ||
|
||
_, err := ParseIndexIdentifierString("") | ||
|
||
// Assert | ||
|
||
Expect(err).NotTo(BeNil()) | ||
}) | ||
|
||
It("should error on empty segment", func() { | ||
// Act | ||
|
||
_, err := ParseIndexIdentifierString("scope..name") | ||
|
||
// Assert | ||
|
||
Expect(err).NotTo(BeNil()) | ||
}) | ||
|
||
It("should error on two segments", func() { | ||
// Act | ||
|
||
_, err := ParseIndexIdentifierString("scope.name") | ||
|
||
// Assert | ||
|
||
Expect(err).NotTo(BeNil()) | ||
}) | ||
|
||
It("should error on four segments", func() { | ||
// Act | ||
|
||
_, err := ParseIndexIdentifierString("scope.collection.name.extra") | ||
|
||
// Assert | ||
|
||
Expect(err).NotTo(BeNil()) | ||
}) | ||
|
||
It("should return simple name in default collection", func() { | ||
// Act | ||
|
||
result, err := ParseIndexIdentifierString("name") | ||
|
||
// Assert | ||
|
||
Expect(result).To(Equal(GlobalSecondaryIndexIdentifier{ | ||
ScopeName: "_default", | ||
CollectionName: "_default", | ||
Name: "name", | ||
})) | ||
Expect(err).To(BeNil()) | ||
}) | ||
|
||
It("should return dotted name", func() { | ||
// Act | ||
|
||
result, err := ParseIndexIdentifierString("scope.collection.name") | ||
|
||
// Assert | ||
|
||
Expect(result).To(Equal(GlobalSecondaryIndexIdentifier{ | ||
ScopeName: "scope", | ||
CollectionName: "collection", | ||
Name: "name", | ||
})) | ||
Expect(err).To(BeNil()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.