Skip to content

Commit 00a4630

Browse files
authored
Add Support for AzureChinaCloud - Fixes #365 (#367)
* Added support for AzureChinaCloud * Fix daily build * Fix changelog
1 parent 853760a commit 00a4630

6 files changed

Lines changed: 112 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Added support for AzureChinaCloud (Mooncake) - Fixes [Issue #365](https://github.com/PlagueHO/CosmosDB/issues/365).
13+
14+
### Changed
15+
16+
- Fix daily build by preventing deployment stage from running on
17+
anything build not named `*.master` - Fixes [Issue #366](https://github.com/PlagueHO/CosmosDB/issues/366).
18+
1019
## [4.0.0] - 2020-05-11
1120

1221
### Changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- [Working with Contexts](#working-with-contexts)
2525
- [Create a Context specifying the Key Manually](#create-a-context-specifying-the-key-manually)
2626
- [Create a Context for a Cosmos DB in Azure US Government Cloud](#create-a-context-for-a-cosmos-db-in-azure-us-government-cloud)
27+
- [Create a Context for a Cosmos DB in Azure China Cloud (Mooncake)](#create-a-context-for-a-cosmos-db-in-azure-china-cloud-mooncake)
2728
- [Use CosmosDB Module to Retrieve Key from Azure Management Portal](#use-cosmosdb-module-to-retrieve-key-from-azure-management-portal)
2829
- [Create a Context for a Cosmos DB Emulator](#create-a-context-for-a-cosmos-db-emulator)
2930
- [Create a Context from Resource Authorization Tokens](#create-a-context-from-resource-authorization-tokens)
@@ -152,6 +153,16 @@ create a context variable and set the `Environment` parameter to
152153
$cosmosDbContext = New-CosmosDbContext -Account 'MyAzureCosmosDB' -Database 'MyDatabase' -Key $primaryKey -Environment AzureUSGovernment
153154
```
154155

156+
#### Create a Context for a Cosmos DB in Azure China Cloud (Mooncake)
157+
158+
Use the key secure string, Azure Cosmos DB account name and database to
159+
create a context variable and set the `Environment` parameter to
160+
`AzureChinaCloud`:
161+
162+
```powershell
163+
$cosmosDbContext = New-CosmosDbContext -Account 'MyAzureCosmosDB' -Database 'MyDatabase' -Key $primaryKey -Environment AzureChinaCloud
164+
```
165+
155166
#### Use CosmosDB Module to Retrieve Key from Azure Management Portal
156167

157168
To create a context object so that the _CosmosDB PowerShell module_

azure-pipelines.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,8 @@ stages:
562562
eq(variables['Build.SourceBranch'], 'refs/heads/master'),
563563
startsWith(variables['Build.SourceBranch'], 'refs/tags/')
564564
),
565-
eq(variables['System.TeamFoundationCollectionUri'], 'https://dscottraynsford.visualstudio.com/')
565+
eq(variables['System.TeamFoundationCollectionUri'], 'https://dscottraynsford.visualstudio.com/'),
566+
endsWith(variables['Build.DefinitionName'],'master')
566567
)
567568
jobs:
568569
- job: Deploy_Module

source/Private/utils/Get-CosmosDbUri.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ function Get-CosmosDbUri
2828
{
2929
$BaseUri = 'documents.azure.us'
3030
}
31+
32+
'AzureChinaCloud'
33+
{
34+
$BaseUri = 'documents.azure.cn'
35+
}
3136
}
3237
}
3338

source/classes/CosmosDB/CosmosDB.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CosmosDB {
44
public enum Environment {
5+
AzureChinaCloud,
56
AzureCloud,
67
AzureUSGovernment
78
}

tests/Unit/CosmosDB.utils.Tests.ps1

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ InModuleScope $ProjectName {
3131
$script:testBaseUri = 'documents.contoso.com'
3232
$script:testBaseUriAzureCloud = 'documents.azure.com'
3333
$script:testBaseUriAzureUsGov = 'documents.azure.us'
34+
$script:testBaseUriAzureChinaCloud = 'documents.azure.cn'
3435
$script:testDate = (Get-Date -Year 2017 -Month 11 -Day 29 -Hour 10 -Minute 45 -Second 10)
3536
$script:testUniversalDate = 'Tue, 28 Nov 2017 21:45:10 GMT'
3637
$script:testContext = [CosmosDb.Context] @{
@@ -261,6 +262,30 @@ console.log("done");
261262
}
262263
}
263264

265+
Context 'When called with Account and Environment AzureChinaCloud parameters' {
266+
$script:result = $null
267+
268+
It 'Should not throw exception' {
269+
$newCosmosDbContextParameters = @{
270+
Account = $script:testAccount
271+
Database = $script:testDatabase
272+
Key = $script:testKeySecureString
273+
KeyType = 'master'
274+
Environment = 'AzureChinaCloud'
275+
}
276+
277+
{ $script:result = New-CosmosDbContext @newCosmosDbContextParameters } | Should -Not -Throw
278+
}
279+
280+
It 'Should return expected result' {
281+
$script:result.Account | Should -Be $script:testAccount
282+
$script:result.Database | Should -Be $script:testDatabase
283+
$script:result.Key | Should -Be $script:testKeySecureString
284+
$script:result.KeyType | Should -Be 'master'
285+
$script:result.BaseUri | Should -Be ('https://{0}.{1}/' -f $script:testAccount, $script:testBaseUriAzureChinaCloud)
286+
}
287+
}
288+
264289
Context 'When called with Account parameters and Back-off Policy' {
265290
$script:result = $null
266291

@@ -375,6 +400,46 @@ console.log("done");
375400
}
376401
}
377402

403+
Context 'When called with AzureAccount and Environment AzureChinaCloud parameters and not connected to Azure and PrimaryMasterKey requested' {
404+
$script:result = $null
405+
406+
Mock -CommandName Get-AzContext -MockWith { throw }
407+
Mock -CommandName Connect-AzAccount
408+
Mock `
409+
-CommandName Get-CosmosDbAccountMasterKey `
410+
-MockWith { $script:testKeySecureString }
411+
412+
It 'Should not throw exception' {
413+
$newCosmosDbContextParameters = @{
414+
Account = $script:testAccount
415+
Database = $script:testDatabase
416+
ResourceGroupName = $script:testResourceGroupName
417+
MasterKeyType = 'PrimaryMasterKey'
418+
Environment = 'AzureChinaCloud'
419+
}
420+
421+
{ $script:result = New-CosmosDbContext @newCosmosDbContextParameters } | Should -Not -Throw
422+
}
423+
424+
It 'Should return expected result' {
425+
$script:result.Account | Should -Be $script:testAccount
426+
$script:result.Database | Should -Be $script:testDatabase
427+
$script:result.KeyType | Should -Be 'master'
428+
$script:result.Key | Convert-CosmosDbSecureStringToString | Should -Be $script:testKey
429+
$script:result.BaseUri | Should -Be ('https://{0}.{1}/' -f $script:testAccount, $script:testBaseUriAzureChinaCloud)
430+
}
431+
432+
It 'Should call expected mocks' {
433+
Assert-MockCalled -CommandName Get-AzContext -Exactly -Times 1
434+
Assert-MockCalled -CommandName Connect-AzAccount `
435+
-ParameterFilter { $Environment -eq 'AzureChinaCloud' } `
436+
-Exactly -Times 1
437+
Assert-MockCalled -CommandName Get-CosmosDbAccountMasterKey `
438+
-ParameterFilter { $MasterKeyType -eq 'PrimaryMasterKey' } `
439+
-Exactly -Times 1
440+
}
441+
}
442+
378443
Context 'When called with AzureAccount parameters and connected to Azure' {
379444
$script:result = $null
380445

@@ -571,6 +636,25 @@ console.log("done");
571636
$script:result.ToString() | Should -Be ('https://{0}.{1}/' -f $script:testAccount, $script:testBaseUriAzureUsGov)
572637
}
573638
}
639+
640+
Context 'When called with Account and AzureChinaCloud Environment parameters' {
641+
$script:result = $null
642+
643+
It 'Should not throw exception' {
644+
$GetCosmosDbUriParameters = @{
645+
Account = $script:testAccount
646+
Environment = [CosmosDb.Environment]::AzureChinaCloud
647+
Verbose = $true
648+
}
649+
650+
{ $script:result = Get-CosmosDbUri @GetCosmosDbUriParameters } | Should -Not -Throw
651+
}
652+
653+
It 'Should return expected result' {
654+
$script:result | Should -BeOfType uri
655+
$script:result.ToString() | Should -Be ('https://{0}.{1}/' -f $script:testAccount, $script:testBaseUriAzureChinaCloud)
656+
}
657+
}
574658
}
575659

576660
Describe 'ConvertTo-CosmosDbTokenDateString' -Tag 'Unit' {

0 commit comments

Comments
 (0)