Skip to content

Commit 43198c7

Browse files
committed
implement bin/xgrade
1 parent 26de3c8 commit 43198c7

4 files changed

Lines changed: 93 additions & 1 deletion

File tree

bin/xgrade

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
command

hypernode_api_python/client.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,6 @@ def check_xgrade(self, app_name, product_code):
761761
HYPERNODE_API_APP_XGRADE_CHECK_ENDPOINT.format(app_name, product_code),
762762
)
763763

764-
# TODO: add entrypoint for this method in bin/ and commands.py
765764
def xgrade(self, app_name, data):
766765
"""
767766
Change the product of a Hypernode to a different plan. This will initiate

hypernode_api_python/commands.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,3 +588,33 @@ def check_xgrade(args=None):
588588
args = parser.parse_args(args=args)
589589
app_name = get_app_name()
590590
print_response(client.check_xgrade(app_name, args.product_code))
591+
592+
593+
def xgrade(args=None):
594+
parser = ArgumentParser(
595+
description="""
596+
Change the plan of your Hypernode.
597+
598+
Example:
599+
$ ./bin/xgrade FALCON_L_202203
600+
The job to xgrade Hypernode 'yourappname' to product 'FALCON_L_202203' has been posted
601+
""",
602+
formatter_class=RawTextHelpFormatter,
603+
)
604+
client = get_client()
605+
parser.add_argument(
606+
"product_code",
607+
help="The code of the product to check",
608+
choices=[p["code"] for p in client.get_active_products().json()],
609+
)
610+
args = parser.parse_args(args=args)
611+
app_name = get_app_name()
612+
data = {"product": args.product_code}
613+
output = client.xgrade(app_name, data=data).content
614+
if output:
615+
print(output)
616+
else:
617+
print(
618+
"The job to xgrade Hypernode '{}' to product '{}' "
619+
"has been posted".format(app_name, args.product_code)
620+
)

tests/commands/test_xgrade.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from unittest.mock import Mock
2+
3+
from hypernode_api_python.commands import xgrade
4+
from tests.testcase import TestCase
5+
6+
7+
class TestXgrade(TestCase):
8+
def setUp(self):
9+
self.print = self.set_up_patch("hypernode_api_python.commands.print")
10+
self.get_client = self.set_up_patch("hypernode_api_python.commands.get_client")
11+
self.client = self.get_client.return_value
12+
self.get_app_name = self.set_up_patch(
13+
"hypernode_api_python.commands.get_app_name"
14+
)
15+
self.get_app_name.return_value = "myappname"
16+
self.client.get_xgrade_descriptions.return_value = Mock(
17+
json=lambda: {"FALCON_L_202203": "", "BlockDownloaderBruteForce": ""}
18+
)
19+
self.client.xgrade.return_value = Mock(content="")
20+
self.client.get_active_products.return_value.json.return_value = [
21+
{
22+
"code": "FALCON_L_202203",
23+
},
24+
{
25+
"code": "FALCON_6XL_202203",
26+
},
27+
]
28+
29+
def test_xgrade_gets_client(self):
30+
xgrade(["FALCON_L_202203"])
31+
32+
self.get_client.assert_called_once_with()
33+
34+
def test_xgrade_performs_xgrade(self):
35+
xgrade(["FALCON_L_202203"])
36+
37+
self.client.xgrade.assert_called_once_with(
38+
"myappname", data={"product": "FALCON_L_202203"}
39+
)
40+
41+
def test_xgrade_prints_output_on_success(self):
42+
xgrade(["FALCON_L_202203"])
43+
44+
self.print.assert_called_once_with(
45+
"The job to xgrade Hypernode 'myappname' to product 'FALCON_L_202203' has been posted"
46+
)
47+
48+
def test_xgrade_prints_output_on_failure(self):
49+
self.client.xgrade.return_value = Mock(
50+
content='{"product":["Object with code=FALCON_6XL_202203 does not exist."]}'
51+
)
52+
53+
xgrade(["FALCON_6XL_202203"])
54+
55+
self.print.assert_called_once_with(
56+
'{"product":["Object with code=FALCON_6XL_202203 does not exist."]}'
57+
)
58+
59+
def test_xgrade_raises_on_invalid_choice(self):
60+
with self.assertRaises(SystemExit):
61+
# We get the valid choices from get_xgrade_descriptions
62+
xgrade(["DoesNotExist"])

0 commit comments

Comments
 (0)