-
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 hello world sample for ACR (#20050)
* Add hello world samples * update readme with initial samples * more readme updates * fix link * update sample to use SamplesBase * remove recorded samples
- Loading branch information
1 parent
8255b61
commit f7ca071
Showing
5 changed files
with
287 additions
and
4 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
14 changes: 14 additions & 0 deletions
14
sdk/containerregistry/Azure.Containers.ContainerRegistry/samples/README.md
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,14 @@ | ||
--- | ||
page_type: sample | ||
languages: | ||
- csharp | ||
products: | ||
- azure | ||
- azure-container-registry | ||
name: Azure.Containers.ContainerRegistry samples for .NET | ||
description: Samples for the Azure.Containers.ContainerRegistry client library | ||
--- | ||
|
||
# Azure.Containers.ContainerRegistry Samples | ||
|
||
- Get started either [synchronously](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/containerregistry/Azure.Containers.ContainerRegistry/samples/Sample01a_HelloWorld.md) or [asynchronously](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/containerregistry/Azure.Containers.ContainerRegistry/samples/Sample01b_HelloWorldAsync.md). |
47 changes: 47 additions & 0 deletions
47
...inerregistry/Azure.Containers.ContainerRegistry/samples/Sample01a_HelloWorld.md
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,47 @@ | ||
# Azure.Containers.ContainerRegistry Samples - Hello World (sync) | ||
|
||
## Import the namespaces | ||
|
||
```C# Snippet:ContainerRegistry_Tests_Samples_Namespaces | ||
using Azure.Containers.ContainerRegistry; | ||
``` | ||
|
||
## Create a client | ||
|
||
Create a `ContainerRegistryClient` and send a request. | ||
|
||
```C# Snippet:ContainerRegistry_Tests_Samples_CreateClient | ||
// Get the service endpoint from the environment | ||
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT")); | ||
|
||
// Create a new ContainerRegistryClient | ||
ContainerRegistryClient client = new ContainerRegistryClient(endpoint, new DefaultAzureCredential()); | ||
|
||
// Perform an operation | ||
Pageable<string> repositories = client.GetRepositories(); | ||
foreach (string repository in repositories) | ||
{ | ||
Console.WriteLine(repository); | ||
} | ||
``` | ||
|
||
## Handle Errors | ||
|
||
All Container Registry operations will throw a RequestFailedException on failure. | ||
|
||
```C# Snippet:ContainerRegistry_Tests_Samples_HandleErrors | ||
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT")); | ||
|
||
// Create an invalid ContainerRepositoryClient | ||
string fakeRepositoryName = "doesnotexist"; | ||
ContainerRepositoryClient client = new ContainerRepositoryClient(endpoint, fakeRepositoryName, new DefaultAzureCredential()); | ||
|
||
try | ||
{ | ||
client.GetProperties(); | ||
} | ||
catch (RequestFailedException ex) when (ex.Status == 404) | ||
{ | ||
Console.WriteLine("Repository wasn't found."); | ||
} | ||
``` |
47 changes: 47 additions & 0 deletions
47
...egistry/Azure.Containers.ContainerRegistry/samples/Sample01b_HelloWorldAsync.md
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,47 @@ | ||
# Azure.Search.Documents Samples - Hello World (async) | ||
|
||
## Import the namespaces | ||
|
||
```C# Snippet:ContainerRegistry_Tests_Samples_Namespaces | ||
using Azure.Containers.ContainerRegistry; | ||
``` | ||
|
||
## Create a client | ||
|
||
Create a `ContainerRegistryClient` and send a request. | ||
|
||
```C# Snippet:ContainerRegistry_Tests_Samples_CreateClientAsync | ||
// Get the service endpoint from the environment | ||
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT")); | ||
|
||
// Create a new ContainerRegistryClient | ||
ContainerRegistryClient client = new ContainerRegistryClient(endpoint, new DefaultAzureCredential()); | ||
|
||
// Perform an operation | ||
AsyncPageable<string> repositories = client.GetRepositoriesAsync(); | ||
await foreach (string repository in repositories) | ||
{ | ||
Console.WriteLine(repository); | ||
} | ||
``` | ||
|
||
## Handle Errors | ||
|
||
All Container Registry operations will throw a RequestFailedException on failure. | ||
|
||
```C# Snippet:ContainerRegistry_Tests_Samples_HandleErrorsAsync | ||
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT")); | ||
|
||
// Create an invalid ContainerRepositoryClient | ||
string fakeRepositoryName = "doesnotexist"; | ||
ContainerRepositoryClient client = new ContainerRepositoryClient(endpoint, fakeRepositoryName, new DefaultAzureCredential()); | ||
|
||
try | ||
{ | ||
await client.GetPropertiesAsync(); | ||
} | ||
catch (RequestFailedException ex) when (ex.Status == 404) | ||
{ | ||
Console.WriteLine("Repository wasn't found."); | ||
} | ||
``` |
109 changes: 109 additions & 0 deletions
109
...containerregistry/Azure.Containers.ContainerRegistry/tests/Samples/Sample01_HelloWorld.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,109 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Azure.Core.TestFramework; | ||
#region Snippet:ContainerRegistry_Tests_Samples_Namespaces | ||
using Azure.Containers.ContainerRegistry; | ||
#endregion Snippet:ContainerRegistry_Tests_Samples_Namespaces | ||
using Azure.Identity; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.Containers.ContainerRegistry.Tests.Samples | ||
{ | ||
public partial class HelloWorld : SamplesBase<ContainerRegistryTestEnvironment> | ||
{ | ||
[Test] | ||
[SyncOnly] | ||
public void CreateClient() | ||
{ | ||
Environment.SetEnvironmentVariable("REGISTRY_ENDPOINT", TestEnvironment.Endpoint); | ||
|
||
#region Snippet:ContainerRegistry_Tests_Samples_CreateClient | ||
// Get the service endpoint from the environment | ||
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT")); | ||
|
||
// Create a new ContainerRegistryClient | ||
ContainerRegistryClient client = new ContainerRegistryClient(endpoint, new DefaultAzureCredential()); | ||
|
||
// Perform an operation | ||
Pageable<string> repositories = client.GetRepositories(); | ||
foreach (string repository in repositories) | ||
{ | ||
Console.WriteLine(repository); | ||
} | ||
#endregion Snippet:ContainerRegistry_Tests_Samples_CreateClient | ||
} | ||
|
||
[Test] | ||
[AsyncOnly] | ||
public async Task CreateClientAsync() | ||
{ | ||
Environment.SetEnvironmentVariable("REGISTRY_ENDPOINT", TestEnvironment.Endpoint); | ||
|
||
#region Snippet:ContainerRegistry_Tests_Samples_CreateClientAsync | ||
// Get the service endpoint from the environment | ||
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT")); | ||
|
||
// Create a new ContainerRegistryClient | ||
ContainerRegistryClient client = new ContainerRegistryClient(endpoint, new DefaultAzureCredential()); | ||
|
||
// Perform an operation | ||
AsyncPageable<string> repositories = client.GetRepositoriesAsync(); | ||
await foreach (string repository in repositories) | ||
{ | ||
Console.WriteLine(repository); | ||
} | ||
#endregion Snippet:ContainerRegistry_Tests_Samples_CreateClientAsync | ||
} | ||
|
||
[Test] | ||
[SyncOnly] | ||
public void HandleErrors() | ||
{ | ||
Environment.SetEnvironmentVariable("REGISTRY_ENDPOINT", TestEnvironment.Endpoint); | ||
|
||
#region Snippet:ContainerRegistry_Tests_Samples_HandleErrors | ||
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT")); | ||
|
||
// Create an invalid ContainerRepositoryClient | ||
string fakeRepositoryName = "doesnotexist"; | ||
ContainerRepositoryClient client = new ContainerRepositoryClient(endpoint, fakeRepositoryName, new DefaultAzureCredential()); | ||
|
||
try | ||
{ | ||
client.GetProperties(); | ||
} | ||
catch (RequestFailedException ex) when (ex.Status == 404) | ||
{ | ||
Console.WriteLine("Repository wasn't found."); | ||
} | ||
#endregion Snippet:ContainerRegistry_Tests_Samples_HandleErrors | ||
} | ||
|
||
[Test] | ||
[AsyncOnly] | ||
public async Task HandleErrorsAsync() | ||
{ | ||
Environment.SetEnvironmentVariable("REGISTRY_ENDPOINT", TestEnvironment.Endpoint); | ||
|
||
#region Snippet:ContainerRegistry_Tests_Samples_HandleErrorsAsync | ||
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT")); | ||
|
||
// Create an invalid ContainerRepositoryClient | ||
string fakeRepositoryName = "doesnotexist"; | ||
ContainerRepositoryClient client = new ContainerRepositoryClient(endpoint, fakeRepositoryName, new DefaultAzureCredential()); | ||
|
||
try | ||
{ | ||
await client.GetPropertiesAsync(); | ||
} | ||
catch (RequestFailedException ex) when (ex.Status == 404) | ||
{ | ||
Console.WriteLine("Repository wasn't found."); | ||
} | ||
#endregion Snippet:ContainerRegistry_Tests_Samples_HandleErrorsAsync | ||
} | ||
} | ||
} |