|
1 | 1 | ## Azure PolicyClient SDK for JavaScript
|
2 | 2 |
|
3 |
| -This package contains an isomorphic SDK for PolicyClient. |
| 3 | +This package contains an isomorphic SDK (runs both in node.js and in browsers) for PolicyClient. |
4 | 4 |
|
5 | 5 | ### Currently supported environments
|
6 | 6 |
|
7 |
| -- Node.js version 6.x.x or higher |
8 |
| -- Browser JavaScript |
| 7 | +- [LTS versions of Node.js](https://nodejs.org/about/releases/) |
| 8 | +- Latest versions of Safari, Chrome, Edge and Firefox. |
9 | 9 |
|
10 |
| -### How to Install |
| 10 | +### Prerequisites |
11 | 11 |
|
| 12 | +You must have an [Azure subscription](https://azure.microsoft.com/free/). |
| 13 | + |
| 14 | +### How to install |
| 15 | + |
| 16 | +To use this SDK in your project, you will need to install two packages. |
| 17 | +- `@azure/arm-policy` that contains the client. |
| 18 | +- `@azure/identity` that provides different mechanisms for the client to authenticate your requests using Azure Active Directory. |
| 19 | + |
| 20 | +Install both packages using the below command: |
12 | 21 | ```bash
|
13 |
| -npm install @azure/arm-policy |
| 22 | +npm install --save @azure/arm-policy @azure/identity |
14 | 23 | ```
|
| 24 | +> **Note**: You may have used either `@azure/ms-rest-nodeauth` or `@azure/ms-rest-browserauth` in the past. These packages are in maintenance mode receiving critical bug fixes, but no new features. |
| 25 | +If you are on a [Node.js that has LTS status](https://nodejs.org/about/releases/), or are writing a client side browser application, we strongly encourage you to upgrade to `@azure/identity` which uses the latest versions of Azure Active Directory and MSAL APIs and provides more authentication options. |
15 | 26 |
|
16 | 27 | ### How to use
|
17 | 28 |
|
18 |
| -#### nodejs - client creation and getByPolicyMode dataPolicyManifests as an example written in TypeScript. |
| 29 | +- If you are writing a client side browser application, |
| 30 | + - Follow the instructions in the section on Authenticating client side browser applications in [Azure Identity examples](https://aka.ms/azsdk/js/identity/examples) to register your application in the Microsoft identity platform and set the right permissions. |
| 31 | + - Copy the client ID and tenant ID from the Overview section of your app registration in Azure portal and use it in the browser sample below. |
| 32 | +- If you are writing a server side application, |
| 33 | + - [Select a credential from `@azure/identity` based on the authentication method of your choice](https://aka.ms/azsdk/js/identity/examples) |
| 34 | + - Complete the set up steps required by the credential if any. |
| 35 | + - Use the credential you picked in the place of `DefaultAzureCredential` in the Node.js sample below. |
19 | 36 |
|
20 |
| -##### Install @azure/ms-rest-nodeauth |
21 |
| - |
22 |
| -- Please install minimum version of `"@azure/ms-rest-nodeauth": "^3.0.0"`. |
23 |
| - |
24 |
| -```bash |
25 |
| -npm install @azure/ms-rest-nodeauth@"^3.0.0" |
26 |
| -``` |
| 37 | +In the below samples, we pass the credential and the Azure subscription id to instantiate the client. |
| 38 | +Once the client is created, explore the operations on it either in your favorite editor or in our [API reference documentation](https://docs.microsoft.com/javascript/api) to get started. |
| 39 | +#### nodejs - Authentication, client creation, and getByPolicyMode dataPolicyManifests as an example written in JavaScript. |
27 | 40 |
|
28 | 41 | ##### Sample code
|
29 | 42 |
|
30 |
| -While the below sample uses the interactive login, other authentication options can be found in the [README.md file of @azure/ms-rest-nodeauth](https://www.npmjs.com/package/@azure/ms-rest-nodeauth) package |
31 |
| - |
32 |
| -```typescript |
33 |
| -const msRestNodeAuth = require("@azure/ms-rest-nodeauth"); |
| 43 | +```javascript |
| 44 | +const { DefaultAzureCredential } = require("@azure/identity"); |
34 | 45 | const { PolicyClient } = require("@azure/arm-policy");
|
35 | 46 | const subscriptionId = process.env["AZURE_SUBSCRIPTION_ID"];
|
36 | 47 |
|
37 |
| -msRestNodeAuth |
38 |
| - .interactiveLogin() |
39 |
| - .then((creds) => { |
40 |
| - const client = new PolicyClient(creds, subscriptionId); |
41 |
| - const policyMode = "testpolicyMode"; |
42 |
| - client.dataPolicyManifests.getByPolicyMode(policyMode).then((result) => { |
43 |
| - console.log("The result is:"); |
44 |
| - console.log(result); |
45 |
| - }); |
46 |
| - }) |
47 |
| - .catch((err) => { |
48 |
| - console.error(err); |
49 |
| - }); |
| 48 | +// Use `DefaultAzureCredential` or any other credential of your choice based on https://aka.ms/azsdk/js/identity/examples |
| 49 | +// Please note that you can also use credentials from the `@azure/ms-rest-nodeauth` package instead. |
| 50 | +const creds = new DefaultAzureCredential(); |
| 51 | +const client = new PolicyClient(creds, subscriptionId); |
| 52 | +const policyMode = "testpolicyMode"; |
| 53 | +client.dataPolicyManifests.getByPolicyMode(policyMode).then((result) => { |
| 54 | + console.log("The result is:"); |
| 55 | + console.log(result); |
| 56 | +}).catch((err) => { |
| 57 | + console.log("An error occurred:"); |
| 58 | + console.error(err); |
| 59 | +}); |
50 | 60 | ```
|
51 | 61 |
|
52 |
| -#### browser - Authentication, client creation and getByPolicyMode dataPolicyManifests as an example written in JavaScript. |
53 |
| - |
54 |
| -##### Install @azure/ms-rest-browserauth |
| 62 | +#### browser - Authentication, client creation, and getByPolicyMode dataPolicyManifests as an example written in JavaScript. |
55 | 63 |
|
56 |
| -```bash |
57 |
| -npm install @azure/ms-rest-browserauth |
58 |
| -``` |
| 64 | +In browser applications, we recommend using the `InteractiveBrowserCredential` that interactively authenticates using the default system browser. |
| 65 | + - See [Single-page application: App registration guide](https://docs.microsoft.com/azure/active-directory/develop/scenario-spa-app-registration) to configure your app registration for the browser. |
| 66 | + - Note down the client Id from the previous step and use it in the browser sample below. |
59 | 67 |
|
60 | 68 | ##### Sample code
|
61 | 69 |
|
62 |
| -See https://github.com/Azure/ms-rest-browserauth to learn how to authenticate to Azure in the browser. |
63 |
| - |
64 | 70 | - index.html
|
65 | 71 |
|
66 | 72 | ```html
|
67 | 73 | <!DOCTYPE html>
|
68 | 74 | <html lang="en">
|
69 | 75 | <head>
|
70 | 76 | <title>@azure/arm-policy sample</title>
|
71 |
| - <script src="node_modules/@azure/ms-rest-js/dist/msRest.browser.js"></script> |
72 | 77 | <script src="node_modules/@azure/ms-rest-azure-js/dist/msRestAzure.js"></script>
|
73 |
| - <script src="node_modules/@azure/ms-rest-browserauth/dist/msAuth.js"></script> |
| 78 | + <script src="node_modules/@azure/identity/dist/index.js"></script> |
74 | 79 | <script src="node_modules/@azure/arm-policy/dist/arm-policy.js"></script>
|
75 | 80 | <script type="text/javascript">
|
76 | 81 | const subscriptionId = "<Subscription_Id>";
|
77 |
| - const authManager = new msAuth.AuthManager({ |
| 82 | + // Create credentials using the `@azure/identity` package. |
| 83 | + // Please note that you can also use credentials from the `@azure/ms-rest-browserauth` package instead. |
| 84 | + const credential = new InteractiveBrowserCredential( |
| 85 | + { |
78 | 86 | clientId: "<client id for your Azure AD app>",
|
79 | 87 | tenant: "<optional tenant for your organization>"
|
80 | 88 | });
|
81 |
| - authManager.finalizeLogin().then((res) => { |
82 |
| - if (!res.isLoggedIn) { |
83 |
| - // may cause redirects |
84 |
| - authManager.login(); |
85 |
| - } |
86 |
| - const client = new Azure.ArmPolicy.PolicyClient(res.creds, subscriptionId); |
87 |
| - const policyMode = "testpolicyMode"; |
88 |
| - client.dataPolicyManifests |
89 |
| - .getByPolicyMode(policyMode) |
90 |
| - .then((result) => { |
91 |
| - console.log("The result is:"); |
92 |
| - console.log(result); |
93 |
| - }) |
94 |
| - .catch((err) => { |
95 |
| - console.log("An error occurred:"); |
96 |
| - console.error(err); |
97 |
| - }); |
| 89 | + const client = new Azure.ArmPolicy.PolicyClient(creds, subscriptionId); |
| 90 | + const policyMode = "testpolicyMode"; |
| 91 | + client.dataPolicyManifests.getByPolicyMode(policyMode).then((result) => { |
| 92 | + console.log("The result is:"); |
| 93 | + console.log(result); |
| 94 | + }).catch((err) => { |
| 95 | + console.log("An error occurred:"); |
| 96 | + console.error(err); |
98 | 97 | });
|
99 | 98 | </script>
|
100 | 99 | </head>
|
|
0 commit comments