@ -13,18 +13,159 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import logging
from django . utils . translation import ugettext_lazy as _
from horizon import exceptions
from horizon import forms
from horizon import messages
from pytz import timezone
from blazar_dashboard import api
LOG = logging . getLogger ( __name__ )
class CreateForm ( forms . SelfHandlingForm ) :
# General fields
name = forms . CharField (
label = _ ( " Lease Name " ) ,
required = True ,
max_length = 80
)
start_date = forms . DateTimeField (
label = _ ( " Start Date " ) ,
required = False ,
help_text = _ ( ' Enter YYYY-MM-DD HH:MM or blank for now ' ) ,
input_formats = [ ' % Y- % m- %d % H: % M ' ] ,
widget = forms . DateTimeInput (
attrs = { ' placeholder ' : ' YYYY-MM-DD HH:MM (blank for now) ' } )
)
end_date = forms . DateTimeField (
label = _ ( " End Date " ) ,
required = False ,
help_text = _ ( ' Enter YYYY-MM-DD HH:MM or blank for Start Date + 24h ' ) ,
input_formats = [ ' % Y- % m- %d % H: % M ' ] ,
widget = forms . DateTimeInput (
attrs = { ' placeholder ' : ' YYYY-MM-DD HH:MM (blank for Start Date + '
' 24h) ' } )
)
resource_type = forms . ChoiceField (
label = _ ( " Resource Type " ) ,
required = True ,
choices = (
( ' host ' , _ ( ' Physical Host ' ) ) ,
( ' instance ' , _ ( ' Virtual Instance (Not yet supported in GUI) ' ) )
) ,
widget = forms . ThemableSelectWidget ( attrs = {
' class ' : ' switchable ' ,
' data-slug ' : ' source ' } ) )
# Fields for host reservation
min_hosts = forms . IntegerField (
label = _ ( ' Minimum Number of Hosts ' ) ,
required = False ,
help_text = _ ( ' Enter the minimum number of hosts to reserve. ' ) ,
min_value = 1 ,
initial = 1 ,
widget = forms . NumberInput ( attrs = {
' class ' : ' switched ' ,
' data-switch-on ' : ' source ' ,
' data-source-host ' : _ ( ' Minimum Number of Hosts ' ) } )
)
max_hosts = forms . IntegerField (
label = _ ( ' Maximum Number of Hosts ' ) ,
required = False ,
help_text = _ ( ' Enter the maximum number of hosts to reserve. ' ) ,
min_value = 1 ,
initial = 1 ,
widget = forms . NumberInput ( attrs = {
' class ' : ' switched ' ,
' data-switch-on ' : ' source ' ,
' data-source-host ' : _ ( ' Maximum Number of Hosts ' ) } )
)
hypervisor_properties = forms . CharField (
label = _ ( " Hypervisor Properties " ) ,
required = False ,
help_text = _ ( ' Enter properties of a hypervisor to reserve. ' ) ,
max_length = 255 ,
widget = forms . TextInput ( attrs = {
' class ' : ' switched ' ,
' data-switch-on ' : ' source ' ,
' data-source-host ' : _ ( ' Hypervisor Properties ' ) ,
' placeholder ' : ' e.g. [ " >= " , " $vcpus " , " 2 " ] ' } )
)
resource_properties = forms . CharField (
label = _ ( " Resource Properties " ) ,
required = False ,
help_text = _ ( ' Enter properties of a resource to reserve. ' ) ,
max_length = 255 ,
widget = forms . TextInput ( attrs = {
' class ' : ' switched ' ,
' data-switch-on ' : ' source ' ,
' data-source-host ' : _ ( ' Resource Properties ' ) ,
' placeholder ' : ' e.g. [ " == " , " $extra_key " , " extra_value " ] ' } )
)
def handle ( self , request , data ) :
if data [ ' resource_type ' ] == ' host ' :
reservations = [
{
' resource_type ' : ' physical:host ' ,
' min ' : data [ ' min_hosts ' ] ,
' max ' : data [ ' max_hosts ' ] ,
' hypervisor_properties ' : ( data [ ' hypervisor_properties ' ]
or ' ' ) ,
' resource_properties ' : data [ ' resource_properties ' ] or ' '
}
]
elif data [ ' resource_type ' ] == ' instance ' :
raise forms . ValidationError ( ' Virtual instance is not yet '
' supported in GUI ' )
events = [ ]
try :
api . client . lease_create (
request , data [ ' name ' ] ,
data [ ' start_date ' ] . strftime ( ' % Y- % m- %d % H: % M ' ) ,
data [ ' end_date ' ] . strftime ( ' % Y- % m- %d % H: % M ' ) ,
reservations , events )
messages . success ( request , _ ( ' Lease %s was successfully '
' created. ' ) % data [ ' name ' ] )
return True
except Exception as e :
LOG . error ( ' Error submitting lease: %s ' , e )
exceptions . handle ( request ,
message = ' An error occurred while creating this '
' lease: %s . Please try again. ' % e )
def clean ( self ) :
cleaned_data = super ( CreateForm , self ) . clean ( )
local = timezone ( self . request . session . get (
' django_timezone ' ,
self . request . COOKIES . get ( ' django_timezone ' , ' UTC ' ) ) )
if cleaned_data [ ' start_date ' ] :
cleaned_data [ ' start_date ' ] = local . localize (
cleaned_data [ ' start_date ' ] . replace ( tzinfo = None )
) . astimezone ( timezone ( ' UTC ' ) )
else :
cleaned_data [ ' start_date ' ] = datetime . datetime . utcnow ( )
if cleaned_data [ ' end_date ' ] :
cleaned_data [ ' end_date ' ] = local . localize (
cleaned_data [ ' end_date ' ] . replace ( tzinfo = None )
) . astimezone ( timezone ( ' UTC ' ) )
else :
cleaned_data [ ' end_date ' ] = ( cleaned_data [ ' start_date ' ]
+ datetime . timedelta ( days = 1 ) )
if cleaned_data [ ' resource_type ' ] == ' instance ' :
raise forms . ValidationError ( ' Resource type " virtual instance " is '
' not yet supported in GUI ' )
class UpdateForm ( forms . SelfHandlingForm ) :
class Meta ( object ) :