1
- /**
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the MIT License. See License.txt in the project root for
4
- * license information.
5
- */
6
-
7
- package com .microsoft .azure .management .cosmosdb .samples ;
8
-
9
- import com .microsoft .azure .CloudException ;
10
- import com .microsoft .azure .credentials .ApplicationTokenCredentials ;
11
- import com .microsoft .azure .management .Azure ;
12
- import com .microsoft .azure .management .cosmosdb .DatabaseAccountKind ;
13
- import com .microsoft .azure .management .cosmosdb .CosmosDBAccount ;
14
- import com .microsoft .azure .management .resources .fluentcore .arm .Region ;
15
- import com .microsoft .azure .management .resources .fluentcore .utils .SdkContext ;
16
- import com .microsoft .azure .management .samples .Utils ;
17
- import com .microsoft .rest .LogLevel ;
18
-
19
- import java .io .File ;
1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
3
+
4
+ package com .azure .resourcemanager .cosmos .samples ;
5
+
6
+ import com .azure .core .credential .TokenCredential ;
7
+ import com .azure .core .http .policy .HttpLogDetailLevel ;
8
+ import com .azure .core .management .AzureEnvironment ;
9
+ import com .azure .core .management .exception .ManagementException ;
10
+ import com .azure .identity .DefaultAzureCredentialBuilder ;
11
+ import com .azure .resourcemanager .AzureResourceManager ;
12
+ import com .azure .resourcemanager .cosmos .models .CosmosDBAccount ;
13
+ import com .azure .resourcemanager .cosmos .models .DatabaseAccountKind ;
14
+ import com .azure .core .management .Region ;
15
+ import com .azure .core .management .profile .AzureProfile ;
16
+ import com .azure .resourcemanager .samples .Utils ;
20
17
21
18
/**
22
19
* Azure CosmosDB sample for high availability.
26
23
public final class CreateCosmosDBWithIPRange {
27
24
/**
28
25
* Main function which runs the actual sample.
29
- * @param azure instance of the azure client
30
- * @param clientId client id
26
+ * @param azureResourceManager instance of the azure client
31
27
* @return true if sample runs successfully
32
28
*/
33
- public static boolean runSample (Azure azure , String clientId ) {
34
- final String docDBName = SdkContext .randomResourceName ("docDb" , 10 );
35
- final String rgName = SdkContext .randomResourceName ("rgNEMV" , 24 );
29
+ public static boolean runSample (AzureResourceManager azureResourceManager ) {
30
+ final String docDBName = Utils .randomResourceName (azureResourceManager , "docDb" , 10 );
31
+ final String rgName = Utils .randomResourceName (azureResourceManager , "rgNEMV" , 24 );
36
32
37
33
try {
38
34
//============================================================
39
35
// Create a CosmosDB
40
36
41
37
System .out .println ("Creating a CosmosDB..." );
42
- CosmosDBAccount cosmosDBAccount = azure .cosmosDBAccounts ().define (docDBName )
38
+ CosmosDBAccount cosmosDBAccount = azureResourceManager .cosmosDBAccounts ().define (docDBName )
43
39
.withRegion (Region .US_EAST )
44
40
.withNewResourceGroup (rgName )
45
41
.withKind (DatabaseAccountKind .GLOBAL_DOCUMENT_DB )
@@ -55,29 +51,25 @@ public static boolean runSample(Azure azure, String clientId) {
55
51
//============================================================
56
52
// Delete CosmosDB
57
53
System .out .println ("Deleting the CosmosDB" );
58
- // work around CosmosDB service issue returning 404 CloudException on delete operation
54
+ // work around CosmosDB service issue returning 404 ManagementException on delete operation
59
55
try {
60
- azure .cosmosDBAccounts ().deleteById (cosmosDBAccount .id ());
61
- } catch (CloudException e ) {
56
+ azureResourceManager .cosmosDBAccounts ().deleteById (cosmosDBAccount .id ());
57
+ } catch (ManagementException e ) {
62
58
}
63
59
System .out .println ("Deleted the CosmosDB" );
64
60
65
61
return true ;
66
- } catch (Exception e ) {
67
- System .err .println (e .getMessage ());
68
62
} finally {
69
63
try {
70
64
System .out .println ("Deleting resource group: " + rgName );
71
- azure .resourceGroups ().beginDeleteByName (rgName );
65
+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
72
66
System .out .println ("Deleted resource group: " + rgName );
73
67
} catch (NullPointerException npe ) {
74
68
System .out .println ("Did not create any resources in Azure. No clean up is necessary" );
75
69
} catch (Exception g ) {
76
70
g .printStackTrace ();
77
71
}
78
72
}
79
-
80
- return false ;
81
73
}
82
74
83
75
/**
@@ -90,17 +82,21 @@ public static void main(String[] args) {
90
82
//=============================================================
91
83
// Authenticate
92
84
93
- final File credFile = new File (System .getenv ("AZURE_AUTH_LOCATION" ));
85
+ final AzureProfile profile = new AzureProfile (AzureEnvironment .AZURE );
86
+ final TokenCredential credential = new DefaultAzureCredentialBuilder ()
87
+ .authorityHost (profile .getEnvironment ().getActiveDirectoryEndpoint ())
88
+ .build ();
94
89
95
- Azure azure = Azure .configure ()
96
- .withLogLevel (LogLevel .BASIC )
97
- .authenticate (credFile )
98
- .withDefaultSubscription ();
90
+ AzureResourceManager azureResourceManager = AzureResourceManager
91
+ .configure ()
92
+ .withLogLevel (HttpLogDetailLevel .BASIC )
93
+ .authenticate (credential , profile )
94
+ .withDefaultSubscription ();
99
95
100
96
// Print selected subscription
101
- System .out .println ("Selected subscription: " + azure .subscriptionId ());
97
+ System .out .println ("Selected subscription: " + azureResourceManager .subscriptionId ());
102
98
103
- runSample (azure , ApplicationTokenCredentials . fromFile ( credFile ). clientId () );
99
+ runSample (azureResourceManager );
104
100
} catch (Exception e ) {
105
101
System .out .println (e .getMessage ());
106
102
e .printStackTrace ();
0 commit comments