Exemple #1
0
    def test_delete_namespace_key(self):
        client = apiclient.Client()
        client.delete_namespace_key('testspace', 'keyname')

        self.mock_request.assert_called_with(
            'DELETE',
            'http://localhost:13000/auth/namespaces/testspace/keys/keyname')
Exemple #2
0
    def test_cache_image(self):
        client = apiclient.Client()
        client.cache_image('imageurl')

        self.mock_request.assert_called_with('POST',
                                             'http://localhost:13000/images',
                                             data={'url': 'imageurl'})
Exemple #3
0
    def test_hard_reboot_instance(self):
        client = apiclient.Client()
        client.reboot_instance('notreallyauuid', hard=True)

        self.mock_request.assert_called_with(
            'POST',
            'http://localhost:13000/instances/notreallyauuid/reboothard')
Exemple #4
0
    def test_delete_instance_metadata_item(self):
        client = apiclient.Client()
        client.delete_instance_metadata_item('instance1', 'petname')

        self.mock_request.assert_called_with(
            'DELETE',
            'http://localhost:13000/instances/instance1/metadata/petname')
Exemple #5
0
    def test_get_networks(self):
        client = apiclient.Client()
        client.get_networks()

        self.mock_request.assert_called_with('GET',
                                             'http://localhost:13000/networks',
                                             data={'all': False})
Exemple #6
0
    def test_get_instance_interfaces(self):
        client = apiclient.Client()
        client.get_instance_interfaces('notreallyauuid')

        self.mock_request.assert_called_with(
            'GET',
            'http://localhost:13000/instances/notreallyauuid/interfaces')
Exemple #7
0
    def test_delete_namespace_metadata_item(self):
        client = apiclient.Client()
        client.delete_namespace_metadata_item('testspace', 'billy')

        self.mock_request.assert_called_with(
            'DELETE',
            'http://localhost:13000/auth/namespaces/testspace/metadata/billy')
Exemple #8
0
    def test_set_namespace_metadata_item(self):
        client = apiclient.Client()
        client.set_namespace_metadata_item('testspace', 'billy', 'bob')

        self.mock_request.assert_called_with(
            'PUT',
            'http://localhost:13000/auth/namespaces/testspace/metadata/billy',
            data={'value': 'bob'})
Exemple #9
0
    def test_create_namespace(self):
        client = apiclient.Client()
        client.create_namespace('testspace')

        self.mock_request.assert_called_with(
            'POST',
            'http://localhost:13000/auth/namespaces',
            data={'namespace': 'testspace'})
Exemple #10
0
    def test_snapshot_instance(self):
        client = apiclient.Client()
        client.snapshot_instance('notreallyauuid', all=True)

        self.mock_request.assert_called_with(
            'POST',
            'http://localhost:13000/instances/notreallyauuid/snapshot',
            data={'all': True})
Exemple #11
0
    def _make_namespace(self, name, key):
        self._remove_namespace(name)

        self.system_client.create_namespace(name)
        self.system_client.add_namespace_key(name, 'test', key)
        return apiclient.Client(base_url=self.system_client.base_url,
                                namespace=name,
                                key=key)
Exemple #12
0
def cli(ctx, output, verbose):
    if not ctx.obj:
        ctx.obj = {}
    ctx.obj['OUTPUT'] = output

    if verbose:
        LOG.setLevel(logging.DEBUG)

        global CLIENT
        CLIENT = apiclient.Client(verbose=True)
Exemple #13
0
def auto_complete(func):
    global CLIENT

    if not CLIENT:
        CLIENT = apiclient.Client(
            namespace=os.getenv("SHAKENFIST_NAMESPACE"),
            key=os.getenv('SHAKENFIST_KEY'),
            base_url=os.getenv('SHAKENFIST_API_URL', 'http://localhost:13000'),
        )

    return func
Exemple #14
0
    def test_add_namespace_key(self):
        client = apiclient.Client()
        client.add_namespace_key('testspace', 'testkeyname', 'secretkey')

        self.mock_request.assert_called_with(
            'POST',
            'http://localhost:13000/auth/namespaces/testspace/keys',
            data={
                'key_name': 'testkeyname',
                'key': 'secretkey'
            })
Exemple #15
0
    def test_allocate_network(self):
        client = apiclient.Client()
        client.allocate_network('192.168.1.0/24', True, True, 'gerkin')

        self.mock_request.assert_called_with('POST',
                                             'http://localhost:13000/networks',
                                             data={
                                                 'netblock': '192.168.1.0/24',
                                                 'provide_dhcp': True,
                                                 'provide_nat': True,
                                                 'name': 'gerkin'
                                             })
Exemple #16
0
def cli(ctx, output, verbose, namespace, key, apiurl):
    if not ctx.obj:
        ctx.obj = {}
    ctx.obj['OUTPUT'] = output

    if verbose:
        LOG.setLevel(logging.INFO)

    global CLIENT
    CLIENT = apiclient.Client(
        namespace=namespace,
        key=key,
        base_url=apiurl,
        verbose=verbose)
Exemple #17
0
    def test_create_instance_user_data(self):
        client = apiclient.Client()
        client.create_instance('foo', 1, 2, ['netuuid1'], ['8@cirros'],
                               'sshkey', 'userdatabeforebase64')

        self.mock_request.assert_called_with(
            'POST',
            'http://*****:*****@cirros'],
                'ssh_key': 'sshkey',
                'user_data': "userdatabeforebase64"
            })
