Exemple #1
0
 def on_get(self, req, resp, tenant_id, target_id):
     target = Target.get_target(tenant_id, target_id)
     if target:
         resp.body = self.format_response_body(target.as_dict())
     else:
         msg = 'Cannot find target: {target_id}'.format(target_id=target_id)
         resp.status = falcon.HTTP_404
         resp.body = json.dumps({'description': msg})
Exemple #2
0
        def can_deserialize_from_a_dictionary(self):
            tenant_id = str(uuid.uuid4())

            test_target = Target.build_target_from_dict(tenant_id,
                                                        self.example_dict)
            expect(test_target.id).to.equal(self.example_dict['id'])
            expect(test_target.name).to.equal(self.example_dict['name'])
            expect(test_target.target_type).to.equal(self.example_dict['type'])
Exemple #3
0
 def on_get(self, req, resp, tenant_id, target_id):
     target = Target.get_target(tenant_id, target_id)
     if target:
         resp.body = self.format_response_body(target.as_dict())
     else:
         msg = 'Cannot find target: {target_id}'.format(target_id=target_id)
         resp.status = falcon.HTTP_404
         resp.body = json.dumps({'description': msg})
Exemple #4
0
    def on_post(self, req, resp, tenant_id):
        target_id = str(uuid.uuid4())

        body = self.load_body(req, self.validator)
        body['id'] = target_id

        target = Target.build_target_from_dict(tenant_id, body)
        duplicate_target = Target.get_target(tenant_id, target_id=target.name)

        if duplicate_target:
            raise falcon.exceptions.HTTPConflict(
                'Duplicate Target Name',
                'Target names must be unique: {0}'.format(target.name))

        Target.save_target(target)

        resp.status = falcon.HTTP_201
        resp.body = self.format_response_body({'target_id': target_id})
Exemple #5
0
    def on_post(self, req, resp, tenant_id):
        target_id = str(uuid.uuid4())

        body = self.load_body(req, self.validator)
        body['id'] = target_id

        target = Target.build_target_from_dict(tenant_id, body)
        duplicate_target = Target.get_target(tenant_id, target_id=target.name)

        if duplicate_target:
            raise falcon.exceptions.HTTPConflict(
                'Duplicate Target Name',
                'Target names must be unique: {0}'.format(target.name))

        Target.save_target(target)

        resp.status = falcon.HTTP_201
        resp.body = self.format_response_body({'target_id': target_id})
Exemple #6
0
 def execute_action(self, job, action):
     # TODO: Replace with actual logging
     print('Executing a sample action')
     print('Tenant ID: {0}'.format(job.tenant_id))
     print('Target IDs: {0}'.format(action.targets))
     targets = [Target.get_target(job.tenant_id, target_id)
                for target_id in action.targets]
     print('Targets: {0}'.format(targets))
     print('Type: {0}'.format(action.action_type))
     print('Params: {0}'.format(action.parameters))
Exemple #7
0
 def execute_action(self, job, action):
     # TODO: Replace with actual logging
     print('Executing a sample action')
     print('Tenant ID: {0}'.format(job.tenant_id))
     print('Target IDs: {0}'.format(action.targets))
     targets = [
         Target.get_target(job.tenant_id, target_id)
         for target_id in action.targets
     ]
     print('Targets: {0}'.format(targets))
     print('Type: {0}'.format(action.action_type))
     print('Params: {0}'.format(action.parameters))
Exemple #8
0
    def execute_action(self, job, action):
        cmd = action.parameters.get("command")

        for target_id in action.targets:
            target = Target.get_target(job.tenant_id, target_id)
            ip = target.address.address_child
            ssh = target.authentication.get("ssh")

            creds = SSHKeyCredentials(username=ssh.get("username"), key_contents=ssh.get("private_key"))
            client = SSHClient(host=ip.address, port=ip.port, credentials=creds)
            client.connect()
            LOG.info("Remote command plugin executing: %s", cmd)
            resp = client.execute(command=cmd)
            client.close()
            LOG.info("Remote command plugin execution response: %s", resp)
