def test_ServerCreateOptions_with_cpu_only(self):
        args = {
            '<chassis_id>': 999,
            '--all': False,
            '--datacenter': False,
            '--cpu': True,
            '--nic': False,
            '--disk': False,
            '--os': False,
            '--memory': False,
            '--controller': False,
        }

        client = self._setup_package_mocks(self.client)

        output = server.ServerCreateOptions.execute(client, args)

        expected = {
            'cpu': [
                {'description': 'Dual Quad Core Pancake 200 - 1.60GHz',
                 'id': 723},
                {'description': 'Dual Quad Core Pancake 200 - 1.80GHz',
                 'id': 724}
            ],
        }

        self.assertEqual(expected, format_output(output, 'python'))
    def test_ServerCreateOptions_with_cpu_only(self, packages):
        args = {
            '<chassis_id>': '999',
            '--all': False,
            '--datacenter': False,
            '--cpu': True,
            '--nic': False,
            '--disk': False,
            '--os': False,
            '--memory': False,
            '--controller': False,
        }

        test_data = [
            (999, 'Chassis 999'),
        ]
        packages.return_value = test_data

        runnable = server.ServerCreateOptions(client=self.client)

        output = runnable.execute(args)

        expected = {
            'cpu': [
                {'Description': 'Dual Quad Core Pancake 200 - 1.60GHz',
                 'ID': 723},
                {'Description': 'Dual Quad Core Pancake 200 - 1.80GHz',
                 'ID': 724}
            ],
        }

        self.assertEqual(expected, format_output(output, 'python'))
    def test_ServerDetails(self):
        hw_id = 1234

        runnable = server.ServerDetails(client=self.client)

        args = {'<identifier>': hw_id, '--passwords': True, '--price': True}
        output = runnable.execute(args)

        expected = {
            'status': 'ACTIVE',
            'datacenter': 'TEST00',
            'created': '2013-08-01 15:23:45',
            'notes': 'These are test notes.',
            'hostname': 'hardware-test1.test.sftlyr.ws',
            'public_ip': '172.16.1.100',
            'private_ip': '10.1.0.2',
            'ipmi_ip': '10.1.0.3',
            'price rate': 1.54,
            'memory': 2048,
            'cores': 2,
            'ptr': '2.0.1.10.in-addr.arpa',
            'os': 'Ubuntu',
            'id': 1000,
            'tags': ['test_tag'],
            'users': ['root abc123'],
            'vlans': [{'id': 9653, 'number': 1800, 'type': 'PRIVATE'},
                      {'id': 19082, 'number': 3672, 'type': 'PUBLIC'}]
        }

        self.assertEqual(expected, format_output(output, 'python'))
    def test_ListServers(self):
        self.client['Account'].getHardware = account_mock.getHardware_Mock()

        output = server.ListServers.execute(
            self.client, {'--tags': 'openstack'})

        expected = [
            {
                'datacenter': 'TEST00',
                'primary_ip': '172.16.1.100',
                'host': 'hardware-test1.test.sftlyr.ws',
                'memory': 2048,
                'cores': 2,
                'id': 1000,
                'backend_ip': '10.1.0.2',
                'active_transaction': 'TXN_NAME'
            },
            {
                'datacenter': 'TEST00',
                'primary_ip': '172.16.4.94',
                'host': 'hardware-test2.test.sftlyr.ws',
                'memory': 4096,
                'cores': 4,
                'id': 1001,
                'backend_ip': '10.1.0.3',
                'active_transaction': None
            }
        ]

        self.assertEqual(expected, format_output(output, 'python'))
    def test_ServerDetails(self):
        hw_id = 1234

        client = Mock()
        client.__getitem__ = Mock()
        service = client['Hardware_Server']
        service.getObject = hardware_mock.getObject_Mock(1000)
        dns_mock = hardware_mock.getReverseDomainRecords_Mock(1000)
        service.getReverseDomainRecords = dns_mock

        args = {'<identifier>': hw_id, '--passwords': True, '--price': True}
        output = server.ServerDetails.execute(client, args)

        expected = {
            'status': 'ACTIVE',
            'datacenter': 'TEST00',
            'created': '2013-08-01 15:23:45',
            'notes': 'These are test notes.',
            'hostname': 'hardware-test1.test.sftlyr.ws',
            'public_ip': '172.16.1.100',
            'private_ip': '10.1.0.2',
            'price rate': 1.54,
            'memory': 2048,
            'cores': 2,
            'ptr': '2.0.1.10.in-addr.arpa',
            'os': 'Ubuntu',
            'id': 1000,
            'tags': ['test_tag'],
            'users': ['root abc123'],
            'vlans': [{'id': 9653, 'number': 1800, 'type': 'PRIVATE'},
                      {'id': 19082, 'number': 3672, 'type': 'PUBLIC'}]
        }

        self.assertEqual(expected, format_output(output, 'python'))
    def test_ServerCancelReasons(self):
        output = server.ServerCancelReasons.execute(self.client, {})

        expected = [
            {'Code': 'datacenter',
             'Reason': 'Migrating to a different SoftLayer datacenter'},
            {'Code': 'cost', 'Reason': 'Server / Upgrade Costs'},
            {'Code': 'moving', 'Reason': 'Moving to competitor'},
            {'Code': 'migrate_smaller',
             'Reason': 'Migrating to smaller server'},
            {'Code': 'sales', 'Reason': 'Sales process / upgrades'},
            {'Code': 'performance',
             'Reason': 'Network performance / latency'},
            {'Code': 'unneeded', 'Reason': 'No longer needed'},
            {'Code': 'support', 'Reason': 'Support response / timing'},
            {'Code': 'closing', 'Reason': 'Business closing down'},
            {'Code': 'migrate_larger', 'Reason': 'Migrating to larger server'}
        ]

        method = 'assertItemsEqual'
        if not hasattr(self, method):
            # For Python 3.3 compatibility
            method = 'assertCountEqual'

        f = getattr(self, method)
        f(expected, format_output(output, 'python'))
    def test_summary(self):
        command = summary.Summary(client=self.client, env=Environment())

        output = command.execute({})
        expected = [{'datacenter': 'dal00',
                     'networking': 1,
                     'subnets': 0,
                     'hardware': 1,
                     'IPs': 3,
                     'vs': 1,
                     'vlans': 1}]
        self.assertEqual(expected, format_output(output, 'python'))
    def test_list_keys(self):
        command = sshkey.ListSshKey(client=self.client)
        output = command.execute({})

        self.assertEqual(format_output(output, 'python'),
                         [{'notes': '-',
                           'fingerprint': None,
                           'id': '100',
                           'label': 'Test 1'},
                          {'notes': 'my key',
                           'fingerprint': None,
                           'id': '101',
                           'label': 'Test 2'}])
    def test_show(self):
        command = rwhois.RWhoisShow(client=self.client)

        output = command.execute({})
        expected = {'Abuse Email': 'abuseEmail',
                    'Address 1': 'address1',
                    'Address 2': 'address2',
                    'City': 'city',
                    'Company': 'companyName',
                    'Country': 'country',
                    'Name': 'firstName lastName',
                    'Postal Code': 'postalCode',
                    'State': '-'}
        self.assertEqual(expected, format_output(output, 'python'))
    def test_ListChassisServer(self, packages):
        test_data = [
            (1, 'Chassis 1'),
            (2, 'Chassis 2')
        ]
        packages.return_value = test_data

        output = server.ListChassisServer.execute(self.client, {})

        expected = [
            {'Chassis': 'Chassis 1', 'Code': 1},
            {'Chassis': 'Chassis 2', 'Code': 2}
        ]

        self.assertEqual(expected, format_output(output, 'python'))
    def test_ServerCreateOptions(self, packages):
        args = {
            '<chassis_id>': '999',
            '--all': True,
            '--datacenter': False,
            '--cpu': False,
            '--nic': False,
            '--disk': False,
            '--os': False,
            '--memory': False,
            '--controller': False,
        }

        test_data = [
            (999, 'Chassis 999'),
        ]
        packages.return_value = test_data

        runnable = server.ServerCreateOptions(client=self.client)

        output = runnable.execute(args)

        expected = {
            'cpu': [
                {'Description': 'Dual Quad Core Pancake 200 - 1.60GHz',
                 'ID': 723},
                {'Description': 'Dual Quad Core Pancake 200 - 1.80GHz',
                 'ID': 724}],
            'datacenter': ['RANDOM_LOCATION'],
            'disk': ['250_SATA_II', '500_SATA_II'],
            'disk_controllers': ['None', 'RAID0'],
            'dual nic': ['1000_DUAL', '100_DUAL', '10_DUAL'],
            'memory': [4, 6],
            'os (CENTOS)': ['CENTOS_6_64_LAMP', 'CENTOS_6_64_MINIMAL'],
            'os (REDHAT)': ['REDHAT_6_64_LAMP', 'REDHAT_6_64_MINIMAL'],
            'os (UBUNTU)': ['UBUNTU_12_64_LAMP', 'UBUNTU_12_64_MINIMAL'],
            'os (WIN)': [
                'WIN_2008-DC_64',
                'WIN_2008-ENT_64',
                'WIN_2008-STD-R2_64',
                'WIN_2008-STD_64',
                'WIN_2012-DC-HYPERV_64'],
            'single nic': ['100', '1000']}

        self.assertEqual(expected, format_output(output, 'python'))
    def test_ServerCreateOptions_for_bmc(self, bmpi, packages):
        args = {
            '<chassis_id>': '1099',
            '--all': True,
            '--datacenter': False,
            '--cpu': False,
            '--nic': False,
            '--disk': False,
            '--os': False,
            '--memory': False,
            '--controller': False,
        }

        test_data = [
            (1099, 'Bare Metal Instance'),
        ]
        packages.return_value = test_data

        bmpi.return_value = '1099'

        runnable = server.ServerCreateOptions(client=self.client)

        output = runnable.execute(args)

        expected = {
            'memory/cpu': [
                {'cpu': ['2'], 'memory': '2'},
                {'cpu': ['2', '4'], 'memory': '4'},
            ],
            'datacenter': ['RANDOM_LOCATION'],
            'disk': ['250_SATA_II', '500_SATA_II'],
            'dual nic': ['1000_DUAL', '100_DUAL', '10_DUAL'],
            'os (CENTOS)': ['CENTOS_6_64_LAMP', 'CENTOS_6_64_MINIMAL'],
            'os (REDHAT)': ['REDHAT_6_64_LAMP', 'REDHAT_6_64_MINIMAL'],
            'os (UBUNTU)': ['UBUNTU_12_64_LAMP', 'UBUNTU_12_64_MINIMAL'],
            'os (WIN)': [
                'WIN_2008-DC_64',
                'WIN_2008-ENT_64',
                'WIN_2008-STD-R2_64',
                'WIN_2008-STD_64',
                'WIN_2012-DC-HYPERV_64'],
            'single nic': ['100', '1000']}

        self.assertEqual(expected, format_output(output, 'python'))
    def test_ServerCreateOptions(self):
        args = {
            '<chassis_id>': 999,
            '--all': True,
            '--datacenter': False,
            '--cpu': False,
            '--nic': False,
            '--disk': False,
            '--os': False,
            '--memory': False,
            '--controller': False,
        }

        client = self._setup_package_mocks(self.client)
        runnable = server.ServerCreateOptions(client=client)

        output = runnable.execute(args)

        expected = {
            'datacenter': ['RANDOM_LOCATION'],
            'dual nic': ['100_DUAL', '10_DUAL'],
            'disk_controllers': ['None', 'RAID0'],
            'os (CENTOS)': ['CENTOS_6_64'],
            'os (DEBIAN)': ['DEBIAN_6_32'],
            'os (REDHAT)': ['REDHAT_6_64_6'],
            'os (UBUNTU)': ['UBUNTU_12_64', 'UBUNTU_12_64_MINIMAL'],
            'os (WIN)': ['WIN_2003-STD-R2_64', 'WIN_2008-DC-HYPERV_64',
                         'WIN_2008-ENT_64', 'WIN_2008-STD_64'],
            'memory': [4, 6],
            'disk': ['1000_DRIVE'],
            'single nic': ['100', '1000'],
            'cpu': [
                {'description': 'Dual Quad Core Pancake 200 - 1.60GHz',
                 'id': 723},
                {'description': 'Dual Quad Core Pancake 200 - 1.80GHz',
                 'id': 724}
            ],
        }

        self.assertEqual(expected, format_output(output, 'python'))
    def test_BMCCreateOptions_with_cpu_only(self):
        args = {
            '--all': False,
            '--datacenter': False,
            '--cpu': True,
            '--nic': False,
            '--disk': False,
            '--os': False,
            '--memory': False,
            '--controller': False,
        }

        output = bmc.BMCCreateOptions(client=self.client).execute(args)

        expected = {
            'memory/cpu': [
                {'cpu': ['2'], 'memory': '2'},
                {'cpu': ['2', '4'], 'memory': '4'}
            ],
        }

        self.assertEqual(expected, format_output(output, 'python'))
    def test_ListServers(self):
        runnable = server.ListServers(client=self.client)

        output = runnable.execute({'--tags': 'openstack'})

        expected = [
            {
                'datacenter': 'TEST00',
                'primary_ip': '172.16.1.100',
                'host': 'hardware-test1.test.sftlyr.ws',
                'memory': 2048,
                'cores': 2,
                'id': 1000,
                'backend_ip': '10.1.0.2',
                'active_transaction': 'TXN_NAME'
            },
            {
                'datacenter': 'TEST00',
                'primary_ip': '172.16.4.94',
                'host': 'hardware-test2.test.sftlyr.ws',
                'memory': 4096,
                'cores': 4,
                'id': 1001,
                'backend_ip': '10.1.0.3',
                'active_transaction': None
            },
            {
                'datacenter': 'TEST00',
                'primary_ip': '172.16.4.95',
                'host': 'hardware-bad-memory.test.sftlyr.ws',
                'memory': 0,
                'cores': 4,
                'id': 1002,
                'backend_ip': '10.1.0.4',
                'active_transaction': None
            }
        ]

        self.assertEqual(expected, format_output(output, 'python'))
    def test_BMCCreateOptions(self):
        args = {
            '--all': True,
            '--datacenter': False,
            '--cpu': False,
            '--nic': False,
            '--disk': False,
            '--os': False,
            '--memory': False,
            '--controller': False,
        }

        output = bmc.BMCCreateOptions(client=self.client).execute(args)

        expected = {
            'datacenter': ['RANDOM_LOCATION'],
            'dual nic': ['1000_DUAL', '100_DUAL', '10_DUAL'],
            'os (CENTOS)': ['CENTOS_6_64_LAMP', 'CENTOS_6_64_MINIMAL'],
            'os (REDHAT)': ['REDHAT_6_64_LAMP', 'REDHAT_6_64_MINIMAL'],
            'os (UBUNTU)': ['UBUNTU_12_64_LAMP', 'UBUNTU_12_64_MINIMAL'],
            'os (WIN)': ['WIN_2008-DC_64', 'WIN_2008-ENT_64',
                         'WIN_2008-STD-R2_64', 'WIN_2008-STD_64',
                         'WIN_2012-DC-HYPERV_64'],
            'disks': [250, 500],
            'single nic': ['100', '1000'],
            'memory/cpu': [
                {'cpu': ['2'], 'memory': '2'},
                {'cpu': ['2', '4'], 'memory': '4'}
            ],
        }

        self.assertEqual(expected, format_output(output, 'python'))

        # Check get_create_options() with invalid input
        self.assertEqual(
            [], bmc.BMCCreateOptions().get_create_options([], 'nope'))
    def test_CreateServer(self):
        args = {
            '--chassis': 999,
            '--hostname': 'test',
            '--domain': 'example.com',
            '--datacenter': 'TEST00',
            '--cpu': False,
            '--network': '100',
            '--disk': ['1000_DRIVE', '1000_DRIVE'],
            '--os': 'UBUNTU_12_64_MINIMAL',
            '--memory': False,
            '--controller': False,
            '--test': True,
            '--export': None,
            '--template': None,
            '--key': [1234, 456],
            '--vlan_public': 10234,
            '--vlan_private': 20468,
        }

        client = self._setup_package_mocks(self.client)

        # First, test the --test flag
        with patch('SoftLayer.HardwareManager.verify_order') as verify_mock:
            verify_mock.return_value = {
                'prices': [
                    {
                        'recurringFee': 0.0,
                        'setupFee': 0.0,
                        'item': {'description': 'First Item'},
                    },
                    {
                        'recurringFee': 25.0,
                        'setupFee': 0.0,
                        'item': {'description': 'Second Item'},
                    }
                ]
            }
            output = server.CreateServer.execute(client, args)

            expected = [
                [
                    {'Item': 'First Item', 'cost': '0.00'},
                    {'Item': 'Second Item', 'cost': '25.00'},
                    {'Item': 'Total monthly cost', 'cost': '25.00'}
                ],
                ''
            ]

            self.assertEqual(expected, format_output(output, 'python'))

            # Make sure we can order without specifying the disk as well
            args['--disk'] = []

            output = server.CreateServer.execute(client, args)

            self.assertEqual(expected, format_output(output, 'python'))

            # And make sure we can pass in disk and SSH keys as comma separated
            # strings, which is what templates do
            args['--disk'] = '1000_DRIVE,1000_DRIVE'
            args['--key'] = '123,456'

            output = server.CreateServer.execute(client, args)

            self.assertEqual(expected, format_output(output, 'python'))

            # Test explicitly setting a RAID configuration
            args['--controller'] = 'RAID0'

            output = server.CreateServer.execute(client, args)

            self.assertEqual(expected, format_output(output, 'python'))

        # Now test ordering
        with patch('SoftLayer.HardwareManager.place_order') as order_mock:
            order_mock.return_value = {
                'orderId': 98765,
                'orderDate': '2013-08-02 15:23:47'
            }

            args['--test'] = False
            args['--really'] = True

            output = server.CreateServer.execute(self.client, args)

            expected = {'id': 98765, 'created': '2013-08-02 15:23:47'}
            self.assertEqual(expected, format_output(output, 'python'))

        # Finally, test cancelling the process
        with patch('SoftLayer.CLI.modules.server.confirm') as confirm:
            confirm.return_value = False

            args['--really'] = False

            self.assertRaises(CLIAbort,
                              server.CreateServer.execute, self.client, args)
    def test_print_key(self):
        command = sshkey.PrintSshKey(client=self.client)
        output = command.execute({'<identifier>': '1234'})

        self.assertEqual(format_output(output, 'python'),
                         {'id': 1234, 'label': 'label', 'notes': 'notes'})
    def test_CreateBMCInstance(self):
        args = {
            '--hostname': 'test',
            '--domain': 'example.com',
            '--datacenter': 'TEST00',
            '--cpu': '2',
            '--network': '100',
            '--disk': [250, 250],
            '--os': 'UBUNTU_12_64_MINIMAL',
            '--memory': '2',
            '--controller': False,
            '--test': True,
            '--export': None,
            '--template': None,
            '--hourly': True,
            '--monthly': False,
            '--key': [1234, 456],
            '--vlan_public': 10234,
            '--vlan_private': 20468,
        }

        # First, test the --test flag
        with patch('SoftLayer.HardwareManager.verify_order') as verify_mock:
            verify_mock.return_value = {
                'prices': [
                    {
                        'hourlyRecurringFee': 0.0,
                        'recurringFee': 0.0,
                        'setupFee': 0.0,
                        'item': {'description': 'First Item'},
                    },
                    {
                        'hourlyRecurringFee': 0.50,
                        'recurringFee': 25.0,
                        'setupFee': 0.0,
                        'item': {'description': 'Second Item'},
                    }
                ]
            }
            output = bmc.CreateBMCInstance(client=self.client).execute(args)

            expected = """:...................:......:
:              Item : cost :
:...................:......:
:        First Item : 0.00 :
:       Second Item : 0.50 :
: Total hourly cost : 0.50 :
:...................:......:
 -- ! Prices reflected here are retail and do not take account level \
discounts and are not guaranteed."""

            self.assertEqual(expected, format_output(output, 'table'))

            args['--hourly'] = False
            args['--monthly'] = True

            output = bmc.CreateBMCInstance(client=self.client).execute(args)

            expected = """:....................:.......:
:               Item :  cost :
:....................:.......:
:         First Item :  0.00 :
:        Second Item : 25.00 :
: Total monthly cost : 25.00 :
:....................:.......:
 -- ! Prices reflected here are retail and do not take account level \
discounts and are not guaranteed."""

            self.assertEqual(expected, format_output(output, 'table'))

            # Make sure we can order without specifying the disk as well
            args['--disk'] = []

            output = bmc.CreateBMCInstance(client=self.client).execute(args)

            self.assertEqual(expected, format_output(output, 'table'))

            # And make sure we can pass in disk and SSH keys as comma separated
            # strings, which is what templates do
            args['--disk'] = '1000_DRIVE,1000_DRIVE'
            args['--key'] = '123,456'

            output = bmc.CreateBMCInstance(client=self.client).execute(args)

            self.assertEqual(expected, format_output(output, 'table'))

            # Test explicitly setting a RAID configuration
            args['--controller'] = 'RAID0'

            output = bmc.CreateBMCInstance(client=self.client).execute(args)

            self.assertEqual(expected, format_output(output, 'table'))

        # Now test ordering
        with patch('SoftLayer.HardwareManager.place_order') as order_mock:
            order_mock.return_value = {
                'orderId': 98765,
                'orderDate': '2013-08-02 15:23:47'
            }

            args['--test'] = False
            args['--really'] = True

            output = bmc.CreateBMCInstance(client=self.client).execute(args)

            expected = {'id': 98765, 'created': '2013-08-02 15:23:47'}
            self.assertEqual(expected, format_output(output, 'python'))

        # Finally, test cancelling the process
        with patch('SoftLayer.CLI.modules.bmc.confirm') as confirm:
            confirm.return_value = False

            args['--really'] = False

            self.assertRaises(
                CLIAbort,
                bmc.CreateBMCInstance(client=self.client).execute, args)