2016-01-17 20:12:34 +03:00
|
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
|
|
import socket
|
2016-04-02 00:42:22 +03:00
|
|
|
|
|
|
|
|
|
from SWSCloudCore import models
|
2015-12-14 01:49:38 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ControllerManageIPs:
|
2016-01-17 20:12:34 +03:00
|
|
|
|
def item_create(self, datacenter_id, server_id, ipv4, ipv4_gateway, ipv6, ipv6_gateway, status):
|
|
|
|
|
"""
|
|
|
|
|
Запись данных об IP
|
|
|
|
|
:param datacenter_id:
|
|
|
|
|
:param server_id:
|
|
|
|
|
:param ipv4:
|
|
|
|
|
:param ipv4_gateway:
|
|
|
|
|
:param ipv6:
|
|
|
|
|
:param ipv6_gateway:
|
|
|
|
|
:param status:
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
models.Ips.create(
|
|
|
|
|
server=server_id,
|
|
|
|
|
datacenter=datacenter_id,
|
|
|
|
|
ipv4=ipv4,
|
|
|
|
|
ipv4_gateway=ipv4_gateway,
|
|
|
|
|
ipv6=ipv6,
|
|
|
|
|
ipv6_gateway=ipv6_gateway,
|
|
|
|
|
status=status
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def item_update(self, ip_id, server_id, ipv4, ipv4_gateway, ipv6, ipv6_gateway, status):
|
|
|
|
|
# def update(self, user_id, **kwargs):
|
|
|
|
|
x = models.Ips.update(
|
|
|
|
|
server=server_id,
|
|
|
|
|
ipv4=ipv4,
|
|
|
|
|
ipv4_gateway=ipv4_gateway,
|
|
|
|
|
ipv6=ipv6,
|
|
|
|
|
ipv6_gateway=ipv6_gateway,
|
|
|
|
|
status=status
|
|
|
|
|
).where(
|
|
|
|
|
models.Ips.id == ip_id
|
|
|
|
|
)
|
|
|
|
|
x.execute()
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
2015-12-14 01:49:38 +03:00
|
|
|
|
def items_get(self):
|
|
|
|
|
return {
|
|
|
|
|
'total': models.Ips.select().count(),
|
|
|
|
|
'items': models.Ips.select()
|
|
|
|
|
}
|
2016-01-17 20:12:34 +03:00
|
|
|
|
|
|
|
|
|
def item_get(self, ip_id):
|
|
|
|
|
return models.Ips.select().where(models.Ips.id == ip_id)[0]
|
|
|
|
|
|
|
|
|
|
def check_ipv4_exists(self, ipv4):
|
|
|
|
|
if models.Ips.select().where(models.Ips.ipv4 == ipv4).count() == 0:
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def check_ipv6_exists(self, ipv6):
|
|
|
|
|
if models.Ips.select().where(models.Ips.ipv4 == ipv6).count() > 0:
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def is_valid_ipv4_address(self, address):
|
|
|
|
|
# from: http://stackoverflow.com/questions/319279/how-to-validate-ip-address-in-python
|
|
|
|
|
try:
|
|
|
|
|
socket.inet_pton(socket.AF_INET, address)
|
|
|
|
|
except AttributeError: # no inet_pton here, sorry
|
|
|
|
|
try:
|
|
|
|
|
socket.inet_aton(address)
|
|
|
|
|
except socket.error:
|
|
|
|
|
return False
|
|
|
|
|
return address.count('.') == 3
|
|
|
|
|
except socket.error: # not a valid address
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def is_valid_ipv6_address(self, address):
|
|
|
|
|
# from: http://stackoverflow.com/questions/319279/how-to-validate-ip-address-in-python
|
|
|
|
|
try:
|
|
|
|
|
socket.inet_pton(socket.AF_INET6, address)
|
|
|
|
|
except socket.error: # not a valid address
|
|
|
|
|
return False
|
|
|
|
|
return True
|