-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathcustom.py
More file actions
220 lines (193 loc) · 7.98 KB
/
custom.py
File metadata and controls
220 lines (193 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
#
# Code generated by aaz-dev-tools
# --------------------------------------------------------------------------------------------
# pylint: disable=too-many-lines
# pylint: disable=too-many-statements, protected-access
from azext_stack_hci.aaz.latest.stack_hci.cluster import List as _ClusterList
from azext_stack_hci.aaz.latest.stack_hci.cluster import Create as _ClusterCreate
from azext_stack_hci.aaz.latest.stack_hci.cluster.identity import Assign as _IdentityAssign
from azext_stack_hci.aaz.latest.stack_hci.cluster.identity import Remove as _IdentityRemove
class ClusterList(_ClusterList):
def post_operations(self):
# next_link cannot be accessed
if self.ctx.vars.instance.next_link.to_serialized_data():
self.ctx.vars.instance.next_link = None
class ClusterCreate(_ClusterCreate):
@classmethod
def _build_arguments_schema(cls, *args, **kwargs):
from azure.cli.core.aaz import AAZBoolArg
args_schema = super()._build_arguments_schema(*args, **kwargs)
args_schema.mi_system_assigned = AAZBoolArg(
options=["--mi-system-assigned"],
help="Enable system assigned identity"
)
args_schema.identity._registered = False
return args_schema
def pre_operations(self):
args = self.ctx.args
if args.mi_system_assigned:
args.identity.type = "SystemAssigned"
class IdentityAssign(_IdentityAssign):
@classmethod
def _build_arguments_schema(cls, *args, **kwargs):
from azure.cli.core.aaz import AAZBoolArg
args_schema = super()._build_arguments_schema(*args, **kwargs)
args_schema.system_assigned = AAZBoolArg(
options=["--system-assigned"],
help="Enable system assigned identity"
)
args_schema.type._registered = False
args_schema.type._required = False
args_schema.user_assigned_identities._registered = False
return args_schema
def pre_operations(self):
args = self.ctx.args
if args.system_assigned:
args.type = "SystemAssigned"
class IdentityRemove(_IdentityRemove):
@classmethod
def _build_arguments_schema(cls, *args, **kwargs):
from azure.cli.core.aaz import AAZBoolArg
args_schema = super()._build_arguments_schema(*args, **kwargs)
args_schema.system_assigned = AAZBoolArg(
options=["--system-assigned"],
help="Enable system assigned identity"
)
args_schema.type._registered = False
args_schema.user_assigned_identities._registered = False
return args_schema
def pre_instance_update(self, instance):
args = self.ctx.args
if args.system_assigned:
args.type = 'None'
class VmConnectEnable(_VmConnectEnable):
@classmethod
def _build_arguments_schema(cls, *args, **kwargs):
from azure.cli.core.aaz import AAZResourceGroupNameArg, AAZStrArg
args_schema = super()._build_arguments_schema(*args, **kwargs)
args_schema.cluster_name = AAZStrArg(
options=["--cluster-name", "-n"],
help="The name of the cluster.",
required=True
)
args_schema.resource_group = AAZResourceGroupNameArg(
options=["--resource-group", "-g"],
help="Name of resource group.",
required=True
)
args_schema.vm_name = AAZStrArg(
options=["--vm-name"],
help="The name of the virtual machine.",
required=True
)
return args_schema
def __call__(self, cmd, **kwargs):
from azure.cli.core.commands.client_factory import get_subscription_id
from azure.cli.core.util import send_raw_request
import json
cluster_name = kwargs.get('cluster_name')
resource_group = kwargs.get('resource_group')
vm_name = kwargs.get('vm_name')
subscription_id = get_subscription_id(cmd.cli_ctx)
# Construct the REST API path
path = (
f"/subscriptions/{subscription_id}/resourceGroups/{resource_group}/"
"providers/Microsoft.AzureStackHCI/clusters/"
f"{cluster_name}/jobs/VmConnectDeprovision"
)
# API version
api_version = "2023-12-01-preview"
url = f"https://management.azure.com{path}?api-version={api_version}"
# Default payload with VM name
payload = {
"properties": {
"jobType": "VmConnectProvision",
"deploymentMode": "Deploy",
"vmConnectProvisionJobDetails": [
{
"vmName": vm_name
}
]
}
}
# Make the REST API call
try:
response = send_raw_request(cmd.cli_ctx, "PUT", url, body=json.dumps(payload))
if response.content:
return response.json()
else:
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)}")
class VmConnectDisable(_VmConnectDisable):
@classmethod
def _build_arguments_schema(cls, *args, **kwargs):
from azure.cli.core.aaz import AAZResourceGroupNameArg, AAZStrArg
args_schema = super()._build_arguments_schema(*args, **kwargs)
args_schema.cluster_name = AAZStrArg(
options=["--cluster-name", "-n"],
help="The name of the cluster.",
required=True
)
args_schema.resource_group = AAZResourceGroupNameArg(
options=["--resource-group", "-g"],
help="Name of resource group.",
required=True
)
args_schema.vm_name = AAZStrArg(
options=["--vm-name"],
help="The name of the virtual machine.",
required=True
)
return args_schema
def __call__(self, cmd, **kwargs):
from azure.cli.core.commands.client_factory import get_subscription_id
from azure.cli.core.util import send_raw_request
import json
cluster_name = kwargs.get('cluster_name')
resource_group = kwargs.get('resource_group')
vm_name = kwargs.get('vm_name')
subscription_id = get_subscription_id(cmd.cli_ctx)
# Construct the REST API path for deprovision
path = (
f"/subscriptions/{subscription_id}/resourceGroups/{resource_group}/"
"providers/Microsoft.AzureStackHCI/clusters/"
f"{cluster_name}/jobs/VmConnectDeprovision"
)
# API version
api_version = "2023-12-01-preview"
url = f"https://management.azure.com{path}?api-version={api_version}"
# Payload for VM Connect deprovision
payload = {
"properties": {
"jobType": "VmConnectRemove",
"deploymentMode": "Deploy",
"vmConnectRemoveJobDetails": [
{
"vmName": vm_name
}
]
}
}
# Make the REST API call
try:
response = send_raw_request(cmd.cli_ctx, "PUT", url, body=json.dumps(payload))
if response.content:
return response.json()
else:
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 disable VM Connect for VM '{vm_name}': {str(e)}")