示例#1
0
文件: views.py 项目: zeus911/amon
def add(request, provider_id=None):
    all_for_provider = cloud_credentials_model.get_all_for_provider(
        provider_id=provider_id)
    provider_form = PROVIDERS_FORMS.get(provider_id, False)

    if request.method == "POST":
        form = provider_form(request.POST)

        if form.is_valid():
            data = form.cleaned_data
            cloud_credentials_model.save(data=data, provider_id=provider_id)

            messages.add_message(
                request, messages.INFO,
                '{0} credentials updated'.format(provider_id.title()))
            return redirect(
                reverse('cloudservers_edit',
                        kwargs={'provider_id': provider_id}))
    else:
        form = provider_form()

    return render(
        request, 'cloudservers/view.html', {
            "form": form,
            "provider_id": provider_id,
            "all_for_provider": all_for_provider,
            "add_form": True,
        })
示例#2
0
    def save_digitalocean_test(self):
        self._cleanup()

        data = {'name': 'test', 'token': 'test-token'}

        cloud_credentials_model.save(data=data, provider_id='digitalocean')

        result = cloud_credentials_model.collection.find_one()

        assert self.cypher.decrypt(result['token']) == 'test-token'
示例#3
0
    def save_rackspace_test(self):
        self._cleanup()

        data = {
            'name': 'test',
            'username': '******',
            'api_key': 'super_secret'
        }

        cloud_credentials_model.save(data=data.copy(), provider_id='rackspace')

        result = cloud_credentials_model.collection.find_one()

        assert self.cypher.decrypt(result['username']) == data['username']
        assert self.cypher.decrypt(result['api_key']) == data['api_key']
示例#4
0
    def save_google_test(self):
        self._cleanup()

        data = {
            'name': 'test',
            'email': '*****@*****.**',
            'project_id': 'super_secret'
        }

        cloud_credentials_model.save(data=data.copy(), provider_id='google')

        result = cloud_credentials_model.collection.find_one()

        assert self.cypher.decrypt(result['email']) == data['email']
        assert self.cypher.decrypt(result['project_id']) == data['project_id']
示例#5
0
    def save_amazon_test(self):
        self._cleanup()

        data = {
            'name': 'test',
            'access_key': 'test-token',
            'secret_key': 'super_secret'
        }

        cloud_credentials_model.save(data=data.copy(), provider_id='amazon')

        result = cloud_credentials_model.collection.find_one()

        assert self.cypher.decrypt(result['access_key']) == data['access_key']
        assert self.cypher.decrypt(result['secret_key']) == data['secret_key']
示例#6
0
    def test_get_instance_key(self):
        self._cleanup()

        data = {"access_key": settings.AMAZON_ACCESS_KEY, "secret_key": settings.AMAZON_SECRET_KEY, "regions": 'eu-west-1'}


        credentials_id = cloud_credentials_model.save(data=data, provider_id='amazon')

        valid_credentials = cloud_credentials_model.get_by_id(credentials_id)


        instance_id = "instance_id_test"
        instance_list = []
        instance = {
            'name': 'test',
            'instance_id': instance_id,
            'provider': "amazon",
            'credentials_id': credentials_id,
            'credentials': 'production',
            'region': 'eu-west1',
            'type': 't1-micro',
            'key': 'testserver-key'

        }

        instance_list.append(instance)
        cloud_server_model.save(instances=instance_list, credentials=valid_credentials)
        server = server_model.collection.find_one()
        key = server.get('key')

        url = reverse('api_cloudservers_get_server_key', kwargs={'provider_id': 'amazon'}, )
        url = "{0}?instance_id={1}".format(url, instance_id)

        response = self.c.get(url)
        assert response.content.decode('utf-8') == key
