Skip to content

Commit

Permalink
Merge pull request #954 from melonrush13/cosmoslocation
Browse files Browse the repository at this point in the history
Enable Multi Region Writes for CosmosDB
  • Loading branch information
Justin Pflueger authored Apr 21, 2020
2 parents b98e0b9 + 95002d7 commit e67b110
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
6 changes: 3 additions & 3 deletions api/v1alpha1/cosmosdb_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ const (
// CosmosDBProperties the CosmosDBProperties of CosmosDB.
type CosmosDBProperties struct {
// CosmosDBDatabaseAccountOfferType - The offer type for the Cosmos DB database account.
DatabaseAccountOfferType CosmosDBDatabaseAccountOfferType `json:"databaseAccountOfferType,omitempty"`
//Locations []CosmosDBLocation `json:"locations,omitempty"`
MongoDBVersion string `json:"mongoDBVersion,omitempty"`
DatabaseAccountOfferType CosmosDBDatabaseAccountOfferType `json:"databaseAccountOfferType,omitempty"`
EnableMultipleWriteLocations bool `json:"enableMultipleWriteLocations,omitempty"`
MongoDBVersion string `json:"mongoDBVersion,omitempty"`
}

// +kubebuilder:validation:Enum=Standard
Expand Down
3 changes: 2 additions & 1 deletion config/samples/azure_v1alpha1_cosmosdb.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
apiVersion: azure.microsoft.com/v1alpha1
kind: CosmosDB
metadata:
name: cosmosdb-sample1908xyzkj
name: cosmosdb-sample-1
spec:
kind: GlobalDocumentDB
location: westus
resourceGroup: resourcegroup-azure-operators
properties:
databaseAccountOfferType: Standard
enableMultipleWriteLocations: false
# optionally set the mongoDBVersion to "3.2" or "3.6", if omitted the default is "3.2"
# NOTE: kind must be set to MongoDB for this to take effect
#mongoDBVersion: "3.6"
Expand Down
10 changes: 5 additions & 5 deletions pkg/resourcemanager/cosmosdbs/cosmosdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ func (*AzureCosmosDBManager) CreateOrUpdateCosmosDB(
cosmosDBName string,
location string,
kind v1alpha1.CosmosDBKind,
dbType v1alpha1.CosmosDBDatabaseAccountOfferType,
dbVersion string,
properties v1alpha1.CosmosDBProperties,
tags map[string]*string) (*documentdb.DatabaseAccount, error) {
cosmosDBClient, err := getCosmosDBClient()
if err != nil {
return nil, err
}

dbKind := documentdb.DatabaseAccountKind(kind)
sDBType := string(dbType)
sDBType := string(properties.DatabaseAccountOfferType)
bWriteLocal := bool(properties.EnableMultipleWriteLocations)

var capabilities []documentdb.Capability
if dbKind == documentdb.MongoDB && dbVersion == "3.6" {
if dbKind == documentdb.MongoDB && properties.MongoDBVersion == "3.6" {
capabilities = []documentdb.Capability{
{Name: to.StringPtr("EnableMongo")},
}
Expand Down Expand Up @@ -93,7 +93,7 @@ func (*AzureCosmosDBManager) CreateOrUpdateCosmosDB(
ID: &cosmosDBName,
DatabaseAccountCreateUpdateProperties: &documentdb.DatabaseAccountCreateUpdateProperties{
DatabaseAccountOfferType: &sDBType,
EnableMultipleWriteLocations: to.BoolPtr(false),
EnableMultipleWriteLocations: &bWriteLocal,
IsVirtualNetworkFilterEnabled: to.BoolPtr(false),
Locations: &locationsArray,
Capabilities: &capabilities,
Expand Down
2 changes: 1 addition & 1 deletion pkg/resourcemanager/cosmosdbs/cosmosdb_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewAzureCosmosDBManager(secretClient secrets.SecretClient) *AzureCosmosDBMa
// CosmosDBManager client functions
type CosmosDBManager interface {
// CreateOrUpdateCosmosDB creates a new cosmos database account
CreateOrUpdateCosmosDB(ctx context.Context, groupName string, cosmosDBName string, location string, kind v1alpha1.CosmosDBKind, dbType v1alpha1.CosmosDBDatabaseAccountOfferType, dbVersion string, tags map[string]*string) (*documentdb.DatabaseAccount, error)
CreateOrUpdateCosmosDB(ctx context.Context, groupName string, cosmosDBName string, location string, kind v1alpha1.CosmosDBKind, properties v1alpha1.CosmosDBProperties, tags map[string]*string) (*documentdb.DatabaseAccount, error)

// GetCosmosDB gets a cosmos database account
GetCosmosDB(ctx context.Context, groupName string, cosmosDBName string) (*documentdb.DatabaseAccount, error)
Expand Down
10 changes: 7 additions & 3 deletions pkg/resourcemanager/cosmosdbs/cosmosdb_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,14 @@ func (m *AzureCosmosDBManager) Ensure(ctx context.Context, obj runtime.Object, o
groupName := instance.Spec.ResourceGroup
location := instance.Spec.Location
kind := instance.Spec.Kind
dbType := instance.Spec.Properties.DatabaseAccountOfferType
dbVersion := instance.Spec.Properties.MongoDBVersion

db, err = m.CreateOrUpdateCosmosDB(ctx, groupName, accountName, location, kind, dbType, dbVersion, tags)
cosmosDBProperties := v1alpha1.CosmosDBProperties{
DatabaseAccountOfferType: instance.Spec.Properties.DatabaseAccountOfferType,
EnableMultipleWriteLocations: instance.Spec.Properties.EnableMultipleWriteLocations,
MongoDBVersion: instance.Spec.Properties.MongoDBVersion,
}

db, err = m.CreateOrUpdateCosmosDB(ctx, groupName, accountName, location, kind, cosmosDBProperties, tags)
if err != nil {
azerr := errhelp.NewAzureErrorAzureError(err)
instance.Status.Message = err.Error()
Expand Down

0 comments on commit e67b110

Please sign in to comment.