Exemple #18
0
    def test_create_instance(self):
        client = apiclient.Client()
        client.create_instance('foo', 1, 2048, ['netuuid1'], ['8@cirros'],
                               'sshkey', None, None)

        self.mock_request.assert_called_with(
            'POST',
            'http://*****:*****@cirros'],
                'ssh_key': 'sshkey',
                'user_data': None,
                'namespace': None
            })
Exemple #19
0
import json
import logging
import os
from prettytable import PrettyTable
import sys
import time
import uuid

from shakenfist.client import apiclient

logging.basicConfig(level=logging.INFO)

LOG = logging.getLogger(__file__)
LOG.setLevel(logging.INFO)

CLIENT = apiclient.Client()


def filter_dict(d, allowed_keys):
    out = {}
    for key in allowed_keys:
        if key in d:
            out[key] = d[key]
    return out


@click.group()
@click.option('--pretty', 'output', flag_value='pretty', default=True)
@click.option('--simple', 'output', flag_value='simple')
@click.option('--json', 'output', flag_value='json')
@click.option('--verbose/--no-verbose', default=False)
Exemple #20
0
    def test_power_off_instance(self):
        client = apiclient.Client()
        client.power_off_instance('notreallyauuid')

        self.mock_request.assert_called_with(
            'POST', 'http://localhost:13000/instances/notreallyauuid/poweroff')
Exemple #21
0
    def setUp(self):
        super(BaseTestCase, self).setUp()

        self.system_client = apiclient.Client()
Exemple #22
0
    def test_get_namespace_metadata(self):
        client = apiclient.Client()
        client.get_namespace_metadata('testspace')

        self.mock_request.assert_called_with(
            'GET', 'http://localhost:13000/auth/namespaces/testspace/metadata')
Exemple #23
0
def main():
    c = apiclient.Client(base_url='http://localhost:13000', verbose=True)

    # Return to a clean state
    for i in c.get_instances():
        c.delete_instance(i['uuid'])
        print('Deleted instance %s' % i['uuid'])

    for n in c.get_networks():
        c.delete_network(n['uuid'])
        print('Deleted network %s' % n['uuid'])

    # Some disk specs we randomly select from
    disks = [{
        'base': 'cirros',
        'size': 4,
        'type': 'disk'
    }, {
        'base': 'ubuntu',
        'size': 8,
        'type': 'disk'
    }]

    # Some network specs we randomly select from
    networks = []
    for i in range(5):
        n = c.allocate_network('192.168.%d.0/24' % (50 + i), True, True,
                               randomName())
        networks.append({'network_uuid': n['uuid']})
        print('Created network %s' % n['uuid'])

    # Launch instances until we get an error
    instances = []
    while True:
        choice = random.randint(0, 100)

        if choice == 1:
            n = c.allocate_network(
                '192.168.%d.0/24' % (50 + random.randint(0, 100)), True, True,
                randomName())
            networks.append({'network_uuid': n['uuid']})
            print('Created network %s' % n['uuid'])

        elif choice < 30:
            if len(instances) > 0:
                i = random.choice(instances)
                c.delete_instance(i)
                instances.remove(i)
                print('Deleted instance %s' % i)

        else:
            try:
                i = c.create_instance(randomName(), random.randint(1, 4),
                                      random.randint(1, 4),
                                      [random.choice(networks)],
                                      [random.choice(disks)], None, None)
                instances.append(i['uuid'])
                print('Created instance %s' % i['uuid'])

            except apiclient.InsufficientResourcesException:
                print(
                    'Failed to start an instance due to insufficient resources'
                )
Exemple #24
0
    def test_get_nodes(self, mock_request):
        client = apiclient.Client()
        out = list(client.get_nodes())

        mock_request.assert_called_with('GET', 'http://localhost:13000/nodes')
Exemple #25
0
    def test_delete_network_metadata_item(self):
        client = apiclient.Client()
        client.delete_network_metadata_item('net1', 'herd')

        self.mock_request.assert_called_with(
            'DELETE', 'http://localhost:13000/networks/net1/metadata/herd')
Exemple #26
0
    def test_get_nodes(self, mock_request):
        client = apiclient.Client()
        out = list(client.get_nodes())

        mock_request.assert_called_with('GET', 'http://localhost:13000/nodes')
        assert (type(out[0]['lastseen']) == datetime.datetime)
Exemple #27
0
    def test_unpause_instance(self):
        client = apiclient.Client()
        client.unpause_instance('notreallyauuid')

        self.mock_request.assert_called_with(
            'POST', 'http://localhost:13000/instances/notreallyauuid/unpause')
Exemple #28
0
    def test_get_instances(self):
        client = apiclient.Client()
        list(client.get_instances())

        self.mock_request.assert_called_with(
            'GET', 'http://localhost:13000/instances')
Exemple #29
0
    def test_delete_network(self):
        client = apiclient.Client()
        client.delete_network('notreallyauuid')

        self.mock_request.assert_called_with(
            'DELETE', 'http://localhost:13000/networks/notreallyauuid')
Exemple #30
0
    def test_get_network(self):
        client = apiclient.Client()
        client.get_network('notreallyauuid')

        self.mock_request.assert_called_with(
            'GET', 'http://localhost:13000/networks/notreallyauuid')