Skip to content

Commit

Permalink
[ssh] Add support to "Microsoft.ConnectedVMwarevSphere/virtualMachine…
Browse files Browse the repository at this point in the history
…s" resource type (#5367)
  • Loading branch information
vthiebaut10 authored Oct 21, 2022
1 parent c6a57d5 commit 94bd54a
Show file tree
Hide file tree
Showing 38 changed files with 7,293 additions and 232 deletions.
2 changes: 2 additions & 0 deletions src/ssh/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Release History
===============
1.1.3
-----
* Add support to Microsoft.ConnectedVMwarevSphere/virtualMachines Resource Type.
* Correct the format of expected input for --resource-type parameter. From Resource Provider name (e.g. "Microsoft.HybridCompute") to Resource Type name (e.g. "Microsoft.HybridCompute/machines").
* [bug fix] SSH Banners are printed before authentication.

1.1.2
Expand Down
10 changes: 10 additions & 0 deletions src/ssh/azext_ssh/_client_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,13 @@ def cf_connectedmachine_cl(cli_ctx, *_):

def cf_machine(cli_ctx, *_):
return cf_connectedmachine_cl(cli_ctx).machines


def cf_connectedvmware_cl(cli_ctx, *_):
from azext_ssh.vendored_sdks.connectedvmware import AzureArcVMwareManagementServiceAPI
return get_mgmt_service_client(cli_ctx,
AzureArcVMwareManagementServiceAPI)


