Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hello world sample for ACR #20050

Merged
merged 6 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
annelo-msft marked this conversation as resolved.
Show resolved Hide resolved

// 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
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.