Skip to content

Commit f4d6cf1

Browse files
committed
Merged Image and CloudImage into one model
1 parent b0c4e21 commit f4d6cf1

File tree

3 files changed

+108
-34
lines changed

3 files changed

+108
-34
lines changed

django-cloudlaunch/cloudlaunch/admin.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ class AppCategoryAdmin(admin.ModelAdmin):
3232

3333

3434
class CloudImageAdmin(admin.ModelAdmin):
35-
model = models.CloudImage
36-
list_display = ('name', 'cloud', 'image_id')
37-
list_filter = ('name', 'cloud')
35+
model = models.Image
36+
list_display = ('name', 'region', 'image_id')
37+
list_filter = ('name', 'region')
3838
ordering = ('name',)
3939

4040

@@ -67,33 +67,34 @@ class AppDeploymentsAdmin(admin.ModelAdmin):
6767

6868
class UsageAdmin(admin.ModelAdmin):
6969
models = models.Usage
70-
# Enable column-based display&filtering of entries
71-
list_display = ('added', 'target_cloud', 'instance_type', 'application',
72-
'user')
73-
# Enable filtering of displayed entries
74-
list_filter = ('added', 'app_deployment__target_cloud', 'user',
75-
'app_deployment__application_version__application__name')
76-
# Enable hierarchical navigation by date
77-
date_hierarchy = 'added'
78-
ordering = ('-added',)
79-
# Add search
80-
search_fields = ['user']
8170

82-
def application(self, obj):
71+
def deployment_target(self, obj):
8372
if obj.app_deployment:
84-
return obj.app_deployment.application_version.application.name
73+
return obj.app_deployment.deployment_target
8574
return None
75+
deployment_target.short_description = 'Deployment Target'
8676

87-
def target_cloud(self, obj):
77+
def application(self, obj):
8878
if obj.app_deployment:
89-
return obj.app_deployment.target_cloud.name
79+
return obj.app_deployment.application_version.application.name
9080
return None
91-
target_cloud.short_description = 'Target cloud'
9281

9382
def instance_type(self, obj):
9483
app_config = ast.literal_eval(obj.app_config)
9584
return app_config.get('config_cloudlaunch', {}).get('instanceType')
9685

86+
# Enable column-based display&filtering of entries
87+
list_display = ('added', 'deployment_target', 'instance_type', 'application',
88+
'user')
89+
# Enable filtering of displayed entries
90+
list_filter = ('added', 'app_deployment__deployment_target', 'user',
91+
'app_deployment__application_version__application__name')
92+
# Enable hierarchical navigation by date
93+
date_hierarchy = 'added'
94+
ordering = ('-added',)
95+
# Add search
96+
search_fields = ['user']
97+
9798

9899
class PublicKeyInline(admin.StackedInline):
99100
model = models.PublicKey
@@ -107,7 +108,7 @@ class UserProfileAdmin(djcloudbridge.admin.UserProfileAdmin):
107108
admin.site.register(models.Application, AppAdmin)
108109
admin.site.register(models.AppCategory, AppCategoryAdmin)
109110
admin.site.register(models.ApplicationDeployment, AppDeploymentsAdmin)
110-
admin.site.register(models.CloudImage, CloudImageAdmin)
111+
admin.site.register(models.Image, CloudImageAdmin)
111112
admin.site.register(models.Usage, UsageAdmin)
112113

