Skip to content

Commit

Permalink
implement bin/xgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
vdloo committed May 25, 2024
1 parent 26de3c8 commit 43198c7
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
1 change: 1 addition & 0 deletions bin/xgrade
1 change: 0 additions & 1 deletion hypernode_api_python/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,6 @@ def check_xgrade(self, app_name, product_code):
HYPERNODE_API_APP_XGRADE_CHECK_ENDPOINT.format(app_name, product_code),
)

# TODO: add entrypoint for this method in bin/ and commands.py
def xgrade(self, app_name, data):
"""
Change the product of a Hypernode to a different plan. This will initiate
Expand Down
30 changes: 30 additions & 0 deletions hypernode_api_python/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,33 @@ def check_xgrade(args=None):
args = parser.parse_args(args=args)
app_name = get_app_name()
print_response(client.check_xgrade(app_name, args.product_code))


def xgrade(args=None):
parser = ArgumentParser(
description="""
Change the plan of your Hypernode.
Example:
$ ./bin/xgrade FALCON_L_202203
The job to xgrade Hypernode 'yourappname' to product 'FALCON_L_202203' has been posted
""",
formatter_class=RawTextHelpFormatter,
)
client = get_client()
parser.add_argument(
"product_code",
help="The code of the product to check",
choices=[p["code"] for p in client.get_active_products().json()],
)
args = parser.parse_args(args=args)
app_name = get_app_name()
data = {"product": args.product_code}
output = client.xgrade(app_name, data=data).content
if output:
print(output)
else:
print(
"The job to xgrade Hypernode '{}' to product '{}' "
"has been posted".format(app_name, args.product_code)
)
62 changes: 62 additions & 0 deletions tests/commands/test_xgrade.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from unittest.mock import Mock

from hypernode_api_python.commands import xgrade
from tests.testcase import TestCase


class TestXgrade(TestCase):
def setUp(self):
self.print = self.set_up_patch("hypernode_api_python.commands.print")
self.get_client = self.set_up_patch("hypernode_api_python.commands.get_client")
self.client = self.get_client.return_value
self.get_app_name = self.set_up_patch(
"hypernode_api_python.commands.get_app_name"
)
self.get_app_name.return_value = "myappname"
self.client.get_xgrade_descriptions.return_value = Mock(
json=lambda: {"FALCON_L_202203": "", "BlockDownloaderBruteForce": ""}
)
self.client.xgrade.return_value = Mock(content="")
self.client.get_active_products.return_value.json.return_value = [
{
"code": "FALCON_L_202203",
},
{
"code": "FALCON_6XL_202203",
},
]

def test_xgrade_gets_client(self):
xgrade(["FALCON_L_202203"])

self.get_client.assert_called_once_with()

def test_xgrade_performs_xgrade(self):
xgrade(["FALCON_L_202203"])

self.client.xgrade.assert_called_once_with(
"myappname", data={"product": "FALCON_L_202203"}
)

def test_xgrade_prints_output_on_success(self):
xgrade(["FALCON_L_202203"])

self.print.assert_called_once_with(
"The job to xgrade Hypernode 'myappname' to product 'FALCON_L_202203' has been posted"
)

def test_xgrade_prints_output_on_failure(self):
self.client.xgrade.return_value = Mock(
content='{"product":["Object with code=FALCON_6XL_202203 does not exist."]}'
)

xgrade(["FALCON_6XL_202203"])

self.print.assert_called_once_with(
'{"product":["Object with code=FALCON_6XL_202203 does not exist."]}'
)

def test_xgrade_raises_on_invalid_choice(self):
with self.assertRaises(SystemExit):
# We get the valid choices from get_xgrade_descriptions
xgrade(["DoesNotExist"])

0 comments on commit 43198c7

Please sign in to comment.