Example #1
0
 def post(self, request, *args, **kwargs):
     from ralph.urls import LATEST_API
     actor = User.objects.get(
         username=ApiKeyAuthentication().get_identifier(request)
     )
     if not actor.has_perm('create_devices'):
         raise HttpResponse(_('You cannot create new devices'), status=401)
     data = Serializer().deserialize(
         request.body,
         format=request.META.get('CONTENT_TYPE', 'application/json')
     )
     mac = MACAddressField.normalize(data['mac'])
     parent = self.get_parent_device(data)
     ip_addr = get_first_free_ip(data['network'])
     hostname = self.get_hostname(parent)
     venture = get_object_or_404(
         Venture,
         symbol=data['venture']
     )
     role = get_object_or_404(
         VentureRole,
         venture=venture,
         name=data['venture-role']
     )
     ethernets = [Eth(
         'DEPLOYMENT MAC',
         mac,
         EthernetSpeed.unknown,
     )]
     device = Device.create(
         ethernets=ethernets,
         model_type=DeviceType.unknown,
         model_name='Unknown',
         verified=True,
     )
     device.name = hostname
     device.parent = parent
     device.venture = venture
     device.venture_role = role
     device.save()
     IPAddress.objects.create(
         address=ip_addr,
         device=device,
         hostname=hostname,
     )
     reset_dns(hostname, ip_addr)
     reset_dhcp(ip_addr, data['mac'])
     resp = HttpResponse('', status=201)
     resp['Location'] = LATEST_API.canonical_resource_for(
         'dev'
     ).get_resource_uri(device)
     return resp
Example #2
0
def dhcp(deployment_id):
    deployment = Deployment.objects.get(id=deployment_id)
    try:
        entry = DHCPEntry.objects.get(ip=deployment.ip, mac=deployment.mac)
    except DHCPEntry.DoesNotExist:
        reset_dhcp(deployment.ip, deployment.mac)
    else:
        # Check that all DHCP servers already have this entry.
        servers = DHCPServer.objects.all()
        if not servers.count():
            return False
        for server in servers:
            if entry.created > server.last_synchronized:
                return False
        return True
    return False
Example #3
0
 def post(self, request, *args, **kwargs):
     from ralph.urls import LATEST_API
     actor = User.objects.get(
         username=ApiKeyAuthentication().get_identifier(request))
     if not actor.has_perm('create_devices'):
         raise HttpResponse(_('You cannot create new devices'), status=401)
     data = Serializer().deserialize(request.body,
                                     format=request.META.get(
                                         'CONTENT_TYPE',
                                         'application/json'))
     mac = MACAddressField.normalize(data['mac'])
     parent = self.get_parent_device(data)
     ip_addr = get_first_free_ip(data['network'])
     hostname = self.get_hostname(parent)
     venture = get_object_or_404(Venture, symbol=data['venture'])
     role = get_object_or_404(VentureRole,
                              venture=venture,
                              name=data['venture-role'])
     ethernets = [Eth(
         'DEPLOYMENT MAC',
         mac,
         EthernetSpeed.unknown,
     )]
     device = Device.create(
         ethernets=ethernets,
         model_type=DeviceType.unknown,
         model_name='Unknown',
         verified=True,
     )
     device.name = hostname
     device.parent = parent
     device.venture = venture
     device.venture_role = role
     device.save()
     IPAddress.objects.create(
         address=ip_addr,
         device=device,
         hostname=hostname,
     )
     reset_dns(hostname, ip_addr)
     reset_dhcp(ip_addr, data['mac'])
     resp = HttpResponse('', status=201)
     resp['Location'] = LATEST_API.canonical_resource_for(
         'dev').get_resource_uri(device)
     return resp
