Skip to content

Commit

Permalink
Add cloud images support
Browse files Browse the repository at this point in the history
  • Loading branch information
tomez committed Jun 24, 2019
1 parent 7df7b82 commit bd2d884
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/ralph/admin/sitetrees.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def section(section_name, app, model):
section(_('Cloud projects'), 'virtual', 'CloudProject'),
section(_('Cloud flavors'), 'virtual', 'CloudFlavor'),
section(_('Cloud providers'), 'virtual', 'CloudProvider'),
section(_('Cloud images'), 'virtual', 'CloudImage'),
],
),
ralph_item(
Expand Down
1 change: 1 addition & 0 deletions src/ralph/admin/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
'ralph.trade_marks.models.TradeMarkRegistrarInstitution': 'ralph.trade_marks.tests.factories.TradeMarkRegistrarInstitutionFactory', # noqa
'ralph.virtual.models.CloudFlavor': 'ralph.virtual.tests.factories.CloudFlavorFactory', # noqa
'ralph.virtual.models.CloudHost': 'ralph.virtual.tests.factories.CloudHostFullFactory', # noqa
'ralph.virtual.models.CloudImage': 'ralph.virtual.tests.factories.CloudImageFactory', # noqa
'ralph.virtual.models.CloudProject': 'ralph.virtual.tests.factories.CloudProjectFactory', # noqa
'ralph.virtual.models.CloudProvider': 'ralph.virtual.tests.factories.CloudProviderFactory', # noqa
'ralph.virtual.models.CloudSyncProcessor': 'ralph.virtual.tests.factories.CloudSyncProcessorFactory', # noqa
Expand Down
10 changes: 9 additions & 1 deletion src/ralph/data_importer/management/commands/demodata.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
)
from ralph.reports.models import Report, ReportLanguage, ReportTemplate
from ralph.supports.tests.factories import BaseObjectsSupportFactory
from ralph.virtual.tests.factories import CloudImageFactory


def get_imei(n):
Expand All @@ -66,7 +67,7 @@ class Command(BaseCommand):
help = "Generating demo data."
all_apps = [
'backoffice', 'datacenter', 'licences', 'supports', 'transitions',
'sim_cards'
'sim_cards', 'cloudimages'
]
object_limit = 30

Expand Down Expand Up @@ -434,6 +435,11 @@ def generate_support(self):
back_office_asset.user = self.get_user()
back_office_asset.save()

def generate_cloud_images(self):
self.stdout.write('Generating Cloud Images')
for i in range(self.object_limit):
CloudImageFactory()

def handle(self, *args, **options):
apps = options.get('apps').split(',')
if 'all' in apps:
Expand All @@ -456,6 +462,8 @@ def handle(self, *args, **options):
self.generate_licence()
if 'transitions' in apps:
self.generate_transitions()
if 'cloudimages' in apps:
self.generate_cloud_images()

# Create super user
root = UserFactory(username='ralph', is_superuser=True, is_staff=True)
Expand Down
16 changes: 14 additions & 2 deletions src/ralph/virtual/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
from ralph.virtual.models import (
CloudFlavor,
CloudHost,
CloudImage,
CloudProject,
CloudProvider,
VirtualServer,
VirtualServerType
)
VirtualServerType)

if settings.ENABLE_DNSAAS_INTEGRATION:
from ralph.dns.views import DNSView
Expand Down Expand Up @@ -509,3 +509,15 @@ class CloudProviderAdmin(RalphAdmin):
form = CloudProviderForm
list_display = ['name', 'cloud_sync_enabled', 'cloud_sync_driver']
list_filter = ['name', 'cloud_sync_enabled', 'cloud_sync_driver']


@register(CloudImage)
class CloudImageAdmin(RalphAdmin):
list_display = ['name', 'image_id']
list_filter = ['name', 'image_id']