Exemple #9
0
    def on_get(self, req, resp, tenant_id, target_id):
        target = Target.get_target(tenant_id, target_id)
        if target:
            address = target.address
            # Nova
            if 'nova' in address.as_dict().keys():
                nova_address = address.address_child

                auth = target.authentication
                try:
                    if 'rackspace' in auth:
                        cls = get_driver(Provider.RACKSPACE)
                        cls(auth['rackspace']['username'],
                            auth['rackspace']['api_key'],
                            region=nova_address.region.lower())
                        resp.status = falcon.HTTP_200
                    else:
                        raise Exception("No supported providers in target: {0}"
                                        .format(target.as_dict()))
                except Exception:
                    resp.status = falcon.HTTP_404
            # SSH
            else:
                ip = address.address_child
                ssh = target.authentication.get('ssh')

                creds = SSHKeyCredentials(
                    username=ssh.get('username'),
                    key_contents=ssh.get('private_key')
                )
                client = SSHClient(
                    host=ip.address,
                    port=ip.port,
                    credentials=creds
                )
                try:
                    client.connect()
                    resp.status = falcon.HTTP_200
                    client.close()
                except SSHException:
                    resp.status = falcon.HTTP_404
        else:
            msg = 'Cannot find target: {target_id}'.format(target_id=target_id)
            resp.status = falcon.HTTP_404
            resp.body = json.dumps({'description': msg})
Exemple #10
0
    def on_get(self, req, resp, tenant_id, target_id):
        target = Target.get_target(tenant_id, target_id)
        if target:
            address = target.address
            # Nova
            if 'nova' in address.as_dict().keys():
                nova_address = address.address_child

                auth = target.authentication
                try:
                    if 'rackspace' in auth:
                        cls = get_driver(Provider.RACKSPACE)
                        cls(auth['rackspace']['username'],
                            auth['rackspace']['api_key'],
                            region=nova_address.region.lower())
                        resp.status = falcon.HTTP_200
                    else:
                        raise Exception(
                            "No supported providers in target: {0}".format(
                                target.as_dict()))
                except Exception:
                    resp.status = falcon.HTTP_404
            # SSH
            else:
                ip = address.address_child
                ssh = target.authentication.get('ssh')

                creds = SSHKeyCredentials(username=ssh.get('username'),
                                          key_contents=ssh.get('private_key'))
                client = SSHClient(host=ip.address,
                                   port=ip.port,
                                   credentials=creds)
                try:
                    client.connect()
                    resp.status = falcon.HTTP_200
                    client.close()
                except SSHException:
                    resp.status = falcon.HTTP_404
        else:
            msg = 'Cannot find target: {target_id}'.format(target_id=target_id)
            resp.status = falcon.HTTP_404
            resp.body = json.dumps({'description': msg})
 def before_each(self):
     self.plugin = remote_command.RemoteCommandPlugin()
     self.job = stub(tenant_id=lambda: 'test_tenant')
     self.paramiko_stub = create_paramiko_client_stub()
     self.target = Target.build_target_from_dict(
         'test_tenant', {
             'id': 'test_id',
             'type': 'something',
             'address': {
                 'ip': {
                     'address': '10.0.0.1',
                     'port': 21
                 }
             },
             'authentication': {
                 'ssh': {
                     'username': '******',
                     'private_key': GARBAGE_PRIVATE_KEY_FOR_TEST,
                 }
             },
             'name': 'test_target'
         })
Exemple #12
0
 def before_each(self):
     self.plugin = remote_command.RemoteCommandPlugin()
     self.job = stub(tenant_id=lambda: 'test_tenant')
     self.paramiko_stub = create_paramiko_client_stub()
     self.target = Target.build_target_from_dict(
         'test_tenant',
         {
             'id': 'test_id',
             'type': 'something',
             'address': {
                 'ip': {
                     'address': '10.0.0.1',
                     'port': 21
                 }
             },
             'authentication': {
                 'ssh': {
                     'username': '******',
                     'private_key': GARBAGE_PRIVATE_KEY_FOR_TEST,
                 }
             },
             'name': 'test_target'
         }
     )
Exemple #13
0
def create_target():
    return Target.build_target_from_dict(str(uuid4()), example_target_dict())
