diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0f6fd444a..1eed6d308 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -25,5 +25,80 @@ Code is tested and style checked with tox, you can run the tox tests individuall * create pull request +## Documentation +CLI command should have a more human readable style of documentation. +Manager methods should have a decent docblock describing any parameters and what the method does. +Docs are generated with [Sphinx](https://docs.readthedocs.io/en/latest/intro/getting-started-with-sphinx.html) and once Sphinx is setup, you can simply do + +`make html` in the softlayer-python/docs directory, which should generate the HTML in softlayer-python/docs/_build/html for testing. + + +## Unit Tests + +All new features should be 100% code covered, and your pull request should at the very least increase total code overage. + +### Mocks +To tests results from the API, we keep mock results in SoftLayer/fixtures// with the method name matching the variable name. + +Any call to a service that doesn't have a fixture will result in a TransportError + +### Overriding Fixtures + +Adding your expected output in the fixtures file with a unique name is a good way to define a fixture that gets used frequently in a test. + +```python +from SoftLayer.fixtures import SoftLayer_Product_Package + + def test_test(self): + amock = self.set_mock('SoftLayer_Product_Package', 'getAllObjects') + amock.return_value = fixtures.SoftLayer_Product_Package.RESERVED_CAPACITY +``` + +Otherwise defining it on the spot works too. +```python + def test_test(self): + mock = self.set_mock('SoftLayer_Network_Storage', 'getObject') + mock.return_value = { + 'billingItem': {'hourlyFlag': True, 'id': 449}, + } +``` + + +### Call testing +Testing your code to make sure it makes the correct API call is also very important. + +The testing.TestCase class has a method call `assert_called_with` which is pretty handy here. + +```python +self.assert_called_with( + 'SoftLayer_Billing_Item', # Service + 'cancelItem', # Method + args=(True, True, ''), # Args + identifier=449, # Id + mask=mock.ANY, # object Mask, + filter=mock.ANY, # object Filter + limit=0, # result Limit + offset=0 # result Offset +) +``` + +Making sure a API was NOT called + +```python +self.assertEqual([], self.calls('SoftLayer_Account', 'getObject')) +``` + +Making sure an API call has a specific arg, but you don't want to list out the entire API call (like with a place order test) + +```python +# Get the API Call signature +order_call = self.calls('SoftLayer_Product_Order', 'placeOrder') + +# Get the args property of that API call, which is a tuple, with the first entry being our data. +order_args = getattr(order_call[0], 'args')[0] + +# Test our specific argument value +self.assertEqual(123, order_args['hostId']) +``` \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..50a35f039 --- /dev/null +++ b/Makefile @@ -0,0 +1,192 @@ +# Makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = sphinx-build +PAPER = +BUILDDIR = build + +# User-friendly check for sphinx-build +ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) +$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) +endif + +# Internal variables. +PAPEROPT_a4 = -D latex_paper_size=a4 +PAPEROPT_letter = -D latex_paper_size=letter +ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source +# the i18n builder cannot share the environment and doctrees with the others +I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source + +.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest coverage gettext + +help: + @echo "Please use \`make ' where is one of" + @echo " html to make standalone HTML files" + @echo " dirhtml to make HTML files named index.html in directories" + @echo " singlehtml to make a single large HTML file" + @echo " pickle to make pickle files" + @echo " json to make JSON files" + @echo " htmlhelp to make HTML files and a HTML help project" + @echo " qthelp to make HTML files and a qthelp project" + @echo " applehelp to make an Apple Help Book" + @echo " devhelp to make HTML files and a Devhelp project" + @echo " epub to make an epub" + @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" + @echo " latexpdf to make LaTeX files and run them through pdflatex" + @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" + @echo " text to make text files" + @echo " man to make manual pages" + @echo " texinfo to make Texinfo files" + @echo " info to make Texinfo files and run them through makeinfo" + @echo " gettext to make PO message catalogs" + @echo " changes to make an overview of all changed/added/deprecated items" + @echo " xml to make Docutils-native XML files" + @echo " pseudoxml to make pseudoxml-XML files for display purposes" + @echo " linkcheck to check all external links for integrity" + @echo " doctest to run all doctests embedded in the documentation (if enabled)" + @echo " coverage to run coverage check of the documentation (if enabled)" + +clean: + rm -rf $(BUILDDIR)/* + +html: + $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." + +dirhtml: + $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." + +singlehtml: + $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml + @echo + @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." + +pickle: + $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle + @echo + @echo "Build finished; now you can process the pickle files." + +json: + $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json + @echo + @echo "Build finished; now you can process the JSON files." + +htmlhelp: + $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp + @echo + @echo "Build finished; now you can run HTML Help Workshop with the" \ + ".hhp project file in $(BUILDDIR)/htmlhelp." + +qthelp: + $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp + @echo + @echo "Build finished; now you can run "qcollectiongenerator" with the" \ + ".qhcp project file in $(BUILDDIR)/qthelp, like this:" + @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/softlayer-python.qhcp" + @echo "To view the help file:" + @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/softlayer-python.qhc" + +applehelp: + $(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp + @echo + @echo "Build finished. The help book is in $(BUILDDIR)/applehelp." + @echo "N.B. You won't be able to view it unless you put it in" \ + "~/Library/Documentation/Help or install it in your application" \ + "bundle." + +devhelp: + $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp + @echo + @echo "Build finished." + @echo "To view the help file:" + @echo "# mkdir -p $$HOME/.local/share/devhelp/softlayer-python" + @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/softlayer-python" + @echo "# devhelp" + +epub: + $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub + @echo + @echo "Build finished. The epub file is in $(BUILDDIR)/epub." + +latex: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo + @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." + @echo "Run \`make' in that directory to run these through (pdf)latex" \ + "(use \`make latexpdf' here to do that automatically)." + +latexpdf: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through pdflatex..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +latexpdfja: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through platex and dvipdfmx..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +text: + $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text + @echo + @echo "Build finished. The text files are in $(BUILDDIR)/text." + +man: + $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man + @echo + @echo "Build finished. The manual pages are in $(BUILDDIR)/man." + +texinfo: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo + @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." + @echo "Run \`make' in that directory to run these through makeinfo" \ + "(use \`make info' here to do that automatically)." + +info: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo "Running Texinfo files through makeinfo..." + make -C $(BUILDDIR)/texinfo info + @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." + +gettext: + $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale + @echo + @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." + +changes: + $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes + @echo + @echo "The overview file is in $(BUILDDIR)/changes." + +linkcheck: + $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck + @echo + @echo "Link check complete; look for any errors in the above output " \ + "or in $(BUILDDIR)/linkcheck/output.txt." + +doctest: + $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest + @echo "Testing of doctests in the sources finished, look at the " \ + "results in $(BUILDDIR)/doctest/output.txt." + +coverage: + $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage + @echo "Testing of coverage in the sources finished, look at the " \ + "results in $(BUILDDIR)/coverage/python.txt." + +xml: + $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml + @echo + @echo "Build finished. The XML files are in $(BUILDDIR)/xml." + +pseudoxml: + $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml + @echo + @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." diff --git a/README.rst b/README.rst index 8a274c8e2..177f15143 100644 --- a/README.rst +++ b/README.rst @@ -88,12 +88,14 @@ To get the exact API call that this library makes, you can do the following. For the CLI, just use the -vvv option. If you are using the REST endpoint, this will print out a curl command that you can use, if using XML, this will print the minimal python code to make the request without the softlayer library. .. code-block:: bash + $ slcli -vvv vs list If you are using the library directly in python, you can do something like this. .. code-bock:: python + import SoftLayer import logging @@ -118,6 +120,8 @@ If you are using the library directly in python, you can do something like this. main.main() main.debug() + + System Requirements ------------------- * Python 2.7, 3.3, 3.4, 3.5, 3.6, or 3.7. diff --git a/SoftLayer/CLI/cdn/purge.py b/SoftLayer/CLI/cdn/purge.py index 7738600a3..bcf055064 100644 --- a/SoftLayer/CLI/cdn/purge.py +++ b/SoftLayer/CLI/cdn/purge.py @@ -5,6 +5,7 @@ import SoftLayer from SoftLayer.CLI import environment +from SoftLayer.CLI import formatting @click.command() @@ -12,7 +13,22 @@ @click.argument('content_url', nargs=-1) @environment.pass_env def cli(env, account_id, content_url): - """Purge cached files from all edge nodes.""" + """Purge cached files from all edge nodes. + + Examples: + slcli cdn purge 97794 http://example.com/cdn/file.txt + slcli cdn purge 97794 http://example.com/cdn/file.txt https://dal01.example.softlayer.net/image.png + """ manager = SoftLayer.CDNManager(env.client) - manager.purge_content(account_id, content_url) + content_list = manager.purge_content(account_id, content_url) + + table = formatting.Table(['url', 'status']) + + for content in content_list: + table.add_row([ + content['url'], + content['statusCode'] + ]) + + env.fout(table) diff --git a/SoftLayer/CLI/config/setup.py b/SoftLayer/CLI/config/setup.py index cd5a24c1a..54aa1c79e 100644 --- a/SoftLayer/CLI/config/setup.py +++ b/SoftLayer/CLI/config/setup.py @@ -5,7 +5,6 @@ import click import SoftLayer -from SoftLayer import auth from SoftLayer.CLI import config from SoftLayer.CLI import environment from SoftLayer.CLI import exceptions @@ -22,7 +21,6 @@ def get_api_key(client, username, secret): # Try to use a client with username/api key if len(secret) == 64: try: - client.auth = auth.BasicAuthentication(username, secret) client['Account'].getCurrentUser() return secret except SoftLayer.SoftLayerAPIError as ex: @@ -32,12 +30,10 @@ def get_api_key(client, username, secret): # Try to use a client with username/password client.authenticate_with_password(username, secret) - user_record = client['Account'].getCurrentUser( - mask='id, apiAuthenticationKeys') + user_record = client['Account'].getCurrentUser(mask='id, apiAuthenticationKeys') api_keys = user_record['apiAuthenticationKeys'] if len(api_keys) == 0: - return client['User_Customer'].addApiAuthenticationKey( - id=user_record['id']) + return client['User_Customer'].addApiAuthenticationKey(id=user_record['id']) return api_keys[0]['authenticationKey'] @@ -47,9 +43,8 @@ def cli(env): """Edit configuration.""" username, secret, endpoint_url, timeout = get_user_input(env) - - env.client.transport.transport.endpoint_url = endpoint_url - api_key = get_api_key(env.client, username, secret) + new_client = SoftLayer.Client(username=username, api_key=secret, endpoint_url=endpoint_url, timeout=timeout) + api_key = get_api_key(new_client, username, secret) path = '~/.softlayer' if env.config_file: @@ -103,17 +98,20 @@ def get_user_input(env): secret = env.getpass('API Key or Password', default=defaults['api_key']) # Ask for which endpoint they want to use + endpoint = defaults.get('endpoint_url', 'public') endpoint_type = env.input( - 'Endpoint (public|private|custom)', default='public') + 'Endpoint (public|private|custom)', default=endpoint) endpoint_type = endpoint_type.lower() - if endpoint_type == 'custom': - endpoint_url = env.input('Endpoint URL', - default=defaults['endpoint_url']) + if endpoint_type == 'public': + endpoint_url = SoftLayer.API_PUBLIC_ENDPOINT elif endpoint_type == 'private': endpoint_url = SoftLayer.API_PRIVATE_ENDPOINT else: - endpoint_url = SoftLayer.API_PUBLIC_ENDPOINT + if endpoint_type == 'custom': + endpoint_url = env.input('Endpoint URL', default=endpoint) + else: + endpoint_url = endpoint_type # Ask for timeout timeout = env.input('Timeout', default=defaults['timeout'] or 0) diff --git a/SoftLayer/CLI/file/detail.py b/SoftLayer/CLI/file/detail.py index e34c1d419..02bb5c17a 100644 --- a/SoftLayer/CLI/file/detail.py +++ b/SoftLayer/CLI/file/detail.py @@ -39,7 +39,7 @@ def cli(env, volume_id): table.add_row(['Used Space', "%dGB" % (used_space / (1 << 30))]) if file_volume.get('provisionedIops'): - table.add_row(['IOPs', int(file_volume['provisionedIops'])]) + table.add_row(['IOPs', float(file_volume['provisionedIops'])]) if file_volume.get('storageTierLevel'): table.add_row([ diff --git a/SoftLayer/CLI/hardware/list.py b/SoftLayer/CLI/hardware/list.py index 1a607880f..54bc4f823 100644 --- a/SoftLayer/CLI/hardware/list.py +++ b/SoftLayer/CLI/hardware/list.py @@ -21,7 +21,6 @@ 'action', lambda server: formatting.active_txn(server), mask='activeTransaction[id, transactionStatus[name, friendlyName]]'), - column_helper.Column('power_state', ('powerState', 'name')), column_helper.Column( 'created_by', ('billingItem', 'orderItem', 'order', 'userRecord', 'username')), diff --git a/SoftLayer/CLI/hardware/toggle_ipmi.py b/SoftLayer/CLI/hardware/toggle_ipmi.py new file mode 100644 index 000000000..2e49bd72f --- /dev/null +++ b/SoftLayer/CLI/hardware/toggle_ipmi.py @@ -0,0 +1,22 @@ +"""Toggle the IPMI interface on and off.""" +# :license: MIT, see LICENSE for more details. + +import click + +import SoftLayer +from SoftLayer.CLI import environment +from SoftLayer.CLI import helpers + + +@click.command() +@click.argument('identifier') +@click.option('--enable/--disable', default=True, + help="Whether enable (DEFAULT) or disable the interface.") +@environment.pass_env +def cli(env, identifier, enable): + """Toggle the IPMI interface on and off""" + + mgr = SoftLayer.HardwareManager(env.client) + hw_id = helpers.resolve_id(mgr.resolve_ids, identifier, 'hardware') + result = env.client['Hardware_Server'].toggleManagementInterface(enable, id=hw_id) + env.fout(result) diff --git a/SoftLayer/CLI/helpers.py b/SoftLayer/CLI/helpers.py index f32595e59..24a5dd445 100644 --- a/SoftLayer/CLI/helpers.py +++ b/SoftLayer/CLI/helpers.py @@ -30,17 +30,20 @@ def multi_option(*param_decls, **attrs): def resolve_id(resolver, identifier, name='object'): """Resolves a single id using a resolver function. - :param resolver: function that resolves ids. Should return None or a list - of ids. + :param resolver: function that resolves ids. Should return None or a list of ids. :param string identifier: a string identifier used to resolve ids :param string name: the object type, to be used in error messages """ + try: + return int(identifier) + except ValueError: + pass # It was worth a shot + ids = resolver(identifier) if len(ids) == 0: - raise exceptions.CLIAbort("Error: Unable to find %s '%s'" - % (name, identifier)) + raise exceptions.CLIAbort("Error: Unable to find %s '%s'" % (name, identifier)) if len(ids) > 1: raise exceptions.CLIAbort( diff --git a/SoftLayer/CLI/metadata.py b/SoftLayer/CLI/metadata.py index 1d25ee38b..3cc3e384d 100644 --- a/SoftLayer/CLI/metadata.py +++ b/SoftLayer/CLI/metadata.py @@ -36,8 +36,8 @@ \b Examples : %s -""" % ('*'+'\n*'.join(META_CHOICES), - 'slcli metadata '+'\nslcli metadata '.join(META_CHOICES)) +""" % ('*' + '\n*'.join(META_CHOICES), + 'slcli metadata ' + '\nslcli metadata '.join(META_CHOICES)) @click.command(help=HELP, diff --git a/SoftLayer/CLI/order/item_list.py b/SoftLayer/CLI/order/item_list.py index 74f8fd4e7..92f83ceaf 100644 --- a/SoftLayer/CLI/order/item_list.py +++ b/SoftLayer/CLI/order/item_list.py @@ -7,7 +7,7 @@ from SoftLayer.managers import ordering from SoftLayer.utils import lookup -COLUMNS = ['category', 'keyName', 'description'] +COLUMNS = ['category', 'keyName', 'description', 'priceId'] @click.command() @@ -60,7 +60,7 @@ def cli(env, package_keyname, keyword, category): categories = sorted_items.keys() for catname in sorted(categories): for item in sorted_items[catname]: - table.add_row([catname, item['keyName'], item['description']]) + table.add_row([catname, item['keyName'], item['description'], get_price(item)]) env.fout(table) @@ -75,3 +75,12 @@ def sort_items(items): sorted_items[category].append(item) return sorted_items + + +def get_price(item): + """Given an SoftLayer_Product_Item, returns its default price id""" + + for price in item.get('prices', []): + if not price.get('locationGroupId'): + return price.get('id') + return 0 diff --git a/SoftLayer/CLI/order/place.py b/SoftLayer/CLI/order/place.py index 6d51ab935..4e9608c98 100644 --- a/SoftLayer/CLI/order/place.py +++ b/SoftLayer/CLI/order/place.py @@ -83,7 +83,7 @@ def cli(env, package_keyname, location, preset, verify, billing, complex_type, 'extras': extras, 'quantity': 1, 'complex_type': complex_type, - 'hourly': True if billing == 'hourly' else False} + 'hourly': bool(billing == 'hourly')} if verify: table = formatting.Table(COLUMNS) diff --git a/SoftLayer/CLI/routes.py b/SoftLayer/CLI/routes.py index e80a0b50a..51914c30b 100644 --- a/SoftLayer/CLI/routes.py +++ b/SoftLayer/CLI/routes.py @@ -31,6 +31,7 @@ ('virtual:upgrade', 'SoftLayer.CLI.virt.upgrade:cli'), ('virtual:credentials', 'SoftLayer.CLI.virt.credentials:cli'), ('virtual:capacity', 'SoftLayer.CLI.virt.capacity:cli'), + ('virtual:placementgroup', 'SoftLayer.CLI.virt.placementgroup:cli'), ('dedicatedhost', 'SoftLayer.CLI.dedicatedhost'), ('dedicatedhost:list', 'SoftLayer.CLI.dedicatedhost.list:cli'), @@ -237,6 +238,7 @@ ('hardware:update-firmware', 'SoftLayer.CLI.hardware.update_firmware:cli'), ('hardware:rescue', 'SoftLayer.CLI.hardware.power:rescue'), ('hardware:ready', 'SoftLayer.CLI.hardware.ready:cli'), + ('hardware:toggle-ipmi', 'SoftLayer.CLI.hardware.toggle_ipmi:cli'), ('securitygroup', 'SoftLayer.CLI.securitygroup'), ('securitygroup:list', 'SoftLayer.CLI.securitygroup.list:cli'), @@ -316,4 +318,5 @@ 'vm': 'virtual', 'vs': 'virtual', 'dh': 'dedicatedhost', + 'pg': 'placementgroup', } diff --git a/SoftLayer/CLI/virt/capacity/__init__.py b/SoftLayer/CLI/virt/capacity/__init__.py index 2b10885df..3f891c194 100644 --- a/SoftLayer/CLI/virt/capacity/__init__.py +++ b/SoftLayer/CLI/virt/capacity/__init__.py @@ -45,4 +45,3 @@ def get_command(self, ctx, cmd_name): @click.group(cls=CapacityCommands, context_settings=CONTEXT) def cli(): """Base command for all capacity related concerns""" - pass diff --git a/SoftLayer/CLI/virt/capacity/create_guest.py b/SoftLayer/CLI/virt/capacity/create_guest.py index 854fe3f94..4b8a983a6 100644 --- a/SoftLayer/CLI/virt/capacity/create_guest.py +++ b/SoftLayer/CLI/virt/capacity/create_guest.py @@ -36,8 +36,7 @@ def cli(env, **args): """Allows for creating a virtual guest in a reserved capacity.""" create_args = _parse_create_args(env.client, args) - if args.get('ipv6'): - create_args['ipv6'] = True + create_args['primary_disk'] = args.get('primary_disk') manager = CapacityManager(env.client) capacity_id = args.get('capacity_id') diff --git a/SoftLayer/CLI/virt/create.py b/SoftLayer/CLI/virt/create.py index 79af92585..631793475 100644 --- a/SoftLayer/CLI/virt/create.py +++ b/SoftLayer/CLI/virt/create.py @@ -32,6 +32,7 @@ def _update_with_like_args(ctx, _, value): 'postinstall': like_details.get('postInstallScriptUri'), 'dedicated': like_details['dedicatedAccountHostOnlyFlag'], 'private': like_details['privateNetworkOnlyFlag'], + 'placement_id': like_details.get('placementGroupId', None), } like_args['flavor'] = utils.lookup(like_details, @@ -73,15 +74,25 @@ def _parse_create_args(client, args): """ data = { "hourly": args.get('billing', 'hourly') == 'hourly', - "domain": args['domain'], - "hostname": args['hostname'], - "private": args.get('private', None), - "dedicated": args.get('dedicated', None), - "disks": args['disk'], "cpus": args.get('cpu', None), + "ipv6": args.get('ipv6', None), + "disks": args.get('disk', None), + "os_code": args.get('os', None), "memory": args.get('memory', None), "flavor": args.get('flavor', None), - "boot_mode": args.get('boot_mode', None) + "domain": args.get('domain', None), + "host_id": args.get('host_id', None), + "private": args.get('private', None), + "hostname": args.get('hostname', None), + "nic_speed": args.get('network', None), + "boot_mode": args.get('boot_mode', None), + "dedicated": args.get('dedicated', None), + "post_uri": args.get('postinstall', None), + "datacenter": args.get('datacenter', None), + "public_vlan": args.get('vlan_public', None), + "private_vlan": args.get('vlan_private', None), + "public_subnet": args.get('subnet_public', None), + "private_subnet": args.get('subnet_private', None), } # The primary disk is included in the flavor and the local_disk flag is not needed @@ -91,33 +102,20 @@ def _parse_create_args(client, args): else: data['local_disk'] = not args.get('san') - if args.get('os'): - data['os_code'] = args['os'] - if args.get('image'): if args.get('image').isdigit(): image_mgr = SoftLayer.ImageManager(client) - image_details = image_mgr.get_image(args.get('image'), - mask="id,globalIdentifier") + image_details = image_mgr.get_image(args.get('image'), mask="id,globalIdentifier") data['image_id'] = image_details['globalIdentifier'] else: data['image_id'] = args['image'] - if args.get('datacenter'): - data['datacenter'] = args['datacenter'] - - if args.get('network'): - data['nic_speed'] = args.get('network') - if args.get('userdata'): data['userdata'] = args['userdata'] elif args.get('userfile'): with open(args['userfile'], 'r') as userfile: data['userdata'] = userfile.read() - if args.get('postinstall'): - data['post_uri'] = args.get('postinstall') - # Get the SSH keys if args.get('key'): keys = [] @@ -127,16 +125,6 @@ def _parse_create_args(client, args): keys.append(key_id) data['ssh_keys'] = keys - if args.get('vlan_public'): - data['public_vlan'] = args['vlan_public'] - - if args.get('vlan_private'): - data['private_vlan'] = args['vlan_private'] - - data['public_subnet'] = args.get('subnet_public', None) - - data['private_subnet'] = args.get('subnet_private', None) - if args.get('public_security_group'): pub_groups = args.get('public_security_group') data['public_security_groups'] = [group for group in pub_groups] @@ -145,210 +133,143 @@ def _parse_create_args(client, args): priv_groups = args.get('private_security_group') data['private_security_groups'] = [group for group in priv_groups] - if args.get('tag'): + if args.get('tag', False): data['tags'] = ','.join(args['tag']) if args.get('host_id'): data['host_id'] = args['host_id'] + if args.get('placementgroup'): + resolver = SoftLayer.managers.PlacementManager(client).resolve_ids + data['placement_id'] = helpers.resolve_id(resolver, args.get('placementgroup'), 'PlacementGroup') + return data @click.command(epilog="See 'slcli vs create-options' for valid options") -@click.option('--hostname', '-H', - help="Host portion of the FQDN", - required=True, - prompt=True) -@click.option('--domain', '-D', - help="Domain portion of the FQDN", - required=True, - prompt=True) -@click.option('--cpu', '-c', - help="Number of CPU cores (not available with flavors)", - type=click.INT) -@click.option('--memory', '-m', - help="Memory in mebibytes (not available with flavors)", - type=virt.MEM_TYPE) -@click.option('--flavor', '-f', - help="Public Virtual Server flavor key name", - type=click.STRING) -@click.option('--datacenter', '-d', - help="Datacenter shortname", - required=True, - prompt=True) -@click.option('--os', '-o', - help="OS install code. Tip: you can specify _LATEST") -@click.option('--image', - help="Image ID. See: 'slcli image list' for reference") -@click.option('--boot-mode', - help="Specify the mode to boot the OS in. Supported modes are HVM and PV.", - type=click.STRING) -@click.option('--billing', - type=click.Choice(['hourly', 'monthly']), - default='hourly', - show_default=True, +@click.option('--hostname', '-H', required=True, prompt=True, help="Host portion of the FQDN") +@click.option('--domain', '-D', required=True, prompt=True, help="Domain portion of the FQDN") +@click.option('--cpu', '-c', type=click.INT, help="Number of CPU cores (not available with flavors)") +@click.option('--memory', '-m', type=virt.MEM_TYPE, help="Memory in mebibytes (not available with flavors)") +@click.option('--flavor', '-f', type=click.STRING, help="Public Virtual Server flavor key name") +@click.option('--datacenter', '-d', required=True, prompt=True, help="Datacenter shortname") +@click.option('--os', '-o', help="OS install code. Tip: you can specify _LATEST") +@click.option('--image', help="Image ID. See: 'slcli image list' for reference") +@click.option('--boot-mode', type=click.STRING, + help="Specify the mode to boot the OS in. Supported modes are HVM and PV.") +@click.option('--billing', type=click.Choice(['hourly', 'monthly']), default='hourly', show_default=True, help="Billing rate") -@click.option('--dedicated/--public', - is_flag=True, - help="Create a Dedicated Virtual Server") -@click.option('--host-id', - type=click.INT, - help="Host Id to provision a Dedicated Host Virtual Server onto") -@click.option('--san', - is_flag=True, - help="Use SAN storage instead of local disk.") -@click.option('--test', - is_flag=True, - help="Do not actually create the virtual server") -@click.option('--export', - type=click.Path(writable=True, resolve_path=True), +@click.option('--dedicated/--public', is_flag=True, help="Create a Dedicated Virtual Server") +@click.option('--host-id', type=click.INT, help="Host Id to provision a Dedicated Host Virtual Server onto") +@click.option('--san', is_flag=True, help="Use SAN storage instead of local disk.") +@click.option('--test', is_flag=True, help="Do not actually create the virtual server") +@click.option('--export', type=click.Path(writable=True, resolve_path=True), help="Exports options to a template file") @click.option('--postinstall', '-i', help="Post-install script to download") -@helpers.multi_option('--key', '-k', - help="SSH keys to add to the root user") +@helpers.multi_option('--key', '-k', help="SSH keys to add to the root user") @helpers.multi_option('--disk', help="Disk sizes") -@click.option('--private', - is_flag=True, +@click.option('--private', is_flag=True, help="Forces the VS to only have access the private network") -@click.option('--like', - is_eager=True, - callback=_update_with_like_args, +@click.option('--like', is_eager=True, callback=_update_with_like_args, help="Use the configuration from an existing VS") @click.option('--network', '-n', help="Network port speed in Mbps") @helpers.multi_option('--tag', '-g', help="Tags to add to the instance") -@click.option('--template', '-t', - is_eager=True, - callback=template.TemplateCallback(list_args=['disk', - 'key', - 'tag']), +@click.option('--template', '-t', is_eager=True, + callback=template.TemplateCallback(list_args=['disk', 'key', 'tag']), help="A template file that defaults the command-line options", type=click.Path(exists=True, readable=True, resolve_path=True)) @click.option('--userdata', '-u', help="User defined metadata string") -@click.option('--userfile', '-F', - help="Read userdata from file", - type=click.Path(exists=True, readable=True, resolve_path=True)) -@click.option('--vlan-public', - help="The ID of the public VLAN on which you want the virtual " - "server placed", - type=click.INT) -@click.option('--vlan-private', - help="The ID of the private VLAN on which you want the virtual " - "server placed", - type=click.INT) -@click.option('--subnet-public', - help="The ID of the public SUBNET on which you want the virtual server placed", - type=click.INT) -@click.option('--subnet-private', - help="The ID of the private SUBNET on which you want the virtual server placed", - type=click.INT) -@helpers.multi_option('--public-security-group', - '-S', - help=('Security group ID to associate with ' - 'the public interface')) -@helpers.multi_option('--private-security-group', - '-s', - help=('Security group ID to associate with ' - 'the private interface')) -@click.option('--wait', - type=click.INT, - help="Wait until VS is finished provisioning for up to X " - "seconds before returning") +@click.option('--userfile', '-F', type=click.Path(exists=True, readable=True, resolve_path=True), + help="Read userdata from file") +@click.option('--vlan-public', type=click.INT, + help="The ID of the public VLAN on which you want the virtual server placed") +@click.option('--vlan-private', type=click.INT, + help="The ID of the private VLAN on which you want the virtual server placed") +@click.option('--subnet-public', type=click.INT, + help="The ID of the public SUBNET on which you want the virtual server placed") +@click.option('--subnet-private', type=click.INT, + help="The ID of the private SUBNET on which you want the virtual server placed") +@helpers.multi_option('--public-security-group', '-S', + help=('Security group ID to associate with the public interface')) +@helpers.multi_option('--private-security-group', '-s', + help=('Security group ID to associate with the private interface')) +@click.option('--wait', type=click.INT, + help="Wait until VS is finished provisioning for up to X seconds before returning") +@click.option('--placementgroup', + help="Placement Group name or Id to order this guest on. See: slcli vs placementgroup list") +@click.option('--ipv6', is_flag=True, help="Adds an IPv6 address to this guest") @environment.pass_env def cli(env, **args): """Order/create virtual servers.""" + vsi = SoftLayer.VSManager(env.client) _validate_args(env, args) - - # Do not create a virtual server with test or export - do_create = not (args['export'] or args['test']) - - table = formatting.Table(['Item', 'cost']) - table.align['Item'] = 'r' - table.align['cost'] = 'r' - data = _parse_create_args(env.client, args) - - output = [] - if args.get('test'): - result = vsi.verify_create_instance(**data) - - if result['presetId']: - ordering_mgr = SoftLayer.OrderingManager(env.client) - item_prices = ordering_mgr.get_item_prices(result['packageId']) - preset_prices = ordering_mgr.get_preset_prices(result['presetId']) - search_keys = ["guest_core", "ram"] - for price in preset_prices['prices']: - if price['item']['itemCategory']['categoryCode'] in search_keys: - item_key_name = price['item']['keyName'] - _add_item_prices(item_key_name, item_prices, result) - - table = _build_receipt_table(result['prices'], args.get('billing')) - - output.append(table) - output.append(formatting.FormattedItem( - None, - ' -- ! Prices reflected here are retail and do not ' - 'take account level discounts and are not guaranteed.')) - - if args['export']: - export_file = args.pop('export') - template.export_to_template(export_file, args, - exclude=['wait', 'test']) - env.fout('Successfully exported options to a template file.') + create_args = _parse_create_args(env.client, args) + test = args.get('test', False) + do_create = not (args.get('export') or test) if do_create: if not (env.skip_confirmations or formatting.confirm( "This action will incur charges on your account. Continue?")): raise exceptions.CLIAbort('Aborting virtual server order.') - result = vsi.create_instance(**data) + if args.get('export'): + export_file = args.pop('export') + template.export_to_template(export_file, args, exclude=['wait', 'test']) + env.fout('Successfully exported options to a template file.') + + else: + result = vsi.order_guest(create_args, test) + output = _build_receipt_table(result, args.get('billing'), test) - table = formatting.KeyValueTable(['name', 'value']) - table.align['name'] = 'r' - table.align['value'] = 'l' - table.add_row(['id', result['id']]) - table.add_row(['created', result['createDate']]) - table.add_row(['guid', result['globalIdentifier']]) - output.append(table) + if do_create: + env.fout(_build_guest_table(result)) + env.fout(output) if args.get('wait'): - ready = vsi.wait_for_ready(result['id'], args.get('wait') or 1) - table.add_row(['ready', ready]) + virtual_guests = utils.lookup(result, 'orderDetails', 'virtualGuests') + guest_id = virtual_guests[0]['id'] + click.secho("Waiting for %s to finish provisioning..." % guest_id, fg='green') + ready = vsi.wait_for_ready(guest_id, args.get('wait') or 1) if ready is False: env.out(env.fmt(output)) raise exceptions.CLIHalt(code=1) - env.fout(output) - - -def _add_item_prices(item_key_name, item_prices, result): - """Add the flavor item prices to the rest o the items prices""" - for item in item_prices: - if item_key_name == item['item']['keyName']: - if 'pricingLocationGroup' in item: - for location in item['pricingLocationGroup']['locations']: - if result['location'] == str(location['id']): - result['prices'].append(item) - -def _build_receipt_table(prices, billing="hourly"): +def _build_receipt_table(result, billing="hourly", test=False): """Retrieve the total recurring fee of the items prices""" - total = 0.000 - table = formatting.Table(['Cost', 'Item']) + title = "OrderId: %s" % (result.get('orderId', 'No order placed')) + table = formatting.Table(['Cost', 'Description'], title=title) table.align['Cost'] = 'r' - table.align['Item'] = 'l' - for price in prices: + table.align['Description'] = 'l' + total = 0.000 + if test: + prices = result['prices'] + else: + prices = result['orderDetails']['prices'] + + for item in prices: rate = 0.000 if billing == "hourly": - rate += float(price.get('hourlyRecurringFee', 0.000)) + rate += float(item.get('hourlyRecurringFee', 0.000)) else: - rate += float(price.get('recurringFee', 0.000)) + rate += float(item.get('recurringFee', 0.000)) total += rate - - table.add_row(["%.3f" % rate, price['item']['description']]) + table.add_row([rate, item['item']['description']]) table.add_row(["%.3f" % total, "Total %s cost" % billing]) return table +def _build_guest_table(result): + table = formatting.Table(['ID', 'FQDN', 'guid', 'Order Date']) + table.align['name'] = 'r' + table.align['value'] = 'l' + virtual_guests = utils.lookup(result, 'orderDetails', 'virtualGuests') + for guest in virtual_guests: + table.add_row([guest['id'], guest['fullyQualifiedDomainName'], guest['globalIdentifier'], result['orderDate']]) + return table + + def _validate_args(env, args): """Raises an ArgumentError if the given arguments are not valid.""" @@ -378,8 +299,6 @@ def _validate_args(env, args): '[-o | --os] not allowed with [--image]') while not any([args['os'], args['image']]): - args['os'] = env.input("Operating System Code", - default="", - show_default=False) + args['os'] = env.input("Operating System Code", default="", show_default=False) if not args['os']: args['image'] = env.input("Image", default="", show_default=False) diff --git a/SoftLayer/CLI/virt/placementgroup/__init__.py b/SoftLayer/CLI/virt/placementgroup/__init__.py new file mode 100644 index 000000000..aa748a5b1 --- /dev/null +++ b/SoftLayer/CLI/virt/placementgroup/__init__.py @@ -0,0 +1,46 @@ +"""Manages Reserved Capacity.""" +# :license: MIT, see LICENSE for more details. + +import importlib +import os + +import click + +CONTEXT = {'help_option_names': ['-h', '--help'], + 'max_content_width': 999} + + +class PlacementGroupCommands(click.MultiCommand): + """Loads module for placement group related commands. + + Currently the base command loader only supports going two commands deep. + So this small loader is required for going that third level. + """ + + def __init__(self, **attrs): + click.MultiCommand.__init__(self, **attrs) + self.path = os.path.dirname(__file__) + + def list_commands(self, ctx): + """List all sub-commands.""" + commands = [] + for filename in os.listdir(self.path): + if filename == '__init__.py': + continue + if filename.endswith('.py'): + commands.append(filename[:-3].replace("_", "-")) + commands.sort() + return commands + + def get_command(self, ctx, cmd_name): + """Get command for click.""" + path = "%s.%s" % (__name__, cmd_name) + path = path.replace("-", "_") + module = importlib.import_module(path) + return getattr(module, 'cli') + + +# Required to get the sub-sub-sub command to work. +@click.group(cls=PlacementGroupCommands, context_settings=CONTEXT) +def cli(): + """Base command for all capacity related concerns""" diff --git a/SoftLayer/CLI/virt/placementgroup/create.py b/SoftLayer/CLI/virt/placementgroup/create.py new file mode 100644 index 000000000..af1fb8db5 --- /dev/null +++ b/SoftLayer/CLI/virt/placementgroup/create.py @@ -0,0 +1,31 @@ +"""Create a placement group""" + +import click + +from SoftLayer.CLI import environment +from SoftLayer.CLI import helpers +from SoftLayer.managers.vs_placement import PlacementManager as PlacementManager + + +@click.command() +@click.option('--name', type=click.STRING, required=True, prompt=True, help="Name for this new placement group.") +@click.option('--backend_router', '-b', required=True, prompt=True, + help="backendRouter, can be either the hostname or id.") +@click.option('--rule', '-r', required=True, prompt=True, + help="The keyName or Id of the rule to govern this placement group.") +@environment.pass_env +def cli(env, **args): + """Create a placement group""" + manager = PlacementManager(env.client) + backend_router_id = helpers.resolve_id(manager.get_backend_router_id_from_hostname, + args.get('backend_router'), + 'backendRouter') + rule_id = helpers.resolve_id(manager.get_rule_id_from_name, args.get('rule'), 'Rule') + placement_object = { + 'name': args.get('name'), + 'backendRouterId': backend_router_id, + 'ruleId': rule_id + } + + result = manager.create(placement_object) + click.secho("Successfully created placement group: ID: %s, Name: %s" % (result['id'], result['name']), fg='green') diff --git a/SoftLayer/CLI/virt/placementgroup/create_options.py b/SoftLayer/CLI/virt/placementgroup/create_options.py new file mode 100644 index 000000000..3107fc334 --- /dev/null +++ b/SoftLayer/CLI/virt/placementgroup/create_options.py @@ -0,0 +1,38 @@ +"""List options for creating Placement Groups""" +# :license: MIT, see LICENSE for more details. + +import click + +from SoftLayer.CLI import environment +from SoftLayer.CLI import formatting +from SoftLayer.managers.vs_placement import PlacementManager as PlacementManager + + +@click.command() +@environment.pass_env +def cli(env): + """List options for creating Reserved Capacity""" + manager = PlacementManager(env.client) + + routers = manager.get_routers() + env.fout(get_router_table(routers)) + + rules = manager.get_all_rules() + env.fout(get_rule_table(rules)) + + +def get_router_table(routers): + """Formats output from _get_routers and returns a table. """ + table = formatting.Table(['Datacenter', 'Hostname', 'Backend Router Id'], "Available Routers") + for router in routers: + datacenter = router['topLevelLocation']['longName'] + table.add_row([datacenter, router['hostname'], router['id']]) + return table + + +def get_rule_table(rules): + """Formats output from get_all_rules and returns a table. """ + table = formatting.Table(['Id', 'KeyName'], "Rules") + for rule in rules: + table.add_row([rule['id'], rule['keyName']]) + return table diff --git a/SoftLayer/CLI/virt/placementgroup/delete.py b/SoftLayer/CLI/virt/placementgroup/delete.py new file mode 100644 index 000000000..260b3cd35 --- /dev/null +++ b/SoftLayer/CLI/virt/placementgroup/delete.py @@ -0,0 +1,49 @@ +"""Delete a placement group.""" + +import click + +from SoftLayer.CLI import environment +from SoftLayer.CLI import exceptions +from SoftLayer.CLI import formatting +from SoftLayer.CLI import helpers +from SoftLayer.managers.vs import VSManager as VSManager +from SoftLayer.managers.vs_placement import PlacementManager as PlacementManager + + +@click.command(epilog="Once provisioned, virtual guests can be managed with the slcli vs commands") +@click.argument('identifier') +@click.option('--purge', is_flag=True, + help="Delete all guests in this placement group. " + "The group itself can be deleted once all VMs are fully reclaimed") +@environment.pass_env +def cli(env, identifier, purge): + """Delete a placement group. + + Placement Group MUST be empty before you can delete it. + + IDENTIFIER can be either the Name or Id of the placement group you want to view + """ + manager = PlacementManager(env.client) + group_id = helpers.resolve_id(manager.resolve_ids, identifier, 'placement_group') + + if purge: + placement_group = manager.get_object(group_id) + guest_list = ', '.join([guest['fullyQualifiedDomainName'] for guest in placement_group['guests']]) + if len(placement_group['guests']) < 1: + raise exceptions.CLIAbort('No virtual servers were found in placement group %s' % identifier) + + click.secho("You are about to delete the following guests!\n%s" % guest_list, fg='red') + if not (env.skip_confirmations or formatting.confirm("This action will cancel all guests! Continue?")): + raise exceptions.CLIAbort('Aborting virtual server order.') + vm_manager = VSManager(env.client) + for guest in placement_group['guests']: + click.secho("Deleting %s..." % guest['fullyQualifiedDomainName']) + vm_manager.cancel_instance(guest['id']) + return + + click.secho("You are about to delete the following placement group! %s" % identifier, fg='red') + if not (env.skip_confirmations or formatting.confirm("This action will cancel the placement group! Continue?")): + raise exceptions.CLIAbort('Aborting virtual server order.') + cancel_result = manager.delete(group_id) + if cancel_result: + click.secho("Placement Group %s has been canceld." % identifier, fg='green') diff --git a/SoftLayer/CLI/virt/placementgroup/detail.py b/SoftLayer/CLI/virt/placementgroup/detail.py new file mode 100644 index 000000000..9adf58932 --- /dev/null +++ b/SoftLayer/CLI/virt/placementgroup/detail.py @@ -0,0 +1,54 @@ +"""View details of a placement group""" + +import click + +from SoftLayer.CLI import environment +from SoftLayer.CLI import formatting +from SoftLayer.CLI import helpers +from SoftLayer.managers.vs_placement import PlacementManager as PlacementManager + + +@click.command(epilog="Once provisioned, virtual guests can be managed with the slcli vs commands") +@click.argument('identifier') +@environment.pass_env +def cli(env, identifier): + """View details of a placement group. + + IDENTIFIER can be either the Name or Id of the placement group you want to view + """ + manager = PlacementManager(env.client) + group_id = helpers.resolve_id(manager.resolve_ids, identifier, 'placement_group') + result = manager.get_object(group_id) + table = formatting.Table(["Id", "Name", "Backend Router", "Rule", "Created"]) + + table.add_row([ + result['id'], + result['name'], + result['backendRouter']['hostname'], + result['rule']['name'], + result['createDate'] + ]) + guest_table = formatting.Table([ + "Id", + "FQDN", + "Primary IP", + "Backend IP", + "CPU", + "Memory", + "Provisioned", + "Transaction" + ]) + for guest in result['guests']: + guest_table.add_row([ + guest.get('id'), + guest.get('fullyQualifiedDomainName'), + guest.get('primaryIpAddress'), + guest.get('primaryBackendIpAddress'), + guest.get('maxCpu'), + guest.get('maxMemory'), + guest.get('provisionDate'), + formatting.active_txn(guest) + ]) + + env.fout(table) + env.fout(guest_table) diff --git a/SoftLayer/CLI/virt/placementgroup/list.py b/SoftLayer/CLI/virt/placementgroup/list.py new file mode 100644 index 000000000..365205e74 --- /dev/null +++ b/SoftLayer/CLI/virt/placementgroup/list.py @@ -0,0 +1,30 @@ +"""List Placement Groups""" + +import click + +from SoftLayer.CLI import environment +from SoftLayer.CLI import formatting +from SoftLayer.managers.vs_placement import PlacementManager as PlacementManager + + +@click.command() +@environment.pass_env +def cli(env): + """List Reserved Capacity groups.""" + manager = PlacementManager(env.client) + result = manager.list() + table = formatting.Table( + ["Id", "Name", "Backend Router", "Rule", "Guests", "Created"], + title="Placement Groups" + ) + for group in result: + table.add_row([ + group['id'], + group['name'], + group['backendRouter']['hostname'], + group['rule']['name'], + group['guestCount'], + group['createDate'] + ]) + + env.fout(table) diff --git a/SoftLayer/CLI/virt/upgrade.py b/SoftLayer/CLI/virt/upgrade.py index 039e7cbfc..463fc077e 100644 --- a/SoftLayer/CLI/virt/upgrade.py +++ b/SoftLayer/CLI/virt/upgrade.py @@ -16,13 +16,12 @@ completed. However for Network, no reboot is required.""") @click.argument('identifier') @click.option('--cpu', type=click.INT, help="Number of CPU cores") -@click.option('--private', - is_flag=True, +@click.option('--private', is_flag=True, help="CPU core will be on a dedicated host server.") @click.option('--memory', type=virt.MEM_TYPE, help="Memory in megabytes") @click.option('--network', type=click.INT, help="Network port speed in Mbps") -@click.option('--flavor', type=click.STRING, help="Flavor keyName\n" - "Do not use --memory, --cpu or --private, if you are using flavors") +@click.option('--flavor', type=click.STRING, + help="Flavor keyName\nDo not use --memory, --cpu or --private, if you are using flavors") @environment.pass_env def cli(env, identifier, cpu, private, memory, network, flavor): """Upgrade a virtual server.""" @@ -30,26 +29,17 @@ def cli(env, identifier, cpu, private, memory, network, flavor): vsi = SoftLayer.VSManager(env.client) if not any([cpu, memory, network, flavor]): - raise exceptions.ArgumentError( - "Must provide [--cpu], [--memory], [--network], or [--flavor] to upgrade") + raise exceptions.ArgumentError("Must provide [--cpu], [--memory], [--network], or [--flavor] to upgrade") if private and not cpu: - raise exceptions.ArgumentError( - "Must specify [--cpu] when using [--private]") + raise exceptions.ArgumentError("Must specify [--cpu] when using [--private]") vs_id = helpers.resolve_id(vsi.resolve_ids, identifier, 'VS') - if not (env.skip_confirmations or formatting.confirm( - "This action will incur charges on your account. " - "Continue?")): + if not (env.skip_confirmations or formatting.confirm("This action will incur charges on your account. Continue?")): raise exceptions.CLIAbort('Aborted') if memory: memory = int(memory / 1024) - if not vsi.upgrade(vs_id, - cpus=cpu, - memory=memory, - nic_speed=network, - public=not private, - preset=flavor): + if not vsi.upgrade(vs_id, cpus=cpu, memory=memory, nic_speed=network, public=not private, preset=flavor): raise exceptions.CLIAbort('VS Upgrade Failed') diff --git a/SoftLayer/__init__.py b/SoftLayer/__init__.py index 3e79f6cd4..04ba36aaa 100644 --- a/SoftLayer/__init__.py +++ b/SoftLayer/__init__.py @@ -14,7 +14,8 @@ :license: MIT, see LICENSE for more details. """ -# pylint: disable=w0401,invalid-name +# pylint: disable=r0401,invalid-name,wildcard-import +# NOQA appears to no longer be working. The code might have been upgraded. from SoftLayer import consts from SoftLayer.API import * # NOQA diff --git a/SoftLayer/exceptions.py b/SoftLayer/exceptions.py index 5652730fa..b3530aa8c 100644 --- a/SoftLayer/exceptions.py +++ b/SoftLayer/exceptions.py @@ -57,34 +57,27 @@ class TransportError(SoftLayerAPIError): # XMLRPC Errors class NotWellFormed(ParseError): """Request was not well formed.""" - pass class UnsupportedEncoding(ParseError): """Encoding not supported.""" - pass class InvalidCharacter(ParseError): """There was an invalid character.""" - pass class SpecViolation(ServerError): """There was a spec violation.""" - pass class MethodNotFound(SoftLayerAPIError): """Method name not found.""" - pass class InvalidMethodParameters(SoftLayerAPIError): """Invalid method paramters.""" - pass class InternalError(ServerError): """Internal Server Error.""" - pass diff --git a/SoftLayer/fixtures/SoftLayer_Account.py b/SoftLayer/fixtures/SoftLayer_Account.py index 891df9ecb..b4bafac92 100644 --- a/SoftLayer/fixtures/SoftLayer_Account.py +++ b/SoftLayer/fixtures/SoftLayer_Account.py @@ -591,7 +591,7 @@ 'modifyDate': '', 'name': 'test-capacity', 'availableInstanceCount': 1, - 'instanceCount': 2, + 'instanceCount': 3, 'occupiedInstanceCount': 1, 'backendRouter': { 'accountId': 1, @@ -638,7 +638,27 @@ 'description': 'B1.1x2 (1 Year Term)', 'hourlyRecurringFee': '.032' } + }, + { + 'id': 3519 } ] } ] + + +getPlacementGroups = [{ + "createDate": "2019-01-18T16:08:44-06:00", + "id": 12345, + "name": "test01", + "guestCount": 0, + "backendRouter": { + "hostname": "bcr01a.mex01", + "id": 329266 + }, + "rule": { + "id": 1, + "keyName": "SPREAD", + "name": "SPREAD" + } +}] diff --git a/SoftLayer/fixtures/SoftLayer_Hardware_Server.py b/SoftLayer/fixtures/SoftLayer_Hardware_Server.py index 61bdbf984..9a9587b81 100644 --- a/SoftLayer/fixtures/SoftLayer_Hardware_Server.py +++ b/SoftLayer/fixtures/SoftLayer_Hardware_Server.py @@ -73,6 +73,7 @@ setTags = True setPrivateNetworkInterfaceSpeed = True setPublicNetworkInterfaceSpeed = True +toggleManagementInterface = True powerOff = True powerOn = True powerCycle = True diff --git a/SoftLayer/fixtures/SoftLayer_Network_ContentDelivery_Account.py b/SoftLayer/fixtures/SoftLayer_Network_ContentDelivery_Account.py index 28e043bc8..643df9c10 100644 --- a/SoftLayer/fixtures/SoftLayer_Network_ContentDelivery_Account.py +++ b/SoftLayer/fixtures/SoftLayer_Network_ContentDelivery_Account.py @@ -32,6 +32,10 @@ loadContent = True -purgeContent = True +purgeContent = [ + {'url': 'http://z/img/0z020.png', 'statusCode': 'SUCCESS'}, + {'url': 'http://y/img/0z010.png', 'statusCode': 'FAILED'}, + {'url': 'http://', 'statusCode': 'INVALID_URL'} +] -purgeCache = True +purgeCache = [{'url': 'http://example.com', 'statusCode': 'SUCCESS'}] diff --git a/SoftLayer/fixtures/SoftLayer_Product_Order.py b/SoftLayer/fixtures/SoftLayer_Product_Order.py index 5be637c9b..3774f63a8 100644 --- a/SoftLayer/fixtures/SoftLayer_Product_Order.py +++ b/SoftLayer/fixtures/SoftLayer_Product_Order.py @@ -13,7 +13,29 @@ 'setupFee': '1', 'item': {'id': 1, 'description': 'this is a thing'}, }]} -placeOrder = verifyOrder +placeOrder = { + 'orderId': 1234, + 'orderDate': '2013-08-01 15:23:45', + 'orderDetails': { + 'prices': [{ + 'id': 1, + 'laborFee': '2', + 'oneTimeFee': '2', + 'oneTimeFeeTax': '.1', + 'quantity': 1, + 'recurringFee': '2', + 'recurringFeeTax': '.1', + 'hourlyRecurringFee': '2', + 'setupFee': '1', + 'item': {'id': 1, 'description': 'this is a thing'}, + }], + 'virtualGuests': [{ + 'id': 1234567, + 'globalIdentifier': '1a2b3c-1701', + 'fullyQualifiedDomainName': 'test.guest.com' + }] + } +} # Reserved Capacity Stuff @@ -75,7 +97,9 @@ 'id': 1, 'description': 'B1.1x2 (1 Year ''Term)', 'keyName': 'B1_1X2_1_YEAR_TERM', - } + }, + 'hourlyRecurringFee': 1.0, + 'recurringFee': 2.0 } ] } diff --git a/SoftLayer/fixtures/SoftLayer_Virtual_Guest.py b/SoftLayer/fixtures/SoftLayer_Virtual_Guest.py index 69a1b95e6..c01fd17a8 100644 --- a/SoftLayer/fixtures/SoftLayer_Virtual_Guest.py +++ b/SoftLayer/fixtures/SoftLayer_Virtual_Guest.py @@ -533,7 +533,16 @@ setUserMetadata = ['meta'] reloadOperatingSystem = 'OK' setTags = True -createArchiveTransaction = {} +createArchiveTransaction = { + 'createDate': '2018-12-10T17:29:18-06:00', + 'elapsedSeconds': 0, + 'guestId': 12345678, + 'hardwareId': None, + 'id': 12345, + 'modifyDate': '2018-12-10T17:29:18-06:00', + 'statusChangeDate': '2018-12-10T17:29:18-06:00' +} + executeRescueLayer = True getUpgradeItemPrices = [ diff --git a/SoftLayer/fixtures/SoftLayer_Virtual_PlacementGroup.py b/SoftLayer/fixtures/SoftLayer_Virtual_PlacementGroup.py new file mode 100644 index 000000000..0159c6333 --- /dev/null +++ b/SoftLayer/fixtures/SoftLayer_Virtual_PlacementGroup.py @@ -0,0 +1,63 @@ +getAvailableRouters = [{ + "accountId": 1, + "fullyQualifiedDomainName": "bcr01.dal01.softlayer.com", + "hostname": "bcr01.dal01", + "id": 1, + "topLevelLocation": { + "id": 3, + "longName": "Dallas 1", + "name": "dal01", + } +}] + +createObject = { + "accountId": 123, + "backendRouterId": 444, + "createDate": "2019-01-18T16:08:44-06:00", + "id": 5555, + "modifyDate": None, + "name": "test01", + "ruleId": 1 +} +getObject = { + "createDate": "2019-01-17T14:36:42-06:00", + "id": 1234, + "name": "test-group", + "backendRouter": { + "hostname": "bcr01a.mex01", + "id": 329266 + }, + "guests": [{ + "accountId": 123456789, + "createDate": "2019-01-17T16:44:46-06:00", + "domain": "test.com", + "fullyQualifiedDomainName": "issues10691547765077.test.com", + "hostname": "issues10691547765077", + "id": 69131875, + "maxCpu": 1, + "maxMemory": 1024, + "placementGroupId": 1234, + "provisionDate": "2019-01-17T16:47:17-06:00", + "activeTransaction": { + "id": 107585077, + "transactionStatus": { + "friendlyName": "TESTING TXN", + "name": "RECLAIM_WAIT" + } + }, + "globalIdentifier": "c786ac04-b612-4649-9d19-9662434eeaea", + "primaryBackendIpAddress": "10.131.11.14", + "primaryIpAddress": "169.57.70.180", + "status": { + "keyName": "DISCONNECTED", + "name": "Disconnected" + } + }], + "rule": { + "id": 1, + "keyName": "SPREAD", + "name": "SPREAD" + } +} + +deleteObject = True diff --git a/SoftLayer/fixtures/SoftLayer_Virtual_PlacementGroup_Rule.py b/SoftLayer/fixtures/SoftLayer_Virtual_PlacementGroup_Rule.py new file mode 100644 index 000000000..c933fd2db --- /dev/null +++ b/SoftLayer/fixtures/SoftLayer_Virtual_PlacementGroup_Rule.py @@ -0,0 +1,7 @@ +getAllObjects = [ + { + "id": 1, + "keyName": "SPREAD", + "name": "SPREAD" + } +] diff --git a/SoftLayer/managers/__init__.py b/SoftLayer/managers/__init__.py index b6cc1faa5..02e54b30e 100644 --- a/SoftLayer/managers/__init__.py +++ b/SoftLayer/managers/__init__.py @@ -28,7 +28,7 @@ from SoftLayer.managers.user import UserManager from SoftLayer.managers.vs import VSManager from SoftLayer.managers.vs_capacity import CapacityManager - +from SoftLayer.managers.vs_placement import PlacementManager __all__ = [ 'BlockStorageManager', @@ -47,6 +47,7 @@ 'NetworkManager', 'ObjectStorageManager', 'OrderingManager', + 'PlacementManager', 'SshKeyManager', 'SSLManager', 'TicketManager', diff --git a/SoftLayer/managers/cdn.py b/SoftLayer/managers/cdn.py index 994ab8fab..19a88efb7 100644 --- a/SoftLayer/managers/cdn.py +++ b/SoftLayer/managers/cdn.py @@ -129,17 +129,16 @@ def purge_content(self, account_id, urls): be purged. :param urls: a string or a list of strings representing the CDN URLs that should be purged. - :returns: true if all purge requests were successfully submitted; - otherwise, returns the first error encountered. + :returns: a list of SoftLayer_Container_Network_ContentDelivery_PurgeService_Response objects + which indicates if the purge for each url was SUCCESS, FAILED or INVALID_URL. """ if isinstance(urls, six.string_types): urls = [urls] + content_list = [] for i in range(0, len(urls), MAX_URLS_PER_PURGE): - result = self.account.purgeCache(urls[i:i + MAX_URLS_PER_PURGE], - id=account_id) - if not result: - return result + content = self.account.purgeCache(urls[i:i + MAX_URLS_PER_PURGE], id=account_id) + content_list.extend(content) - return True + return content_list diff --git a/SoftLayer/managers/file.py b/SoftLayer/managers/file.py index e0a250328..b6d16053f 100644 --- a/SoftLayer/managers/file.py +++ b/SoftLayer/managers/file.py @@ -482,6 +482,9 @@ def cancel_file_volume(self, volume_id, reason='No longer needed', immediate=Fal file_volume = self.get_file_volume_details( volume_id, mask='mask[id,billingItem[id,hourlyFlag]]') + + if 'billingItem' not in file_volume: + raise exceptions.SoftLayerError('The volume has already been canceled') billing_item_id = file_volume['billingItem']['id'] if utils.lookup(file_volume, 'billingItem', 'hourlyFlag'): diff --git a/SoftLayer/managers/ordering.py b/SoftLayer/managers/ordering.py index c0eca1dc4..fbc56b654 100644 --- a/SoftLayer/managers/ordering.py +++ b/SoftLayer/managers/ordering.py @@ -16,7 +16,7 @@ itemCategory[id, name, categoryCode] ''' -ITEM_MASK = '''id, keyName, description, itemCategory, categories''' +ITEM_MASK = '''id, keyName, description, itemCategory, categories, prices''' PACKAGE_MASK = '''id, name, keyName, isActive, type''' diff --git a/SoftLayer/managers/vs.py b/SoftLayer/managers/vs.py index dbbaa98c4..a3f26126e 100644 --- a/SoftLayer/managers/vs.py +++ b/SoftLayer/managers/vs.py @@ -18,8 +18,7 @@ LOGGER = logging.getLogger(__name__) - -# pylint: disable=no-self-use +# pylint: disable=no-self-use,too-many-lines class VSManager(utils.IdentifierMixin, object): @@ -234,7 +233,8 @@ def get_instance(self, instance_id, **kwargs): preset.keyName]],''' 'tagReferences[id,tag[name,id]],' 'networkVlans[id,vlanNumber,networkSpace],' - 'dedicatedHost.id' + 'dedicatedHost.id,' + 'placementGroupId' ) return self.guest.getObject(id=instance_id, **kwargs) @@ -664,12 +664,10 @@ def change_port_speed(self, instance_id, public, speed): A port speed of 0 will disable the interface. """ if public: - return self.client.call('Virtual_Guest', - 'setPublicNetworkInterfaceSpeed', + return self.client.call('Virtual_Guest', 'setPublicNetworkInterfaceSpeed', speed, id=instance_id) else: - return self.client.call('Virtual_Guest', - 'setPrivateNetworkInterfaceSpeed', + return self.client.call('Virtual_Guest', 'setPrivateNetworkInterfaceSpeed', speed, id=instance_id) def _get_ids_from_hostname(self, hostname): @@ -784,10 +782,7 @@ def capture(self, instance_id, name, additional_disks=False, notes=None): continue # We never want swap devices - type_name = utils.lookup(block_device, - 'diskImage', - 'type', - 'keyName') + type_name = utils.lookup(block_device, 'diskImage', 'type', 'keyName') if type_name == 'SWAP': continue @@ -804,8 +799,7 @@ def capture(self, instance_id, name, additional_disks=False, notes=None): return self.guest.createArchiveTransaction( name, disks_to_capture, notes, id=instance_id) - def upgrade(self, instance_id, cpus=None, memory=None, - nic_speed=None, public=True, preset=None): + def upgrade(self, instance_id, cpus=None, memory=None, nic_speed=None, public=True, preset=None): """Upgrades a VS instance. Example:: @@ -832,17 +826,16 @@ def upgrade(self, instance_id, cpus=None, memory=None, data = {'nic_speed': nic_speed} if cpus is not None and preset is not None: - raise exceptions.SoftLayerError("Do not use cpu, private and memory if you are using flavors") + raise ValueError("Do not use cpu, private and memory if you are using flavors") data['cpus'] = cpus if memory is not None and preset is not None: - raise exceptions.SoftLayerError("Do not use memory, private or cpu if you are using flavors") + raise ValueError("Do not use memory, private or cpu if you are using flavors") data['memory'] = memory maintenance_window = datetime.datetime.now(utils.UTC()) order = { - 'complexType': 'SoftLayer_Container_Product_Order_Virtual_Guest_' - 'Upgrade', + 'complexType': 'SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade', 'properties': [{ 'name': 'MAINTENANCE_WINDOW', 'value': maintenance_window.strftime("%Y-%m-%d %H:%M:%S%z") @@ -874,6 +867,64 @@ def upgrade(self, instance_id, cpus=None, memory=None, return True return False + def order_guest(self, guest_object, test=False): + """Uses Product_Order::placeOrder to create a virtual guest. + + Useful when creating a virtual guest with options not supported by Virtual_Guest::createObject + specifically ipv6 support. + + :param dictionary guest_object: See SoftLayer.CLI.virt.create._parse_create_args + + Example:: + + new_vsi = { + 'domain': u'test01.labs.sftlyr.ws', + 'hostname': u'minion05', + 'datacenter': u'hkg02', + 'flavor': 'BL1_1X2X100' + 'dedicated': False, + 'private': False, + 'os_code' : u'UBUNTU_LATEST', + 'hourly': True, + 'ssh_keys': [1234], + 'disks': ('100','25'), + 'local_disk': True, + 'tags': 'test, pleaseCancel', + 'public_security_groups': [12, 15], + 'ipv6': True + } + + vsi = mgr.order_guest(new_vsi) + # vsi will have the newly created vsi receipt. + # vsi['orderDetails']['virtualGuests'] will be an array of created Guests + print vsi + """ + tags = guest_object.pop('tags', None) + template = self.verify_create_instance(**guest_object) + + if guest_object.get('ipv6'): + ipv6_price = self.ordering_manager.get_price_id_list('PUBLIC_CLOUD_SERVER', ['1_IPV6_ADDRESS']) + template['prices'].append({'id': ipv6_price[0]}) + + # Notice this is `userdata` from the cli, but we send it in as `userData` + if guest_object.get('userdata'): + # SL_Virtual_Guest::generateOrderTemplate() doesn't respect userData, so we need to add it ourself + template['virtualGuests'][0]['userData'] = [{"value": guest_object.get('userdata')}] + if guest_object.get('host_id'): + template['hostId'] = guest_object.get('host_id') + if guest_object.get('placement_id'): + template['virtualGuests'][0]['placementGroupId'] = guest_object.get('placement_id') + + if test: + result = self.client.call('Product_Order', 'verifyOrder', template) + else: + result = self.client.call('Product_Order', 'placeOrder', template) + if tags is not None: + virtual_guests = utils.lookup(result, 'orderDetails', 'virtualGuests') + for guest in virtual_guests: + self.set_tags(tags, guest_id=guest['id']) + return result + def _get_package_items(self): """Following Method gets all the item ids related to VS. diff --git a/SoftLayer/managers/vs_placement.py b/SoftLayer/managers/vs_placement.py new file mode 100644 index 000000000..d40a845e9 --- /dev/null +++ b/SoftLayer/managers/vs_placement.py @@ -0,0 +1,115 @@ +""" + SoftLayer.vs_placement + ~~~~~~~~~~~~~~~~~~~~~~~ + Placement Group Manager + + :license: MIT, see License for more details. +""" + +import logging + +from SoftLayer import utils + +# Invalid names are ignored due to long method names and short argument names +# pylint: disable=invalid-name, no-self-use + +LOGGER = logging.getLogger(__name__) + + +class PlacementManager(utils.IdentifierMixin, object): + """Manages SoftLayer Reserved Capacity Groups. + + Product Information + + - https://console.test.cloud.ibm.com/docs/vsi/vsi_placegroup.html#placement-groups + - https://softlayer.github.io/reference/services/SoftLayer_Account/getPlacementGroups/ + - https://softlayer.github.io/reference/services/SoftLayer_Virtual_PlacementGroup_Rule/ + + Existing instances cannot be added to a placement group. + You can only add a virtual server instance to a placement group at provisioning. + To remove an instance from a placement group, you must delete or reclaim the instance. + + :param SoftLayer.API.BaseClient client: the client instance + """ + + def __init__(self, client): + self.client = client + self.account = client['Account'] + self.resolvers = [self._get_id_from_name] + + def list(self, mask=None): + """List existing placement groups + + Calls SoftLayer_Account::getPlacementGroups + """ + if mask is None: + mask = "mask[id, name, createDate, rule, guestCount, backendRouter[id, hostname]]" + groups = self.client.call('Account', 'getPlacementGroups', mask=mask, iter=True) + return groups + + def create(self, placement_object): + """Creates a placement group + + :param dictionary placement_object: Below are the fields you can specify, taken from + https://softlayer.github.io/reference/datatypes/SoftLayer_Virtual_PlacementGroup/ + placement_object = { + 'backendRouterId': 12345, + 'name': 'Test Name', + 'ruleId': 12345 + } + + """ + return self.client.call('SoftLayer_Virtual_PlacementGroup', 'createObject', placement_object) + + def get_routers(self): + """Calls SoftLayer_Virtual_PlacementGroup::getAvailableRouters()""" + return self.client.call('SoftLayer_Virtual_PlacementGroup', 'getAvailableRouters') + + def get_object(self, group_id, mask=None): + """Returns a PlacementGroup Object + + https://softlayer.github.io/reference/services/SoftLayer_Virtual_PlacementGroup/getObject + """ + if mask is None: + mask = "mask[id, name, createDate, rule, backendRouter[id, hostname]," \ + "guests[activeTransaction[id,transactionStatus[name,friendlyName]]]]" + return self.client.call('SoftLayer_Virtual_PlacementGroup', 'getObject', id=group_id, mask=mask) + + def delete(self, group_id): + """Deletes a PlacementGroup + + Placement group must be empty to be deleted. + https://softlayer.github.io/reference/services/SoftLayer_Virtual_PlacementGroup/deleteObject + """ + return self.client.call('SoftLayer_Virtual_PlacementGroup', 'deleteObject', id=group_id) + + def get_all_rules(self): + """Returns all available rules for creating a placement group""" + return self.client.call('SoftLayer_Virtual_PlacementGroup_Rule', 'getAllObjects') + + def get_rule_id_from_name(self, name): + """Finds the rule that matches name. + + SoftLayer_Virtual_PlacementGroup_Rule.getAllObjects doesn't support objectFilters. + """ + results = self.client.call('SoftLayer_Virtual_PlacementGroup_Rule', 'getAllObjects') + return [result['id'] for result in results if result['keyName'] == name.upper()] + + def get_backend_router_id_from_hostname(self, hostname): + """Finds the backend router Id that matches the hostname given + + No way to use an objectFilter to find a backendRouter, so we have to search the hard way. + """ + results = self.client.call('SoftLayer_Network_Pod', 'getAllObjects') + return [result['backendRouterId'] for result in results if result['backendRouterName'] == hostname.lower()] + + def _get_id_from_name(self, name): + """List placement group ids which match the given name.""" + _filter = { + 'placementGroups': { + 'name': {'operation': name} + } + } + mask = "mask[id, name]" + results = self.client.call('Account', 'getPlacementGroups', filter=_filter, mask=mask) + return [result['id'] for result in results] diff --git a/SoftLayer/shell/cmd_help.py b/SoftLayer/shell/cmd_help.py index eeceef068..2f548d75d 100644 --- a/SoftLayer/shell/cmd_help.py +++ b/SoftLayer/shell/cmd_help.py @@ -22,6 +22,8 @@ def cli(ctx, env): shell_commands = [] for name in cli_core.cli.list_commands(ctx): command = cli_core.cli.get_command(ctx, name) + if command.short_help is None: + command.short_help = command.help details = (name, command.short_help) if name in dict(routes.ALL_ROUTES): shell_commands.append(details) diff --git a/SoftLayer/shell/core.py b/SoftLayer/shell/core.py index ed90f9c95..32c250584 100644 --- a/SoftLayer/shell/core.py +++ b/SoftLayer/shell/core.py @@ -26,7 +26,6 @@ class ShellExit(Exception): """Exception raised to quit the shell.""" - pass @click.command() diff --git a/SoftLayer/testing/__init__.py b/SoftLayer/testing/__init__.py index 477815725..d5279c03f 100644 --- a/SoftLayer/testing/__init__.py +++ b/SoftLayer/testing/__init__.py @@ -96,11 +96,9 @@ def tearDownClass(cls): def set_up(self): """Aliased from setUp.""" - pass def tear_down(self): """Aliased from tearDown.""" - pass def setUp(self): # NOQA testtools.TestCase.setUp(self) diff --git a/SoftLayer/testing/xmlrpc.py b/SoftLayer/testing/xmlrpc.py index 257a6be75..bd74afe93 100644 --- a/SoftLayer/testing/xmlrpc.py +++ b/SoftLayer/testing/xmlrpc.py @@ -80,7 +80,6 @@ def do_POST(self): def log_message(self, fmt, *args): """Override log_message.""" - pass def _item_by_key_postfix(dictionary, key_prefix): diff --git a/SoftLayer/transports.py b/SoftLayer/transports.py index 3aa896f11..eaee84716 100644 --- a/SoftLayer/transports.py +++ b/SoftLayer/transports.py @@ -34,7 +34,7 @@ ] REST_SPECIAL_METHODS = { - 'deleteObject': 'DELETE', + # 'deleteObject': 'DELETE', 'createObject': 'POST', 'createObjects': 'POST', 'editObject': 'PUT', @@ -379,7 +379,15 @@ def __call__(self, request): request.url = resp.url resp.raise_for_status() - result = json.loads(resp.text) + + if resp.text != "": + try: + result = json.loads(resp.text) + except ValueError as json_ex: + raise exceptions.SoftLayerAPIError(resp.status_code, str(json_ex)) + else: + raise exceptions.SoftLayerAPIError(resp.status_code, "Empty response.") + request.result = result if isinstance(result, list): @@ -388,8 +396,15 @@ def __call__(self, request): else: return result except requests.HTTPError as ex: - message = json.loads(ex.response.text)['error'] - request.url = ex.response.url + try: + message = json.loads(ex.response.text)['error'] + request.url = ex.response.url + except ValueError as json_ex: + if ex.response.text == "": + raise exceptions.SoftLayerAPIError(resp.status_code, "Empty response.") + else: + raise exceptions.SoftLayerAPIError(resp.status_code, str(json_ex)) + raise exceptions.SoftLayerAPIError(ex.response.status_code, message) except requests.RequestException as ex: raise exceptions.TransportError(0, str(ex)) diff --git a/SoftLayer/utils.py b/SoftLayer/utils.py index 131c681f1..96efac342 100644 --- a/SoftLayer/utils.py +++ b/SoftLayer/utils.py @@ -121,8 +121,8 @@ def query_filter_date(start, end): return { 'operation': 'betweenDate', 'options': [ - {'name': 'startDate', 'value': [startdate+' 0:0:0']}, - {'name': 'endDate', 'value': [enddate+' 0:0:0']} + {'name': 'startDate', 'value': [startdate + ' 0:0:0']}, + {'name': 'endDate', 'value': [enddate + ' 0:0:0']} ] } diff --git a/docs/cli/users.rst b/docs/cli/users.rst index 44cd71551..3c98199a7 100644 --- a/docs/cli/users.rst +++ b/docs/cli/users.rst @@ -5,6 +5,7 @@ Users Version 5.6.0 introduces the ability to interact with user accounts from the cli. .. _cli_user_create: + user create ----------- This command will create a user on your account. @@ -19,6 +20,7 @@ Options -h, --help Show this message and exit. :: + slcli user create my@email.com -e my@email.com -p generate -a -t '{"firstName": "Test", "lastName": "Testerson"}' .. _cli_user_list: @@ -83,11 +85,12 @@ Edit a User's details JSON strings should be enclosed in '' and each item should be enclosed in "\" :: + slcli user edit-details testUser -t '{"firstName": "Test", "lastName": "Testerson"}' Options ^^^^^^^ --t, --template TEXT A json string describing `SoftLayer_User_Customer -https://softlayer.github.io/reference/datatypes/SoftLayer_User_Customer/`_. [required] + +-t, --template TEXT A json string describing `SoftLayer_User_Customer `_ . [required] -h, --help Show this message and exit. diff --git a/docs/cli/vs.rst b/docs/cli/vs.rst index 55ee3c189..2276bd7e9 100644 --- a/docs/cli/vs.rst +++ b/docs/cli/vs.rst @@ -81,15 +81,33 @@ datacenter using the command `slcli vs create`. :: - $ slcli vs create --hostname=example --domain=softlayer.com --cpu 2 --memory 1024 -o DEBIAN_LATEST_64 --datacenter=ams01 --billing=hourly + $ slcli vs create --hostname=example --domain=softlayer.com -f B1_1X2X25 -o DEBIAN_LATEST_64 --datacenter=ams01 --billing=hourly This action will incur charges on your account. Continue? [y/N]: y - :.........:......................................: - : name : value : - :.........:......................................: - : id : 1234567 : - : created : 2013-06-13T08:29:44-06:00 : - : guid : 6e013cde-a863-46ee-8s9a-f806dba97c89 : - :.........:......................................: + :..........:.................................:......................................:...........................: + : ID : FQDN : guid : Order Date : + :..........:.................................:......................................:...........................: + : 70112999 : testtesttest.test.com : 1abc7afb-9618-4835-89c9-586f3711d8ea : 2019-01-30T17:16:58-06:00 : + :..........:.................................:......................................:...........................: + :.........................................................................: + : OrderId: 12345678 : + :.......:.................................................................: + : Cost : Description : + :.......:.................................................................: + : 0.0 : Debian GNU/Linux 9.x Stretch/Stable - Minimal Install (64 bit) : + : 0.0 : 25 GB (SAN) : + : 0.0 : Reboot / Remote Console : + : 0.0 : 100 Mbps Public & Private Network Uplinks : + : 0.0 : 0 GB Bandwidth Allotment : + : 0.0 : 1 IP Address : + : 0.0 : Host Ping and TCP Service Monitoring : + : 0.0 : Email and Ticket : + : 0.0 : Automated Reboot from Monitoring : + : 0.0 : Unlimited SSL VPN Users & 1 PPTP VPN User per account : + : 0.0 : Nessus Vulnerability Assessment & Reporting : + : 0.0 : 2 GB : + : 0.0 : 1 x 2.0 GHz or higher Core : + : 0.000 : Total hourly cost : + :.......:.................................................................: After the last command, the virtual server is now being built. It should @@ -194,3 +212,10 @@ Reserved Capacity vs/reserved_capacity +Placement Groups +---------------- +.. toctree:: + :maxdepth: 2 + + vs/placement_group + diff --git a/docs/cli/vs/placement_group.rst b/docs/cli/vs/placement_group.rst new file mode 100644 index 000000000..c6aa09944 --- /dev/null +++ b/docs/cli/vs/placement_group.rst @@ -0,0 +1,132 @@ +.. _vs_placement_group_user_docs: + +Working with Placement Groups +============================= +A `Placement Group `_ is a way to control which physical servers your virtual servers get provisioned onto. + +To create a `Virtual_PlacementGroup `_ object, you will need to know the following: + +- backendRouterId, from `getAvailableRouters `_ +- ruleId, from `getAllObjects `_ +- name, can be any string, but most be unique on your account + +Once a placement group is created, you can create new virtual servers in that group. Existing VSIs cannot be moved into a placement group. When ordering a VSI in a placement group, make sure to set the `placementGroupId `_ for each guest in your order. + +use the --placementgroup option with `vs create` to specify creating a VSI in a specific group. + +:: + + + $ slcli vs create -H testGroup001 -D test.com -f B1_1X2X25 -d mex01 -o DEBIAN_LATEST --placementgroup testGroup + +Placement groups can only be deleted once all the virtual guests in the group have been reclaimed. + +.. _cli_vs_placementgroup_create: + +vs placementgroup create +------------------------ +This command will create a placement group. + +:: + + $ slcli vs placementgroup create --name testGroup -b bcr02a.dal06 -r SPREAD + +Options +^^^^^^^ +--name TEXT Name for this new placement group. [required] +-b, --backend_router TEXT backendRouter, can be either the hostname or id. [required] +-r, --rule TEXT The keyName or Id of the rule to govern this placement group. [required] + + +.. _cli_vs_placementgroup_create_options: + +vs placementgroup create-options +-------------------------------- +This command will print out the available routers and rule sets for use in creating a placement group. + +:: + + $ slcli vs placementgroup create-options + :.................................................: + : Available Routers : + :..............:..............:...................: + : Datacenter : Hostname : Backend Router Id : + :..............:..............:...................: + : Washington 1 : bcr01.wdc01 : 16358 : + : Tokyo 5 : bcr01a.tok05 : 1587015 : + :..............:..............:...................: + :..............: + : Rules : + :....:.........: + : Id : KeyName : + :....:.........: + : 1 : SPREAD : + :....:.........: + +.. _cli_vs_placementgroup_delete: + +vs placementgroup delete +------------------------ +This command will remove a placement group. The placement group needs to be empty for this command to succeed. + +Options +^^^^^^^ +--purge Delete all guests in this placement group. The group itself can be deleted once all VMs are fully reclaimed + +:: + + $ slcli vs placementgroup delete testGroup + +You can use the flag --purge to auto-cancel all VSIs in a placement group. You will still need to wait for them to be reclaimed before proceeding to delete the group itself. + +:: + + $ slcli vs placementgroup delete testGroup --purge + You are about to delete the following guests! + issues10691547768562.test.com, issues10691547768572.test.com, issues10691547768552.test.com, issues10691548718280.test.com + This action will cancel all guests! Continue? [y/N]: y + Deleting issues10691547768562.test.com... + Deleting issues10691547768572.test.com... + Deleting issues10691547768552.test.com... + Deleting issues10691548718280.test.com... + + +.. _cli_vs_placementgroup_list: + +vs placementgroup list +---------------------- +This command will list all placement groups on your account. + +:: + + $ slcli vs placementgroup list + :..........................................................................................: + : Placement Groups : + :.......:...................:................:........:........:...........................: + : Id : Name : Backend Router : Rule : Guests : Created : + :.......:...................:................:........:........:...........................: + : 31741 : fotest : bcr01a.tor01 : SPREAD : 1 : 2018-11-22T14:36:10-06:00 : + : 64535 : testGroup : bcr01a.mex01 : SPREAD : 3 : 2019-01-17T14:36:42-06:00 : + :.......:...................:................:........:........:...........................: + +.. _cli_vs_placementgroup_detail: + +vs placementgroup detail +------------------------ +This command will provide some detailed information about a specific placement group + +:: + + $ slcli vs placementgroup detail testGroup + :.......:............:................:........:...........................: + : Id : Name : Backend Router : Rule : Created : + :.......:............:................:........:...........................: + : 64535 : testGroup : bcr01a.mex01 : SPREAD : 2019-01-17T14:36:42-06:00 : + :.......:............:................:........:...........................: + :..........:........................:...............:..............:.....:........:...........................:.............: + : Id : FQDN : Primary IP : Backend IP : CPU : Memory : Provisioned : Transaction : + :..........:........................:...............:..............:.....:........:...........................:.............: + : 69134895 : testGroup62.test.com : 169.57.70.166 : 10.131.11.32 : 1 : 1024 : 2019-01-17T17:44:50-06:00 : - : + : 69134901 : testGroup72.test.com : 169.57.70.184 : 10.131.11.59 : 1 : 1024 : 2019-01-17T17:44:53-06:00 : - : + : 69134887 : testGroup52.test.com : 169.57.70.187 : 10.131.11.25 : 1 : 1024 : 2019-01-17T17:44:43-06:00 : - : + :..........:........................:...............:..............:.....:........:...........................:.............: \ No newline at end of file diff --git a/tests/CLI/modules/cdn_tests.py b/tests/CLI/modules/cdn_tests.py index d15259ab5..b39e8b8eb 100644 --- a/tests/CLI/modules/cdn_tests.py +++ b/tests/CLI/modules/cdn_tests.py @@ -49,9 +49,9 @@ def test_load_content(self): def test_purge_content(self): result = self.run_command(['cdn', 'purge', '1234', 'http://example.com']) - + expected = [{"url": "http://example.com", "status": "SUCCESS"}] self.assert_no_fail(result) - self.assertEqual(result.output, "") + self.assertEqual(json.loads(result.output), expected) def test_list_origins(self): result = self.run_command(['cdn', 'origin-list', '1234']) diff --git a/tests/CLI/modules/config_tests.py b/tests/CLI/modules/config_tests.py index 5c21b1da8..4fe9cf867 100644 --- a/tests/CLI/modules/config_tests.py +++ b/tests/CLI/modules/config_tests.py @@ -51,10 +51,12 @@ def set_up(self): transport = testing.MockableTransport(SoftLayer.FixtureTransport()) self.env.client = SoftLayer.BaseClient(transport=transport) + @mock.patch('SoftLayer.Client') @mock.patch('SoftLayer.CLI.formatting.confirm') @mock.patch('SoftLayer.CLI.environment.Environment.getpass') @mock.patch('SoftLayer.CLI.environment.Environment.input') - def test_setup(self, mocked_input, getpass, confirm_mock): + def test_setup(self, mocked_input, getpass, confirm_mock, client): + client.return_value = self.env.client if(sys.platform.startswith("win")): self.skipTest("Test doesn't work in Windows") with tempfile.NamedTemporaryFile() as config_file: @@ -62,19 +64,16 @@ def test_setup(self, mocked_input, getpass, confirm_mock): getpass.return_value = 'A' * 64 mocked_input.side_effect = ['user', 'public', 0] - result = self.run_command(['--config=%s' % config_file.name, - 'config', 'setup']) + result = self.run_command(['--config=%s' % config_file.name, 'config', 'setup']) self.assert_no_fail(result) - self.assertTrue('Configuration Updated Successfully' - in result.output) + self.assertTrue('Configuration Updated Successfully' in result.output) contents = config_file.read().decode("utf-8") + self.assertTrue('[softlayer]' in contents) self.assertTrue('username = user' in contents) - self.assertTrue('api_key = AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' - 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAA' in contents) - self.assertTrue('endpoint_url = %s' % consts.API_PUBLIC_ENDPOINT - in contents) + self.assertTrue('api_key = AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' in contents) + self.assertTrue('endpoint_url = %s' % consts.API_PUBLIC_ENDPOINT in contents) @mock.patch('SoftLayer.CLI.formatting.confirm') @mock.patch('SoftLayer.CLI.environment.Environment.getpass') @@ -115,6 +114,17 @@ def test_get_user_input_custom(self, mocked_input, getpass): self.assertEqual(endpoint_url, 'custom-endpoint') + @mock.patch('SoftLayer.CLI.environment.Environment.getpass') + @mock.patch('SoftLayer.CLI.environment.Environment.input') + def test_github_1074(self, mocked_input, getpass): + """Tests to make sure directly using an endpoint works""" + getpass.return_value = 'A' * 64 + mocked_input.side_effect = ['user', 'test-endpoint', 0] + + _, _, endpoint_url, _ = config.get_user_input(self.env) + + self.assertEqual(endpoint_url, 'test-endpoint') + @mock.patch('SoftLayer.CLI.environment.Environment.getpass') @mock.patch('SoftLayer.CLI.environment.Environment.input') def test_get_user_input_default(self, mocked_input, getpass): diff --git a/tests/CLI/modules/file_tests.py b/tests/CLI/modules/file_tests.py index 9bc56320b..465e9ec03 100644 --- a/tests/CLI/modules/file_tests.py +++ b/tests/CLI/modules/file_tests.py @@ -4,6 +4,7 @@ :license: MIT, see LICENSE for more details. """ +from SoftLayer import exceptions from SoftLayer import testing import json @@ -94,6 +95,31 @@ def test_volume_cancel(self): self.assert_called_with('SoftLayer_Billing_Item', 'cancelItem', args=(False, True, None)) + def test_volume_cancel_with_billing_item(self): + result = self.run_command([ + '--really', 'file', 'volume-cancel', '1234']) + + self.assert_no_fail(result) + self.assertEqual('File volume with id 1234 has been marked' + ' for cancellation\n', result.output) + self.assert_called_with('SoftLayer_Network_Storage', 'getObject') + + def test_volume_cancel_without_billing_item(self): + p_mock = self.set_mock('SoftLayer_Network_Storage', 'getObject') + p_mock.return_value = { + "accountId": 1234, + "capacityGb": 20, + "createDate": "2015-04-29T06:55:55-07:00", + "id": 11111, + "nasType": "NAS", + "username": "SL01SEV307608_1" + } + + result = self.run_command([ + '--really', 'file', 'volume-cancel', '1234']) + + self.assertIsInstance(result.exception, exceptions.SoftLayerError) + def test_volume_detail(self): result = self.run_command(['file', 'volume-detail', '1234']) diff --git a/tests/CLI/modules/order_tests.py b/tests/CLI/modules/order_tests.py index 6b16e5014..c35d6d380 100644 --- a/tests/CLI/modules/order_tests.py +++ b/tests/CLI/modules/order_tests.py @@ -38,13 +38,9 @@ def test_item_list(self): self.assert_no_fail(result) self.assert_called_with('SoftLayer_Product_Package', 'getItems') - expected_results = [{'category': 'testing', - 'keyName': 'item1', - 'description': 'description1'}, - {'category': 'testing', - 'keyName': 'item2', - 'description': 'description2'}] - self.assertEqual(expected_results, json.loads(result.output)) + self.assertIn('description2', result.output) + self.assertIn('testing', result.output) + self.assertIn('item2', result.output) def test_package_list(self): p_mock = self.set_mock('SoftLayer_Product_Package', 'getAllObjects') diff --git a/tests/CLI/modules/server_tests.py b/tests/CLI/modules/server_tests.py index 2f26c4ec6..c9e030770 100644 --- a/tests/CLI/modules/server_tests.py +++ b/tests/CLI/modules/server_tests.py @@ -580,3 +580,15 @@ def test_going_ready(self, _sleep): result = self.run_command(['hw', 'ready', '100', '--wait=100']) self.assert_no_fail(result) self.assertEqual(result.output, '"READY"\n') + + def test_toggle_ipmi_on(self): + mock.return_value = True + result = self.run_command(['server', 'toggle-ipmi', '--enable', '12345']) + self.assert_no_fail(result) + self.assertEqual(result.output, 'True\n') + + def test_toggle_ipmi_off(self): + mock.return_value = True + result = self.run_command(['server', 'toggle-ipmi', '--disable', '12345']) + self.assert_no_fail(result) + self.assertEqual(result.output, 'True\n') diff --git a/tests/CLI/modules/shell_tests.py b/tests/CLI/modules/shell_tests.py new file mode 100644 index 000000000..bf71d7004 --- /dev/null +++ b/tests/CLI/modules/shell_tests.py @@ -0,0 +1,27 @@ +""" + SoftLayer.tests.CLI.modules.shell_tests + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + :license: MIT, see LICENSE for more details. +""" +from SoftLayer import testing + +import mock + + +class ShellTests(testing.TestCase): + @mock.patch('prompt_toolkit.shortcuts.prompt') + def test_shell_quit(self, prompt): + prompt.return_value = "quit" + result = self.run_command(['shell']) + self.assertEqual(result.exit_code, 0) + + @mock.patch('prompt_toolkit.shortcuts.prompt') + @mock.patch('shlex.split') + def test_shell_help(self, prompt, split): + split.side_effect = [(['help']), (['vs', 'list']), (False), (['quit'])] + prompt.return_value = "none" + result = self.run_command(['shell']) + if split.call_count is not 5: + raise Exception("Split not called correctly. Count: " + str(split.call_count)) + self.assertEqual(result.exit_code, 1) diff --git a/tests/CLI/modules/subnet_tests.py b/tests/CLI/modules/subnet_tests.py index 72825e0f9..eaa0b1d99 100644 --- a/tests/CLI/modules/subnet_tests.py +++ b/tests/CLI/modules/subnet_tests.py @@ -37,5 +37,5 @@ def test_detail(self): json.loads(result.output)) def test_list(self): - result = self.run_command(['subnet', 'list']) + result = self.run_command(['subnet', 'list']) self.assert_no_fail(result) diff --git a/tests/CLI/modules/vs/__init__.py b/tests/CLI/modules/vs/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/CLI/modules/vs_capacity_tests.py b/tests/CLI/modules/vs/vs_capacity_tests.py similarity index 79% rename from tests/CLI/modules/vs_capacity_tests.py rename to tests/CLI/modules/vs/vs_capacity_tests.py index 922bf2118..2cee000a0 100644 --- a/tests/CLI/modules/vs_capacity_tests.py +++ b/tests/CLI/modules/vs/vs_capacity_tests.py @@ -1,9 +1,10 @@ """ - SoftLayer.tests.CLI.modules.vs_capacity_tests + SoftLayer.tests.CLI.modules.vs.vs_capacity_tests ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :license: MIT, see LICENSE for more details. """ +import json from SoftLayer.fixtures import SoftLayer_Product_Order from SoftLayer.fixtures import SoftLayer_Product_Package from SoftLayer import testing @@ -15,6 +16,26 @@ def test_list(self): result = self.run_command(['vs', 'capacity', 'list']) self.assert_no_fail(result) + def test_list_no_billing(self): + account_mock = self.set_mock('SoftLayer_Account', 'getReservedCapacityGroups') + account_mock.return_value = [ + { + 'id': 3103, + 'name': 'test-capacity', + 'createDate': '2018-09-24T16:33:09-06:00', + 'availableInstanceCount': 1, + 'instanceCount': 3, + 'occupiedInstanceCount': 1, + 'backendRouter': { + 'hostname': 'bcr02a.dal13', + }, + 'instances': [{'id': 3501}] + } + ] + result = self.run_command(['vs', 'capacity', 'list']) + self.assert_no_fail(result) + self.assertEqual(json.loads(result.output)[0]['Flavor'], 'Unknown Billing Item') + def test_detail(self): result = self.run_command(['vs', 'capacity', 'detail', '1234']) self.assert_no_fail(result) diff --git a/tests/CLI/modules/vs/vs_create_tests.py b/tests/CLI/modules/vs/vs_create_tests.py new file mode 100644 index 000000000..5075d225e --- /dev/null +++ b/tests/CLI/modules/vs/vs_create_tests.py @@ -0,0 +1,616 @@ +""" + SoftLayer.tests.CLI.modules.vs.vs_create_tests + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + :license: MIT, see LICENSE for more details. +""" +import mock +import sys +import tempfile + +from SoftLayer.fixtures import SoftLayer_Product_Package as SoftLayer_Product_Package +from SoftLayer import testing + + +class VirtCreateTests(testing.TestCase): + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create(self, confirm_mock): + confirm_mock.return_value = True + result = self.run_command(['vs', 'create', + '--cpu=2', + '--domain=example.com', + '--hostname=host', + '--os=UBUNTU_LATEST', + '--memory=1', + '--network=100', + '--billing=hourly', + '--datacenter=dal05', + '--tag=dev', + '--tag=green']) + + self.assert_no_fail(result) + self.assertIn('"guid": "1a2b3c-1701"', result.output) + self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') + + args = ({'datacenter': {'name': 'dal05'}, + 'domain': 'example.com', + 'hourlyBillingFlag': True, + 'localDiskFlag': True, + 'maxMemory': 1024, + 'hostname': 'host', + 'startCpus': 2, + 'operatingSystemReferenceCode': 'UBUNTU_LATEST', + 'networkComponents': [{'maxSpeed': '100'}], + 'supplementalCreateObjectOptions': {'bootMode': None}},) + self.assert_called_with('SoftLayer_Virtual_Guest', 'generateOrderTemplate', args=args) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_vlan_subnet(self, confirm_mock): + confirm_mock.return_value = True + + result = self.run_command(['vs', 'create', + '--cpu=2', + '--domain=example.com', + '--hostname=host', + '--os=UBUNTU_LATEST', + '--memory=1', + '--billing=hourly', + '--datacenter=dal05', + '--vlan-private=577940', + '--subnet-private=478700', + '--vlan-public=1639255', + '--subnet-public=297614', + '--tag=dev', + '--tag=green']) + + self.assert_no_fail(result) + self.assertIn('"guid": "1a2b3c-1701"', result.output) + self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') + args = ({ + 'startCpus': 2, + 'maxMemory': 1024, + 'hostname': 'host', + 'domain': 'example.com', + 'localDiskFlag': True, + 'hourlyBillingFlag': True, + 'supplementalCreateObjectOptions': {'bootMode': None}, + 'operatingSystemReferenceCode': 'UBUNTU_LATEST', + 'datacenter': {'name': 'dal05'}, + 'primaryBackendNetworkComponent': { + 'networkVlan': { + 'id': 577940, + 'primarySubnet': {'id': 478700} + } + }, + 'primaryNetworkComponent': { + 'networkVlan': { + 'id': 1639255, + 'primarySubnet': {'id': 297614} + } + } + },) + + self.assert_called_with('SoftLayer_Virtual_Guest', 'generateOrderTemplate', args=args) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_with_wait_ready(self, confirm_mock): + mock = self.set_mock('SoftLayer_Virtual_Guest', 'getObject') + mock.return_value = { + "provisionDate": "2018-06-10T12:00:00-05:00", + "id": 100 + } + confirm_mock.return_value = True + + result = self.run_command(['vs', 'create', + '--cpu=2', + '--domain=example.com', + '--hostname=host', + '--os=UBUNTU_LATEST', + '--memory=1', + '--network=100', + '--billing=hourly', + '--datacenter=dal05', + '--wait=1']) + + self.assert_no_fail(result) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_with_wait_not_ready(self, confirm_mock): + mock = self.set_mock('SoftLayer_Virtual_Guest', 'getObject') + mock.return_value = { + "ready": False, + "guid": "1a2b3c-1701", + "id": 100, + "created": "2018-06-10 12:00:00" + } + confirm_mock.return_value = True + + result = self.run_command(['vs', 'create', + '--cpu=2', + '--domain=example.com', + '--hostname=host', + '--os=UBUNTU_LATEST', + '--memory=1', + '--network=100', + '--billing=hourly', + '--datacenter=dal05', + '--wait=1']) + + self.assertEqual(result.exit_code, 1) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_with_integer_image_id(self, confirm_mock): + confirm_mock.return_value = True + result = self.run_command(['vs', 'create', + '--cpu=2', + '--domain=example.com', + '--hostname=host', + '--image=12345', + '--memory=1', + '--network=100', + '--billing=hourly', + '--datacenter=dal05']) + + self.assert_no_fail(result) + self.assertIn('"guid": "1a2b3c-1701"', result.output) + self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_with_integer_image_guid(self, confirm_mock): + confirm_mock.return_value = True + result = self.run_command(['vs', 'create', + '--cpu=2', + '--domain=example.com', + '--hostname=host', + '--image=aaaa1111bbbb2222', + '--memory=1', + '--network=100', + '--billing=hourly', + '--datacenter=dal05']) + + self.assert_no_fail(result) + self.assertIn('"guid": "1a2b3c-1701"', result.output) + args = ({ + 'startCpus': 2, + 'maxMemory': 1024, + 'hostname': 'host', + 'domain': 'example.com', + 'localDiskFlag': True, + 'hourlyBillingFlag': True, + 'supplementalCreateObjectOptions': {'bootMode': None}, + 'blockDeviceTemplateGroup': {'globalIdentifier': 'aaaa1111bbbb2222'}, + 'datacenter': {'name': 'dal05'}, + 'networkComponents': [{'maxSpeed': '100'}] + },) + + self.assert_called_with('SoftLayer_Virtual_Guest', 'generateOrderTemplate', args=args) + self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_with_flavor(self, confirm_mock): + confirm_mock.return_value = True + result = self.run_command(['vs', 'create', + '--domain=example.com', + '--hostname=host', + '--os=UBUNTU_LATEST', + '--network=100', + '--billing=hourly', + '--datacenter=dal05', + '--flavor=B1_1X2X25']) + + self.assert_no_fail(result) + self.assertIn('"guid": "1a2b3c-1701"', result.output) + self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') + args = ({'datacenter': {'name': 'dal05'}, + 'domain': 'example.com', + 'hourlyBillingFlag': True, + 'hostname': 'host', + 'startCpus': None, + 'maxMemory': None, + 'localDiskFlag': None, + 'supplementalCreateObjectOptions': { + 'bootMode': None, + 'flavorKeyName': 'B1_1X2X25'}, + 'operatingSystemReferenceCode': 'UBUNTU_LATEST', + 'networkComponents': [{'maxSpeed': '100'}]},) + + self.assert_called_with('SoftLayer_Virtual_Guest', 'generateOrderTemplate', args=args) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_with_flavor_and_memory(self, confirm_mock): + confirm_mock.return_value = True + + result = self.run_command(['vs', 'create', + '--domain=example.com', + '--hostname=host', + '--os=UBUNTU_LATEST', + '--network=100', + '--datacenter=TEST00', + '--flavor=BL_1X2X25', + '--memory=2048MB']) + + self.assertEqual(result.exit_code, 2) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_with_dedicated_and_flavor(self, confirm_mock): + confirm_mock.return_value = True + + result = self.run_command(['vs', 'create', + '--domain=example.com', + '--hostname=host', + '--os=UBUNTU_LATEST', + '--network=100', + '--datacenter=TEST00', + '--dedicated', + '--flavor=BL_1X2X25']) + + self.assertEqual(result.exit_code, 2) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_with_hostid_and_flavor(self, confirm_mock): + confirm_mock.return_value = True + + result = self.run_command(['vs', 'create', + '--domain=example.com', + '--hostname=host', + '--os=UBUNTU_LATEST', + '--network=100', + '--datacenter=dal05', + '--host-id=100', + '--flavor=BL_1X2X25']) + + self.assertEqual(result.exit_code, 2) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_with_flavor_and_cpu(self, confirm_mock): + confirm_mock.return_value = True + + result = self.run_command(['vs', 'create', + '--domain=example.com', + '--hostname=host', + '--os=UBUNTU_LATEST', + '--network=100', + '--datacenter=TEST00', + '--flavor=BL_1X2X25', + '--cpu=2']) + + self.assertEqual(result.exit_code, 2) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_with_host_id(self, confirm_mock): + confirm_mock.return_value = True + result = self.run_command(['vs', 'create', + '--cpu=2', + '--domain=example.com', + '--hostname=host', + '--os=UBUNTU_LATEST', + '--memory=1', + '--network=100', + '--billing=hourly', + '--datacenter=dal05', + '--dedicated', + '--host-id=123']) + + self.assert_no_fail(result) + self.assertIn('"guid": "1a2b3c-1701"', result.output) + # Argument testing Example + order_call = self.calls('SoftLayer_Product_Order', 'placeOrder') + order_args = getattr(order_call[0], 'args')[0] + self.assertEqual(123, order_args['hostId']) + + self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') + template_args = ({ + 'startCpus': 2, + 'maxMemory': 1024, + 'hostname': 'host', + 'domain': 'example.com', + 'localDiskFlag': True, + 'hourlyBillingFlag': True, + 'supplementalCreateObjectOptions': { + 'bootMode': None + }, + 'dedicatedHost': { + 'id': 123 + }, + 'operatingSystemReferenceCode': 'UBUNTU_LATEST', + 'datacenter': { + 'name': 'dal05' + }, + 'networkComponents': [ + { + 'maxSpeed': '100' + } + ] + },) + + self.assert_called_with('SoftLayer_Virtual_Guest', 'generateOrderTemplate', args=template_args) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_like(self, confirm_mock): + mock = self.set_mock('SoftLayer_Virtual_Guest', 'getObject') + mock.return_value = { + 'hostname': 'vs-test-like', + 'domain': 'test.sftlyr.ws', + 'maxCpu': 2, + 'maxMemory': 1024, + 'datacenter': {'name': 'dal05'}, + 'networkComponents': [{'maxSpeed': 100}], + 'dedicatedAccountHostOnlyFlag': False, + 'privateNetworkOnlyFlag': False, + 'billingItem': {'orderItem': {'preset': {}}}, + 'operatingSystem': {'softwareLicense': { + 'softwareDescription': {'referenceCode': 'UBUNTU_LATEST'} + }}, + 'hourlyBillingFlag': False, + 'localDiskFlag': True, + 'userData': {} + } + + confirm_mock.return_value = True + result = self.run_command(['vs', 'create', + '--like=123', + '--san', + '--billing=hourly']) + + self.assert_no_fail(result) + self.assertIn('"guid": "1a2b3c-1701"', result.output) + self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') + + args = ({'datacenter': {'name': 'dal05'}, + 'domain': 'test.sftlyr.ws', + 'hourlyBillingFlag': True, + 'hostname': 'vs-test-like', + 'startCpus': 2, + 'maxMemory': 1024, + 'localDiskFlag': False, + 'operatingSystemReferenceCode': 'UBUNTU_LATEST', + 'networkComponents': [{'maxSpeed': 100}], + 'supplementalCreateObjectOptions': {'bootMode': None}},) + self.assert_called_with('SoftLayer_Virtual_Guest', 'generateOrderTemplate', args=args) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_like_tags(self, confirm_mock): + mock = self.set_mock('SoftLayer_Virtual_Guest', 'getObject') + mock.return_value = { + 'hostname': 'vs-test-like', + 'domain': 'test.sftlyr.ws', + 'maxCpu': 2, + 'maxMemory': 1024, + 'datacenter': {'name': 'dal05'}, + 'networkComponents': [{'maxSpeed': 100}], + 'dedicatedAccountHostOnlyFlag': False, + 'privateNetworkOnlyFlag': False, + 'billingItem': {'orderItem': {'preset': {}}}, + 'operatingSystem': {'softwareLicense': { + 'softwareDescription': {'referenceCode': 'UBUNTU_LATEST'} + }}, + 'hourlyBillingFlag': False, + 'localDiskFlag': True, + 'userData': {}, + 'tagReferences': [{'tag': {'name': 'production'}}], + } + + confirm_mock.return_value = True + result = self.run_command(['vs', 'create', + '--like=123', + '--san', + '--billing=hourly']) + + self.assert_no_fail(result) + self.assertIn('"guid": "1a2b3c-1701"', result.output) + self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') + _args = ('production',) + self.assert_called_with('SoftLayer_Virtual_Guest', 'setTags', identifier=1234567, args=_args) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_like_image(self, confirm_mock): + mock = self.set_mock('SoftLayer_Virtual_Guest', 'getObject') + mock.return_value = { + 'hostname': 'vs-test-like', + 'domain': 'test.sftlyr.ws', + 'maxCpu': 2, + 'maxMemory': 1024, + 'datacenter': {'name': 'dal05'}, + 'networkComponents': [{'maxSpeed': 100}], + 'dedicatedAccountHostOnlyFlag': False, + 'privateNetworkOnlyFlag': False, + 'billingItem': {'orderItem': {'preset': {}}}, + 'blockDeviceTemplateGroup': {'globalIdentifier': 'aaa1xxx1122233'}, + 'hourlyBillingFlag': False, + 'localDiskFlag': True, + 'userData': {}, + } + + confirm_mock.return_value = True + result = self.run_command(['vs', 'create', + '--like=123', + '--san', + '--billing=hourly']) + + self.assert_no_fail(result) + self.assertIn('"guid": "1a2b3c-1701"', result.output) + self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') + args = ({'datacenter': {'name': 'dal05'}, + 'domain': 'test.sftlyr.ws', + 'hourlyBillingFlag': True, + 'hostname': 'vs-test-like', + 'startCpus': 2, + 'maxMemory': 1024, + 'localDiskFlag': False, + 'blockDeviceTemplateGroup': {'globalIdentifier': 'aaa1xxx1122233'}, + 'networkComponents': [{'maxSpeed': 100}], + 'supplementalCreateObjectOptions': {'bootMode': None}},) + self.assert_called_with('SoftLayer_Virtual_Guest', 'generateOrderTemplate', args=args) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_like_flavor(self, confirm_mock): + mock = self.set_mock('SoftLayer_Virtual_Guest', 'getObject') + mock.return_value = { + 'hostname': 'vs-test-like', + 'domain': 'test.sftlyr.ws', + 'maxCpu': 2, + 'maxMemory': 1024, + 'datacenter': {'name': 'dal05'}, + 'networkComponents': [{'maxSpeed': 100}], + 'dedicatedAccountHostOnlyFlag': False, + 'privateNetworkOnlyFlag': False, + 'billingItem': {'orderItem': {'preset': {'keyName': 'B1_1X2X25'}}}, + 'operatingSystem': {'softwareLicense': { + 'softwareDescription': {'referenceCode': 'UBUNTU_LATEST'} + }}, + 'hourlyBillingFlag': True, + 'localDiskFlag': False, + 'userData': {} + } + + confirm_mock.return_value = True + result = self.run_command(['vs', 'create', '--like=123']) + + self.assert_no_fail(result) + self.assertIn('"guid": "1a2b3c-1701"', result.output) + self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') + + args = ({'datacenter': {'name': 'dal05'}, + 'domain': 'test.sftlyr.ws', + 'hourlyBillingFlag': True, + 'hostname': 'vs-test-like', + 'startCpus': None, + 'maxMemory': None, + 'localDiskFlag': None, + 'supplementalCreateObjectOptions': { + 'bootMode': None, + 'flavorKeyName': 'B1_1X2X25'}, + 'operatingSystemReferenceCode': 'UBUNTU_LATEST', + 'networkComponents': [{'maxSpeed': 100}]},) + self.assert_called_with('SoftLayer_Virtual_Guest', 'generateOrderTemplate', args=args) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_vs_test(self, confirm_mock): + confirm_mock.return_value = True + + result = self.run_command(['vs', 'create', '--test', '--hostname', 'TEST', + '--domain', 'TESTING', '--cpu', '1', + '--memory', '2048MB', '--datacenter', + 'TEST00', '--os', 'UBUNTU_LATEST']) + + self.assertEqual(result.exit_code, 0) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_vs_flavor_test(self, confirm_mock): + confirm_mock.return_value = True + + result = self.run_command(['vs', 'create', '--test', '--hostname', 'TEST', + '--domain', 'TESTING', '--flavor', 'B1_2X8X25', + '--datacenter', 'TEST00', '--os', 'UBUNTU_LATEST']) + + self.assert_no_fail(result) + self.assertEqual(result.exit_code, 0) + + def test_create_vs_bad_memory(self): + result = self.run_command(['vs', 'create', '--hostname', 'TEST', + '--domain', 'TESTING', '--cpu', '1', + '--memory', '2034MB', '--flavor', + 'UBUNTU', '--datacenter', 'TEST00']) + + self.assertEqual(result.exit_code, 2) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_with_ipv6(self, confirm_mock): + amock = self.set_mock('SoftLayer_Product_Package', 'getItems') + amock.return_value = SoftLayer_Product_Package.getItems_1_IPV6_ADDRESS + result = self.run_command(['vs', 'create', '--test', '--hostname', 'TEST', + '--domain', 'TESTING', '--flavor', 'B1_2X8X25', + '--datacenter', 'TEST00', '--os', 'UBUNTU_LATEST', '--ipv6']) + + self.assert_no_fail(result) + self.assertEqual(result.exit_code, 0) + self.assert_called_with('SoftLayer_Product_Order', 'verifyOrder') + args = ({ + 'startCpus': None, + 'maxMemory': None, + 'hostname': 'TEST', + 'domain': 'TESTING', + 'localDiskFlag': None, + 'hourlyBillingFlag': True, + 'supplementalCreateObjectOptions': { + 'bootMode': None, + 'flavorKeyName': 'B1_2X8X25' + }, + 'operatingSystemReferenceCode': 'UBUNTU_LATEST', + 'datacenter': { + 'name': 'TEST00' + } + }, + ) + self.assert_called_with('SoftLayer_Virtual_Guest', 'generateOrderTemplate', args=args) + self.assertEqual([], self.calls('SoftLayer_Virtual_Guest', 'setTags')) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_with_ipv6_no_test(self, confirm_mock): + confirm_mock.return_value = True + amock = self.set_mock('SoftLayer_Product_Package', 'getItems') + amock.return_value = SoftLayer_Product_Package.getItems_1_IPV6_ADDRESS + result = self.run_command(['vs', 'create', '--hostname', 'TEST', + '--domain', 'TESTING', '--flavor', 'B1_2X8X25', + '--datacenter', 'TEST00', '--os', 'UBUNTU_LATEST', '--ipv6']) + + self.assert_no_fail(result) + self.assertEqual(result.exit_code, 0) + self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') + self.assertEqual([], self.calls('SoftLayer_Virtual_Guest', 'setTags')) + + @mock.patch('SoftLayer.CLI.formatting.no_going_back') + def test_create_with_ipv6_no_prices(self, confirm_mock): + """Test makes sure create fails if ipv6 price cannot be found. + + Since its hard to test if the price ids gets added to placeOrder call, + this test juse makes sure that code block isn't being skipped + """ + result = self.run_command(['vs', 'create', '--test', '--hostname', 'TEST', + '--domain', 'TESTING', '--flavor', 'B1_2X8X25', + '--datacenter', 'TEST00', '--os', 'UBUNTU_LATEST', + '--ipv6']) + self.assertEqual(result.exit_code, 1) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_vs_no_confirm(self, confirm_mock): + confirm_mock.return_value = False + + result = self.run_command(['vs', 'create', '--hostname', 'TEST', + '--domain', 'TESTING', '--flavor', 'B1_2X8X25', + '--datacenter', 'TEST00', '--os', 'UBUNTU_LATEST']) + + self.assertEqual(result.exit_code, 2) + + def test_create_vs_export(self): + if(sys.platform.startswith("win")): + self.skipTest("Test doesn't work in Windows") + with tempfile.NamedTemporaryFile() as config_file: + result = self.run_command(['vs', 'create', '--hostname', 'TEST', '--export', config_file.name, + '--domain', 'TESTING', '--flavor', 'B1_2X8X25', + '--datacenter', 'TEST00', '--os', 'UBUNTU_LATEST']) + self.assert_no_fail(result) + self.assertTrue('Successfully exported options to a template file.' + in result.output) + contents = config_file.read().decode("utf-8") + self.assertIn('hostname=TEST', contents) + self.assertIn('flavor=B1_2X8X25', contents) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_with_userdata(self, confirm_mock): + result = self.run_command(['vs', 'create', '--hostname', 'TEST', '--domain', 'TESTING', + '--flavor', 'B1_2X8X25', '--datacenter', 'TEST00', '--os', 'UBUNTU_LATEST', + '--userdata', 'This is my user data ok']) + self.assert_no_fail(result) + expected_guest = [ + { + 'domain': 'test.local', + 'hostname': 'test', + 'userData': [{'value': 'This is my user data ok'}] + } + ] + # Returns a list of API calls that hit SL_Product_Order::placeOrder + api_call = self.calls('SoftLayer_Product_Order', 'placeOrder') + # Doing this because the placeOrder args are huge and mostly not needed to test + self.assertEqual(api_call[0].args[0]['virtualGuests'], expected_guest) diff --git a/tests/CLI/modules/vs/vs_placement_tests.py b/tests/CLI/modules/vs/vs_placement_tests.py new file mode 100644 index 000000000..3b716a6cd --- /dev/null +++ b/tests/CLI/modules/vs/vs_placement_tests.py @@ -0,0 +1,109 @@ +""" + SoftLayer.tests.CLI.modules.vs_placement_tests + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + :license: MIT, see LICENSE for more details. +""" +import mock + +from SoftLayer import testing + + +class VSPlacementTests(testing.TestCase): + + def test_create_options(self): + result = self.run_command(['vs', 'placementgroup', 'create-options']) + self.assert_no_fail(result) + self.assert_called_with('SoftLayer_Virtual_PlacementGroup', 'getAvailableRouters') + self.assert_called_with('SoftLayer_Virtual_PlacementGroup_Rule', 'getAllObjects') + self.assertEqual([], self.calls('SoftLayer_Virtual_PlacementGroup', 'createObject')) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_group(self, confirm_mock): + confirm_mock.return_value = True + result = self.run_command(['vs', 'placementgroup', 'create', '--name=test', '--backend_router=1', '--rule=2']) + create_args = { + 'name': 'test', + 'backendRouterId': 1, + 'ruleId': 2 + } + self.assert_no_fail(result) + self.assert_called_with('SoftLayer_Virtual_PlacementGroup', 'createObject', args=(create_args,)) + self.assertEqual([], self.calls('SoftLayer_Virtual_PlacementGroup', 'getAvailableRouters')) + + def test_list_groups(self): + result = self.run_command(['vs', 'placementgroup', 'list']) + self.assert_no_fail(result) + self.assert_called_with('SoftLayer_Account', 'getPlacementGroups') + + def test_detail_group_id(self): + result = self.run_command(['vs', 'placementgroup', 'detail', '12345']) + self.assert_no_fail(result) + self.assert_called_with('SoftLayer_Virtual_PlacementGroup', 'getObject', identifier=12345) + + def test_detail_group_name(self): + result = self.run_command(['vs', 'placementgroup', 'detail', 'test']) + self.assert_no_fail(result) + group_filter = { + 'placementGroups': { + 'name': {'operation': 'test'} + } + } + self.assert_called_with('SoftLayer_Account', 'getPlacementGroups', filter=group_filter) + self.assert_called_with('SoftLayer_Virtual_PlacementGroup', 'getObject', identifier=12345) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_delete_group_id(self, confirm_mock): + confirm_mock.return_value = True + result = self.run_command(['vs', 'placementgroup', 'delete', '12345']) + self.assert_no_fail(result) + self.assert_called_with('SoftLayer_Virtual_PlacementGroup', 'deleteObject', identifier=12345) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_delete_group_id_cancel(self, confirm_mock): + confirm_mock.return_value = False + result = self.run_command(['vs', 'placementgroup', 'delete', '12345']) + self.assertEqual(result.exit_code, 2) + self.assertEqual([], self.calls('SoftLayer_Virtual_PlacementGroup', 'deleteObject')) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_delete_group_name(self, confirm_mock): + confirm_mock.return_value = True + result = self.run_command(['vs', 'placementgroup', 'delete', 'test']) + group_filter = { + 'placementGroups': { + 'name': {'operation': 'test'} + } + } + self.assert_no_fail(result) + self.assert_called_with('SoftLayer_Account', 'getPlacementGroups', filter=group_filter) + self.assert_called_with('SoftLayer_Virtual_PlacementGroup', 'deleteObject', identifier=12345) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_delete_group_purge(self, confirm_mock): + confirm_mock.return_value = True + result = self.run_command(['vs', 'placementgroup', 'delete', '1234', '--purge']) + self.assert_no_fail(result) + self.assert_called_with('SoftLayer_Virtual_PlacementGroup', 'getObject') + self.assert_called_with('SoftLayer_Virtual_Guest', 'deleteObject', identifier=69131875) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_delete_group_purge_cancel(self, confirm_mock): + confirm_mock.return_value = False + result = self.run_command(['vs', 'placementgroup', 'delete', '1234', '--purge']) + self.assertEqual(result.exit_code, 2) + self.assertEqual([], self.calls('SoftLayer_Virtual_Guest', 'deleteObject')) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_delete_group_purge_nothing(self, confirm_mock): + group_mock = self.set_mock('SoftLayer_Virtual_PlacementGroup', 'getObject') + group_mock.return_value = { + "id": 1234, + "name": "test-group", + "guests": [], + } + confirm_mock.return_value = True + result = self.run_command(['vs', 'placementgroup', 'delete', '1234', '--purge']) + self.assertEqual(result.exit_code, 2) + self.assert_called_with('SoftLayer_Virtual_PlacementGroup', 'getObject') + self.assertEqual([], self.calls('SoftLayer_Virtual_Guest', 'deleteObject')) diff --git a/tests/CLI/modules/vs_tests.py b/tests/CLI/modules/vs/vs_tests.py similarity index 61% rename from tests/CLI/modules/vs_tests.py rename to tests/CLI/modules/vs/vs_tests.py index d22fcddc1..ce1bb9d73 100644 --- a/tests/CLI/modules/vs_tests.py +++ b/tests/CLI/modules/vs/vs_tests.py @@ -302,397 +302,6 @@ def test_create_options(self): 'os (DEBIAN)': 'DEBIAN_7_64', 'os (UBUNTU)': 'UBUNTU_12_64'}) - @mock.patch('SoftLayer.CLI.formatting.confirm') - def test_create(self, confirm_mock): - confirm_mock.return_value = True - - result = self.run_command(['vs', 'create', - '--cpu=2', - '--domain=example.com', - '--hostname=host', - '--os=UBUNTU_LATEST', - '--memory=1', - '--network=100', - '--billing=hourly', - '--datacenter=dal05', - '--tag=dev', - '--tag=green']) - - self.assert_no_fail(result) - self.assertEqual(json.loads(result.output), - {'guid': '1a2b3c-1701', - 'id': 100, - 'created': '2013-08-01 15:23:45'}) - - args = ({'datacenter': {'name': 'dal05'}, - 'domain': 'example.com', - 'hourlyBillingFlag': True, - 'localDiskFlag': True, - 'maxMemory': 1024, - 'hostname': 'host', - 'startCpus': 2, - 'operatingSystemReferenceCode': 'UBUNTU_LATEST', - 'networkComponents': [{'maxSpeed': '100'}], - 'supplementalCreateObjectOptions': {'bootMode': None}},) - self.assert_called_with('SoftLayer_Virtual_Guest', 'createObject', - args=args) - - @mock.patch('SoftLayer.CLI.formatting.confirm') - def test_create_vlan_subnet(self, confirm_mock): - confirm_mock.return_value = True - - result = self.run_command(['vs', 'create', - '--cpu=2', - '--domain=example.com', - '--hostname=host', - '--os=UBUNTU_LATEST', - '--memory=1', - '--billing=hourly', - '--datacenter=dal05', - '--vlan-private=577940', - '--subnet-private=478700', - '--vlan-public=1639255', - '--subnet-public=297614', - '--tag=dev', - '--tag=green']) - - self.assert_no_fail(result) - self.assertEqual(json.loads(result.output), - {'guid': '1a2b3c-1701', - 'id': 100, - 'created': '2013-08-01 15:23:45'}) - - @mock.patch('SoftLayer.CLI.formatting.confirm') - def test_create_with_wait_ready(self, confirm_mock): - mock = self.set_mock('SoftLayer_Virtual_Guest', 'getObject') - mock.return_value = { - "provisionDate": "2018-06-10T12:00:00-05:00", - "id": 100 - } - confirm_mock.return_value = True - - result = self.run_command(['vs', 'create', - '--cpu=2', - '--domain=example.com', - '--hostname=host', - '--os=UBUNTU_LATEST', - '--memory=1', - '--network=100', - '--billing=hourly', - '--datacenter=dal05', - '--wait=1']) - - self.assert_no_fail(result) - - @mock.patch('SoftLayer.CLI.formatting.confirm') - def test_create_with_wait_not_ready(self, confirm_mock): - mock = self.set_mock('SoftLayer_Virtual_Guest', 'getObject') - mock.return_value = { - "ready": False, - "guid": "1a2b3c-1701", - "id": 100, - "created": "2018-06-10 12:00:00" - } - confirm_mock.return_value = True - - result = self.run_command(['vs', 'create', - '--cpu=2', - '--domain=example.com', - '--hostname=host', - '--os=UBUNTU_LATEST', - '--memory=1', - '--network=100', - '--billing=hourly', - '--datacenter=dal05', - '--wait=1']) - - self.assertEqual(result.exit_code, 1) - - @mock.patch('SoftLayer.CLI.formatting.confirm') - def test_create_with_integer_image_id(self, confirm_mock): - confirm_mock.return_value = True - result = self.run_command(['vs', 'create', - '--cpu=2', - '--domain=example.com', - '--hostname=host', - '--image=12345', - '--memory=1', - '--network=100', - '--billing=hourly', - '--datacenter=dal05']) - - self.assert_no_fail(result) - self.assertEqual(json.loads(result.output), - {'guid': '1a2b3c-1701', - 'id': 100, - 'created': '2013-08-01 15:23:45'}) - - args = ({ - 'datacenter': {'name': 'dal05'}, - 'domain': 'example.com', - 'hourlyBillingFlag': True, - 'localDiskFlag': True, - 'maxMemory': 1024, - 'hostname': 'host', - 'startCpus': 2, - 'blockDeviceTemplateGroup': { - 'globalIdentifier': '0B5DEAF4-643D-46CA-A695-CECBE8832C9D', - }, - 'networkComponents': [{'maxSpeed': '100'}], - 'supplementalCreateObjectOptions': {'bootMode': None} - },) - self.assert_called_with('SoftLayer_Virtual_Guest', 'createObject', - args=args) - - @mock.patch('SoftLayer.CLI.formatting.confirm') - def test_create_with_flavor(self, confirm_mock): - confirm_mock.return_value = True - result = self.run_command(['vs', 'create', - '--domain=example.com', - '--hostname=host', - '--os=UBUNTU_LATEST', - '--network=100', - '--billing=hourly', - '--datacenter=dal05', - '--flavor=B1_1X2X25']) - - self.assert_no_fail(result) - self.assertEqual(json.loads(result.output), - {'guid': '1a2b3c-1701', - 'id': 100, - 'created': '2013-08-01 15:23:45'}) - - args = ({'datacenter': {'name': 'dal05'}, - 'domain': 'example.com', - 'hourlyBillingFlag': True, - 'hostname': 'host', - 'startCpus': None, - 'maxMemory': None, - 'localDiskFlag': None, - 'supplementalCreateObjectOptions': { - 'bootMode': None, - 'flavorKeyName': 'B1_1X2X25'}, - 'operatingSystemReferenceCode': 'UBUNTU_LATEST', - 'networkComponents': [{'maxSpeed': '100'}]},) - self.assert_called_with('SoftLayer_Virtual_Guest', 'createObject', - args=args) - - @mock.patch('SoftLayer.CLI.formatting.confirm') - def test_create_with_flavor_and_memory(self, confirm_mock): - confirm_mock.return_value = True - - result = self.run_command(['vs', 'create', - '--domain=example.com', - '--hostname=host', - '--os=UBUNTU_LATEST', - '--network=100', - '--datacenter=TEST00', - '--flavor=BL_1X2X25', - '--memory=2048MB']) - - self.assertEqual(result.exit_code, 2) - - @mock.patch('SoftLayer.CLI.formatting.confirm') - def test_create_with_dedicated_and_flavor(self, confirm_mock): - confirm_mock.return_value = True - - result = self.run_command(['vs', 'create', - '--domain=example.com', - '--hostname=host', - '--os=UBUNTU_LATEST', - '--network=100', - '--datacenter=TEST00', - '--dedicated', - '--flavor=BL_1X2X25']) - - self.assertEqual(result.exit_code, 2) - - @mock.patch('SoftLayer.CLI.formatting.confirm') - def test_create_with_hostid_and_flavor(self, confirm_mock): - confirm_mock.return_value = True - - result = self.run_command(['vs', 'create', - '--domain=example.com', - '--hostname=host', - '--os=UBUNTU_LATEST', - '--network=100', - '--datacenter=dal05', - '--host-id=100', - '--flavor=BL_1X2X25']) - - self.assertEqual(result.exit_code, 2) - - @mock.patch('SoftLayer.CLI.formatting.confirm') - def test_create_with_flavor_and_cpu(self, confirm_mock): - confirm_mock.return_value = True - - result = self.run_command(['vs', 'create', - '--domain=example.com', - '--hostname=host', - '--os=UBUNTU_LATEST', - '--network=100', - '--datacenter=TEST00', - '--flavor=BL_1X2X25', - '--cpu=2']) - - self.assertEqual(result.exit_code, 2) - - @mock.patch('SoftLayer.CLI.formatting.confirm') - def test_create_with_host_id(self, confirm_mock): - confirm_mock.return_value = True - result = self.run_command(['vs', 'create', - '--cpu=2', - '--domain=example.com', - '--hostname=host', - '--os=UBUNTU_LATEST', - '--memory=1', - '--network=100', - '--billing=hourly', - '--datacenter=dal05', - '--dedicated', - '--host-id=123']) - - self.assert_no_fail(result) - self.assertEqual(json.loads(result.output), - {'guid': '1a2b3c-1701', - 'id': 100, - 'created': '2013-08-01 15:23:45'}) - - args = ({'datacenter': {'name': 'dal05'}, - 'domain': 'example.com', - 'hourlyBillingFlag': True, - 'localDiskFlag': True, - 'maxMemory': 1024, - 'hostname': 'host', - 'startCpus': 2, - 'operatingSystemReferenceCode': 'UBUNTU_LATEST', - 'networkComponents': [{'maxSpeed': '100'}], - 'dedicatedHost': {'id': 123}, - 'supplementalCreateObjectOptions': {'bootMode': None}},) - self.assert_called_with('SoftLayer_Virtual_Guest', 'createObject', - args=args) - - @mock.patch('SoftLayer.CLI.formatting.confirm') - def test_create_like(self, confirm_mock): - mock = self.set_mock('SoftLayer_Virtual_Guest', 'getObject') - mock.return_value = { - 'hostname': 'vs-test-like', - 'domain': 'test.sftlyr.ws', - 'maxCpu': 2, - 'maxMemory': 1024, - 'datacenter': {'name': 'dal05'}, - 'networkComponents': [{'maxSpeed': 100}], - 'dedicatedAccountHostOnlyFlag': False, - 'privateNetworkOnlyFlag': False, - 'billingItem': {'orderItem': {'preset': {}}}, - 'operatingSystem': {'softwareLicense': { - 'softwareDescription': {'referenceCode': 'UBUNTU_LATEST'} - }}, - 'hourlyBillingFlag': False, - 'localDiskFlag': True, - 'userData': {} - } - - confirm_mock.return_value = True - result = self.run_command(['vs', 'create', - '--like=123', - '--san', - '--billing=hourly']) - - self.assert_no_fail(result) - self.assertEqual(json.loads(result.output), - {'guid': '1a2b3c-1701', - 'id': 100, - 'created': '2013-08-01 15:23:45'}) - - args = ({'datacenter': {'name': 'dal05'}, - 'domain': 'test.sftlyr.ws', - 'hourlyBillingFlag': True, - 'hostname': 'vs-test-like', - 'startCpus': 2, - 'maxMemory': 1024, - 'localDiskFlag': False, - 'operatingSystemReferenceCode': 'UBUNTU_LATEST', - 'networkComponents': [{'maxSpeed': 100}], - 'supplementalCreateObjectOptions': {'bootMode': None}},) - self.assert_called_with('SoftLayer_Virtual_Guest', 'createObject', - args=args) - - @mock.patch('SoftLayer.CLI.formatting.confirm') - def test_create_like_flavor(self, confirm_mock): - mock = self.set_mock('SoftLayer_Virtual_Guest', 'getObject') - mock.return_value = { - 'hostname': 'vs-test-like', - 'domain': 'test.sftlyr.ws', - 'maxCpu': 2, - 'maxMemory': 1024, - 'datacenter': {'name': 'dal05'}, - 'networkComponents': [{'maxSpeed': 100}], - 'dedicatedAccountHostOnlyFlag': False, - 'privateNetworkOnlyFlag': False, - 'billingItem': {'orderItem': {'preset': {'keyName': 'B1_1X2X25'}}}, - 'operatingSystem': {'softwareLicense': { - 'softwareDescription': {'referenceCode': 'UBUNTU_LATEST'} - }}, - 'hourlyBillingFlag': True, - 'localDiskFlag': False, - 'userData': {} - } - - confirm_mock.return_value = True - result = self.run_command(['vs', 'create', '--like=123']) - - self.assert_no_fail(result) - self.assertEqual(json.loads(result.output), - {'guid': '1a2b3c-1701', - 'id': 100, - 'created': '2013-08-01 15:23:45'}) - - args = ({'datacenter': {'name': 'dal05'}, - 'domain': 'test.sftlyr.ws', - 'hourlyBillingFlag': True, - 'hostname': 'vs-test-like', - 'startCpus': None, - 'maxMemory': None, - 'localDiskFlag': None, - 'supplementalCreateObjectOptions': { - 'bootMode': None, - 'flavorKeyName': 'B1_1X2X25'}, - 'operatingSystemReferenceCode': 'UBUNTU_LATEST', - 'networkComponents': [{'maxSpeed': 100}]},) - self.assert_called_with('SoftLayer_Virtual_Guest', 'createObject', - args=args) - - @mock.patch('SoftLayer.CLI.formatting.confirm') - def test_create_vs_test(self, confirm_mock): - confirm_mock.return_value = True - - result = self.run_command(['vs', 'create', '--test', '--hostname', 'TEST', - '--domain', 'TESTING', '--cpu', '1', - '--memory', '2048MB', '--datacenter', - 'TEST00', '--os', 'UBUNTU_LATEST']) - - self.assertEqual(result.exit_code, 0) - - @mock.patch('SoftLayer.CLI.formatting.confirm') - def test_create_vs_flavor_test(self, confirm_mock): - confirm_mock.return_value = True - - result = self.run_command(['vs', 'create', '--test', '--hostname', 'TEST', - '--domain', 'TESTING', '--flavor', 'B1_2X8X25', - '--datacenter', 'TEST00', '--os', 'UBUNTU_LATEST']) - - self.assert_no_fail(result) - self.assertEqual(result.exit_code, 0) - - def test_create_vs_bad_memory(self): - result = self.run_command(['vs', 'create', '--hostname', 'TEST', - '--domain', 'TESTING', '--cpu', '1', - '--memory', '2034MB', '--flavor', - 'UBUNTU', '--datacenter', 'TEST00']) - - self.assertEqual(result.exit_code, 2) - @mock.patch('SoftLayer.CLI.formatting.confirm') def test_dns_sync_both(self, confirm_mock): confirm_mock.return_value = True @@ -924,10 +533,13 @@ def test_upgrade_with_flavor(self, confirm_mock): self.assertIn({'id': 100}, order_container['virtualGuests']) self.assertEqual(order_container['virtualGuests'], [{'id': 100}]) - def test_upgrade_with_cpu_memory_and_flavor(self): + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_upgrade_with_cpu_memory_and_flavor(self, confirm_mock): + confirm_mock.return_value = True result = self.run_command(['vs', 'upgrade', '100', '--cpu=4', '--memory=1024', '--flavor=M1_64X512X100']) - self.assertEqual("Do not use cpu, private and memory if you are using flavors", str(result.exception)) + self.assertEqual(result.exit_code, 1) + self.assertIsInstance(result.exception, ValueError) def test_edit(self): result = self.run_command(['vs', 'edit', @@ -1042,3 +654,9 @@ def test_cancel_no_confirm(self, confirm_mock): result = self.run_command(['vs', 'cancel', '100']) self.assertEqual(result.exit_code, 2) + + def test_vs_capture(self): + + result = self.run_command(['vs', 'capture', '100', '--name', 'TestName']) + self.assert_no_fail(result) + self.assert_called_with('SoftLayer_Virtual_Guest', 'createArchiveTransaction', identifier=100) diff --git a/tests/managers/cdn_tests.py b/tests/managers/cdn_tests.py index 6f4387760..8de4bd508 100644 --- a/tests/managers/cdn_tests.py +++ b/tests/managers/cdn_tests.py @@ -5,6 +5,7 @@ :license: MIT, see LICENSE for more details. """ import math +import mock from SoftLayer import fixtures from SoftLayer.managers import cdn @@ -110,25 +111,30 @@ def test_purge_content(self): math.ceil(len(urls) / float(cdn.MAX_URLS_PER_PURGE))) def test_purge_content_failure(self): - urls = ['http://z/img/0x004.png', + urls = ['http://', 'http://y/img/0x002.png', 'http://x/img/0x001.png'] - mock = self.set_mock('SoftLayer_Network_ContentDelivery_Account', - 'purgeCache') - mock.return_value = False + contents = [ + {'url': urls[0], 'statusCode': 'INVALID_URL'}, + {'url': urls[1], 'statusCode': 'FAILED'}, + {'url': urls[2], 'statusCode': 'FAILED'} + ] - self.cdn_client.purge_content(12345, urls) - calls = self.calls('SoftLayer_Network_ContentDelivery_Account', - 'purgeCache') - self.assertEqual(len(calls), - math.ceil(len(urls) / float(cdn.MAX_URLS_PER_PURGE))) + self.cdn_client.account = mock.Mock() + self.cdn_client.account.purgeCache.return_value = contents + + result = self.cdn_client.purge_content(12345, urls) + + self.assertEqual(contents, result) def test_purge_content_single(self): url = 'http://geocities.com/Area51/Meteor/12345/under_construction.gif' + self.cdn_client.account = mock.Mock() + self.cdn_client.account.purgeCache.return_value = [{'url': url, 'statusCode': 'SUCCESS'}] - self.cdn_client.purge_content(12345, url) - self.assert_called_with('SoftLayer_Network_ContentDelivery_Account', - 'purgeCache', - args=([url],), - identifier=12345) + expected = [{'url': url, 'statusCode': 'SUCCESS'}] + + result = self.cdn_client.purge_content(12345, url) + + self.assertEqual(expected, result) diff --git a/tests/managers/network_tests.py b/tests/managers/network_tests.py index 7a84bebae..e78b91f1a 100644 --- a/tests/managers/network_tests.py +++ b/tests/managers/network_tests.py @@ -92,7 +92,7 @@ def test_add_subnet_for_ipv4(self): version=4, test_order=False) - self.assertEqual(fixtures.SoftLayer_Product_Order.verifyOrder, result) + self.assertEqual(fixtures.SoftLayer_Product_Order.placeOrder, result) result = self.network.add_subnet('global', test_order=True) diff --git a/tests/managers/vs/__init__.py b/tests/managers/vs/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/managers/vs_capacity_tests.py b/tests/managers/vs/vs_capacity_tests.py similarity index 91% rename from tests/managers/vs_capacity_tests.py rename to tests/managers/vs/vs_capacity_tests.py index 43db16afb..5229ebec4 100644 --- a/tests/managers/vs_capacity_tests.py +++ b/tests/managers/vs/vs_capacity_tests.py @@ -1,5 +1,5 @@ """ - SoftLayer.tests.managers.vs_capacity_tests + SoftLayer.tests.managers.vs.vs_capacity_tests ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :license: MIT, see LICENSE for more details. @@ -13,7 +13,7 @@ from SoftLayer import testing -class VSCapacityTests(testing.TestCase): +class VSManagerCapacityTests(testing.TestCase): def set_up(self): self.manager = SoftLayer.CapacityManager(self.client) @@ -46,6 +46,16 @@ def test_get_available_routers(self): self.assert_called_with('SoftLayer_Network_Pod', 'getAllObjects') self.assertEqual(result[0]['keyname'], 'WASHINGTON07') + def test_get_available_routers_search(self): + + result = self.manager.get_available_routers('wdc07') + package_filter = {'keyName': {'operation': 'RESERVED_CAPACITY'}} + pod_filter = {'datacenterName': {'operation': 'wdc07'}} + self.assert_called_with('SoftLayer_Product_Package', 'getAllObjects', mask=mock.ANY, filter=package_filter) + self.assert_called_with('SoftLayer_Product_Package', 'getRegions', mask=mock.ANY) + self.assert_called_with('SoftLayer_Network_Pod', 'getAllObjects', filter=pod_filter) + self.assertEqual(result[0]['keyname'], 'WASHINGTON07') + def test_create(self): item_mock = self.set_mock('SoftLayer_Product_Package', 'getItems') item_mock.return_value = SoftLayer_Product_Package.getItems_RESERVED_CAPACITY diff --git a/tests/managers/vs/vs_order_tests.py b/tests/managers/vs/vs_order_tests.py new file mode 100644 index 000000000..12750224d --- /dev/null +++ b/tests/managers/vs/vs_order_tests.py @@ -0,0 +1,175 @@ +""" + SoftLayer.tests.managers.vs.vs_order_tests + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + These tests deal with ordering in the VS manager. + :license: MIT, see LICENSE for more details. + +""" +import mock + +import SoftLayer +from SoftLayer import fixtures +from SoftLayer import testing + + +class VSOrderTests(testing.TestCase): + + def set_up(self): + self.vs = SoftLayer.VSManager(self.client) + + @mock.patch('SoftLayer.managers.vs.VSManager._generate_create_dict') + def test_create_verify(self, create_dict): + create_dict.return_value = {'test': 1, 'verify': 1} + + self.vs.verify_create_instance(test=1, verify=1, tags=['test', 'tags']) + + create_dict.assert_called_once_with(test=1, verify=1) + self.assert_called_with('SoftLayer_Virtual_Guest', + 'generateOrderTemplate', + args=({'test': 1, 'verify': 1},)) + + def test_upgrade(self): + # test single upgrade + result = self.vs.upgrade(1, cpus=4, public=False) + + self.assertEqual(result, True) + self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') + call = self.calls('SoftLayer_Product_Order', 'placeOrder')[0] + order_container = call.args[0] + self.assertEqual(order_container['prices'], [{'id': 1007}]) + self.assertEqual(order_container['virtualGuests'], [{'id': 1}]) + + def test_upgrade_blank(self): + # Now test a blank upgrade + result = self.vs.upgrade(1) + + self.assertEqual(result, False) + self.assertEqual(self.calls('SoftLayer_Product_Order', 'placeOrder'), + []) + + def test_upgrade_full(self): + # Testing all parameters Upgrade + result = self.vs.upgrade(1, + cpus=4, + memory=2, + nic_speed=1000, + public=True) + + self.assertEqual(result, True) + self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') + call = self.calls('SoftLayer_Product_Order', 'placeOrder')[0] + order_container = call.args[0] + self.assertIn({'id': 1144}, order_container['prices']) + self.assertIn({'id': 1133}, order_container['prices']) + self.assertIn({'id': 1122}, order_container['prices']) + self.assertEqual(order_container['virtualGuests'], [{'id': 1}]) + + def test_upgrade_with_flavor(self): + # Testing Upgrade with parameter preset + result = self.vs.upgrade(1, + preset="M1_64X512X100", + nic_speed=1000, + public=True) + + self.assertEqual(result, True) + self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') + call = self.calls('SoftLayer_Product_Order', 'placeOrder')[0] + order_container = call.args[0] + self.assertEqual(799, order_container['presetId']) + self.assertIn({'id': 1}, order_container['virtualGuests']) + self.assertIn({'id': 1122}, order_container['prices']) + self.assertEqual(order_container['virtualGuests'], [{'id': 1}]) + + def test_upgrade_dedicated_host_instance(self): + mock = self.set_mock('SoftLayer_Virtual_Guest', 'getUpgradeItemPrices') + mock.return_value = fixtures.SoftLayer_Virtual_Guest.DEDICATED_GET_UPGRADE_ITEM_PRICES + + # test single upgrade + result = self.vs.upgrade(1, cpus=4, public=False) + + self.assertEqual(result, True) + self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') + call = self.calls('SoftLayer_Product_Order', 'placeOrder')[0] + order_container = call.args[0] + self.assertEqual(order_container['prices'], [{'id': 115566}]) + self.assertEqual(order_container['virtualGuests'], [{'id': 1}]) + + def test_get_item_id_for_upgrade(self): + item_id = 0 + package_items = self.client['Product_Package'].getItems(id=46) + for item in package_items: + if ((item['prices'][0]['categories'][0]['id'] == 3) + and (item.get('capacity') == '2')): + item_id = item['prices'][0]['id'] + break + self.assertEqual(1133, item_id) + + def test_get_package_items(self): + self.vs._get_package_items() + self.assert_called_with('SoftLayer_Product_Package', 'getItems') + + def test_get_price_id_for_upgrade(self): + package_items = self.vs._get_package_items() + + price_id = self.vs._get_price_id_for_upgrade(package_items=package_items, + option='cpus', + value='4') + self.assertEqual(1144, price_id) + + def test_get_price_id_for_upgrade_skips_location_price(self): + package_items = self.vs._get_package_items() + + price_id = self.vs._get_price_id_for_upgrade(package_items=package_items, + option='cpus', + value='55') + self.assertEqual(None, price_id) + + def test_get_price_id_for_upgrade_finds_nic_price(self): + package_items = self.vs._get_package_items() + + price_id = self.vs._get_price_id_for_upgrade(package_items=package_items, + option='memory', + value='2') + self.assertEqual(1133, price_id) + + def test_get_price_id_for_upgrade_finds_memory_price(self): + package_items = self.vs._get_package_items() + + price_id = self.vs._get_price_id_for_upgrade(package_items=package_items, + option='nic_speed', + value='1000') + self.assertEqual(1122, price_id) + + @mock.patch('SoftLayer.managers.vs.VSManager._generate_create_dict') + def test_order_guest(self, create_dict): + create_dict.return_value = {'test': 1, 'verify': 1} + guest = {'test': 1, 'verify': 1, 'tags': ['First']} + result = self.vs.order_guest(guest, test=False) + create_dict.assert_called_once_with(test=1, verify=1) + self.assertEqual(1234, result['orderId']) + self.assert_called_with('SoftLayer_Virtual_Guest', 'generateOrderTemplate') + self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') + self.assert_called_with('SoftLayer_Virtual_Guest', 'setTags', identifier=1234567) + + @mock.patch('SoftLayer.managers.vs.VSManager._generate_create_dict') + def test_order_guest_verify(self, create_dict): + create_dict.return_value = {'test': 1, 'verify': 1} + guest = {'test': 1, 'verify': 1, 'tags': ['First']} + result = self.vs.order_guest(guest, test=True) + create_dict.assert_called_once_with(test=1, verify=1) + self.assertEqual(1234, result['orderId']) + self.assert_called_with('SoftLayer_Virtual_Guest', 'generateOrderTemplate') + self.assert_called_with('SoftLayer_Product_Order', 'verifyOrder') + + @mock.patch('SoftLayer.managers.vs.VSManager._generate_create_dict') + def test_order_guest_ipv6(self, create_dict): + amock = self.set_mock('SoftLayer_Product_Package', 'getItems') + amock.return_value = fixtures.SoftLayer_Product_Package.getItems_1_IPV6_ADDRESS + create_dict.return_value = {'test': 1, 'verify': 1} + guest = {'test': 1, 'verify': 1, 'tags': ['First'], 'ipv6': True} + result = self.vs.order_guest(guest, test=True) + self.assertEqual(1234, result['orderId']) + self.assert_called_with('SoftLayer_Virtual_Guest', 'generateOrderTemplate') + self.assert_called_with('SoftLayer_Product_Package', 'getItems', identifier=200) + self.assert_called_with('SoftLayer_Product_Order', 'verifyOrder') diff --git a/tests/managers/vs/vs_placement_tests.py b/tests/managers/vs/vs_placement_tests.py new file mode 100644 index 000000000..b492f69bf --- /dev/null +++ b/tests/managers/vs/vs_placement_tests.py @@ -0,0 +1,77 @@ +""" + SoftLayer.tests.managers.vs.vs_placement_tests + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + :license: MIT, see LICENSE for more details. + +""" +import mock + +from SoftLayer.managers.vs_placement import PlacementManager +from SoftLayer import testing + + +class VSPlacementManagerTests(testing.TestCase): + + def set_up(self): + self.manager = PlacementManager(self.client) + + def test_list(self): + self.manager.list() + self.assert_called_with('SoftLayer_Account', 'getPlacementGroups', mask=mock.ANY) + + def test_list_mask(self): + mask = "mask[id]" + self.manager.list(mask) + self.assert_called_with('SoftLayer_Account', 'getPlacementGroups', mask=mask) + + def test_create(self): + placement_object = { + 'backendRouter': 1234, + 'name': 'myName', + 'ruleId': 1 + } + self.manager.create(placement_object) + self.assert_called_with('SoftLayer_Virtual_PlacementGroup', 'createObject', args=(placement_object,)) + + def test_get_object(self): + self.manager.get_object(1234) + self.assert_called_with('SoftLayer_Virtual_PlacementGroup', 'getObject', identifier=1234, mask=mock.ANY) + + def test_get_object_with_mask(self): + mask = "mask[id]" + self.manager.get_object(1234, mask) + self.assert_called_with('SoftLayer_Virtual_PlacementGroup', 'getObject', identifier=1234, mask=mask) + + def test_delete(self): + self.manager.delete(1234) + self.assert_called_with('SoftLayer_Virtual_PlacementGroup', 'deleteObject', identifier=1234) + + def test_get_id_from_name(self): + self.manager._get_id_from_name('test') + _filter = { + 'placementGroups': { + 'name': {'operation': 'test'} + } + } + self.assert_called_with('SoftLayer_Account', 'getPlacementGroups', filter=_filter, mask="mask[id, name]") + + def test_get_rule_id_from_name(self): + result = self.manager.get_rule_id_from_name('SPREAD') + self.assertEqual(result[0], 1) + result = self.manager.get_rule_id_from_name('SpReAd') + self.assertEqual(result[0], 1) + + def test_get_rule_id_from_name_failure(self): + result = self.manager.get_rule_id_from_name('SPREAD1') + self.assertEqual(result, []) + + def test_router_search(self): + result = self.manager.get_backend_router_id_from_hostname('bcr01a.ams01') + self.assertEqual(result[0], 117917) + result = self.manager.get_backend_router_id_from_hostname('bcr01A.AMS01') + self.assertEqual(result[0], 117917) + + def test_router_search_failure(self): + result = self.manager.get_backend_router_id_from_hostname('1234.ams01') + self.assertEqual(result, []) diff --git a/tests/managers/vs_tests.py b/tests/managers/vs/vs_tests.py similarity index 69% rename from tests/managers/vs_tests.py rename to tests/managers/vs/vs_tests.py index c24124dd6..f13c3e7b9 100644 --- a/tests/managers/vs_tests.py +++ b/tests/managers/vs/vs_tests.py @@ -1,5 +1,5 @@ """ - SoftLayer.tests.managers.vs_tests + SoftLayer.tests.managers.vs.vs_tests ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :license: MIT, see LICENSE for more details. @@ -16,8 +16,7 @@ class VSTests(testing.TestCase): def set_up(self): - self.vs = SoftLayer.VSManager(self.client, - SoftLayer.OrderingManager(self.client)) + self.vs = SoftLayer.VSManager(self.client, SoftLayer.OrderingManager(self.client)) def test_list_instances(self): results = self.vs.list_instances(hourly=True, monthly=True) @@ -156,17 +155,6 @@ def test_reload_instance_with_new_os(self): args=args, identifier=1) - @mock.patch('SoftLayer.managers.vs.VSManager._generate_create_dict') - def test_create_verify(self, create_dict): - create_dict.return_value = {'test': 1, 'verify': 1} - - self.vs.verify_create_instance(test=1, verify=1, tags=['test', 'tags']) - - create_dict.assert_called_once_with(test=1, verify=1) - self.assert_called_with('SoftLayer_Virtual_Guest', - 'generateOrderTemplate', - args=({'test': 1, 'verify': 1},)) - @mock.patch('SoftLayer.managers.vs.VSManager._generate_create_dict') def test_create_instance(self, create_dict): create_dict.return_value = {'test': 1, 'verify': 1} @@ -842,266 +830,3 @@ def test_capture_additional_disks(self): 'createArchiveTransaction', args=args, identifier=1) - - def test_upgrade(self): - # test single upgrade - result = self.vs.upgrade(1, cpus=4, public=False) - - self.assertEqual(result, True) - self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') - call = self.calls('SoftLayer_Product_Order', 'placeOrder')[0] - order_container = call.args[0] - self.assertEqual(order_container['prices'], [{'id': 1007}]) - self.assertEqual(order_container['virtualGuests'], [{'id': 1}]) - - def test_upgrade_blank(self): - # Now test a blank upgrade - result = self.vs.upgrade(1) - - self.assertEqual(result, False) - self.assertEqual(self.calls('SoftLayer_Product_Order', 'placeOrder'), - []) - - def test_upgrade_full(self): - # Testing all parameters Upgrade - result = self.vs.upgrade(1, - cpus=4, - memory=2, - nic_speed=1000, - public=True) - - self.assertEqual(result, True) - self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') - call = self.calls('SoftLayer_Product_Order', 'placeOrder')[0] - order_container = call.args[0] - self.assertIn({'id': 1144}, order_container['prices']) - self.assertIn({'id': 1133}, order_container['prices']) - self.assertIn({'id': 1122}, order_container['prices']) - self.assertEqual(order_container['virtualGuests'], [{'id': 1}]) - - def test_upgrade_with_flavor(self): - # Testing Upgrade with parameter preset - result = self.vs.upgrade(1, - preset="M1_64X512X100", - nic_speed=1000, - public=True) - - self.assertEqual(result, True) - self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') - call = self.calls('SoftLayer_Product_Order', 'placeOrder')[0] - order_container = call.args[0] - self.assertEqual(799, order_container['presetId']) - self.assertIn({'id': 1}, order_container['virtualGuests']) - self.assertIn({'id': 1122}, order_container['prices']) - self.assertEqual(order_container['virtualGuests'], [{'id': 1}]) - - def test_upgrade_dedicated_host_instance(self): - mock = self.set_mock('SoftLayer_Virtual_Guest', 'getUpgradeItemPrices') - mock.return_value = fixtures.SoftLayer_Virtual_Guest.DEDICATED_GET_UPGRADE_ITEM_PRICES - - # test single upgrade - result = self.vs.upgrade(1, cpus=4, public=False) - - self.assertEqual(result, True) - self.assert_called_with('SoftLayer_Product_Order', 'placeOrder') - call = self.calls('SoftLayer_Product_Order', 'placeOrder')[0] - order_container = call.args[0] - self.assertEqual(order_container['prices'], [{'id': 115566}]) - self.assertEqual(order_container['virtualGuests'], [{'id': 1}]) - - def test_get_item_id_for_upgrade(self): - item_id = 0 - package_items = self.client['Product_Package'].getItems(id=46) - for item in package_items: - if ((item['prices'][0]['categories'][0]['id'] == 3) - and (item.get('capacity') == '2')): - item_id = item['prices'][0]['id'] - break - self.assertEqual(1133, item_id) - - def test_get_package_items(self): - self.vs._get_package_items() - self.assert_called_with('SoftLayer_Product_Package', 'getItems') - - def test_get_price_id_for_upgrade(self): - package_items = self.vs._get_package_items() - - price_id = self.vs._get_price_id_for_upgrade(package_items=package_items, - option='cpus', - value='4') - self.assertEqual(1144, price_id) - - def test_get_price_id_for_upgrade_skips_location_price(self): - package_items = self.vs._get_package_items() - - price_id = self.vs._get_price_id_for_upgrade(package_items=package_items, - option='cpus', - value='55') - self.assertEqual(None, price_id) - - def test_get_price_id_for_upgrade_finds_nic_price(self): - package_items = self.vs._get_package_items() - - price_id = self.vs._get_price_id_for_upgrade(package_items=package_items, - option='memory', - value='2') - self.assertEqual(1133, price_id) - - def test_get_price_id_for_upgrade_finds_memory_price(self): - package_items = self.vs._get_package_items() - - price_id = self.vs._get_price_id_for_upgrade(package_items=package_items, - option='nic_speed', - value='1000') - self.assertEqual(1122, price_id) - - -class VSWaitReadyGoTests(testing.TestCase): - - def set_up(self): - self.client = mock.MagicMock() - self.vs = SoftLayer.VSManager(self.client) - self.guestObject = self.client['Virtual_Guest'].getObject - - @mock.patch('SoftLayer.managers.vs.VSManager.wait_for_ready') - def test_wait_interface(self, ready): - # verify interface to wait_for_ready is intact - self.vs.wait_for_transaction(1, 1) - ready.assert_called_once_with(1, 1, delay=10, pending=True) - - def test_active_not_provisioned(self): - # active transaction and no provision date should be false - self.guestObject.return_value = {'activeTransaction': {'id': 1}} - value = self.vs.wait_for_ready(1, 0) - self.assertFalse(value) - - def test_active_and_provisiondate(self): - # active transaction and provision date should be True - self.guestObject.side_effect = [ - {'activeTransaction': {'id': 1}, - 'provisionDate': 'aaa'}, - ] - value = self.vs.wait_for_ready(1, 1) - self.assertTrue(value) - - @mock.patch('time.sleep') - @mock.patch('time.time') - def test_active_provision_pending(self, _now, _sleep): - _now.side_effect = [0, 0, 1, 1, 2, 2] - # active transaction and provision date - # and pending should be false - self.guestObject.return_value = {'activeTransaction': {'id': 2}, 'provisionDate': 'aaa'} - - value = self.vs.wait_for_ready(instance_id=1, limit=1, delay=1, pending=True) - _sleep.assert_has_calls([mock.call(0)]) - self.assertFalse(value) - - def test_reload_no_pending(self): - # reload complete, maintance transactions - self.guestObject.return_value = { - 'activeTransaction': {'id': 2}, - 'provisionDate': 'aaa', - 'lastOperatingSystemReload': {'id': 1}, - } - - value = self.vs.wait_for_ready(1, 1) - self.assertTrue(value) - - @mock.patch('time.sleep') - @mock.patch('time.time') - def test_reload_pending(self, _now, _sleep): - _now.side_effect = [0, 0, 1, 1, 2, 2] - # reload complete, pending maintance transactions - self.guestObject.return_value = {'activeTransaction': {'id': 2}, - 'provisionDate': 'aaa', - 'lastOperatingSystemReload': {'id': 1}} - value = self.vs.wait_for_ready(instance_id=1, limit=1, delay=1, pending=True) - _sleep.assert_has_calls([mock.call(0)]) - self.assertFalse(value) - - @mock.patch('time.sleep') - def test_ready_iter_once_incomplete(self, _sleep): - # no iteration, false - self.guestObject.return_value = {'activeTransaction': {'id': 1}} - value = self.vs.wait_for_ready(1, 0, delay=1) - self.assertFalse(value) - _sleep.assert_has_calls([mock.call(0)]) - - @mock.patch('time.sleep') - def test_iter_once_complete(self, _sleep): - # no iteration, true - self.guestObject.return_value = {'provisionDate': 'aaa'} - value = self.vs.wait_for_ready(1, 1, delay=1) - self.assertTrue(value) - self.assertFalse(_sleep.called) - - @mock.patch('time.sleep') - def test_iter_four_complete(self, _sleep): - # test 4 iterations with positive match - self.guestObject.side_effect = [ - {'activeTransaction': {'id': 1}}, - {'activeTransaction': {'id': 1}}, - {'activeTransaction': {'id': 1}}, - {'provisionDate': 'aaa'}, - ] - - value = self.vs.wait_for_ready(1, 4, delay=1) - self.assertTrue(value) - _sleep.assert_has_calls([mock.call(1), mock.call(1), mock.call(1)]) - self.guestObject.assert_has_calls([ - mock.call(id=1, mask=mock.ANY), mock.call(id=1, mask=mock.ANY), - mock.call(id=1, mask=mock.ANY), mock.call(id=1, mask=mock.ANY), - ]) - - @mock.patch('time.time') - @mock.patch('time.sleep') - def test_iter_two_incomplete(self, _sleep, _time): - # test 2 iterations, with no matches - self.guestObject.side_effect = [ - {'activeTransaction': {'id': 1}}, - {'activeTransaction': {'id': 1}}, - {'activeTransaction': {'id': 1}}, - {'provisionDate': 'aaa'} - ] - # logging calls time.time as of pytest3.3, not sure if there is a better way of getting around that. - _time.side_effect = [0, 1, 2, 3, 4, 5, 6] - value = self.vs.wait_for_ready(1, 2, delay=1) - self.assertFalse(value) - _sleep.assert_has_calls([mock.call(1), mock.call(0)]) - self.guestObject.assert_has_calls([ - mock.call(id=1, mask=mock.ANY), - mock.call(id=1, mask=mock.ANY), - ]) - - @mock.patch('time.time') - @mock.patch('time.sleep') - def test_iter_20_incomplete(self, _sleep, _time): - """Wait for up to 20 seconds (sleeping for 10 seconds) for a server.""" - self.guestObject.return_value = {'activeTransaction': {'id': 1}} - # logging calls time.time as of pytest3.3, not sure if there is a better way of getting around that. - _time.side_effect = [0, 0, 10, 10, 20, 20, 50, 60] - value = self.vs.wait_for_ready(1, 20, delay=10) - self.assertFalse(value) - self.guestObject.assert_has_calls([mock.call(id=1, mask=mock.ANY)]) - - _sleep.assert_has_calls([mock.call(10)]) - - @mock.patch('SoftLayer.decoration.sleep') - @mock.patch('SoftLayer.transports.FixtureTransport.__call__') - @mock.patch('time.time') - @mock.patch('time.sleep') - def test_exception_from_api(self, _sleep, _time, _vs, _dsleep): - """Tests escalating scale back when an excaption is thrown""" - _dsleep.return_value = False - - self.guestObject.side_effect = [ - exceptions.TransportError(104, "Its broken"), - {'activeTransaction': {'id': 1}}, - {'provisionDate': 'aaa'} - ] - # logging calls time.time as of pytest3.3, not sure if there is a better way of getting around that. - _time.side_effect = [0, 1, 2, 3, 4] - value = self.vs.wait_for_ready(1, 20, delay=1) - _sleep.assert_called_once() - _dsleep.assert_called_once() - self.assertTrue(value) diff --git a/tests/managers/vs/vs_waiting_for_ready_tests.py b/tests/managers/vs/vs_waiting_for_ready_tests.py new file mode 100644 index 000000000..802b945fd --- /dev/null +++ b/tests/managers/vs/vs_waiting_for_ready_tests.py @@ -0,0 +1,163 @@ +""" + SoftLayer.tests.managers.vs.vs_waiting_for_ready_tests + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + :license: MIT, see LICENSE for more details. + +""" +import mock + +import SoftLayer +from SoftLayer import exceptions +from SoftLayer import testing + + +class VSWaitReadyGoTests(testing.TestCase): + + def set_up(self): + self.client = mock.MagicMock() + self.vs = SoftLayer.VSManager(self.client) + self.guestObject = self.client['Virtual_Guest'].getObject + + @mock.patch('SoftLayer.managers.vs.VSManager.wait_for_ready') + def test_wait_interface(self, ready): + # verify interface to wait_for_ready is intact + self.vs.wait_for_transaction(1, 1) + ready.assert_called_once_with(1, 1, delay=10, pending=True) + + def test_active_not_provisioned(self): + # active transaction and no provision date should be false + self.guestObject.return_value = {'activeTransaction': {'id': 1}} + value = self.vs.wait_for_ready(1, 0) + self.assertFalse(value) + + def test_active_and_provisiondate(self): + # active transaction and provision date should be True + self.guestObject.side_effect = [ + {'activeTransaction': {'id': 1}, + 'provisionDate': 'aaa'}, + ] + value = self.vs.wait_for_ready(1, 1) + self.assertTrue(value) + + @mock.patch('time.sleep') + @mock.patch('time.time') + def test_active_provision_pending(self, _now, _sleep): + _now.side_effect = [0, 0, 1, 1, 2, 2] + # active transaction and provision date + # and pending should be false + self.guestObject.return_value = {'activeTransaction': {'id': 2}, 'provisionDate': 'aaa'} + + value = self.vs.wait_for_ready(instance_id=1, limit=1, delay=1, pending=True) + _sleep.assert_has_calls([mock.call(0)]) + self.assertFalse(value) + + def test_reload_no_pending(self): + # reload complete, maintance transactions + self.guestObject.return_value = { + 'activeTransaction': {'id': 2}, + 'provisionDate': 'aaa', + 'lastOperatingSystemReload': {'id': 1}, + } + + value = self.vs.wait_for_ready(1, 1) + self.assertTrue(value) + + @mock.patch('time.sleep') + @mock.patch('time.time') + def test_reload_pending(self, _now, _sleep): + _now.side_effect = [0, 0, 1, 1, 2, 2] + # reload complete, pending maintance transactions + self.guestObject.return_value = {'activeTransaction': {'id': 2}, + 'provisionDate': 'aaa', + 'lastOperatingSystemReload': {'id': 1}} + value = self.vs.wait_for_ready(instance_id=1, limit=1, delay=1, pending=True) + _sleep.assert_has_calls([mock.call(0)]) + self.assertFalse(value) + + @mock.patch('time.sleep') + def test_ready_iter_once_incomplete(self, _sleep): + # no iteration, false + self.guestObject.return_value = {'activeTransaction': {'id': 1}} + value = self.vs.wait_for_ready(1, 0, delay=1) + self.assertFalse(value) + _sleep.assert_has_calls([mock.call(0)]) + + @mock.patch('time.sleep') + def test_iter_once_complete(self, _sleep): + # no iteration, true + self.guestObject.return_value = {'provisionDate': 'aaa'} + value = self.vs.wait_for_ready(1, 1, delay=1) + self.assertTrue(value) + self.assertFalse(_sleep.called) + + @mock.patch('time.sleep') + def test_iter_four_complete(self, _sleep): + # test 4 iterations with positive match + self.guestObject.side_effect = [ + {'activeTransaction': {'id': 1}}, + {'activeTransaction': {'id': 1}}, + {'activeTransaction': {'id': 1}}, + {'provisionDate': 'aaa'}, + ] + + value = self.vs.wait_for_ready(1, 4, delay=1) + self.assertTrue(value) + _sleep.assert_has_calls([mock.call(1), mock.call(1), mock.call(1)]) + self.guestObject.assert_has_calls([ + mock.call(id=1, mask=mock.ANY), mock.call(id=1, mask=mock.ANY), + mock.call(id=1, mask=mock.ANY), mock.call(id=1, mask=mock.ANY), + ]) + + @mock.patch('time.time') + @mock.patch('time.sleep') + def test_iter_two_incomplete(self, _sleep, _time): + # test 2 iterations, with no matches + self.guestObject.side_effect = [ + {'activeTransaction': {'id': 1}}, + {'activeTransaction': {'id': 1}}, + {'activeTransaction': {'id': 1}}, + {'provisionDate': 'aaa'} + ] + # logging calls time.time as of pytest3.3, not sure if there is a better way of getting around that. + _time.side_effect = [0, 1, 2, 3, 4, 5, 6] + value = self.vs.wait_for_ready(1, 2, delay=1) + self.assertFalse(value) + _sleep.assert_has_calls([mock.call(1), mock.call(0)]) + self.guestObject.assert_has_calls([ + mock.call(id=1, mask=mock.ANY), + mock.call(id=1, mask=mock.ANY), + ]) + + @mock.patch('time.time') + @mock.patch('time.sleep') + def test_iter_20_incomplete(self, _sleep, _time): + """Wait for up to 20 seconds (sleeping for 10 seconds) for a server.""" + self.guestObject.return_value = {'activeTransaction': {'id': 1}} + # logging calls time.time as of pytest3.3, not sure if there is a better way of getting around that. + _time.side_effect = [0, 0, 10, 10, 20, 20, 50, 60] + value = self.vs.wait_for_ready(1, 20, delay=10) + self.assertFalse(value) + self.guestObject.assert_has_calls([mock.call(id=1, mask=mock.ANY)]) + + _sleep.assert_has_calls([mock.call(10)]) + + @mock.patch('SoftLayer.decoration.sleep') + @mock.patch('SoftLayer.transports.FixtureTransport.__call__') + @mock.patch('time.time') + @mock.patch('time.sleep') + def test_exception_from_api(self, _sleep, _time, _vs, _dsleep): + """Tests escalating scale back when an excaption is thrown""" + _dsleep.return_value = False + + self.guestObject.side_effect = [ + exceptions.TransportError(104, "Its broken"), + {'activeTransaction': {'id': 1}}, + {'provisionDate': 'aaa'} + ] + # logging calls time.time as of pytest3.3, not sure if there is a better way of getting around that. + _time.side_effect = [0, 1, 2, 3, 4] + value = self.vs.wait_for_ready(1, 20, delay=1) + _sleep.assert_called_once() + _dsleep.assert_called_once() + self.assertTrue(value) diff --git a/tests/transport_tests.py b/tests/transport_tests.py index 87a43de62..a14ec9238 100644 --- a/tests/transport_tests.py +++ b/tests/transport_tests.py @@ -349,15 +349,29 @@ def test_basic(self, request): timeout=None) @mock.patch('SoftLayer.transports.requests.Session.request') - def test_error(self, request): + def test_http_and_json_error(self, request): # Test JSON Error e = requests.HTTPError('error') e.response = mock.MagicMock() e.response.status_code = 404 - e.response.text = '''{ + e.response.text = ''' "error": "description", "code": "Error Code" - }''' + ''' + request().raise_for_status.side_effect = e + + req = transports.Request() + req.service = 'SoftLayer_Service' + req.method = 'Resource' + self.assertRaises(SoftLayer.SoftLayerAPIError, self.transport, req) + + @mock.patch('SoftLayer.transports.requests.Session.request') + def test_http_and_empty_error(self, request): + # Test JSON Error + e = requests.HTTPError('error') + e.response = mock.MagicMock() + e.response.status_code = 404 + e.response.text = '' request().raise_for_status.side_effect = e req = transports.Request() @@ -365,6 +379,26 @@ def test_error(self, request): req.method = 'Resource' self.assertRaises(SoftLayer.SoftLayerAPIError, self.transport, req) + @mock.patch('SoftLayer.transports.requests.Session.request') + def test_empty_error(self, request): + # Test empty response error. + request().text = '' + + req = transports.Request() + req.service = 'SoftLayer_Service' + req.method = 'Resource' + self.assertRaises(SoftLayer.SoftLayerAPIError, self.transport, req) + + @mock.patch('SoftLayer.transports.requests.Session.request') + def test_json_error(self, request): + # Test non-json response error. + request().text = 'Not JSON' + + req = transports.Request() + req.service = 'SoftLayer_Service' + req.method = 'Resource' + self.assertRaises(SoftLayer.SoftLayerAPIError, self.transport, req) + def test_proxy_without_protocol(self): req = transports.Request() req.service = 'SoftLayer_Service'