Skip to content

Commit

Permalink
Add hello world sample for ACR (#20050)
Browse files Browse the repository at this point in the history
* 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
annelo-msft authored Apr 2, 2021
1 parent 8255b61 commit f7ca071
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 4 deletions.
74 changes: 70 additions & 4 deletions sdk/containerregistry/Azure.Containers.ContainerRegistry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ The [Azure Identity library][identity] provides easy Azure Active Directory supp

```C#
// Create a ContainerRegistryClient that will authenticate through Active Directory
Uri registryUri = new Uri("https://MYCONTAINERREGISTRY.azurecr.io/");
ContainerRegistryClient client = new ContainerRegistryClient(registryUri, new DefaultAzureCredential());
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT"));
ContainerRegistryClient client = new ContainerRegistryClient(endpoint, new DefaultAzureCredential());
```

## Key concepts
Expand All @@ -66,16 +66,82 @@ We guarantee that all client instance methods are thread-safe and independent of

## Examples

<!-- Pending Sample Creation -->
### Sync examples

- [List repositories](#list-repositories)

### Async examples

- [List repositories asynchronously](#list-repositories-asynchronously)

### List repositories

Iterate through the collection of repositories in the registry.

```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);
}
```

### List repositories asynchronously

The asynchronous APIs are identical to their synchronous counterparts, but methods end with the standard .NET "Async" suffix and return a Task.

```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);
}
```

## Troubleshooting

All container registry service operations will throw a
[RequestFailedException][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.");
}
```

You can also easily [enable console logging](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core/samples/Diagnostics.md#logging) if you want to dig
deeper into the requests you're making against the service.

## Next steps

<!-- Pending Sample Creation -->
- Go further with Azure.Containers.ContainerRegistry and our [samples][samples]
- Watch a [demo or deep dive video](https://azure.microsoft.com/resources/videos/index/?service=container-registry)
- Read more about the [Azure Container Registry service](https://docs.microsoft.com/azure/container-registry/container-registry-intro)

## Contributing

Expand Down
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).
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.");
}
```
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.");
}
```
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
}
}
}

0 comments on commit f7ca071

Please sign in to comment.