This repository was archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
update demo code #1
Open
yungezz
wants to merge
3
commits into
mgmt_one-sdk-demo
Choose a base branch
from
yungez-test1
base: mgmt_one-sdk-demo
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
src/main/java/com/microsoft/azure/azuresdktest/Demo.java
This file contains hidden or 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,106 @@ | ||
| package com.microsoft.azure.azuresdktest; | ||
|
|
||
| import com.azure.core.credential.TokenCredential; | ||
| import com.azure.core.http.policy.HttpLogDetailLevel; | ||
| import com.azure.core.http.rest.PagedIterable; | ||
| import com.azure.core.http.HttpClient; | ||
| import com.azure.core.http.netty.NettyAsyncHttpClientBuilder; | ||
| import com.azure.core.management.AzureEnvironment; | ||
| import com.azure.core.util.logging.ClientLogger; | ||
| import com.azure.core.util.polling.SyncPoller; | ||
| import com.azure.identity.DefaultAzureCredentialBuilder; | ||
| import com.azure.identity.EnvironmentCredentialBuilder; | ||
| import com .azure.resourcemanager.Azure; | ||
| import com.azure.resourcemanager.keyvault.models.Vault; | ||
| import com.azure.resourcemanager.resources.fluentcore.arm.Region; | ||
| import com.azure.resourcemanager.resources.fluentcore.profile.AzureProfile; | ||
| import com.azure.resourcemanager.resources.models.ResourceGroup; | ||
| import com.azure.security.keyvault.keys.KeyClient; | ||
| import com.azure.security.keyvault.keys.KeyClientBuilder; | ||
| import com.azure.security.keyvault.keys.models.CreateRsaKeyOptions; | ||
| import com.azure.security.keyvault.keys.models.DeletedKey; | ||
| import com.azure.security.keyvault.keys.models.KeyOperation; | ||
| import com.azure.security.keyvault.keys.models.KeyProperties; | ||
| import com.azure.security.keyvault.keys.models.KeyVaultKey; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
| public class Demo { | ||
|
|
||
| private static final ClientLogger logger = new ClientLogger(Demo.class); | ||
|
|
||
| public static void main(String args[]) { | ||
| String resourceGroupName = "Demo0706"; | ||
| String vaultName = "Vault0706"; | ||
|
|
||
| // Httpclient in azure-core, will be used in both management and data SDKs | ||
| HttpClient httpClient = new NettyAsyncHttpClientBuilder().build(); | ||
|
|
||
| // TokenCredential in azure-identity, will be used for auth both in management and data operation | ||
| TokenCredential credential = new EnvironmentCredentialBuilder().build(); | ||
| AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE); | ||
|
|
||
| // management | ||
| logger.info("Creating key vault.."); | ||
| Azure azure = Azure.configure() | ||
| .withLogLevel(HttpLogDetailLevel.NONE) | ||
| // same http client | ||
| .withHttpClient(httpClient) | ||
| // same credential | ||
| .authenticate(credential, profile) | ||
| .withSubscription(System.getenv(("AZURE_SUBSCRIPTION_ID"))); | ||
|
|
||
| try { | ||
| // management - create key vault | ||
| Vault vault = azure.vaults() | ||
| .define(vaultName) | ||
| .withRegion(Region.US_WEST) | ||
| .withNewResourceGroup(resourceGroupName) | ||
| .defineAccessPolicy() | ||
| .forServicePrincipal(System.getenv("AZURE_CLIENT_ID")) | ||
| .allowKeyAllPermissions() | ||
| .attach() | ||
| .create(); | ||
|
|
||
| // management - pagination on list resource groups | ||
| PagedIterable<ResourceGroup> resourceGroups = azure.resourceGroups().list(); | ||
| resourceGroups.stream().forEach(r -> logger.info("Resource group: {}", r.id())); | ||
|
|
||
| // management - pagination on list vaults | ||
| PagedIterable<Vault> vaults = azure.vaults().listByResourceGroup(resourceGroupName); | ||
| vaults.stream().forEach(v -> logger.info("Vault uri {}", v.vaultUri())); | ||
|
|
||
| // data - create client | ||
| KeyClient keyClient = new KeyClientBuilder() | ||
| // same http client | ||
| .httpClient(httpClient) | ||
| // same credential | ||
| .credential(credential) | ||
| .vaultUrl(vault.vaultUri()) | ||
| .buildClient(); | ||
|
|
||
| // data - create rsa key | ||
| KeyVaultKey key = keyClient.createRsaKey(new CreateRsaKeyOptions("key1") | ||
| .setKeySize(2048) | ||
| .setKeyOperations(KeyOperation.UNWRAP_KEY, KeyOperation.WRAP_KEY, KeyOperation.DECRYPT, KeyOperation.ENCRYPT)); | ||
|
|
||
| // data - pagination on list keys | ||
| PagedIterable<KeyProperties> keys = keyClient.listPropertiesOfKeys(); | ||
| keys.stream().forEach(k -> logger.info("Key id {}", k.getId())); | ||
|
|
||
| // data - LRO on delete key | ||
| SyncPoller<DeletedKey, Void> deleteKeyPoller = keyClient.beginDeleteKey(key.getName()); | ||
| deleteKeyPoller.waitForCompletion(); | ||
|
|
||
| // management - LRO on delete resource group | ||
| SyncPoller<Void, Void> syncPoller = azure.resourceGroups().beginDeleteByName(resourceGroupName).getSyncPoller(); | ||
| Thread.sleep(Duration.ofSeconds(3).toMillis()); | ||
| syncPoller.poll(); | ||
| } catch (Throwable e) { | ||
| logger.logThrowableAsError(e); | ||
| } finally { | ||
| // clean up | ||
| azure.resourceGroups().beginDeleteByName(resourceGroupName); | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra space? "import com .azure.resourcemanager.Azure;"