Example #4
0
 def post(self, request, *args, **kwargs):
     from ralph.urls import LATEST_API
     try:
         actor = User.objects.get(
             username=ApiKeyAuthentication().get_identifier(request)
         )
     except User.DoesNotExist:
         actor = None
     if not (actor and actor.profile.has_perm(Perm.has_core_access)):
         return HttpResponse(
             unicode(_('You cannot create new devices')),
             status=401
         )
     data = Serializer().deserialize(
         request.body,
         format=request.META.get('CONTENT_TYPE', 'application/json')
     )
     missing_keys = {
         'mac',
         'network',
         'management-ip',
         'venture',
         'venture-role'
     } - set(data.keys())
     if missing_keys:
         return HttpResponse(
             _('Missing data: {}').format(', '.join(missing_keys)),
             status=400,
         )
     mac = MACAddressField.normalize(data['mac'])
     existing_mac = DHCPEntry.objects.filter(mac=mac).count()
     if existing_mac:
         return HttpResponse(unicode(_('MAC already exists')), status=400)
     parent = self.get_parent_device(data)
     try:
         ip_addr = get_first_free_ip(data['network'])
     except Network.DoesNotExist:
         raise Http404('No network with such name exists')
     hostname = self.get_hostname(parent)
     venture = get_object_or_404(
         Venture,
         symbol=data['venture']
     )
     role_name = data.get('venture-role')
     role = role_name and get_object_or_404(
         VentureRole,
         venture=venture,
         name=role_name,
     )
     ethernets = [Eth(
         'DEPLOYMENT MAC',
         mac,
         EthernetSpeed.unknown,
     )]
     device = Device.create(
         ethernets=ethernets,
         model_type=DeviceType.unknown,
         model_name='Unknown',
         verified=True,
     )
     device.name = hostname
     device.parent = parent
     device.venture = venture
     device.venture_role = role
     device.rack = parent.rack
     device.dc = parent.dc
     device.save()
     IPAddress.objects.create(
         address=ip_addr,
         device=device,
         hostname=hostname,
     )
     reset_dns(hostname, ip_addr)
     reset_dhcp(ip_addr, data['mac'])
     resp = HttpResponse('', status=201)
     resp['Location'] = LATEST_API.canonical_resource_for(
         'dev'
     ).get_resource_uri(device)
     return resp
Example #5
0
 def post(self, request, *args, **kwargs):
     from ralph.urls import LATEST_API
     try:
         actor = User.objects.get(
             username=ApiKeyAuthentication().get_identifier(request))
     except User.DoesNotExist:
         actor = None
     if not (actor and actor.has_perm('create_devices')):
         return HttpResponse(unicode(_('You cannot create new devices')),
                             status=401)
     data = Serializer().deserialize(request.body,
                                     format=request.META.get(
                                         'CONTENT_TYPE',
                                         'application/json'))
     missing_keys = {
         'mac', 'network', 'management-ip', 'venture', 'venture-role'
     } - set(data.keys())
     if missing_keys:
         return HttpResponse(
             _('Missing data: {}').format(', '.join(missing_keys)),
             status=400,
         )
     mac = MACAddressField.normalize(data['mac'])
     existing_mac = DHCPEntry.objects.filter(mac=mac).count()
     if existing_mac:
         return HttpResponse(unicode(_('MAC already exists')), status=400)
     parent = self.get_parent_device(data)
     try:
         ip_addr = get_first_free_ip(data['network'])
     except Network.DoesNotExist:
         raise Http404('No network with such name exists')
     hostname = self.get_hostname(parent)
     venture = get_object_or_404(Venture, symbol=data['venture'])
     role_name = data.get('venture-role')
     role = role_name and get_object_or_404(
         VentureRole,
         venture=venture,
         name=role_name,
     )
     ethernets = [Eth(
         'DEPLOYMENT MAC',
         mac,
         EthernetSpeed.unknown,
     )]
     device = Device.create(
         ethernets=ethernets,
         model_type=DeviceType.unknown,
         model_name='Unknown',
         verified=True,
     )
     device.name = hostname
     device.parent = parent
     device.venture = venture
     device.venture_role = role
     device.rack = parent.rack
     device.dc = parent.dc
     device.save()
     IPAddress.objects.create(
         address=ip_addr,
         device=device,
         hostname=hostname,
     )
     reset_dns(hostname, ip_addr)
     reset_dhcp(ip_addr, data['mac'])
     resp = HttpResponse('', status=201)
     resp['Location'] = LATEST_API.canonical_resource_for(
         'dev').get_resource_uri(device)
     return resp