Skip to content
This repository has been archived by the owner on Aug 13, 2024. It is now read-only.

Add AZURE_LOCATION as default --location in capi create #143

Merged
merged 4 commits into from
Jun 14, 2022
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
5 changes: 3 additions & 2 deletions src/capi/azext_capi/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@
- name: --location -l
type: string
long-summary: |
If not specified, the location of the --resource-group will be used.
Required if --resource-group is not specified or does not yet exist
If not specified, default configured location or AZURE_LOCATION will be used (in this order).
If --resource-group is specified and already exists, the location has to match.
Required if default location is not configured or AZURE_LOCATION is not set.
- name: --machinepool -m
type: bool
short-summary: Use experimental MachinePools instead of MachineDeployments
Expand Down
3 changes: 3 additions & 0 deletions src/capi/azext_capi/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,9 @@ def create_workload_cluster( # pylint: disable=too-many-arguments,too-many-loca
bootstrap_commands=None,
yes=False):

if location is None:
location = os.environ.get("AZURE_LOCATION", None)

if not capi_name:
from .helpers.names import generate_cluster_name
capi_name = generate_cluster_name()
Expand Down
9 changes: 8 additions & 1 deletion src/capi/azext_capi/tests/latest/test_capi_scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# --------------------------------------------------------------------------------------------

import os
from unittest.mock import MagicMock, Mock, patch
from unittest.mock import MagicMock, Mock, patch, ANY

from azure.cli.testsdk.scenario_tests import AllowLargeResponse
from azure.cli.core.azclierror import InvalidArgumentValueError
Expand Down Expand Up @@ -37,13 +37,20 @@ def test_capi_create(self, mock_def):
with patch('azext_capi._client_factory.cf_resource_groups') as cf_resource_groups:
with self.assertRaises(InvalidArgumentValueError):
self.cmd('capi create -n myCluster -g existingRG --location bogusLocation')

mock_client = MagicMock()
mock_client.get.side_effect = CloudError(Mock(response_status=404), "Resource group 'myCluster' could not be found.")
# New RG, but no --location specified
with patch('azext_capi._client_factory.cf_resource_groups') as cf_resource_groups:
cf_resource_groups.return_value = mock_client
with self.assertRaises(RequiredArgumentMissingError):
self.cmd('capi create -n myClusterName -g myCluster')
# New RG, no --location, AZURE_LOCATION set
with patch.dict('os.environ', {"AZURE_LOCATION": "westus3"}):
with patch('azext_capi.custom.check_resource_group') as mock_check_rg:
with self.assertRaises(NoTTYException):
self.cmd('capi create -n myClusterName -g myCluster')
mock_check_rg.assert_called_with(ANY, ANY, ANY, "westus3")
# New RG, --location specified but no --resource-group name
with patch('azext_capi._client_factory.cf_resource_groups') as cf_resource_groups:
cf_resource_groups.return_value = mock_client
Expand Down