示例#7
0
    def _cloud_sync(self, provider_id=None):
        valid_dict = self.valid_data[provider_id]
        invalid_dict = self.invalid_data[provider_id]
    

        credentials_id = cloud_credentials_model.save(data=valid_dict, provider_id=provider_id)

        valid_credentials = cloud_credentials_model.get_by_id(credentials_id)
        
        now = unix_utc_now() - 5 # Just in case, the case could pass in less then a second
        sync_credentials(credentials=valid_credentials)

        entry = cloud_credentials_model.get_by_id(credentials_id)

        assert entry['last_sync'] > now

        # for r in server_model.get_all():
        #     eq_(r['provider'], 'rackspace')
        #     eq_(r['credentials'], entry['_id'])

    
        # Invalid credentials
        cloud_credentials_model.update(data=invalid_dict, id=credentials_id, provider_id=provider_id)


        invalid_credentials = cloud_credentials_model.get_by_id(credentials_id)


        now = unix_utc_now()-5 
        sync_credentials(credentials=invalid_credentials)
        entry = cloud_credentials_model.get_by_id(credentials_id)


        assert entry['error']
        assert entry['last_sync'] > now
示例#8
0
    def delete_all_for_credentials_test(self):
        self._cleanup()

        data = {'name': 'test', 'token': 'test-token'}
        credentials_id = cloud_credentials_model.save(
            data=data, provider_id='digitalocean')

        for i in range(5):
            self.collection.insert({
                "account_id": 1,
                "name": "test",
                "key": "server_key_test",
                "credentials_id": credentials_id
            })

        cloud_server_model.delete_all_for_provider(
            credentials_id=credentials_id)

        result = self.collection.find().count()
        eq_(result, 0)

        self._cleanup()
示例#9
0
    def save_test(self):
        self._cleanup()

        credentials_id = "test_credentials"

        data = {'name': 'test', 'token': 'test-token'}
        credentials_id = cloud_credentials_model.save(
            data=data, provider_id='digitalocean')
        credentials = cloud_credentials_model.collection.find_one()

        # Empty list
        instance_list = []

        cloud_server_model.save(instances=instance_list,
                                credentials=credentials)

        result = self.collection.find()
        eq_(result.count(), 0)

        # Normal list
        for i in range(5):
            instance = {
                'name': 'test',
                'instance_id': "instance_id_{0}".format(i),
                'provider': "rackspace",
                'credentials_id': credentials_id,
                'region': 'eu-west1',
                'type': 't1-micro'
            }

            instance_list.append(instance)

        cloud_server_model.save(instances=instance_list,
                                credentials=credentials)

        result = self.collection.find()

        for r in result.clone():

            assert len(r['tags']) == 3
            for tag in r['tags']:
                tag_object = tags_model.get_by_id(tag)

                name = tag_object.get('name')
                group = tag_object.get('group', {}).get('name')
                assert name in ['rackspace', 'eu-west1', 't1-micro']
                assert group in ['region', 'provider', 'type']

            eq_(r['credentials_id'], credentials_id)

        eq_(result.count(), 5)

        self._cleanup()

        # Filter and delete some old instances
        for i in range(4):
            self.collection.insert({
                "account_id": 1,
                "name": "test",
                "key": "server_key_test",
                "credentials_id": credentials_id,
                "instance_id": "instance_id_{0}".format(i)
            })

        result = self.collection.find().count()
        eq_(result, 4)

        # Check if duplicate tags are being saved
        for i in ['rackspace', 'bla']:
            tags_model.get_or_create_by_name(name=i)

        instance_list = []
        for i in range(5, 10):
            instance = {
                'name': 'test',
                'instance_id': i,
                'provider': "rackspace",
                'credentials_id': credentials_id,
            }

            instance_list.append(instance)

        cloud_server_model.save(instances=instance_list,
                                credentials=credentials)

        result = self.collection.find()
        eq_(result.count(), 5)

        for r in result:
            for tag in r['tags']:
                tag_object = tags_model.get_by_id(tag)

                assert tag_object['name'] in ['rackspace', 'bla']

            self.assertTrue(r['key'])
            assert r['instance_id'] <= 10
            assert r['instance_id'] >= 5

        # Filter and delete all instances, the instance list is empty
        instance_list = []
        cloud_server_model.save(instances=instance_list,
                                credentials=credentials)

        result = self.collection.find()
        eq_(result.count(), 0)

        self._cleanup()