<s> from tastypie import fields <EOL> from tastypie . authorization import DjangoAuthorization <EOL> from django . core . exceptions import ValidationError <EOL> from tastypie . authentication import Authentication <EOL> from tastypie . resources import ModelResource <EOL> from tastypie . resources import ALL , ALL_WITH_RELATIONS <EOL> import systems . models as system_model <EOL> from django . conf . urls . defaults import url <EOL> from tastypie . serializers import Serializer <EOL> from django . core . serializers import json as djson <EOL> from tastytools . test . resources import ResourceTestData <EOL> from tastypie . authorization import Authorization <EOL> from core . interface . static_intr . models import StaticInterface <EOL> from core . interface . static_intr . models import StaticIntrKeyValue <EOL> from core . range . models import Range <EOL> from mozdns . view . models import View <EOL> from mozdns . domain . models import Domain <EOL> from core . lib . utils import create_ipv4_intr_from_domain <EOL> import json <EOL> import re <EOL> class PrettyJSONSerializer ( Serializer ) : <EOL> json_indent = <NUM_LIT:2> <EOL> def to_json ( self , data , options = None ) : <EOL> options = options or { } <EOL> data = self . to_simple ( data , options ) <EOL> return json . dumps ( data , cls = djson . DjangoJSONEncoder , <EOL> sort_keys = True , ensure_ascii = False , indent = self . json_indent ) <EOL> class CustomAPIResource ( ModelResource ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( CustomAPIResource , self ) . __init__ ( * args , ** kwargs ) <EOL> def determine_format ( self , request ) : <EOL> format = request . GET . get ( '<STR_LIT>' ) <EOL> if format : <EOL> return super ( CustomAPIResource , self ) . determine_format ( request ) <EOL> else : <EOL> return "<STR_LIT:application/json>" <EOL> class Meta : <EOL> serializer = PrettyJSONSerializer ( ) <EOL> authorization = Authorization ( ) <EOL> authentication = Authentication ( ) <EOL> allowed_methods = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class SystemResource ( CustomAPIResource ) : <EOL> key_value = fields . ToManyField ( '<STR_LIT>' , '<STR_LIT>' , full = True , null = True ) <EOL> server_model = fields . ForeignKey ( '<STR_LIT>' , '<STR_LIT>' , null = True , full = True ) <EOL> system_status = fields . ForeignKey ( '<STR_LIT>' , '<STR_LIT>' , null = True , full = True ) <EOL> operating_system = fields . ForeignKey ( '<STR_LIT>' , '<STR_LIT>' , null = True , full = True ) <EOL> system_rack = fields . ForeignKey ( '<STR_LIT>' , '<STR_LIT>' , null = True , full = True ) <EOL> allocation = fields . ForeignKey ( '<STR_LIT>' , '<STR_LIT>' , null = True , full = True ) <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( SystemResource , self ) . __init__ ( * args , ** kwargs ) <EOL> def get_schema ( self , request , ** kwargs ) : <EOL> ret = super ( SystemResource , self ) . get_schema ( request , ** kwargs ) <EOL> retjson = json . loads ( ret . content ) <EOL> retjson [ '<STR_LIT>' ] [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:default>' : False , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> retjson [ '<STR_LIT>' ] [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:default>' : False , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> retjson [ '<STR_LIT>' ] [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:default>' : False , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> retjson [ '<STR_LIT>' ] [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:default>' : False , <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> retjson [ '<STR_LIT>' ] [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:default>' : False , <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> retjson [ '<STR_LIT>' ] [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:default>' : False , <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> ret . content = json . dumps ( retjson ) <EOL> return ret <EOL> @ staticmethod <EOL> def extract_nic_attrs ( nic_name ) : <EOL> nic_type , primary , alias = "<STR_LIT>" , <NUM_LIT:0> , <NUM_LIT:0> <EOL> m = re . search ( "<STR_LIT>" , nic_name ) <EOL> if m : <EOL> nic_type = m . group ( <NUM_LIT:1> ) if m . group ( <NUM_LIT:1> ) else "<STR_LIT>" <EOL> primary = m . group ( <NUM_LIT:2> ) if m . group ( <NUM_LIT:2> ) else "<STR_LIT:0>" <EOL> nic_alias = m . group ( <NUM_LIT:3> ) if m . group ( <NUM_LIT:3> ) else "<STR_LIT:0>" <EOL> return nic_type , primary , nic_alias <EOL> else : <EOL> raise ValidationError ( "<STR_LIT>" ) <EOL> def process_extra ( self , bundle , request , ** kwargs ) : <EOL> patch_dict = json . loads ( request . POST . items ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> if patch_dict . has_key ( '<STR_LIT>' ) and patch_dict . has_key ( '<STR_LIT>' ) : <EOL> patch_dict . pop ( '<STR_LIT>' ) <EOL> sys = bundle . obj <EOL> interface = patch_dict . pop ( '<STR_LIT>' , None ) <EOL> if sys and interface : <EOL> sys . delete_adapter ( interface ) <EOL> if patch_dict . has_key ( '<STR_LIT>' ) and patch_dict . has_key ( '<STR_LIT>' ) : <EOL> patch_dict . pop ( '<STR_LIT>' ) <EOL> sys = bundle . obj <EOL> if '<STR_LIT>' in patch_dict : <EOL> sys . update_adapter ( ** patch_dict ) <EOL> if patch_dict . has_key ( '<STR_LIT>' ) : <EOL> enable_dns = True <EOL> enable_private = True <EOL> enable_public = False <EOL> sys = bundle . obj <EOL> range = patch_dict . pop ( '<STR_LIT>' , None ) <EOL> fqdn = patch_dict . pop ( '<STR_LIT>' , None ) <EOL> ip_str = patch_dict . pop ( '<STR_LIT>' , None ) <EOL> interface = patch_dict . pop ( '<STR_LIT>' , None ) <EOL> domain = patch_dict . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> from core . lib . utils import create_ipv4_intr_from_range <EOL> mac = patch_dict [ '<STR_LIT>' ] <EOL> if interface : <EOL> interface_type , primary , alias = SystemResource . extract_nic_attrs ( interface ) <EOL> else : <EOL> interface_type , primary , alias = sys . get_next_adapter ( ) <EOL> if not fqdn : <EOL> domain_parsed = "<STR_LIT:.>" . join ( sys . hostname . split ( '<STR_LIT:.>' ) [ <NUM_LIT:1> : ] ) + '<STR_LIT:.>' + domain <EOL> domain_name = domain_parsed . lower ( ) <EOL> label = sys . hostname . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> else : <EOL> domain_parsed = "<STR_LIT:.>" . join ( fqdn . split ( '<STR_LIT:.>' ) [ <NUM_LIT:1> : ] ) <EOL> domain_name = domain_parsed . lower ( ) <EOL> label = fqdn . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> if range : <EOL> range_start_str = range . split ( '<STR_LIT:U+002C>' ) [ <NUM_LIT:0> ] <EOL> range_end_str = range . split ( '<STR_LIT:U+002C>' ) [ <NUM_LIT:1> ] <EOL> s , errors = create_ipv4_intr_from_range ( label , domain_name , sys , mac , range_start_str , range_end_str ) <EOL> if ip_str : <EOL> domain = Domain . objects . filter ( name = domain_parsed ) [ <NUM_LIT:0> ] <EOL> s = StaticInterface ( label = label , mac = mac , domain = domain , ip_str = ip_str , ip_type = '<STR_LIT:4>' , system = sys ) <EOL> try : <EOL> if s : <EOL> s . save ( ) <EOL> s . update_attrs ( ) <EOL> if enable_dns and enable_public : <EOL> private = View . objects . get ( name = '<STR_LIT>' ) <EOL> s . views . add ( private ) <EOL> s . save ( ) <EOL> elif enable_dns and enable_private and not enable_public : <EOL> private = View . objects . get ( name = '<STR_LIT>' ) <EOL> s . views . add ( private ) <EOL> s . save ( ) <EOL> if interface : <EOL> interface_type , primary , alias = SystemResource . extract_nic_attrs ( interface ) <EOL> else : <EOL> interface_type , primary , alias = sys . get_next_adapter ( ) <EOL> s . attrs . primary = primary <EOL> s . attrs . interface_type = interface_type <EOL> s . attrs . alias = alias <EOL> else : <EOL> print '<STR_LIT>' <EOL> bundle . errors [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> raise ValidationError ( join ( e . messages ) ) <EOL> except ValidationError , e : <EOL> bundle . errors [ '<STR_LIT>' ] = "<STR_LIT:U+0020>" . join ( e . messages ) <EOL> raise ValidationError ( join ( e . messages ) ) <EOL> except Exception , e : <EOL> print e <EOL> def obj_create ( self , bundle , request , ** kwargs ) : <EOL> ret_bundle = super ( SystemResource , self ) . obj_create ( bundle , request , ** kwargs ) <EOL> self . process_extra ( ret_bundle , request , ** kwargs ) <EOL> def obj_update ( self , bundle , request , ** kwargs ) : <EOL> for intr in bundle . obj . staticinterface_set . all ( ) : <EOL> intr . update_attrs ( ) <EOL> ret_bundle = super ( SystemResource , self ) . obj_update ( bundle , request , ** kwargs ) <EOL> ret_bundle = self . process_extra ( ret_bundle , request , ** kwargs ) <EOL> return ret_bundle <EOL> def full_dehydrate ( self , bundle ) : <EOL> """<STR_LIT>""" <EOL> super ( SystemResource , self ) . full_dehydrate ( bundle ) <EOL> for intr in bundle . obj . staticinterface_set . all ( ) : <EOL> intr . update_attrs ( ) <EOL> if hasattr ( intr , '<STR_LIT>' ) : <EOL> if hasattr ( intr . attrs , '<STR_LIT>' ) : <EOL> bundle . data [ '<STR_LIT>' % ( intr . attrs . interface_type , intr . attrs . primary , intr . attrs . alias ) ] = intr . ip_str <EOL> bundle . data [ '<STR_LIT>' % ( intr . attrs . interface_type , intr . attrs . primary , intr . attrs . alias ) ] = intr . fqdn <EOL> bundle . data [ '<STR_LIT>' % ( intr . attrs . interface_type , intr . attrs . primary , intr . attrs . alias ) ] = intr . mac <EOL> bundle . data [ '<STR_LIT>' % ( intr . attrs . interface_type , intr . attrs . primary , intr . attrs . alias ) ] = intr . dns_enabled <EOL> bundle . data [ '<STR_LIT>' % ( intr . attrs . interface_type , intr . attrs . primary , intr . attrs . alias ) ] = intr . dhcp_enabled <EOL> bundle . data [ '<STR_LIT>' % ( intr . attrs . interface_type , intr . attrs . primary , intr . attrs . alias ) ] = intr . label <EOL> return bundle <EOL> def prepend_urls ( self ) : <EOL> return [ <EOL> url ( r"<STR_LIT>" % self . _meta . resource_name , self . wrap_view ( '<STR_LIT>' ) , name = "<STR_LIT>" ) , <EOL> url ( r"<STR_LIT>" % self . _meta . resource_name , self . wrap_view ( '<STR_LIT>' ) , name = "<STR_LIT>" ) , <EOL> ] <EOL> class Meta ( CustomAPIResource . Meta ) : <EOL> filtering = { <EOL> '<STR_LIT>' : ALL_WITH_RELATIONS , <EOL> '<STR_LIT>' : ALL_WITH_RELATIONS , <EOL> '<STR_LIT>' : ALL_WITH_RELATIONS , <EOL> '<STR_LIT>' : ALL , <EOL> '<STR_LIT>' : ALL , <EOL> '<STR_LIT>' : ALL_WITH_RELATIONS , <EOL> '<STR_LIT>' : ALL_WITH_RELATIONS , <EOL> '<STR_LIT>' : ALL_WITH_RELATIONS , <EOL> } <EOL> fields = [ <EOL> ] <EOL> exclude = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> resource_name = '<STR_LIT>' <EOL> queryset = system_model . System . objects . all ( ) <EOL> class ServerModelResource ( CustomAPIResource ) : <EOL> class Meta ( CustomAPIResource . Meta ) : <EOL> filtering = { <EOL> '<STR_LIT:name>' : ALL , <EOL> '<STR_LIT>' : ALL , <EOL> '<STR_LIT>' : ALL <EOL> } <EOL> serializer = PrettyJSONSerializer ( ) <EOL> resource_name = '<STR_LIT>' <EOL> queryset = system_model . ServerModel . objects . all ( ) <EOL> class AllocationResource ( CustomAPIResource ) : <EOL> class Meta ( CustomAPIResource . Meta ) : <EOL> filtering = { <EOL> '<STR_LIT:name>' : ALL , <EOL> } <EOL> resource_name = '<STR_LIT>' <EOL> queryset = system_model . Allocation . objects . all ( ) <EOL> class LocationResource ( CustomAPIResource ) : <EOL> class Meta ( CustomAPIResource . Meta ) : <EOL> resource_name = '<STR_LIT:location>' <EOL> queryset = system_model . Location . objects . all ( ) <EOL> class SystemRackResource ( CustomAPIResource ) : <EOL> class Meta ( CustomAPIResource . Meta ) : <EOL> resource_name = '<STR_LIT>' <EOL> queryset = system_model . SystemRack . objects . all ( ) <EOL> filtering = { <EOL> '<STR_LIT:name>' : ALL_WITH_RELATIONS , <EOL> '<STR_LIT:id>' : ALL_WITH_RELATIONS , <EOL> } <EOL> class AdvisoryDataResource ( CustomAPIResource ) : <EOL> class Meta ( CustomAPIResource . Meta ) : <EOL> resource_name = '<STR_LIT>' <EOL> queryset = system_model . AdvisoryData . objects . all ( ) <EOL> filtering = { <EOL> '<STR_LIT>' : ALL_WITH_RELATIONS , <EOL> '<STR_LIT:title>' : ALL_WITH_RELATIONS , <EOL> '<STR_LIT>' : ALL_WITH_RELATIONS , <EOL> '<STR_LIT>' : ALL_WITH_RELATIONS , <EOL> '<STR_LIT>' : ALL_WITH_RELATIONS , <EOL> } <EOL> class PortDataResource ( CustomAPIResource ) : <EOL> class Meta ( CustomAPIResource . Meta ) : <EOL> resource_name = '<STR_LIT>' <EOL> queryset = system_model . PortData . objects . all ( ) <EOL> filtering = { <EOL> '<STR_LIT>' : ALL_WITH_RELATIONS , <EOL> '<STR_LIT:state>' : ALL_WITH_RELATIONS , <EOL> '<STR_LIT>' : ALL_WITH_RELATIONS , <EOL> '<STR_LIT:port>' : ALL_WITH_RELATIONS , <EOL> } <EOL> class SystemStatusResource ( CustomAPIResource ) : <EOL> class Meta ( CustomAPIResource . Meta ) : <EOL> resource_name = '<STR_LIT>' <EOL> queryset = system_model . SystemStatus . objects . all ( ) <EOL> filtering = { <EOL> '<STR_LIT:status>' : ALL_WITH_RELATIONS , <EOL> } <EOL> class StaticInterfaceResource ( CustomAPIResource ) : <EOL> system = fields . ToOneField ( SystemResource , '<STR_LIT>' , full = True ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( StaticInterfaceResource , self ) . __init__ ( * args , ** kwargs ) <EOL> def full_dehydrate ( self , bundle ) : <EOL> super ( StaticInterfaceResource , self ) . full_dehydrate ( bundle ) <EOL> bundle . obj . update_attrs ( ) <EOL> bundle . data [ '<STR_LIT>' ] = "<STR_LIT>" % ( bundle . obj . attrs . interface_type , <EOL> bundle . obj . attrs . primary , bundle . obj . attrs . alias ) <EOL> del bundle . data [ '<STR_LIT>' ] <EOL> del bundle . data [ '<STR_LIT>' ] <EOL> del bundle . data [ '<STR_LIT>' ] <EOL> return bundle <EOL> class Meta ( CustomAPIResource . Meta ) : <EOL> resource_name = '<STR_LIT>' <EOL> queryset = StaticInterface . objects . select_related ( ) . all ( ) <EOL> class OperatingSystemResource ( CustomAPIResource ) : <EOL> class Meta ( CustomAPIResource . Meta ) : <EOL> resource_name = '<STR_LIT>' <EOL> queryset = system_model . OperatingSystem . objects . all ( ) <EOL> filtering = { <EOL> '<STR_LIT:version>' : ALL_WITH_RELATIONS , <EOL> '<STR_LIT:name>' : ALL_WITH_RELATIONS , <EOL> } <EOL> class OperatingSystemData ( CustomAPIResource ) : <EOL> resource = "<STR_LIT>" <EOL> def get_data ( self , data ) : <EOL> data . set ( '<STR_LIT:id>' , '<STR_LIT>' ) <EOL> data . set ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> data . set ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> data . set ( '<STR_LIT:version>' , '<STR_LIT>' ) <EOL> return data <EOL> class KeyValueResource ( CustomAPIResource ) : <EOL> system = fields . ToOneField ( '<STR_LIT>' , '<STR_LIT>' , full = False ) <EOL> class Meta ( CustomAPIResource . Meta ) : <EOL> filtering = { <EOL> '<STR_LIT>' : ALL_WITH_RELATIONS , <EOL> '<STR_LIT:key>' : ALL_WITH_RELATIONS , <EOL> '<STR_LIT:value>' : ALL_WITH_RELATIONS , <EOL> } <EOL> resource_name = '<STR_LIT>' <EOL> queryset = system_model . KeyValue . objects . all ( ) </s>