113114
# Add public key to existing UserProfile
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Generated by Django 2.1.7 on 2019-03-10 08:17
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
import smart_selects.db_fields
6+
7+
8+
def copy_cloud_image_data(apps, schema_editor):
9+
Image = apps.get_model('cloudlaunch', 'Image')
10+
CloudImage = apps.get_model('cloudlaunch', 'CloudImage')
11+
Region = apps.get_model('djcloudbridge', 'Region')
12+
13+
for cloud_image in CloudImage.objects.all():
14+
image = Image.objects.get(id=cloud_image.image_ptr_id)
15+
image.region = Region.objects.get(cloud=cloud_image.cloud)
16+
image.save()
17+
18+
19+
class Migration(migrations.Migration):
20+
21+
dependencies = [
22+
('djcloudbridge', '0007_delete_cloudold'),
23+
('cloudlaunch', '0008_match_djcloudbridge_and_introduce_targets'),
24+
]
25+
26+
operations = [
27+
migrations.AddField(
28+
model_name='image',
29+
name='region',
30+
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE,
31+
to='djcloudbridge.Region'),
32+
),
33+
migrations.AlterField(
34+
model_name='applicationversioncloudconfig',
35+
name='image',
36+
field=smart_selects.db_fields.ChainedForeignKey(chained_field='target',
37+
chained_model_field='target__zone__region',
38+
on_delete=django.db.models.deletion.CASCADE,
39+
to='cloudlaunch.Image'),
40+
),
41+
migrations.RunPython(copy_cloud_image_data),
42+
migrations.AlterField(
43+
model_name='image',
44+
name='region',
45+
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE,
46+
to='djcloudbridge.Region'),
47+
),
48+
migrations.DeleteModel(
49+
name='CloudImage',
50+
),
51+
]

django-cloudlaunch/cloudlaunch/models.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,17 @@
1616

1717

1818
class Image(cb_models.DateNameAwareModel):
19-
"""
20-
A base Image model used by a virtual appliance.
21-
22-
Applications will use Images and the same application may be available
23-
on multiple infrastructures so we need this base class so a single
24-
application field can be used to retrieve all images across
25-
infrastructures.
26-
"""
2719
image_id = models.CharField(max_length=50, verbose_name="Image ID")
2820
description = models.CharField(max_length=255, blank=True, null=True)
21+
region = models.ForeignKey(cb_models.Region, on_delete=models.CASCADE,
22+
null=False)
2923

30-
31-
class CloudImage(Image):
32-
cloud = models.ForeignKey(cb_models.Cloud, on_delete=models.CASCADE,
33-
blank=True, null=True)
24+
# Cannot be unique together because Azure images are global, not regional
25+
# class Meta:
26+
# unique_together = (("region", "image_id"),)
3427

3528
def __str__(self):
36-
return "{0} (on {1})".format(self.name, self.cloud.name)
29+
return "{0} (on {1})".format(self.name, self.region)
3730

3831

3932
class DeploymentTarget(PolymorphicModel):
@@ -42,6 +35,9 @@ class Meta:
4235
verbose_name = "Deployment Target"
4336
verbose_name_plural = "Deployment Targets"
4437

38+
def __str__(self):
39+
return "{0}: {1}".format(self._meta.verbose_name, self.id)
40+
4541

4642
class HostDeploymentTarget(DeploymentTarget):
4743

@@ -68,6 +64,9 @@ class Meta:
6864
verbose_name = "Cloud"
6965
verbose_name_plural = "Clouds"
7066

67+
def __str__(self):
68+
return "{0}: {1}".format(self._meta.verbose_name, self.target_zone)
69+
7170

7271
class AppCategory(models.Model):
7372
"""Categories an app can be associated with."""
@@ -223,11 +222,34 @@ def compute_merged_config(self):
223222
default_appwide_config, default_version_config)
224223
return jsonmerge.merge(default_combined_config, default_cloud_config)
225224

225+
def to_dict(self):
226+
"""
227+
Serialize the supplied model to a dict.
228+
229+
A subset of the the model fields is returned as used by current
230+
plugins but more fields can be serialized as needed.
231+
232+
@rtype: ``dict``
233+
@return: A serialized version of the supplied model.
234+
"""
235+
return {
236+
'id': self.id,
237+
'default_launch_config': self.default_launch_config,
238+
'target': self.target.pk,
239+
'application_version': self.application_version.pk
240+
}
241+
226242

227243
class ApplicationVersionCloudConfig(ApplicationVersionTargetConfig):
228-
image = ChainedForeignKey(CloudImage, chained_field="target",
244+
image = ChainedForeignKey(Image, chained_field="target",
229245
chained_model_field="target__zone__region")
230246

247+
def to_dict(self):
248+
return {
249+
**super(ApplicationVersionCloudConfig, self).to_dict(),
250+
**{'image_id': self.image.image_id}
251+
}
252+
231253

232254
class ApplicationDeployment(cb_models.DateNameAwareModel):
233255
"""Application deployment details."""

0 commit comments

Comments
 (0)