Skip to content
21 changes: 21 additions & 0 deletions src/stack-hci/azext_stack_hci/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,24 @@
# pylint: disable=too-many-lines

from knack.help_files import helps # pylint: disable=unused-import

helps['stack-hci vmconnect'] = """
type: group
short-summary: Manage VM Connect for Azure Stack HCI virtual machines.
"""

helps['stack-hci vmconnect enable'] = """
type: command
short-summary: Enable VM Connect for a virtual machine in an Azure Stack HCI cluster.
examples:
- name: Enable VM Connect
text: az stack-hci vmconnect enable --cluster-name MyCluster --resource-group MyResourceGroup --vm-name MyVM
"""

helps['stack-hci vmconnect disable'] = """
type: command
short-summary: Disable VM Connect for a virtual machine in an Azure Stack HCI cluster.
examples:
- name: Disable VM Connect
text: az stack-hci vmconnect disable --cluster-name MyCluster --resource-group MyResourceGroup --vm-name MyVM
"""
10 changes: 9 additions & 1 deletion src/stack-hci/azext_stack_hci/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@


def load_arguments(self, _): # pylint: disable=unused-argument
pass
with self.argument_context('stack-hci vmconnect enable') as c:
c.argument('cluster_name', options_list=['--cluster-name', '-n'], help='The name of the cluster.')
c.argument('resource_group', options_list=['--resource-group', '-g'], help='Name of resource group.')
c.argument('vm_name', options_list=['--vm-name'], help='The name of the virtual machine.')

with self.argument_context('stack-hci vmconnect disable') as c:
c.argument('cluster_name', options_list=['--cluster-name', '-n'], help='The name of the cluster.')
c.argument('resource_group', options_list=['--resource-group', '-g'], help='Name of resource group.')
c.argument('vm_name', options_list=['--vm-name'], help='The name of the virtual machine.')
4 changes: 4 additions & 0 deletions src/stack-hci/azext_stack_hci/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ def load_command_table(self, _): # pylint: disable=unused-argument
self.command_table['stack-hci cluster create'] = ClusterCreate(loader=self)
self.command_table['stack-hci cluster identity assign'] = IdentityAssign(loader=self)
self.command_table['stack-hci cluster identity remove'] = IdentityRemove(loader=self)

with self.command_group('stack-hci vmconnect', ...) as g:
g.custom_command('enable', 'vmconnect_enable')
g.custom_command('disable', 'vmconnect_disable')
70 changes: 70 additions & 0 deletions src/stack-hci/azext_stack_hci/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,73 @@ def pre_instance_update(self, instance):
args = self.ctx.args
if args.system_assigned:
args.type = 'None'


def vmconnect_enable(cmd, cluster_name, resource_group, vm_name):
"""
Enable VM Connect for a virtual machine in an Azure Stack HCI cluster.
"""
from azure.cli.core.commands.client_factory import get_subscription_id
from azure.cli.core.util import send_raw_request
import json

subscription_id = get_subscription_id(cmd.cli_ctx)
path = (
f"/subscriptions/{subscription_id}/resourceGroups/{resource_group}/"
f"providers/Microsoft.AzureStackHCI/clusters/{cluster_name}/jobs/VmConnectProvision"
)
api_version = "2023-12-01-preview"
url = f"https://management.azure.com{path}?api-version={api_version}"
payload = {
"properties": {
"jobType": "VmConnectProvision",
"deploymentMode": "Deploy",
"vmConnectProvisionJobDetails": [
{"vmName": vm_name}
]
}
}

try:
response = send_raw_request(cmd.cli_ctx, "PUT", url, body=json.dumps(payload))
if response.content:
return response.json()
return {"message": f"VM Connect provision job initiated successfully for VM: {vm_name}"}
except Exception as e:
from azure.cli.core.util import CLIError
raise CLIError(f"Failed to enable VM Connect for VM '{vm_name}': {str(e)}")


def vmconnect_disable(cmd, cluster_name, resource_group, vm_name):
"""
Disable VM Connect for a virtual machine in an Azure Stack HCI cluster.
"""
from azure.cli.core.commands.client_factory import get_subscription_id
from azure.cli.core.util import send_raw_request
import json

subscription_id = get_subscription_id(cmd.cli_ctx)
path = (
f"/subscriptions/{subscription_id}/resourceGroups/{resource_group}/"
f"providers/Microsoft.AzureStackHCI/clusters/{cluster_name}/jobs/VmConnectRemove"
)
api_version = "2023-12-01-preview"
url = f"https://management.azure.com{path}?api-version={api_version}"
payload = {
"properties": {
"jobType": "VmConnectRemove",
"deploymentMode": "Deploy",
"vmConnectRemoveJobDetails": [
{"vmName": vm_name}
]
}
}

try:
response = send_raw_request(cmd.cli_ctx, "PUT", url, body=json.dumps(payload))
if response.content:
return response.json()
return {"message": f"VM Connect remove job initiated successfully for VM: {vm_name}"}
except Exception as e:
from azure.cli.core.util import CLIError
raise CLIError(f"Failed to disable VM Connect for VM '{vm_name}': {str(e)}")
Loading