Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/azure-cli/azure/cli/command_modules/acs/_consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,21 @@


# consts for decorator pattern
# decorator mode
class DecoratorMode(Enum):
"""Enumerations used to distinguish whether to handle creation or update.
"""
CREATE = 1
UPDATE = 2


class AgentPoolDecoratorMode(Enum):
"""Enumerations used to distinguish whether to deal with the default system agentpool in the context of the cluster
or any specific agentpool.
"""
MANAGED_CLUSTER = 1
STANDALONE = 2


# custom exception for decorator pattern, used for gracefully exit
class DecoratorEarlyExitException(Exception):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
from typing import Dict, Tuple, TypeVar, Union

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

def __init__(
self,
cmd: AzCommandsLoader,
resource_type: ResourceType,
agentpool_decorator_mode: AgentPoolDecoratorMode,
):
super().__init__(cmd, resource_type)
self.agentpool_decorator_mode = agentpool_decorator_mode
self.UnifiedAgentPoolModel = self.__choose_agentpool_model_by_agentpool_decorator_mode()

def __choose_agentpool_model_by_agentpool_decorator_mode(self):
"""Choose the model reference for agentpool based on agentpool_decorator_mode.
"""
if self.agentpool_decorator_mode == AgentPoolDecoratorMode.MANAGED_CLUSTER:
return self.ManagedClusterAgentPoolProfile
return self.AgentPool


# pylint: disable=too-few-public-methods
class AKSAgentPoolParamDict(BaseAKSParamDict):
Expand All @@ -48,8 +66,10 @@ def __init__(
raw_parameters: AKSAgentPoolParamDict,
models: AKSAgentPoolModels,
decorator_mode: DecoratorMode,
agentpool_decorator_mode: AgentPoolDecoratorMode,
):
super().__init__(cmd, raw_parameters, models, decorator_mode)
self.agentpool_decorator_mode = agentpool_decorator_mode
self.agentpool = None

# pylint: disable=no-self-use
Expand Down Expand Up @@ -350,6 +370,7 @@ def __init__(
client: AgentPoolsOperations,
raw_parameters: Dict,
resource_type: ResourceType,
agentpool_decorator_mode: AgentPoolDecoratorMode,
):
"""Internal controller of aks_agentpool_add.

Expand All @@ -361,10 +382,11 @@ def __init__(
"""
self.cmd = cmd
self.client = client
self.models = AKSAgentPoolModels(cmd, resource_type)
self.agentpool_decorator_mode = agentpool_decorator_mode
self.models = AKSAgentPoolModels(cmd, resource_type, agentpool_decorator_mode)
# store the context in the process of assemble the AgentPool object
self.context = AKSAgentPoolContext(
cmd, AKSAgentPoolParamDict(raw_parameters), self.models, decorator_mode=DecoratorMode.CREATE
cmd, AKSAgentPoolParamDict(raw_parameters), self.models, DecoratorMode.CREATE, agentpool_decorator_mode
)

def _ensure_agentpool(self, agentpool: AgentPool) -> None:
Expand All @@ -376,7 +398,7 @@ def _ensure_agentpool(self, agentpool: AgentPool) -> None:

:return: None
"""
if not isinstance(agentpool, self.models.AgentPool):
if not isinstance(agentpool, self.models.UnifiedAgentPoolModel):
raise CLIInternalError(
"Unexpected agentpool object with type '{}'.".format(type(agentpool))
)
Expand All @@ -390,17 +412,18 @@ def _ensure_agentpool(self, agentpool: AgentPool) -> None:
def init_agentpool(self) -> AgentPool:
"""Initialize an AgentPool object with name and attach it to internal context.

Note: As a read only property, name would be ignored when serialized.

:return: the AgentPool object
"""
# Initialize a AgentPool object with name.
agentpool = self.models.AgentPool()
# Note: As a read only property, name would be ignored when serialized.
# Set the name property by explicit assignment, otherwise it will be ignored by initialization.
agentpool.name = self.context.get_nodepool_name()
if self.agentpool_decorator_mode == AgentPoolDecoratorMode.MANAGED_CLUSTER:
# Note: As a required property, name must be provided during initialization.
agentpool = self.models.UnifiedAgentPoolModel(name=self.context.get_nodepool_name())
else:
# Note: As a read only property, name would be ignored when serialized.
# Set the name property by explicit assignment, otherwise it will be ignored by initialization.
agentpool = self.models.UnifiedAgentPoolModel()
agentpool.name = self.context.get_nodepool_name()

# attach mc to AKSContext
# attach agentpool to AKSAgentPoolContext
self.context.attach_agentpool(agentpool)
return agentpool

Expand Down
13 changes: 9 additions & 4 deletions src/azure-cli/azure/cli/command_modules/acs/base_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def validate_decorator_mode(decorator_mode) -> bool:
return is_valid_decorator_mode


# pylint: disable=too-few-public-methods
class BaseAKSModels:
"""A base class for storing the models used by aks commands.

Expand All @@ -43,18 +44,22 @@ def __init__(
self.__cmd = cmd
self.__raw_models = None
self.resource_type = resource_type
self.set_up_models()
self.__set_up_base_aks_models()

@property
def raw_models(self):
"""Load the module that stores all aks models.
"""
if self.__raw_models is None:
self.__raw_models = self.__cmd.get_models(
resource_type=self.resource_type,
operation_group="managed_clusters",
).models
return self.__raw_models

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

def get(self, key):
def get(self, key, default=None):
self.__increase(key)
return self.__store.get(key)
return self.__store.get(key, default)

def keys(self):
return self.__store.keys()
Expand Down
Loading