Skip to content

Commit

Permalink
Link HCRP (Arc for servers) machine to vCenter (#7203)
Browse files Browse the repository at this point in the history
* Link HCRP machine to vCenter

* Update VM create examples

* small nit

* lint fixes

* lint fixes

* error msg improv

* lint fix

* release 0.2.4

* Update src/connectedvmware/azext_connectedvmware/_help.py

Co-authored-by: ZelinWang <zelinwang@microsoft.com>

* Update src/connectedvmware/azext_connectedvmware/_help.py

Co-authored-by: ZelinWang <zelinwang@microsoft.com>

---------

Co-authored-by: ZelinWang <zelinwang@microsoft.com>
  • Loading branch information
nascarsayan and wangzelin007 authored Jan 24, 2024
1 parent 38dec89 commit dab6605
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 95 deletions.
5 changes: 5 additions & 0 deletions src/connectedvmware/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Release History
===============
0.2.4
++++++
* Link existing HCRP machine to vCenter using the CLI.
* Add examples in help.

0.2.3
++++++
* Fix docs and help for vm creation from template with disk override.
Expand Down
2 changes: 1 addition & 1 deletion src/connectedvmware/azext_connectedvmware/_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
# pylint: disable= protected-access, too-few-public-methods, raise-missing-from, no-self-use, consider-using-f-string
# pylint: disable= protected-access, too-few-public-methods, raise-missing-from, consider-using-f-string

"""
This file contains actions for parsing complex arguments.
Expand Down
26 changes: 25 additions & 1 deletion src/connectedvmware/azext_connectedvmware/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,38 @@
'connectedvmware vm create'
] = """
type: command
short-summary: "Create vm in vcenter from existing vm template"
short-summary: "Create VMInstance resource"
examples:
- name: Create vm
text: |-
az connectedvmware vm create --custom-location "custom location name" \
--location "location name" --inventory-item "name or id of the inventory item" \
--name "virtual machine name" --resource-group "resource group name" \
--vcenter "name or id of the vcenter"
- name: Enable an exiting VM to azure.
text: |-
az connectedvmware vm create --subscription contoso-sub \
--resource-group contoso-rg --location eastus --custom-location contoso-cl \
--inventory-item 01234567-0123-0123-0123-0123456789ab --name contoso-vm
- name: Link an HCRP Machine to a vCenter in another subscription using the machine id.
text: |-
az connectedvmware vm create \
--machine-id /subscriptions/01234567-0123-0123-0123-0123456789ab/resourceGroups/contoso-rg/providers/Microsoft.HybridCompute/machines/contoso-vm \
--vcenter /subscriptions/fedcba98-7654-3210-0123-456789abcdef/resourceGroups/contoso-rg/providers/Microsoft.HybridCompute/vcenters/contoso-vcenter
- name: Link an HCRP Machine to a vCenter in another subscription using the machine name.
text: |-
az connectedvmware vm create \
--resource-group contoso-rg --location eastus --name hcrp-contoso-machine \
--vcenter /subscriptions/fedcba98-7654-3210-0123-456789abcdef/resourceGroups/contoso-rg/providers/Microsoft.HybridCompute/vcenters/contoso-vcenter
- name: Link an HCRP Machine to a vCenter in the same subscription and resource group.
text: |-
az connectedvmware vm create \
--resource-group contoso-rg --location eastus --name hcrp-contoso-machine \
--vcenter contoso-vcenter
"""

helps[
Expand Down
11 changes: 11 additions & 0 deletions src/connectedvmware/azext_connectedvmware/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
get_default_location_from_resource_group,
validate_file_or_dict
)
from ._validators import process_missing_vm_resource_parameters
from ._actions import VmNicAddAction, VmDiskAddAction


Expand Down Expand Up @@ -108,6 +109,16 @@ def load_arguments(self, _):
c.argument('force', action='store_true', help="Whether force delete or not.")

with self.argument_context('connectedvmware vm create') as c:
c.argument(
'resource_name', resource_name, options_list=['--name', '-n'],
help="Name of the HCRP Machine resource.",
)
c.argument(
'machine_id',
help="ARM ID of the Microsoft.HybridCompute Machine resource which you want to link to vCenter",
options_list=['--machine-id', '-m'],
validator=process_missing_vm_resource_parameters
)
c.argument(
'vm_template', help="Name or ID of the vm template for deploying the vm.",
)
Expand Down
72 changes: 55 additions & 17 deletions src/connectedvmware/azext_connectedvmware/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,59 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from operator import and_, xor
from azure.cli.core.azclierror import (
ValidationError
)
from azure.cli.core.commands.client_factory import get_subscription_id
from msrestazure.tools import is_valid_resource_id, resource_id


def example_name_or_id_validator(cmd, namespace):
# This is an example validator , we can write our validators using this pattern as needed.
# Example of a storage account name or ID validator.
# See: https://github.com/Azure/azure-cli/blob/dev/doc/authoring_command_modules/authoring_commands.md#supporting-name-or-id-parameters # pylint: disable=C0301

if namespace.storage_account:
if not is_valid_resource_id(namespace.RESOURCE):
namespace.storage_account = resource_id(
subscription=get_subscription_id(cmd.cli_ctx),
resource_group=namespace.resource_group_name,
namespace='Microsoft.Storage',
type='storageAccounts',
name=namespace.storage_account,
)
from msrestazure.tools import is_valid_resource_id


def process_missing_vm_resource_parameters(cmd, namespace):
err_msg = (
"Invalid machine id provided. "
"Expected format: '/subscriptions/01234567-0123-0123-0123-0123456789ab"
"/resourceGroups/contoso-rg/providers/Microsoft.HybridCompute/machines/contoso-vm'."
)

def exists(val):
return val is not None

if not and_(
xor(
exists(namespace.machine_id),
and_(exists(namespace.resource_group_name), exists(namespace.resource_name))
),
not xor(
exists(namespace.resource_group_name),
exists(namespace.resource_name)
)
):
raise ValidationError(
"Please specify either (--machine-id) or (--resource-group-name and --resource-name) but not both."
)

if not exists(namespace.machine_id):
return

if not is_valid_resource_id(namespace.machine_id):
raise ValidationError(err_msg)
import re
machine_id_pattern = re.compile(r"^/subscriptions/([^/]+)/resourceGroups/([^/]+)/providers/Microsoft.HybridCompute/machines/([^/]+)$") # noqa: E501 pylint: disable=line-too-long
match = machine_id_pattern.match(namespace.machine_id)
if not match:
raise ValidationError(err_msg)
subscription_id = match.group(1)
resource_group_name = match.group(2)
resource_name = match.group(3)

cli_sub = get_subscription_id(cmd.cli_ctx)
if subscription_id != cli_sub:
raise ValidationError(
f"Subscription id of the machine, '{subscription_id}', "
f"does not match the current subscription, '{cli_sub}'. "
f"Please use or switch to the correct subscription."
)

namespace.resource_group_name = resource_group_name
namespace.resource_name = resource_name
Loading

0 comments on commit dab6605

Please sign in to comment.