Skip to content

Commit

Permalink
Add error return to Key Vault client constructors (#19491)
Browse files Browse the repository at this point in the history
  • Loading branch information
chlowell authored Nov 4, 2022
1 parent 21341c2 commit 19034cd
Show file tree
Hide file tree
Showing 21 changed files with 107 additions and 652 deletions.
9 changes: 2 additions & 7 deletions sdk/keyvault/azcertificates/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
# Release History

## 0.7.2 (Unreleased)

### Features Added
## 0.8.0 (2022-11-08)

### Breaking Changes

### Bugs Fixed

### Other Changes
* `NewClient` returns an `error`

## 0.7.1 (2022-09-20)

Expand Down
198 changes: 5 additions & 193 deletions sdk/keyvault/azcertificates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ func main() {
// TODO: handle error
}

client := azcertificates.NewClient("https://<TODO: your vault name>.vault.azure.net", credential, nil)
client, err := azcertificates.NewClient("https://<TODO: your vault name>.vault.azure.net", credential, nil)
if err != nil {
// TODO: handle error
}
}
```

Expand All @@ -60,198 +63,7 @@ illustrated in the [examples](#examples) below.

## Examples

This section contains code snippets covering common tasks:
* [Create a Certificate](#create-a-certificate)
* [Delete a Certificate](#delete-a-certificate)
* [List Certificates](#list-certificates)
* [Retrieve a Certificate](#retrieve-a-certificate)
* [Update Certificate Metadata](#update-certificate-metadata)

### Create a Certificate

[CreateCertificate](https://aka.ms/azsdk/go/keyvault-certificates/docs#Client.CreateCertificate)
creates a certificate to be stored in the key vault. If a certificate with the same name already exists, a new
version of the certificate is created.

```go
import (
"context"
"fmt"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/keyvault/azcertificates"
)

func main() {
credential, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
// TODO: handle error
}
client := azcertificates.NewClient("https://<TODO: your vault name>.vault.azure.net", credential, nil)

createParams := azcertificates.CreateCertificateParameters{
// this policy is suitable for a self-signed certificate
CertificatePolicy: &azcertificates.CertificatePolicy{
IssuerParameters: &azcertificates.IssuerParameters{Name: to.Ptr("self")},
X509CertificateProperties: &azcertificates.X509CertificateProperties{Subject: to.Ptr("CN=DefaultPolicy")},
},
}
resp, err := client.CreateCertificate(context.TODO(), "certificateName", createParams, nil)
if err != nil {
// TODO: handle error
}

fmt.Println("Created a certificate with ID:", *resp.ID)
}
```

### Retrieve a Certificate

[GetCertificate](https://aka.ms/azsdk/go/keyvault-certificates/docs#Client.GetCertificate)
retrieves the latest version of a certificate previously stored in the Key Vault.

```go
import (
"context"
"fmt"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/keyvault/azcertificates"
)

func main() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
// TODO: handle error
}

client, err := azcertificates.NewClient("https://<TODO: your vault name>.vault.azure.net", cred, nil)
if err != nil {
// TODO: handle error
}

// passing an empty string for the version gets the latest version of the certificate
resp, err := client.GetCertificate(context.TODO(), "certName", "", nil)
if err != nil {
// TODO: handle error
}
fmt.Println(*resp.ID)
}
```


### Update Certificate metadata

[UpdateCertificate](https://aka.ms/azsdk/go/keyvault-certificates/docs#Client.UpdateCertificate)
updates a certificate's metadata.

```go
import (
"context"
"fmt"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/keyvault/azcertificates"
)

func main() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
// TODO: handle error
}

client, err := azcertificates.NewClient("https://<TODO: your vault name>.vault.azure.net", cred, nil)
if err != nil {
// TODO: handle error
}

updateParams := azcertificates.UpdateCertificateParameters{
CertificateAttributes: &azcertificates.CertificateAttributes{Enabled: to.Ptr(false)},
}
// passing an empty string for the version updates the latest version of the certificate
resp, err := client.UpdateCertificate(context.TODO(), "certName", "", updateParams, nil)
if err != nil {
// TODO: handle error
}
fmt.Println(*resp.ID)
}
```

### Delete a Certificate

[DeleteCertificate](https://aka.ms/azsdk/go/keyvault-certificates/docs#Client.DeleteCertificate) requests that Key Vault delete a certificate. It returns when Key Vault has begun deleting the certificate. Deletion can take several seconds to complete, so it may be necessary to wait before performing other operations on the deleted certificate.

```go
import (
"context"
"fmt"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/keyvault/azcertificates"
)

func main() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
// TODO: handle error
}

client, err := azcertificates.NewClient("https://<TODO: your vault name>.vault.azure.net", cred, nil)
if err != nil {
// TODO: handle error
}

// DeleteCertificate returns when Key Vault has begun deleting the certificate. That can take several
// seconds to complete, so it may be necessary to wait before performing other operations on the
// deleted certificate.
resp, err := client.DeleteCertificate(context.TODO(), "certName", nil)
if err != nil {
// TODO: handle error
}