Exemple #14
0
class TestNovaSoftRebootPlugin(Spec):
    def before_each(self):
        self.plugin = NovaSoftReboot()
        self.job = stub(tenant_id='test_tenant')
        self.action = stub(targets=['d8659253-ce8e-48dc-9c93-495fa39fe7ad'])

    @patch('rift.plugins.nova.get_driver')
    @patch('rift.data.models.target.Target.get_target')
    def can_execute_action(self, get_target, get_driver):
        target = self.VALID_TARGET
        nodes = [
            stub(name="my.server.com", reboot=call_recorder(lambda: True)),
            stub(name="not.my.server.com", reboot=call_recorder(lambda: True)),
        ]
        driver_stub = self._get_libcloud_driver_stub(nodes)
        get_target.return_value = target
        get_driver.return_value = driver_stub

        self.plugin.execute_action(self.job, self.action)

        get_target.assert_called_with(self.job.tenant_id,
                                      self.action.targets[0])
        expect(driver_stub.calls).to.equal(
            [call('myusername', 'myapikey', region='dfw')])
        expect(len(nodes[0].reboot.calls)).to.equal(1)
        expect(len(nodes[1].reboot.calls)).to.equal(0)

    @patch('rift.plugins.nova.get_driver')
    @patch('rift.data.models.target.Target.get_target')
    def raises_exception_on_invalid_address(self, get_target, get_driver):
        target = self.TARGET_WITH_IP_ADDRESS
        driver_stub = self._get_libcloud_driver_stub([])
        get_target.return_value = target
        get_driver.return_value = driver_stub

        expect(self.plugin.execute_action, [self.job, self.action]) \
            .to.raise_a(Exception)

    @patch('rift.plugins.nova.get_driver')
    @patch('rift.data.models.target.Target.get_target')
    def raises_exception_on_unsupported_provider(self, get_target, get_driver):
        target = self.TARGET_WITH_UNSUPPORTED_PROVIDER
        driver_stub = self._get_libcloud_driver_stub([])
        get_target.return_value = target
        get_driver.return_value = driver_stub

        expect(self.plugin.execute_action, [self.job, self.action]) \
            .to.raise_a(Exception)

    def plugin_returns_a_name(self):
        expect(self.plugin.get_name()).to.equal('nova-soft-reboot')

    def _get_libcloud_driver_stub(self, nodes):
        libcloud_stub = stub(list_nodes=call_recorder(lambda: nodes))

        def driver_class(user, key, region):
            return libcloud_stub

        return call_recorder(driver_class)

    VALID_TARGET = Target.build_target_from_dict(
        'test_tenant', {
            "name": "a valid nova target with rax auth",
            "type": "cloud-server",
            "address": {
                "nova": {
                    "name": "my.server.com",
                    "region": "DFW",
                }
            },
            "authentication": {
                "rackspace": {
                    "username": "******",
                    "api_key": "myapikey"
                }
            },
        })

    TARGET_WITH_IP_ADDRESS = Target.build_target_from_dict(
        'test_tenant', {
            "name": "a nova target with an ip address",
            "type": "cloud-server",
            "address": {
                "ip": {
                    "address": "127.0.0.1",
                    "port": 21,
                }
            },
            "authentication": {
                "rackspace": {
                    "username": "******",
                    "api_key": "myapikey"
                }
            },
        })

    TARGET_WITH_UNSUPPORTED_PROVIDER = Target.build_target_from_dict(
        'test_tenant', {
            "name": "a nova target with invalid auth",
            "type": "cloud-server",
            "address": {
                "nova": {
                    "name": "my.server.com",
                    "region": "DFW",
                }
            },
            "authentication": {
                "azure": {
                    "username": "******",
                    "api_key": "myapikey"
                }
            },
        })
Exemple #15
0
 def execute_action(self, job, action):
     for target_id in action.targets:
         target = Target.get_target(job.tenant_id, target_id)
         self._reboot_target(target)
Exemple #16
0
 def execute_action(self, job, action):
     for target_id in action.targets:
         target = Target.get_target(job.tenant_id, target_id)
         self._reboot_target(target)
Exemple #17
0
def create_target():
    return Target.build_target_from_dict(str(uuid4()), example_target_dict())
Exemple #18
0
    def on_get(self, req, resp, tenant_id):
        targets = Target.get_targets(tenant_id)
        target_list = [target.summary_dict() for target in targets]

        resp.body = self.format_response_body({'targets': target_list})
Exemple #19
0
 def on_delete(self, req, resp, tenant_id, target_id):
     Target.delete_target(target_id=target_id)
Exemple #20
0
 def on_delete(self, req, resp, tenant_id, target_id):
     Target.delete_target(target_id=target_id)
Exemple #21
0
    def on_get(self, req, resp, tenant_id):
        targets = Target.get_targets(tenant_id)
        target_list = [target.summary_dict() for target in targets]

        resp.body = self.format_response_body({'targets': target_list})