fieldsets = (
('Cloud Image', {
'fields': ['name', 'image_id']
}),
)
12 changes: 12 additions & 0 deletions src/ralph/virtual/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from ralph.virtual.models import (
CloudFlavor,
CloudHost,
CloudImage,
CloudProject,
CloudProvider,
VirtualServer,
Expand Down Expand Up @@ -126,6 +127,11 @@ class Meta(BaseObjectSerializer.Meta):
exclude = ['content_type', 'parent']


class CloudImageSerializer(RalphAPISerializer):
class Meta:
model = CloudImage


class CloudProviderSerializer(RalphAPISerializer):
class Meta:
model = CloudProvider
Expand Down Expand Up @@ -180,6 +186,11 @@ class CloudProviderViewSet(RalphAPIViewSet):
serializer_class = CloudProviderSerializer


class CloudImageViewSet(RalphAPIViewSet):
queryset = CloudImage.objects.all()
serializer_class = CloudImageSerializer


class CloudHostViewSet(BaseObjectViewSetMixin, RalphAPIViewSet):
queryset = CloudHost.objects.all()
serializer_class = CloudHostSerializer
Expand Down Expand Up @@ -266,6 +277,7 @@ class VirtualServerViewSet(BaseObjectViewSetMixin, RalphAPIViewSet):
router.register(r'cloud-hosts', CloudHostViewSet)
router.register(r'cloud-projects', CloudProjectViewSet)
router.register(r'cloud-providers', CloudProviderViewSet)
router.register(r'cloud-images', CloudImageViewSet)
router.register(r'virtual-servers', VirtualServerViewSet)
router.register(r'virtual-server-types', VirtualServerTypeViewSet)
urlpatterns = []
28 changes: 28 additions & 0 deletions src/ralph/virtual/migrations/0012_cloudimage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
import ralph.lib.mixins.models


class Migration(migrations.Migration):

dependencies = [
('virtual', '0011_cloudprovider_client_config'),
]

operations = [
migrations.CreateModel(
name='CloudImage',
fields=[
('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)),
('name', models.CharField(verbose_name='name', max_length=255, unique=True)),
('image_id', models.CharField(verbose_name='image ID', max_length=100, unique=True)),
],
options={
'verbose_name': 'Cloud image',
'verbose_name_plural': 'Cloud images',
},
bases=(ralph.lib.mixins.models.AdminAbsoluteUrlMixin, models.Model),
),
]
16 changes: 16 additions & 0 deletions src/ralph/virtual/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,21 @@ def __str__(self):
return 'Cloud Project: {}'.format(self.name)


class CloudImage(AdminAbsoluteUrlMixin, NamedMixin):
class Meta:
verbose_name = _('Cloud image')
verbose_name_plural = _('Cloud images')

image_id = models.CharField(
verbose_name=_('image ID'),
unique=True,
max_length=100
)

def __str__(self):
return 'Cloud Image: {}'.format(self.name)


@receiver(models.signals.post_save, sender=CloudProject)
def update_service_env_on_cloudproject_save(sender, instance, **kwargs):
"""Update CloudHost service_env while updating CloudProject"""
Expand Down Expand Up @@ -398,5 +413,6 @@ class Meta:
def __str__(self):
return 'VirtualServer: {} ({})'.format(self.hostname, self.sn)


post_commit(publish_host_update, VirtualServer)
post_commit(publish_host_update, CloudHost)
14 changes: 12 additions & 2 deletions src/ralph/virtual/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@
from ralph.virtual.models import (
CloudFlavor,
CloudHost,
CloudImage,
CloudProject,
CloudProvider,
VirtualServer,
VirtualServerType
)
VirtualServerType)


class CloudImageFactory(DjangoModelFactory):

name = factory.Sequence(lambda n: 'image-{0}'.format(n))
image_id = factory.Sequence(lambda n: 'img-uuid-{0}'.format(n))

class Meta:
model = CloudImage
django_get_or_create = ['name', 'image_id']


class CloudProviderFactory(DjangoModelFactory):
Expand Down

0 comments on commit bd2d884

Please sign in to comment.