// In a soft-delete enabled vault, deleted resources can be recovered until they're purged (permanently deleted).
fmt.Printf("Certificate will be purged at %v", *resp.ScheduledPurgeDate)
}
```

### List Certificates

[NewListCertificatesPager](https://aka.ms/azsdk/go/keyvault-certificates/docs#Client.NewListCertificatesPager) creates a pager that lists all certificates in the vault.

```go
import (
"context"
"fmt"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/keyvault/azcertificates"
)

func main() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
// TODO: handle error
}

client, err := azcertificates.NewClient("https://<TODO: your vault name>.vault.azure.net", cred, nil)
if err != nil {
// TODO: handle error
}

pager := client.NewListCertificatesPager(nil)
for pager.More() {
page, err := pager.NextPage(context.TODO())
if err != nil {
// TODO: handle error
}
for _, cert := range page.Value {
fmt.Println(*cert.ID)
}
}
}
```
Get started with our [examples](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/keyvault/azcertificates#pkg-examples).

## Troubleshooting

Expand Down
5 changes: 3 additions & 2 deletions sdk/keyvault/azcertificates/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,10 @@ func TestDisableChallengeResourceVerification(t *testing.T) {
},
DisableChallengeResourceVerification: test.disableVerify,
}
client := azcertificates.NewClient(vaultURL, &FakeCredential{}, options)
client, err := azcertificates.NewClient(vaultURL, &FakeCredential{}, options)
require.NoError(t, err)
pager := client.NewListCertificatesPager(nil)
_, err := pager.NextPage(context.Background())
_, err = pager.NextPage(context.Background())
if test.err {
require.Error(t, err)
require.Contains(t, err.Error(), "challenge resource")
Expand Down
4 changes: 2 additions & 2 deletions sdk/keyvault/azcertificates/custom_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type ClientOptions struct {

// NewClient creates a client that accesses a Key Vault's certificates. You should validate that
// vaultURL references a valid Key Vault. See https://aka.ms/azsdk/blog/vault-uri for details.
func NewClient(vaultURL string, credential azcore.TokenCredential, options *ClientOptions) *Client {
func NewClient(vaultURL string, credential azcore.TokenCredential, options *ClientOptions) (*Client, error) {
if options == nil {
options = &ClientOptions{}
}
Expand All @@ -38,7 +38,7 @@ func NewClient(vaultURL string, credential azcore.TokenCredential, options *Clie
},
)
pl := runtime.NewPipeline(moduleName, version, runtime.PipelineOptions{PerRetry: []policy.Policy{authPolicy}}, &options.ClientOptions)
return &Client{endpoint: vaultURL, pl: pl}
return &Client{endpoint: vaultURL, pl: pl}, nil
}

// ID is a certificate's unique ID, containing its name and version.
Expand Down
6 changes: 5 additions & 1 deletion sdk/keyvault/azcertificates/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ func ExampleNewClient() {
// TODO: handle error
}
vaultURL := "https://<TODO: your vault name>.vault.azure.net"
client := azcertificates.NewClient(vaultURL, cred, nil)
client, err := azcertificates.NewClient(vaultURL, cred, nil)
if err != nil {
// TODO: handle error
}

_ = client
}
Expand All @@ -36,6 +39,7 @@ func ExampleClient_CreateCertificate() {
X509CertificateProperties: &azcertificates.X509CertificateProperties{Subject: to.Ptr("CN=DefaultPolicy")},
},
}
// if a certificate with the same name already exists, a new version of the certificate is created
resp, err := client.CreateCertificate(context.TODO(), "certificateName", createParams, nil)
if err != nil {
// TODO: handle error
Expand Down
9 changes: 7 additions & 2 deletions sdk/keyvault/azcertificates/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ func TestMain(m *testing.M) {
// will be fast because the tests which created these certs requested their
// deletion. Now, at the end of the run, Key Vault will have finished deleting
// most of them...
client := azcertificates.NewClient(vaultURL, credential, nil)
client, err := azcertificates.NewClient(vaultURL, credential, nil)
if err != nil {
panic(err)
}
for _, name := range certsToPurge.names {
// ...but we need a retry loop for the others. Note this wouldn't benefit
// from client-side parallelization because Key Vault's delete operations
Expand Down Expand Up @@ -119,7 +122,9 @@ func startTest(t *testing.T) *azcertificates.Client {
transport, err := recording.NewRecordingHTTPClient(t, nil)
require.NoError(t, err)
opts := &azcertificates.ClientOptions{ClientOptions: azcore.ClientOptions{Transport: transport}}
return azcertificates.NewClient(vaultURL, credential, opts)
client, err := azcertificates.NewClient(vaultURL, credential, opts)
require.NoError(t, err)
return client
}

func getName(t *testing.T, prefix string) string {
Expand Down
2 changes: 1 addition & 1 deletion sdk/keyvault/azcertificates/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ package azcertificates

const (
moduleName = "azcertificates"
version = "v0.7.2"
version = "v0.8.0"
)
9 changes: 2 additions & 7 deletions sdk/keyvault/azkeys/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
# Release History

## 0.8.2 (Unreleased)

### Features Added
## 0.9.0 (2022-11-08)

### Breaking Changes

### Bugs Fixed

### Other Changes
* `NewClient` returns an `error`

## 0.8.1 (2022-09-20)

Expand Down
Loading

0 comments on commit 19034cd

Please sign in to comment.