Skip to content

Commit

Permalink
Add test case for Azure Managed Identity (#2689)
Browse files Browse the repository at this point in the history
* Add test case for Azure Managed Identity

* update
  • Loading branch information
v-jiaodi authored Jul 25, 2024
1 parent 3db911e commit f380fcd
Show file tree
Hide file tree
Showing 49 changed files with 3,407 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/typespec-ts/test/commands/cadl-ranch-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ export const rlcTsps = [
{
outputPath: "azure/core/model",
inputPath: "azure/core/model"
},
{
outputPath: "azure/resource-manager/models/common-types/managed-identity",
inputPath: "azure/resource-manager/models/common-types/managed-identity"
}
];

Expand Down Expand Up @@ -530,6 +534,10 @@ export const modularTsps = [
{
outputPath: "resiliency/srv-driven-main",
inputPath: "resiliency/srv-driven/main.tsp"
},
{
outputPath: "azure/resource-manager/models/common-types/managed-identity",
inputPath: "azure/resource-manager/models/common-types/managed-identity"
}
];

Expand Down
128 changes: 128 additions & 0 deletions packages/typespec-ts/test/integration/azureArmManagedIdentity.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { assert } from "chai";
import AzureArmModelsCommonTypesManagedIdentityClientFactory, {
AzureArmModelsCommonTypesManagedIdentityClient
} from "./generated/azure/resource-manager/models/common-types/managed-identity/src/index.js";
describe("Azure Arm Resources Rest Client", () => {
let client: AzureArmModelsCommonTypesManagedIdentityClient;

beforeEach(() => {
client = AzureArmModelsCommonTypesManagedIdentityClientFactory({
endpoint: "http://localhost:3000",
allowInsecureConnection: true
});
});
const SUBSCRIPTION_ID_EXPECTED = "00000000-0000-0000-0000-000000000000";
const PRINCIPAL_ID_EXPECTED = "00000000-0000-0000-0000-000000000000";
const TENANT_ID_EXPECTED = "00000000-0000-0000-0000-000000000000";
const CLIENT_ID_EXPECTED = "00000000-0000-0000-0000-000000000000";
const LOCATION_REGION_EXPECTED = "eastus";
const RESOURCE_GROUP_EXPECTED = "test-rg";
const IDENTITY_TYPE_SYSTEM_ASSIGNED_EXPECTED = "SystemAssigned";
const IDENTITY_TYPE_SYSTEM_USER_ASSIGNED_EXPECTED =
"SystemAssigned,UserAssigned";
const validSystemAssignedManagedIdentityResource = {
id: `/subscriptions/${SUBSCRIPTION_ID_EXPECTED}/resourceGroups/${RESOURCE_GROUP_EXPECTED}/providers/Azure.ResourceManager.Models.CommonTypes.ManagedIdentity/managedIdentityTrackedResources/identity`,
location: `${LOCATION_REGION_EXPECTED}`,
tags: {
tagKey1: "tagValue1"
},
identity: {
type: `${IDENTITY_TYPE_SYSTEM_ASSIGNED_EXPECTED}`,
principalId: `${PRINCIPAL_ID_EXPECTED}`,
tenantId: `${TENANT_ID_EXPECTED}`
},
properties: {
provisioningState: "Succeeded"
}
};

const validUserAssignedAndSystemAssignedManagedIdentityResource = {
id: `/subscriptions/${SUBSCRIPTION_ID_EXPECTED}/resourceGroups/${RESOURCE_GROUP_EXPECTED}/providers/Azure.ResourceManager.Models.CommonTypes.ManagedIdentity/managedIdentityTrackedResources/identity`,
location: `${LOCATION_REGION_EXPECTED}`,
tags: {
tagKey1: "tagValue1"
},
identity: {
type: `${IDENTITY_TYPE_SYSTEM_USER_ASSIGNED_EXPECTED}`,
userAssignedIdentities: {
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id1":
{
principalId: `${PRINCIPAL_ID_EXPECTED}`,
clientId: `${CLIENT_ID_EXPECTED}`
}
},
principalId: `${PRINCIPAL_ID_EXPECTED}`,
tenantId: `${TENANT_ID_EXPECTED}`
},
properties: {
provisioningState: "Succeeded"
}
};

const createExpectedIdentity = {
type: `${IDENTITY_TYPE_SYSTEM_ASSIGNED_EXPECTED}`
};

const updateExpectedIdentity = {
type: `${IDENTITY_TYPE_SYSTEM_USER_ASSIGNED_EXPECTED}`,
userAssignedIdentities: {
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id1":
{}
}
};

// managed identity tracked resource
it("should get models commonTypes managedIdentityTrackedResources", async () => {
const result = await client
.path(
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Azure.ResourceManager.Models.CommonTypes.ManagedIdentity/managedIdentityTrackedResources/{managedIdentityTrackedResourceName}",
SUBSCRIPTION_ID_EXPECTED,
RESOURCE_GROUP_EXPECTED,
"identity"
)
.get();
assert.strictEqual(result.status, "200");
assert.deepEqual(result.body, validSystemAssignedManagedIdentityResource);
});

it("should put models commonTypes managedIdentityTrackedResources", async () => {
const result = await client
.path(
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Azure.ResourceManager.Models.CommonTypes.ManagedIdentity/managedIdentityTrackedResources/{managedIdentityTrackedResourceName}",
SUBSCRIPTION_ID_EXPECTED,
RESOURCE_GROUP_EXPECTED,
"identity"
)
.put({
body: {
identity: createExpectedIdentity,
location: LOCATION_REGION_EXPECTED,
tags: { tagKey1: "tagValue1" }
}
});
assert.strictEqual(result.status, "200");
assert.deepEqual(result.body, validSystemAssignedManagedIdentityResource);
});

it("should patch models commonTypes managedIdentityTrackedResources", async () => {
const result = await client
.path(
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Azure.ResourceManager.Models.CommonTypes.ManagedIdentity/managedIdentityTrackedResources/{managedIdentityTrackedResourceName}",
SUBSCRIPTION_ID_EXPECTED,
RESOURCE_GROUP_EXPECTED,
"identity"
)
.patch({
body: {
identity: updateExpectedIdentity,
location: LOCATION_REGION_EXPECTED,
tags: { tagKey1: "tagValue1" }
}
});
assert.strictEqual(result.status, "200");
assert.deepEqual(
result.body,
validUserAssignedAndSystemAssignedManagedIdentityResource
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"plugins": ["@azure/azure-sdk"],
"extends": ["plugin:@azure/azure-sdk/azure-sdk-base"],
"rules": {
"@azure/azure-sdk/ts-modules-only-named": "warn",
"@azure/azure-sdk/ts-apiextractor-json-types": "warn",
"@azure/azure-sdk/ts-package-json-types": "warn",
"@azure/azure-sdk/ts-package-json-engine-is-present": "warn",
"tsdoc/syntax": "warn",
"@azure/azure-sdk/ts-package-json-module": "off",
"@azure/azure-sdk/ts-package-json-files-required": "off",
"@azure/azure-sdk/ts-package-json-main-is-cjs": "off"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 Microsoft

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# AzureArmModelsCommonTypesManagedIdentity REST client library for JavaScript

Arm Managed Identity Provider management API.

**If you are not familiar with our REST client, please spend 5 minutes to take a look at our [REST client docs](https://github.com/Azure/azure-sdk-for-js/blob/main/documentation/rest-clients.md) to use this library, the REST client provides a light-weighted & developer friendly way to call azure rest api

Key links:

- [Package (NPM)](https://www.npmjs.com/package/@azure/arm-managedIdentity)
- [API reference documentation](https://docs.microsoft.com/javascript/api/@azure/arm-managedIdentity)

## Getting started

### Currently supported environments

- LTS versions of Node.js

### Prerequisites

- You must have an [Azure subscription](https://azure.microsoft.com/free/) to use this package.

### Install the `@azure/arm-managedIdentity` package

Install the AzureArmModelsCommonTypesManagedIdentity REST client REST client library for JavaScript with `npm`:

```bash
npm install @azure/arm-managedIdentity
```

### Create and authenticate a `AzureArmModelsCommonTypesManagedIdentityClient`

To use an [Azure Active Directory (AAD) token credential](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/samples/AzureIdentityExamples.md#authenticating-with-a-pre-fetched-access-token),
provide an instance of the desired credential type obtained from the
[@azure/identity](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#credentials) library.

To authenticate with AAD, you must first `npm` install [`@azure/identity`](https://www.npmjs.com/package/@azure/identity)

After setup, you can choose which type of [credential](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#credentials) from `@azure/identity` to use.
As an example, [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential)
can be used to authenticate the client.

Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables:
AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET

## Troubleshooting

### Logging

Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`. Alternatively, logging can be enabled at runtime by calling `setLogLevel` in the `@azure/logger`:

```javascript
const { setLogLevel } = require("@azure/logger");

setLogLevel("info");
```

For more detailed instructions on how to enable logs, you can look at the [@azure/logger package docs](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/core/logger).
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"mainEntryPointFilePath": "./dist/esm/index.d.ts",
"docModel": { "enabled": true },
"apiReport": { "enabled": true, "reportFolder": "./review" },
"dtsRollup": {
"enabled": true,
"untrimmedFilePath": "",
"publicTrimmedFilePath": "./types/arm-managedIdentity.d.ts"
},
"messages": {
"tsdocMessageReporting": { "default": { "logLevel": "none" } },
"extractorMessageReporting": {
"ae-missing-release-tag": { "logLevel": "none" },
"ae-unresolved-link": { "logLevel": "none" }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "@azure/arm-managedIdentity",
"version": "1.0.0",
"description": "AzureArm models CommonTypes ManagedIdentity Test Service",
"engines": {
"node": ">=18.0.0"
},
"sideEffects": false,
"autoPublish": false,
"tshy": {
"exports": {
"./package.json": "./package.json",
".": "./src/index.ts"
},
"dialects": ["esm", "commonjs"],
"esmDialects": ["browser", "react-native"],
"selfLink": false
},
"type": "module",
"keywords": ["node", "azure", "cloud", "typescript", "browser", "isomorphic"],
"author": "Microsoft Corporation",
"license": "MIT",
"files": ["dist", "README.md", "LICENSE", "review/*", "CHANGELOG.md"],
"dependencies": {
"@azure-rest/core-client": "^2.1.0",
"@azure/core-auth": "^1.6.0",
"@azure/core-rest-pipeline": "^1.5.0",
"@azure/logger": "^1.0.0",
"tslib": "^2.6.2"
},
"devDependencies": {
"dotenv": "^16.0.0",
"@microsoft/api-extractor": "^7.40.3",
"@types/node": "^18.0.0",
"eslint": "^8.55.0",
"prettier": "^3.2.5",
"rimraf": "^5.0.5",
"mkdirp": "^3.0.1",
"typescript": "~5.5.3",
"tshy": "1.11.1"
},
"scripts": {
"clean": "rimraf --glob dist dist-browser dist-esm test-dist temp types *.tgz *.log",
"extract-api": "rimraf review && mkdirp ./review && api-extractor run --local",
"pack": "npm pack 2>&1",
"lint": "eslint package.json api-extractor.json src --ext .ts",
"lint:fix": "eslint package.json api-extractor.json src --ext .ts --fix --fix-type [problem,suggestion]",
"check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"*.{js,json}\" \"test/**/*.ts\" \"samples-dev/**/*.ts\"",
"format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"*.{js,json}\" \"test/**/*.ts\" \"samples-dev/**/*.ts\"",
"build": "npm run clean && tshy && npm run extract-api"
}
}
Loading

0 comments on commit f380fcd

Please sign in to comment.