1 import structlog

2 from django import forms

3 from django.contrib import admin

4

5 from meshadmin.server.networks.models import (

6 CA,

7 Group,

8 GroupConfig,

9 Host,

10 HostCert,

11 HostConfig,

12 Network,

13 NetworkMembership,

14 Rule,

15 SigningCA,

16 Template,

17)

18 from meshadmin.server.networks.services import create_network

19

20 logger = structlog.get_logger(__name__)

21

22

23 class CaInline(admin.StackedInline):

24 model = CA

25 extra = 0

26 fields = ("name", "cert_print")

27 readonly_fields = (

28 "name",

29 "cert_print",

30 )

31

32

33 class HostInline(admin.TabularInline):

34 model = Host

35 extra = 0

36

37

38 class GroupInline(admin.TabularInline):

39 model = Group

40 extra = 0

41

42

43 class SigningCAInline(admin.StackedInline):

44 model = SigningCA

45 extra = 0

46

47

48 class TemplateInline(admin.TabularInline):

49 model = Template

50 extra = 0

51

52

53 class NetworkAdminForm(forms.ModelForm):

54 class Meta:

55 model = Network

56 fields = ("name", "cidr")

57

58

59 class NetworkMembershipInline(admin.TabularInline):

60 model = NetworkMembership

61 extra = 0

62

63

64@admin.register(Network)

65 class NetworkAdmin(admin.ModelAdmin):

66 form = NetworkAdminForm

67

68 def get_inlines(self, request, obj):

69 if obj:

70 return [

71 CaInline,

72 SigningCAInline,

73 HostInline,

74 GroupInline,

75 TemplateInline,

76 NetworkMembershipInline,

77 ]

78 else:

79 return []

80

81 def save_model(self, request, obj: Network, form, change):

82 if not change:

83 create_network(obj.name, obj.cidr, request.user)

84 else:

85 obj.save()

86

87

88 class HostCertInline(admin.TabularInline):

89 model = HostCert

90 extra = 0

91

92

93 class HostConfigInline(admin.TabularInline):

94 model = HostConfig

95 extra = 0

96

97

98@admin.register(Host)

99 class HostAdmin(admin.ModelAdmin):

100 list_display = ("id", "name", "network__name", "assigned_ip")

101 list_filter = ("network",)

102 inlines = [

103 HostCertInline,

104 HostConfigInline,

105 ]

106

107

108@admin.register(Template)

109 class TemplateAdmin(admin.ModelAdmin):

110 list_display = ["id", "name", "network__name"]

111 list_filter = ("network",)

112

113

114 class RuleInline(admin.TabularInline):

115 model = Rule

116 extra = 0

117

118

119 class GroupConfigInline(admin.TabularInline):

120 model = GroupConfig

121 extra = 0

122

123

124@admin.register(Group)

125 class GroupAdmin(admin.ModelAdmin):

126 list_display = ("id", "name")

127 list_filter = ("network",)

128 inlines = (GroupConfigInline,)