Skip to content

Commit 1d75331

Browse files
authored
[keyvault] add more samples (Azure#23123)
1 parent 3339bc6 commit 1d75331

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

sdk/security/keyvault/azcertificates/example_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ package azcertificates_test
88

99
import (
1010
"context"
11+
"encoding/base64"
1112
"fmt"
13+
"os"
1214

1315
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
1416
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
@@ -94,3 +96,54 @@ func ExampleClient_DeleteCertificate() {
9496
// In a soft-delete enabled vault, deleted resources can be recovered until they're purged (permanently deleted).
9597
fmt.Printf("Certificate will be purged at %v", *resp.ScheduledPurgeDate)
9698
}
99+
100+
// This example uses `ImportCertificate` to import an existing PFX certificate.
101+
func ExampleClient_ImportCertificate_pfx() {
102+
// Getting certificate data from locally stored PFX file then encoding to base64
103+
data, err := os.ReadFile("PFX_CERT_PATH")
104+
if err != nil {
105+
// TODO: handle error
106+
}
107+
encodedData := base64.StdEncoding.EncodeToString(data)
108+
109+
parameters := azcertificates.ImportCertificateParameters{
110+
Base64EncodedCertificate: to.Ptr(encodedData),
111+
CertificatePolicy: &azcertificates.CertificatePolicy{
112+
SecretProperties: &azcertificates.SecretProperties{
113+
ContentType: to.Ptr("application/x-pkcs12"),
114+
},
115+
},
116+
}
117+
118+
resp, err := client.ImportCertificate(context.TODO(), "pfxCertName", parameters, nil)
119+
if err != nil {
120+
// TODO: handle error
121+
}
122+
123+
fmt.Printf("PFX certificate %s imported successfully.", resp.ID.Name())
124+
}
125+
126+
// This example uses `ImportCertificate` to import an existing PEM certificate.
127+
func ExampleClient_ImportCertificate_pem() {
128+
// Getting certificate data from locally stored PEM file. Contents of .pem file are already base64 encoded.
129+
data, err := os.ReadFile("PEM_CERT_PATH")
130+
if err != nil {
131+
// TODO: handle error
132+
}
133+
134+
parameters := azcertificates.ImportCertificateParameters{
135+
Base64EncodedCertificate: to.Ptr(string(data)),
136+
CertificatePolicy: &azcertificates.CertificatePolicy{
137+
SecretProperties: &azcertificates.SecretProperties{
138+
ContentType: to.Ptr("application/x-pem-file"),
139+
},
140+
},
141+
}
142+
143+
resp, err := client.ImportCertificate(context.TODO(), "pemCertName", parameters, nil)
144+
if err != nil {
145+
// TODO: handle error
146+
}
147+
148+
fmt.Printf("PEM certificate %s imported successfully.", resp.ID.Name())
149+
}

0 commit comments

Comments
 (0)