13
13
# limitations under the License.
14
14
from crispy_forms .bootstrap import AppendedText
15
15
from crispy_forms .helper import FormHelper
16
- from crispy_forms .layout import Layout , Field
16
+ from crispy_forms .layout import Layout , Field , Column , Row , Div
17
17
from django import forms
18
18
from django .forms import modelformset_factory
19
+ from django .core .exceptions import ValidationError
19
20
20
21
from scionlab .forms .attachment_conf_form import AttachmentConfForm , AttachmentConfFormSet
21
22
from scionlab .models .core import Link
@@ -36,6 +37,22 @@ def _crispy_helper(instance):
36
37
'installation_type' ,
37
38
template = 'scionlab/partials/installation_type_accordion.html' ,
38
39
),
40
+ Div (
41
+ Div (
42
+ Row (
43
+ 'become_user_ap' ,
44
+ ),
45
+ css_class = "card-header" ,
46
+ ),
47
+ Div (
48
+ Row (
49
+ Column ('public_ip' , css_class = 'col-md-5' ),
50
+ Column ('provide_vpn' , css_class = 'col-md-5' ),
51
+ ),
52
+ css_class = "card-body" , css_id = "user-ap-card-body" ,
53
+ ),
54
+ css_class = "card" ,
55
+ ),
39
56
)
40
57
41
58
# We need this to render the UserASForm along with the AttachmentConfForm
@@ -73,6 +90,25 @@ class UserASForm(forms.Form):
73
90
required = True ,
74
91
widget = forms .RadioSelect (),
75
92
)
93
+ become_user_ap = forms .BooleanField (
94
+ required = False ,
95
+ label = "Make this AS a publicly available Attachment Point" ,
96
+ help_text = """If this option is enabled, this AS will show up in the list of available
97
+ Attachment Points. All users will be able to create provider links to this AS.<br/> Your
98
+ host will have to run the scionlab-config daemon, in order to refresh its configuration
99
+ whenever other users create or modify links to your AS. Please refer to
100
+ <a href='https://docs.scionlab.org' target='_blank'>Tutorials</a> for more details."""
101
+ )
102
+ public_ip = forms .GenericIPAddressField (
103
+ required = False ,
104
+ label = "Public IP" ,
105
+ help_text = "Public IP Address to be used for connections to child ASes"
106
+ )
107
+ provide_vpn = forms .BooleanField (
108
+ required = False ,
109
+ label = "Provide VPN" ,
110
+ help_text = "Allow Users to connect to your AP via VPN"
111
+ )
76
112
77
113
def _get_attachment_conf_form_set (self , data , instance : UserAS ):
78
114
"""
@@ -99,20 +135,37 @@ def __init__(self, data=None, *args, **kwargs):
99
135
self .instance = kwargs .pop ('instance' , None )
100
136
self .attachment_conf_form_set = self ._get_attachment_conf_form_set (data , self .instance )
101
137
initial = kwargs .pop ('initial' , {})
138
+ has_vpn = False
102
139
if self .instance :
140
+ host = self .instance .host
141
+ is_ap = self .instance .is_attachment_point ()
142
+ has_vpn = is_ap and self .instance .attachment_point_info .vpn is not None
103
143
initial .update ({
104
144
'label' : self .instance .label ,
105
145
'installation_type' : self .instance .installation_type ,
146
+ 'public_ip' : host .public_ip ,
147
+ 'become_user_ap' : is_ap ,
148
+ 'provide_vpn' : has_vpn
106
149
})
107
150
self .helper = _crispy_helper (self .instance )
108
151
super ().__init__ (data , * args , initial = initial , ** kwargs )
152
+ if has_vpn :
153
+ self .fields ['provide_vpn' ].widget .attrs ['disabled' ] = 'disabled'
109
154
110
155
def clean (self ):
111
156
cleaned_data = super ().clean ()
112
157
if self .instance is None :
113
158
self .user .check_as_quota ()
114
159
115
160
self .attachment_conf_form_set .full_clean ()
161
+
162
+ if cleaned_data ['become_user_ap' ]:
163
+ if not cleaned_data .get ('public_ip' ):
164
+ self .add_error (
165
+ 'public_ip' ,
166
+ ValidationError ('Please enter a public IP address to become User AP' )
167
+ )
168
+
116
169
return cleaned_data
117
170
118
171
def has_changed (self ):
@@ -139,13 +192,19 @@ def save(self, commit=True):
139
192
isd = self .attachment_conf_form_set .isd ,
140
193
installation_type = self .cleaned_data ['installation_type' ],
141
194
label = self .cleaned_data ['label' ],
195
+ public_ip = self .cleaned_data ['public_ip' ],
196
+ wants_user_ap = self .cleaned_data ['become_user_ap' ],
197
+ wants_vpn = self .cleaned_data ['provide_vpn' ],
142
198
)
143
199
self .attachment_conf_form_set .save (user_as )
144
200
return user_as
145
201
else :
146
202
self .instance .update (
147
203
installation_type = self .cleaned_data ['installation_type' ],
148
- label = self .cleaned_data ['label' ]
204
+ label = self .cleaned_data ['label' ],
205
+ public_ip = self .cleaned_data ['public_ip' ],
206
+ wants_user_ap = self .cleaned_data ['become_user_ap' ],
207
+ wants_vpn = self .cleaned_data ['provide_vpn' ],
149
208
)
150
209
self .attachment_conf_form_set .save (self .instance )
151
210
return self .instance
0 commit comments