Skip to content

Commit

Permalink
[Codespaces] Update to new Microsoft.Codespaces RP (#2011)
Browse files Browse the repository at this point in the history
* [Codespaces] Update to new Microsoft.Codespaces RP

* fix one place where old property was being used
  • Loading branch information
derekbekoe authored Jul 16, 2020
1 parent 9d60443 commit af0b570
Show file tree
Hide file tree
Showing 58 changed files with 968 additions and 694 deletions.
4 changes: 4 additions & 0 deletions src/codespaces/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
Release History
===============

0.3.0
++++++
* Switch to new Resource Provider - Microsoft.Codespaces

0.2.1
++++++
* Added 60 minute auto-suspend timeout option
Expand Down
2 changes: 1 addition & 1 deletion src/codespaces/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Updating the vendored SDK
-------------------------

```
cd /specification/vsonline/resource-manager
cd specification/codespaces/resource-manager
autorest --python readme.md
```

Expand Down
4 changes: 2 additions & 2 deletions src/codespaces/azext_codespaces/_client_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

def cf_codespaces(cli_ctx, *_):
from azure.cli.core.commands.client_factory import get_mgmt_service_client
from .vendored_sdks.vsonline.vs_online_client import VSOnlineClient
from .vendored_sdks.codespaces.codespaces_client import CodespacesClient
custom_api_version = get_rp_api_version(cli_ctx)
return get_mgmt_service_client(cli_ctx, VSOnlineClient, api_version=custom_api_version)
return get_mgmt_service_client(cli_ctx, CodespacesClient, api_version=custom_api_version)


def cf_codespaces_plan(cli_ctx, *_):
Expand Down
12 changes: 6 additions & 6 deletions src/codespaces/azext_codespaces/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
- name: List Codespaces
text: az codespace list -g my-rg --plan my-plan
- name: List Codespaces given plan id
text: az codespace list --plan /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.VSOnline/plans/my-plan
text: az codespace list --plan /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.Codespaces/plans/my-plan
"""

helps['codespace delete'] = """
Expand All @@ -119,7 +119,7 @@
- name: Delete a Codespace given id
text: az codespace delete -g my-rg --plan my-plan --id 00000000-0000-0000-0000-000000000000
- name: Delete a Codespace given plan id and Codespace name
text: az codespace delete --plan /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.VSOnline/plans/my-plan --name my-codespace
text: az codespace delete --plan /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.Codespaces/plans/my-plan --name my-codespace
"""

helps['codespace update'] = """
Expand All @@ -141,7 +141,7 @@
- name: Show details of a Codespace given id
text: az codespace show -g my-rg --plan my-plan --id 00000000-0000-0000-0000-000000000000
- name: Show details of a Codespace given plan id and Codespace name
text: az codespace show --plan /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.VSOnline/plans/my-plan --name my-codespace
text: az codespace show --plan /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.Codespaces/plans/my-plan --name my-codespace
"""

helps['codespace resume'] = """
Expand All @@ -153,7 +153,7 @@
- name: Resume a Codespace given id
text: az codespace resume -g my-rg --plan my-plan --id 00000000-0000-0000-0000-000000000000
- name: Resume a Codespace given plan id and Codespace name
text: az codespace resume --plan /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.VSOnline/plans/my-plan --name my-codespace
text: az codespace resume --plan /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.Codespaces/plans/my-plan --name my-codespace
"""

helps['codespace suspend'] = """
Expand All @@ -165,7 +165,7 @@
- name: Suspend a Codespace given id
text: az codespace suspend -g my-rg --plan my-plan --id 00000000-0000-0000-0000-000000000000
- name: Suspend a Codespace given plan id and Codespace name
text: az codespace suspend --plan /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.VSOnline/plans/my-plan --name my-codespace
text: az codespace suspend --plan /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.Codespaces/plans/my-plan --name my-codespace
"""

helps['codespace open'] = """
Expand All @@ -179,7 +179,7 @@
- name: Open a Codespace given id
text: az codespace open -g my-rg --plan my-plan --id 00000000-0000-0000-0000-000000000000
- name: Open a Codespace given plan id and Codespace name
text: az codespace open --plan /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.VSOnline/plans/my-plan --name my-codespace
text: az codespace open --plan /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.Codespaces/plans/my-plan --name my-codespace
"""

helps['codespace secret create'] = """
Expand Down
2 changes: 1 addition & 1 deletion src/codespaces/azext_codespaces/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def advanced_usage_message(_):
def load_command_table(self, _):

plan_operations = CliCommandType(
operations_tmpl='azext_codespaces.vendored_sdks.vsonline.operations.plan_operations#PlanOperations.{}',
operations_tmpl='azext_codespaces.vendored_sdks.codespaces.operations.plan_operations#PlanOperations.{}',
client_factory=cf_codespaces)

with self.command_group('codespace plan', plan_operations, client_factory=cf_codespaces_plan) as g:
Expand Down
67 changes: 25 additions & 42 deletions src/codespaces/azext_codespaces/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
from knack.prompting import prompt_y_n
from . import _non_arm_apis as cs_api
from . import _config as cs_config
from .vendored_sdks.vsonline.models import (
VSOnlinePlan,
VSOnlinePlanProperties,
from .vendored_sdks.codespaces.models import (
CodespacesPlan,
CodespacesPlanProperties,
VnetProperties,
VSOPlanUpdateParametersProperties)
CodespacesPlanUpdateParametersProperties)

logger = get_logger(__name__)

Expand Down Expand Up @@ -44,30 +44,13 @@ def create_plan(cmd,
subnet_id=None,
default_sku_name=None,
default_autoshutdown_delay=None):
import jwt # pylint: disable=import-error
from azure.cli.core._profile import Profile
profile = Profile(cli_ctx=cmd.cli_ctx)
creds, _, __ = profile.get_raw_token()
tokenType = creds[0]
accessToken = creds[1]
if tokenType != "Bearer":
logger.debug("Got unexpected token type: %s", tokenType)
raise CLIError("Unable to create plan. Use --debug for details.")
decoded_token = jwt.decode(accessToken, verify=False, algorithms=['RS256'])
tid = decoded_token.get('tid')
oid = decoded_token.get('oid')
if not tid or not oid:
logger.debug("Unable to determine 'tid' and 'oid' from token claims: %s", decoded_token)
raise CLIError("Unable to create plan. Use --debug for details.")
user_id = f"{tid}_{oid}"
vnet_props = VnetProperties(subnet_id=subnet_id) if subnet_id else None
plan_props = VSOnlinePlanProperties(
user_id=user_id,
plan_props = CodespacesPlanProperties(
default_auto_suspend_delay_minutes=default_autoshutdown_delay,
default_environment_sku=default_sku_name,
default_codespace_sku=default_sku_name,
vnet_properties=vnet_props)
vsonline_plan = VSOnlinePlan(location=location, properties=plan_props, tags=tags)
return client.create(resource_group_name, plan_name, vsonline_plan)
codespaces_plan = CodespacesPlan(location=location, properties=plan_props, tags=tags)
return client.create(resource_group_name, plan_name, codespaces_plan)


def update_plan(cmd,
Expand All @@ -76,12 +59,12 @@ def update_plan(cmd,
resource_group_name=None,
default_sku_name=None,
default_autoshutdown_delay=None):
vsonline_plan_update_parameters = VSOPlanUpdateParametersProperties(
codespaces_plan_update_parameters = CodespacesPlanUpdateParametersProperties(
default_auto_suspend_delay_minutes=default_autoshutdown_delay,
default_environment_sku=default_sku_name)
default_codespace_sku=default_sku_name)
return client.update(resource_group_name,
plan_name,
vsonline_plan_update_parameters)
codespaces_plan_update_parameters)


def list_available_locations(cmd):
Expand All @@ -95,9 +78,9 @@ def get_location_details(cmd, location_name):
def list_codespaces(cmd, client, plan_name, resource_group_name=None, list_all=None):
plan = client.get(resource_group_name=resource_group_name, plan_name=plan_name)
if list_all:
token = client.read_all_environments_action(resource_group_name=resource_group_name, plan_name=plan_name)
token = client.read_all_codespaces_action(resource_group_name=resource_group_name, plan_name=plan_name)
else:
token = client.write_environments_action(resource_group_name=resource_group_name, plan_name=plan_name)
token = client.write_codespaces_action(resource_group_name=resource_group_name, plan_name=plan_name)
return cs_api.list_codespaces(token.access_token, plan.id, cli_ctx=cmd.cli_ctx)


Expand All @@ -115,10 +98,10 @@ def create_codespace(cmd,
dotfiles_path=None,
dotfiles_command=None):
plan = client.get(resource_group_name=resource_group_name, plan_name=plan_name)
token = client.write_environments_action(resource_group_name=resource_group_name, plan_name=plan_name)
token = client.write_codespaces_action(resource_group_name=resource_group_name, plan_name=plan_name)
# Use plan defaults if needed and available
missing_args = []
sku_name = sku_name or plan.properties.default_environment_sku
sku_name = sku_name or plan.properties.default_codespace_sku
if not sku_name:
logger.warning("No default instance type specified for plan and no instance type specified in command.")
missing_args.append("--instance-type")
Expand Down Expand Up @@ -155,31 +138,31 @@ def create_codespace(cmd,


def get_codespace(cmd, client, plan_name, resource_group_name=None, codespace_id=None, codespace_name=None):
token = client.write_environments_action(resource_group_name=resource_group_name, plan_name=plan_name)
token = client.write_codespaces_action(resource_group_name=resource_group_name, plan_name=plan_name)
if codespace_name:
codespace_id = _determine_codespace_id(
client, resource_group_name, plan_name, token, codespace_name, cli_ctx=cmd.cli_ctx)
return cs_api.get_codespace(token.access_token, codespace_id, cli_ctx=cmd.cli_ctx)


def delete_codespace(cmd, client, plan_name, resource_group_name=None, codespace_id=None, codespace_name=None):
token = client.write_environments_action(resource_group_name=resource_group_name, plan_name=plan_name)
token = client.write_codespaces_action(resource_group_name=resource_group_name, plan_name=plan_name)
if codespace_name:
codespace_id = _determine_codespace_id(
client, resource_group_name, plan_name, token, codespace_name, cli_ctx=cmd.cli_ctx)
cs_api.delete_codespace(token.access_token, codespace_id, cli_ctx=cmd.cli_ctx)


def resume_codespace(cmd, client, plan_name, resource_group_name=None, codespace_id=None, codespace_name=None):
token = client.write_environments_action(resource_group_name=resource_group_name, plan_name=plan_name)
token = client.write_codespaces_action(resource_group_name=resource_group_name, plan_name=plan_name)
if codespace_name:
codespace_id = _determine_codespace_id(
client, resource_group_name, plan_name, token, codespace_name, cli_ctx=cmd.cli_ctx)
return cs_api.start_codespace(token.access_token, codespace_id, cli_ctx=cmd.cli_ctx)


def suspend_codespace(cmd, client, plan_name, resource_group_name=None, codespace_id=None, codespace_name=None):
token = client.write_environments_action(resource_group_name=resource_group_name, plan_name=plan_name)
token = client.write_codespaces_action(resource_group_name=resource_group_name, plan_name=plan_name)
if codespace_name:
codespace_id = _determine_codespace_id(
client, resource_group_name, plan_name, token, codespace_name, cli_ctx=cmd.cli_ctx)
Expand All @@ -194,7 +177,7 @@ def update_codespace(cmd,
codespace_name=None,
sku_name=None,
autoshutdown_delay=None):
token = client.write_environments_action(resource_group_name=resource_group_name, plan_name=plan_name)
token = client.write_codespaces_action(resource_group_name=resource_group_name, plan_name=plan_name)
if codespace_name:
codespace_id = _determine_codespace_id(
client, resource_group_name, plan_name, token, codespace_name, cli_ctx=cmd.cli_ctx)
Expand All @@ -212,7 +195,7 @@ def update_codespace(cmd,

def open_codespace(cmd, client, plan_name, resource_group_name=None, codespace_id=None,
codespace_name=None, do_not_prompt=None):
token = client.write_environments_action(resource_group_name=resource_group_name, plan_name=plan_name)
token = client.write_codespaces_action(resource_group_name=resource_group_name, plan_name=plan_name)
if codespace_name:
codespace_id = _determine_codespace_id(
client, resource_group_name, plan_name, token, codespace_name, cli_ctx=cmd.cli_ctx)
Expand Down Expand Up @@ -244,15 +227,15 @@ def show_config(cmd):

def list_plan_secrets(cmd, client, plan_name, resource_group_name=None):
plan = client.get(resource_group_name=resource_group_name, plan_name=plan_name)
token = client.write_environments_action(resource_group_name=resource_group_name, plan_name=plan_name)
token = client.write_codespaces_action(resource_group_name=resource_group_name, plan_name=plan_name)
return cs_api.list_secrets(token.access_token, plan.id, cli_ctx=cmd.cli_ctx)


def update_plan_secrets(cmd, client, plan_name, secret_id,
secret_name=None, secret_value=None, secret_note=None,
secret_filters=None, resource_group_name=None):
plan = client.get(resource_group_name=resource_group_name, plan_name=plan_name)
token = client.write_environments_action(resource_group_name=resource_group_name, plan_name=plan_name)
token = client.write_codespaces_action(resource_group_name=resource_group_name, plan_name=plan_name)
data = {}
data['secretName'] = secret_name
data['value'] = secret_value
Expand All @@ -266,7 +249,7 @@ def create_plan_secret(cmd, client, plan_name,
secret_name, secret_value, secret_note=None,
secret_filters=None, resource_group_name=None):
plan = client.get(resource_group_name=resource_group_name, plan_name=plan_name)
token = client.write_environments_action(resource_group_name=resource_group_name, plan_name=plan_name)
token = client.write_codespaces_action(resource_group_name=resource_group_name, plan_name=plan_name)
data = {}
data['secretName'] = secret_name
data['value'] = secret_value
Expand All @@ -279,5 +262,5 @@ def create_plan_secret(cmd, client, plan_name,

def delete_plan_secret(cmd, client, plan_name, secret_id, resource_group_name=None):
plan = client.get(resource_group_name=resource_group_name, plan_name=plan_name)
token = client.write_environments_action(resource_group_name=resource_group_name, plan_name=plan_name)
token = client.write_codespaces_action(resource_group_name=resource_group_name, plan_name=plan_name)
cs_api.delete_secret(token.access_token, plan.id, secret_id, cs_api.SecretScope.USER.value, cli_ctx=cmd.cli_ctx)

This file was deleted.

Loading

0 comments on commit af0b570

Please sign in to comment.