-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add param checking to resource (#19134)
- Loading branch information
1 parent
c7d5742
commit dafcd2b
Showing
5 changed files
with
141 additions
and
2 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
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
97 changes: 97 additions & 0 deletions
97
sdk/resourcemanager/Proto.Client/src/Scenarios/CheckResourceGroupContainerAsync.cs
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,97 @@ | ||
using Azure.ResourceManager.Core; | ||
using Proto.Compute; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Proto.Client | ||
{ | ||
class CheckResourceGroupContainerAsync : Scenario | ||
{ | ||
public CheckResourceGroupContainerAsync() : base() { } | ||
|
||
public CheckResourceGroupContainerAsync(ScenarioContext context) : base(context) { } | ||
|
||
public override void Execute() | ||
{ | ||
ExecuteAsync().ConfigureAwait(false).GetAwaiter().GetResult(); | ||
} | ||
|
||
private async System.Threading.Tasks.Task ExecuteAsync() | ||
{ | ||
var client = new AzureResourceManagerClient(); | ||
var subscription = client.GetSubscriptionOperations(Context.SubscriptionId); | ||
|
||
// Create Resource Group | ||
Console.WriteLine($"--------Start create group {Context.RgName}--------"); | ||
var resourceGroup = subscription.GetResourceGroupContainer().Construct(Context.Loc).CreateOrUpdate(Context.RgName).Value; | ||
CleanUp.Add(resourceGroup.Id); | ||
var rgOps = subscription.GetResourceGroupOperations(Context.RgName); | ||
var resourceGroupContainer = subscription.GetResourceGroupContainer(); | ||
var rg = new Azure.ResourceManager.Resources.Models.ResourceGroup("East US"); | ||
var resourceGroupData = new ResourceGroupData(rg); | ||
|
||
ShouldThrow<ArgumentNullException>( | ||
() => resourceGroupContainer.Construct(null), | ||
"Construct with null loc didn't throw", | ||
"Construct"); | ||
|
||
ShouldThrow<ArgumentNullException>( | ||
() => resourceGroupContainer.CreateOrUpdate("test", null), | ||
"CreateOrUpdate with null resourceGroupData didn't throw", | ||
"CreateOrUpdate"); | ||
|
||
await ShouldThrowAsync<ArgumentException>( | ||
async () => await resourceGroupContainer.CreateOrUpdateAsync(" ", resourceGroupData), | ||
"CreateOrUpdateAsync with whitespaces only string didn't throw", | ||
"CreateOrUpdateAsync"); | ||
|
||
ShouldThrow<ArgumentNullException>( | ||
() => resourceGroupContainer.StartCreateOrUpdate("test", null), | ||
"StartCreateOrUpdate with null ResourceGroupData didn't throw", | ||
"StartCreateOrUpdate"); | ||
|
||
await ShouldThrowAsync<ArgumentException>( | ||
async () => await resourceGroupContainer.StartCreateOrUpdateAsync(" ", resourceGroupData), | ||
"StartCreateOrUpdateAsync with whitespaces only string didn't throw", | ||
"StartCreateOrUpdateAsync"); | ||
|
||
ShouldThrow<ArgumentException>( | ||
() => resourceGroupContainer.Get(null), | ||
"Get with null string didn't throw", | ||
"Get"); | ||
|
||
await ShouldThrowAsync<ArgumentException>( | ||
async () => await resourceGroupContainer.GetAsync(" "), | ||
"GetAsync with whitespaces only string didn't throw", | ||
"GetAsync"); | ||
|
||
Console.WriteLine("--------Done--------"); | ||
} | ||
|
||
private static void ShouldThrow<T>(Action lambda, string failMessage, string method) | ||
{ | ||
try | ||
{ | ||
lambda(); | ||
throw new Exception(failMessage); | ||
} | ||
catch (Exception e) when (e.GetType() == typeof(T)) | ||
{ | ||
Console.WriteLine($"{method} Exception was thrown as expected."); | ||
} | ||
} | ||
|
||
private static async Task ShouldThrowAsync<T>(Func<Task> lambda, string failMessage, string method) | ||
{ | ||
try | ||
{ | ||
await lambda(); | ||
throw new Exception(failMessage); | ||
} | ||
catch (Exception e) when (e.GetType() == typeof(T)) | ||
{ | ||
Console.WriteLine($"{method} Exception was thrown as expected."); | ||
} | ||
} | ||
} | ||
} |