Skip to content

Commit

Permalink
Use property decorator on cluster_description
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxiang-zhang committed Aug 31, 2023
1 parent 4776fb2 commit ec19094
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
19 changes: 9 additions & 10 deletions awscli/customizations/eks/update_kubeconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ def __init__(self, session, cluster_name, role_arn, parsed_globals=None):
self._cluster_description = None
self._globals = parsed_globals

def _get_cluster_description(self):
@property
def cluster_description(self):
"""
Use an eks describe-cluster call to get the cluster description
Cache the response in self._cluster_description.
Expand Down Expand Up @@ -276,10 +277,9 @@ def get_cluster_entry(self):
the previously obtained description.
"""

cert_data = self._get_cluster_description().get("certificateAuthority",
{"data": ""})["data"]
endpoint = self._get_cluster_description().get("endpoint")
arn = self._get_cluster_description().get("arn")
cert_data = self.cluster_description.get("certificateAuthority", {}).get("data", "")
endpoint = self.cluster_description.get("endpoint")
arn = self.cluster_description.get("arn")

return OrderedDict([
("cluster", OrderedDict([
Expand All @@ -294,20 +294,19 @@ def get_user_entry(self, user_alias=None):
Return a user entry generated using
the previously obtained description.
"""
cluster_description = self._get_cluster_description()
region = cluster_description.get("arn").split(":")[3]
outpost_config = cluster_description.get("outpostConfig")
region = self.cluster_description.get("arn").split(":")[3]
outpost_config = self.cluster_description.get("outpostConfig")

if outpost_config is None:
cluster_identification_parameter = "--cluster-name"
cluster_identification_value = self._cluster_name
else:
# If cluster contains outpostConfig, use id for identification
cluster_identification_parameter = "--cluster-id"
cluster_identification_value = cluster_description.get("id")
cluster_identification_value = self.cluster_description.get("id")

generated_user = OrderedDict([
("name", user_alias or self._get_cluster_description().get("arn", "")),
("name", user_alias or self.cluster_description.get("arn", "")),
("user", OrderedDict([
("exec", OrderedDict([
("apiVersion", API_VERSION),
Expand Down
14 changes: 7 additions & 7 deletions tests/unit/customizations/eks/test_update_kubeconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def setUp(self):
self._client = EKSClient(self._session, "ExampleCluster", None)

def test_get_cluster_description(self):
self.assertEqual(self._client._get_cluster_description(),
self.assertEqual(self._client.cluster_description,
describe_cluster_response()["cluster"])
self._mock_client.describe_cluster.assert_called_once_with(
name="ExampleCluster"
Expand All @@ -230,8 +230,8 @@ def test_get_cluster_description(self):
def test_get_cluster_description_no_status(self):
self._mock_client.describe_cluster.return_value = \
describe_cluster_no_status_response()
self.assertRaises(EKSClusterError,
self._client._get_cluster_description)
with self.assertRaises(EKSClusterError):
self._client.cluster_description
self._mock_client.describe_cluster.assert_called_once_with(
name="ExampleCluster"
)
Expand Down Expand Up @@ -276,8 +276,8 @@ def test_get_both(self):
def test_cluster_creating(self):
self._mock_client.describe_cluster.return_value =\
describe_cluster_creating_response()
self.assertRaises(EKSClusterError,
self._client._get_cluster_description)
with self.assertRaises(EKSClusterError):
self._client.cluster_description
self._mock_client.describe_cluster.assert_called_once_with(
name="ExampleCluster"
)
Expand All @@ -286,8 +286,8 @@ def test_cluster_creating(self):
def test_cluster_deleting(self):
self._mock_client.describe_cluster.return_value =\
describe_cluster_deleting_response()
self.assertRaises(EKSClusterError,
self._client._get_cluster_description)
with self.assertRaises(EKSClusterError):
self._client.cluster_description
self._mock_client.describe_cluster.assert_called_once_with(
name="ExampleCluster"
)
Expand Down

0 comments on commit ec19094

Please sign in to comment.