Files
cadb38c07c DevCenter Data Plane TypeSpec beta SDK v2023-04-01 (#27835)
# Description

For js, we had a previous
[PR](https://github.com/Azure/azure-sdk-for-js/pull/25859) which was
mainly to address changes caused by updating the DevCenter Data Plane
API to 2023-04-01 version. Since the PR was not completed, I have picked
the commits from there with the intention of reusing tests and samples.
On top of that, I have regenerated the SDK from TypeSpec since this is
the new type of generation desired going forward.

This will be released as a beta SDK and later down the road we will be
working on the stable SDK for this same API version. The recordings were
updated to the [assets
repo](https://github.com/Azure/azure-sdk-assets/tree/js/devcenter/developer-devcenter-rest_89c2b6dafe).

The changes to add typespec support can be find at
[feature/devcenter-tsp](https://github.com/Azure/azure-rest-api-specs/pull/26359).
Note the PR hasn't been merged, so we will have a separate pr in the
future to update the commit hash pointing to the new tsp location.

# All SDK Contribution checklist:
- [ ] **The pull request does not introduce [breaking changes]**
- [x] **CHANGELOG is updated for new features, bug fixes or other
significant changes.**
- [x] **I have read the [contribution
guidelines](https://github.com/Azure/azure-sdk-for-python/blob/main/CONTRIBUTING.md).**

## General Guidelines and Best Practices
- [x] Title of the pull request is clear and informative.
- [x] There are a small number of commits, each of which have an
informative message. This means that previously merged commits do not
appear in the history of the PR. For more information on cleaning up the
commits in your PR, [see this
page](https://github.com/Azure/azure-powershell/blob/master/documentation/development-docs/cleaning-up-commits.md).

### [Testing
Guidelines](https://github.com/Azure/azure-sdk-for-python/blob/main/CONTRIBUTING.md##building-and-testing)
- [x] Pull request includes test coverage for the included changes.

### Pipeline runs

Dry runs on the release pipeline:

https://dev.azure.com/azure-sdk/internal/_build?definitionId=5622&_a=summary

### List of tests
(same as previous PR)

DevBox
 - [x] get_pool
 - [x] list_pools
 - [x] get_dev_box
 - [x] get_remote_connection
 - [x] get_action
 - [x] list_actions
 - [x] skip_action
 - [x] delay_action
 - [x] delay_all_actions
 - [x] get_schedule
 - [x] list_schedules
 - [x] list_dev_boxes
 - [x] list_all_dev_boxes
 - [x] list_all_dev_boxes_by_user
 - [x] begin_create_dev_box
 - [x] begin_delete_dev_box
 - [x] begin_start_dev_box
 - [x] begin_stop_dev_box
 - [x] begin_restart_dev_box

DevCenter
 - [x] get_project
 - [x] list_projects

Environments
 - [x] begin_delete_environment
 - [x] begin_create_or_update_environment
 - [x] list_environment_types
 - [x] list_catalogs
 - [x] list_environments
 - [x] list_all_environments
 - [x] get_environment_definition
 - [x] list_environment_definitions
 - [x]  list_environment_definitions_by_catalog
 - [x] get_catalog
 - [x] get_environment

Also updated samples.

---------

Co-authored-by: Sean Brennan <[email protected]>
Co-authored-by: Oleg Demchenko <[email protected]>
2023-11-20 15:41:46 -08:00

111 lines
3.6 KiB
JavaScript

const { DefaultAzureCredential } = require("@azure/identity");
const { isUnexpected, getLongRunningPoller } = require("@azure-rest/developer-devcenter");
const createClient = require("@azure-rest/developer-devcenter").default;
require("dotenv").config();
/**
* @summary Demonstrates creating, fetching outputs from, and deleting an Environment
*/
async function createEnvironment() {
// Build client and fetch required parameters
const endpoint = process.env.DEVCENTER_ENDPOINT || "<devcenter name>";
const client = createClient(endpoint, new DefaultAzureCredential());
const projectList = await client.path("/projects").get();
if (isUnexpected(projectList)) {
throw new Error(projectList.body.error.message);
}
let project = projectList.body.value[0];
if (project === undefined || project.name === undefined) {
throw new Error("No projects found.");
}
const projectName = project.name;
const catalogList = await client.path("/projects/{projectName}/catalogs", projectName).get();
if (isUnexpected(catalogList)) {
throw new Error(catalogList.body.error);
}
const catalog = catalogList.body.value[0];
const catalogName = catalog.name;
const environmentDefinitionsList = await client
.path(
"/projects/{projectName}/catalogs/{catalogName}/environmentDefinitions",
projectName,
catalogName
)
.get();
if (isUnexpected(environmentDefinitionsList)) {
throw new Error(environmentDefinitionsList.body.error);
}
const environmentDefinition = environmentDefinitionsList.body.value[0];
const environmentDefinitionName = environmentDefinition.name;
const environmentTypeList = await client
.path("/projects/{projectName}/environmentTypes", projectName)
.get();
if (isUnexpected(environmentTypeList)) {
throw new Error(environmentTypeList.body.error.message);
}
let environmentType = environmentTypeList.body.value[0];
if (environmentType === undefined || environmentType.name === undefined) {
throw new Error("No environment types found.");
}
const environmentsCreateParameters = {
contentType: "application/json",
body: {
environmentDefinitionName: environmentDefinitionName,
environmentType: environmentType.name,
catalogName: catalogName,
},
};
const environmentName = "DevEnvironment";
const userId = "me";
// Provision an environment
const environmentCreateResponse = await client
.path(
"/projects/{projectName}/users/{userId}/environments/{environmentName}",
projectName,
userId,
environmentName
)
.put(environmentsCreateParameters);
if (isUnexpected(environmentCreateResponse)) {
throw new Error(environmentCreateResponse.body.error.message);
}
const environmentCreatePoller = await getLongRunningPoller(client, environmentCreateResponse);
const environmentCreateResult = await environmentCreatePoller.pollUntilDone();
console.log(
`Provisioned environment with state ${environmentCreateResult.body.provisioningState}.`
);
// Tear down the environment when finished
const environmentDeleteResponse = await client
.path(
"/projects/{projectName}/users/{userId}/environments/{environmentName}",
projectName,
userId,
environmentName
)
.delete();
if (isUnexpected(environmentDeleteResponse)) {
throw new Error(environmentDeleteResponse.body.error.message);
}
const environmentDeletePoller = await getLongRunningPoller(client, environmentDeleteResponse);
await environmentDeletePoller.pollUntilDone();
console.log("Cleaned up environment successfully.");
}
createEnvironment();