def cf_vmware(cli_ctx, *_):
return cf_connectedvmware_cl(cli_ctx).virtual_machines
18 changes: 15 additions & 3 deletions src/ssh/azext_ssh/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ def load_arguments(self, _):
help='Path to a certificate file used for authentication when using local user credentials.')
c.argument('port', options_list=['--port'], help='SSH port')
c.argument('resource_type', options_list=['--resource-type'],
help='Resource type should be either Microsoft.Compute or Microsoft.HybridCompute',
completer=["Microsoft.HybridCompute", "Microsoft.Compute"])
help=('Resource type should be either Microsoft.Compute/virtualMachines, '
'Microsoft.HybridCompute/machines, '
'or Microsoft.ConnectedVMwareSphere/virtualMachines.'),
completer=['Microsoft.Compute/virtualMachines', 'Microsoft.HybridCompute/machines',
'Microsoft.ConnectedVMwareSphere/virtualMachines'])
c.argument('ssh_client_folder', options_list=['--ssh-client-folder'],
help='Folder path that contains ssh executables (ssh.exe, ssh-keygen.exe, etc). '
'Default to ssh pre-installed if not provided.')
Expand Down Expand Up @@ -52,7 +55,11 @@ def load_arguments(self, _):
help='Folder where new generated keys will be stored.')
c.argument('port', options_list=['--port'], help='SSH Port')
c.argument('resource_type', options_list=['--resource-type'],
help='Resource type should be either Microsoft.Compute or Microsoft.HybridCompute')
help=('Resource type should be either Microsoft.Compute/virtualMachines, '
'Microsoft.HybridCompute/machines, '
'or Microsoft.ConnectedVMwareSphere/virtualMachines.'),
completer=['Microsoft.Compute/virtualMachines', 'Microsoft.HybridCompute/machines',
'Microsoft.ConnectedVMwareSphere/virtualMachines'])
c.argument('cert_file', options_list=['--certificate-file', '-c'], help='Path to certificate file')
c.argument('ssh_proxy_folder', options_list=['--ssh-proxy-folder'],
help=('Path to the folder where the ssh proxy should be saved. '
Expand All @@ -79,6 +86,11 @@ def load_arguments(self, _):
help='The username for a local user')
c.argument('cert_file', options_list=['--certificate-file', '-c'], help='Path to certificate file')
c.argument('port', options_list=['--port'], help='Port to connect to on the remote host.')
c.argument('resource_type', options_list=['--resource-type'],
help=('Resource type should be either Microsoft.HybridCompute/machines '
'or Microsoft.ConnectedVMwareSphere/virtualMachines.'),
completer=['Microsoft.HybridCompute/machines',
'Microsoft.ConnectedVMwareSphere/virtualMachines'])
c.argument('ssh_client_folder', options_list=['--ssh-client-folder'],
help='Folder path that contains ssh executables (ssh.exe, ssh-keygen.exe, etc). '
'Default to ssh pre-installed if not provided.')
Expand Down
18 changes: 11 additions & 7 deletions src/ssh/azext_ssh/connectivity_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


# Get the Access Details to connect to Arc Connectivity platform from the HybridConnectivity RP
def get_relay_information(cmd, resource_group, vm_name, certificate_validity_in_seconds):
def get_relay_information(cmd, resource_group, vm_name, resource_type, certificate_validity_in_seconds):
from azext_ssh._client_factory import cf_endpoint
client = cf_endpoint(cmd.cli_ctx)

Expand All @@ -39,16 +39,18 @@ def get_relay_information(cmd, resource_group, vm_name, certificate_validity_in_
try:
t0 = time.time()
result = client.list_credentials(resource_group_name=resource_group, machine_name=vm_name,
endpoint_name="default", expiresin=certificate_validity_in_seconds)
resource_type=resource_type, endpoint_name="default",
expiresin=certificate_validity_in_seconds)
time_elapsed = time.time() - t0
telemetry.add_extension_event('ssh', {'Context.Default.AzureCLI.SSHListCredentialsTime': time_elapsed})
except ResourceNotFoundError:
logger.debug("Default Endpoint couldn't be found. Trying to create Default Endpoint.")
_create_default_endpoint(cmd, resource_group, vm_name, client)
_create_default_endpoint(cmd, resource_group, vm_name, resource_type, client)
try:
t0 = time.time()
result = client.list_credentials(resource_group_name=resource_group, machine_name=vm_name,
endpoint_name="default", expiresin=certificate_validity_in_seconds)
resource_type=resource_type, endpoint_name="default",
expiresin=certificate_validity_in_seconds)
time_elapsed = time.time() - t0
telemetry.add_extension_event('ssh', {'Context.Default.AzureCLI.SSHListCredentialsTime': time_elapsed})
except Exception as e:
Expand All @@ -58,12 +60,14 @@ def get_relay_information(cmd, resource_group, vm_name, certificate_validity_in_
return result


def _create_default_endpoint(cmd, resource_group, vm_name, client):
def _create_default_endpoint(cmd, resource_group, vm_name, resource_type, client):
namespace = resource_type.split('/', 1)[0]
arc_type = resource_type.split('/', 1)[1]
az_resource_id = resource_id(subscription=get_subscription_id(cmd.cli_ctx), resource_group=resource_group,
namespace="Microsoft.HybridCompute", type="machines", name=vm_name)
namespace=namespace, type=arc_type, name=vm_name)
endpoint_resource = {"id": az_resource_id, "type_properties_type": "default"}
try:
client.create_or_update(resource_group, vm_name, "default", endpoint_resource)
client.create_or_update(resource_group, vm_name, resource_type, "default", endpoint_resource)
except Exception as e:
colorama.init()
raise azclierror.UnauthorizedError(f"Unable to create Default Endpoint for {vm_name} in {resource_group}."
Expand Down
32 changes: 32 additions & 0 deletions src/ssh/azext_ssh/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,35 @@
RECOMMENDATION_RESOURCE_NOT_FOUND = (Fore.YELLOW + "Please ensure the active subscription is set properly "
"and resource exists." + Style.RESET_ALL)
RDP_TERMINATE_SSH_WAIT_TIME_IN_SECONDS = 30

ARC_RESOURCE_TYPE_PLACEHOLDER = "arc_resource_type_placeholder"

SUPPORTED_RESOURCE_TYPES = ["microsoft.hybridcompute/machines",
"microsoft.compute/virtualmachines",
"microsoft.connectedvmwarevsphere/virtualmachines",
"microsoft.scvmm/virtualmachines",
"microsoft.azurestackhci/virtualmachines"]

# Old version incorrectly used resource providers instead of resource type.
# Will continue to support to avoid breaking backwards compatibility.
LEGACY_SUPPORTED_RESOURCE_TYPES = ["microsoft.hybridcompute",
"microsoft.compute",
"microsoft.connectedvmwarevsphere",
"microsoft.scvmm",
"microsoft.azurestackhci"]

RESOURCE_PROVIDER_TO_RESOURCE_TYPE = {
"microsoft.hybridcompute": "Microsoft.HybridCompute/machines",
"microsoft.compute": "Microsoft.Compute/virtualMachines",
"microsoft.connectedvmwarevsphere": "Microsoft.ConnectedVMwarevSphere/virtualMachines",
"microsoft.azurestackhci": "Microsoft.AzureStackHCI/virtualMachines",
"microsoft.scvmm": "Microsoft.ScVmm/virtualMachines"
}

RESOURCE_TYPE_LOWER_CASE_TO_CORRECT_CASE = {
"microsoft.hybridcompute/machines": "Microsoft.HybridCompute/machines",
"microsoft.compute/virtualmachines": "Microsoft.Compute/virtualMachines",
"microsoft.connectedvmwarevsphere/virtualmachines": "Microsoft.ConnectedVMwarevSphere/virtualMachines",
"microsoft.scvmm/virtualmachines": "Microsoft.ScVmm/virtualMachines",
"microsoft.azurestackhci/virtualmachines": "Microsoft.AzureStackHCI/virtualMachines"
}
Loading

0 comments on commit 94bd54a

Please sign in to comment.