Skip to content

Commit 4ff8080

Browse files
authored
{AKS} Refactor aks nodepool add - part 5 (#21740)
* add base decorator * align model & add test cases * fix lint & add default param to get in BaseAKSParamDict * fix lint
1 parent f2916ec commit 4ff8080

File tree

5 files changed

+325
-86
lines changed

5 files changed

+325
-86
lines changed

src/azure-cli/azure/cli/command_modules/acs/_consts.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,21 @@
102102

103103

104104
# consts for decorator pattern
105-
# decorator mode
106105
class DecoratorMode(Enum):
106+
"""Enumerations used to distinguish whether to handle creation or update.
107+
"""
107108
CREATE = 1
108109
UPDATE = 2
109110

110111

112+
class AgentPoolDecoratorMode(Enum):
113+
"""Enumerations used to distinguish whether to deal with the default system agentpool in the context of the cluster
114+
or any specific agentpool.
115+
"""
116+
MANAGED_CLUSTER = 1
117+
STANDALONE = 2
118+
119+
111120
# custom exception for decorator pattern, used for gracefully exit
112121
class DecoratorEarlyExitException(Exception):
113122
pass

src/azure-cli/azure/cli/command_modules/acs/agentpool_decorator.py

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
from typing import Dict, Tuple, TypeVar, Union
77

88
from azure.cli.command_modules.acs._client_factory import cf_agent_pools
9-
from azure.cli.command_modules.acs._consts import DecoratorMode
9+
from azure.cli.command_modules.acs._consts import DecoratorMode, AgentPoolDecoratorMode
1010
from azure.cli.command_modules.acs._validators import extract_comma_separated_string
1111
from azure.cli.command_modules.acs.base_decorator import BaseAKSContext, BaseAKSModels, BaseAKSParamDict
12+
from azure.cli.core import AzCommandsLoader
1213
from azure.cli.core.azclierror import CLIInternalError, InvalidArgumentValueError, RequiredArgumentMissingError
1314
from azure.cli.core.commands import AzCliCommand
1415
from azure.cli.core.profiles import ResourceType
@@ -29,6 +30,23 @@ class AKSAgentPoolModels(BaseAKSModels):
2930
The api version of the class corresponding to a model is determined by resource_type.
3031
"""
3132

33+
def __init__(
34+
self,
35+
cmd: AzCommandsLoader,
36+
resource_type: ResourceType,
37+
agentpool_decorator_mode: AgentPoolDecoratorMode,
38+
):
39+
super().__init__(cmd, resource_type)
40+
self.agentpool_decorator_mode = agentpool_decorator_mode
41+
self.UnifiedAgentPoolModel = self.__choose_agentpool_model_by_agentpool_decorator_mode()
42+
43+
def __choose_agentpool_model_by_agentpool_decorator_mode(self):
44+
"""Choose the model reference for agentpool based on agentpool_decorator_mode.
45+
"""
46+
if self.agentpool_decorator_mode == AgentPoolDecoratorMode.MANAGED_CLUSTER:
47+
return self.ManagedClusterAgentPoolProfile
48+
return self.AgentPool
49+
3250

3351
# pylint: disable=too-few-public-methods
3452
class AKSAgentPoolParamDict(BaseAKSParamDict):
@@ -48,8 +66,10 @@ def __init__(
4866
raw_parameters: AKSAgentPoolParamDict,
4967
models: AKSAgentPoolModels,
5068
decorator_mode: DecoratorMode,
69+
agentpool_decorator_mode: AgentPoolDecoratorMode,
5170
):
5271
super().__init__(cmd, raw_parameters, models, decorator_mode)
72+
self.agentpool_decorator_mode = agentpool_decorator_mode
5373
self.agentpool = None
5474

5575
# pylint: disable=no-self-use
@@ -350,6 +370,7 @@ def __init__(
350370
client: AgentPoolsOperations,
351371
raw_parameters: Dict,
352372
resource_type: ResourceType,
373+
agentpool_decorator_mode: AgentPoolDecoratorMode,
353374
):
354375
"""Internal controller of aks_agentpool_add.
355376
@@ -361,10 +382,11 @@ def __init__(
361382
"""
362383
self.cmd = cmd
363384
self.client = client
364-
self.models = AKSAgentPoolModels(cmd, resource_type)
385+
self.agentpool_decorator_mode = agentpool_decorator_mode
386+
self.models = AKSAgentPoolModels(cmd, resource_type, agentpool_decorator_mode)
365387
# store the context in the process of assemble the AgentPool object
366388
self.context = AKSAgentPoolContext(
367-
cmd, AKSAgentPoolParamDict(raw_parameters), self.models, decorator_mode=DecoratorMode.CREATE
389+
cmd, AKSAgentPoolParamDict(raw_parameters), self.models, DecoratorMode.CREATE, agentpool_decorator_mode
368390
)
369391

370392
def _ensure_agentpool(self, agentpool: AgentPool) -> None:
@@ -376,7 +398,7 @@ def _ensure_agentpool(self, agentpool: AgentPool) -> None:
376398
377399
:return: None
378400
"""
379-
if not isinstance(agentpool, self.models.AgentPool):
401+
if not isinstance(agentpool, self.models.UnifiedAgentPoolModel):
380402
raise CLIInternalError(
381403
"Unexpected agentpool object with type '{}'.".format(type(agentpool))
382404
)
@@ -390,17 +412,18 @@ def _ensure_agentpool(self, agentpool: AgentPool) -> None:
390412
def init_agentpool(self) -> AgentPool:
391413
"""Initialize an AgentPool object with name and attach it to internal context.
392414
393-
Note: As a read only property, name would be ignored when serialized.
394-
395415
:return: the AgentPool object
396416
"""
397-
# Initialize a AgentPool object with name.
398-
agentpool = self.models.AgentPool()
399-
# Note: As a read only property, name would be ignored when serialized.
400-
# Set the name property by explicit assignment, otherwise it will be ignored by initialization.
401-
agentpool.name = self.context.get_nodepool_name()
417+
if self.agentpool_decorator_mode == AgentPoolDecoratorMode.MANAGED_CLUSTER:
418+
# Note: As a required property, name must be provided during initialization.
419+
agentpool = self.models.UnifiedAgentPoolModel(name=self.context.get_nodepool_name())
420+
else:
421+
# Note: As a read only property, name would be ignored when serialized.
422+
# Set the name property by explicit assignment, otherwise it will be ignored by initialization.
423+
agentpool = self.models.UnifiedAgentPoolModel()
424+
agentpool.name = self.context.get_nodepool_name()
402425

403-
# attach mc to AKSContext
426+
# attach agentpool to AKSAgentPoolContext
404427
self.context.attach_agentpool(agentpool)
405428
return agentpool
406429

src/azure-cli/azure/cli/command_modules/acs/base_decorator.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def validate_decorator_mode(decorator_mode) -> bool:
3030
return is_valid_decorator_mode
3131

3232

33+
# pylint: disable=too-few-public-methods
3334
class BaseAKSModels:
3435
"""A base class for storing the models used by aks commands.
3536
@@ -43,18 +44,22 @@ def __init__(
4344
self.__cmd = cmd
4445
self.__raw_models = None
4546
self.resource_type = resource_type
46-
self.set_up_models()
47+
self.__set_up_base_aks_models()
4748

4849
@property
4950
def raw_models(self):
51+
"""Load the module that stores all aks models.
52+
"""
5053
if self.__raw_models is None:
5154
self.__raw_models = self.__cmd.get_models(
5255
resource_type=self.resource_type,
5356
operation_group="managed_clusters",
5457
).models
5558
return self.__raw_models
5659

57-
def set_up_models(self):
60+
def __set_up_base_aks_models(self):
61+
"""Expose all aks models as properties of the class.
62+
"""
5863
for model_name, model_class in vars(self.raw_models).items():
5964
if not model_name.startswith('_'):
6065
setattr(self, model_name, model_class)
@@ -78,9 +83,9 @@ def __init__(self, param_dict):
7883
def __increase(self, key):
7984
self.__count[key] = self.__count.get(key, 0) + 1
8085

81-
def get(self, key):
86+
def get(self, key, default=None):
8287
self.__increase(key)
83-
return self.__store.get(key)
88+
return self.__store.get(key, default)
8489

8590
def keys(self):
8691
return self.__store.keys()

0 commit comments

Comments
 (0)