Skip to content

Commit

Permalink
Improve RADL code to remove references to Config class
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Dec 18, 2015
1 parent 402226d commit 084f6ef
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 26 deletions.
16 changes: 2 additions & 14 deletions IM/radl/radl.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from netaddr import IPNetwork, IPAddress
import copy
from distutils.version import LooseVersion
from IM.config import Config

def UnitToValue(unit):
"""Return the value of an unit."""
Expand Down Expand Up @@ -502,7 +500,7 @@ def check_num(self, checks, radl):
return prefixes

class Aspect:
"""A network, system, deploy, configure or contextualize element in a RADL."""
"""A network, ansible, system, deploy, configure or contextualize element in a RADL."""

def getId(self):
"""Return the id of the aspect."""
Expand Down Expand Up @@ -686,17 +684,7 @@ def __init__(self, name, features=None, reference=False, line=None):
self.reference = reference
"""True if it is only a reference and it isn't a definition."""
Features.__init__(self, features)
self.line = line

@staticmethod
def isPrivateIP(ip):
"""
Check if an IP address is private
"""
for mask in Config.PRIVATE_NET_MASKS:
if IPAddress(ip) in IPNetwork(mask):
return True
return False
self.line = line

def getId(self):
return self.id
Expand Down
2 changes: 0 additions & 2 deletions IM/radl/radl_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,6 @@ def p_string_list(self, t):
t[0] = []

def p_error(self, t):
if t is None:
a = 1
raise RADLParseException("Parse error in: " + str(t), line=t.lineno if t else None)

def parse(self, data):
Expand Down
7 changes: 4 additions & 3 deletions connectors/LibVirt.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
from IM.VirtualMachine import VirtualMachine
from CloudConnector import CloudConnector
from IM.radl.radl import Feature
from IM.radl.radl import network as radl_network
from netaddr import IPNetwork, IPAddress
from IM.config import Config

# clases para parsear el resultado de las llamadas a virsh
class forward(XMLObject):
Expand Down Expand Up @@ -167,7 +168,7 @@ def getNetwork(self, outbound, auth_data):
net_priv = None
net_pub = None
for net in res:
if radl_network.isPrivateIP(net.ip.address):
if any([IPAddress(net.ip.address) in IPNetwork(mask) for mask in Config.PRIVATE_NET_MASKS]):
# Red privada
net_priv = net
else:
Expand Down Expand Up @@ -359,7 +360,7 @@ def setIPs(self, vm, domain, auth_data):
# Tenemos que hacer esto https://rwmj.wordpress.com/2010/10/26/tip-find-the-ip-address-of-a-virtual-machine/
for interface in domain.devices.interface:
ip = self.getIPfromMAC(interface.mac.address, auth_data)
if network.isPrivateIP(ip):
if any([IPAddress(ip) in IPNetwork(mask) for mask in Config.PRIVATE_NET_MASKS]):
private_ips.append(ip)
else:
public_ips.append(ip)
Expand Down
9 changes: 5 additions & 4 deletions connectors/OCCI.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
from IM.uriparse import uriparse
from IM.VirtualMachine import VirtualMachine
from CloudConnector import CloudConnector
from IM.radl.radl import Feature, network

from IM.radl.radl import Feature
from netaddr import IPNetwork, IPAddress
from IM.config import Config

class OCCICloudConnector(CloudConnector):
"""
Expand Down Expand Up @@ -189,8 +190,8 @@ def get_net_info(self, occi_res):
for part in parts:
kv = part.split('=')
if kv[0].strip() == "occi.networkinterface.address":
ip_address = kv[1].strip('"')
is_private = network.isPrivateIP(ip_address)
ip_address = kv[1].strip('"')
is_private = any([IPAddress(ip_address) in IPNetwork(mask) for mask in Config.PRIVATE_NET_MASKS])
elif kv[0].strip() == "occi.networkinterface.interface":
net_interface = kv[1].strip('"')
num_interface = re.findall('\d+', net_interface)[0]
Expand Down
8 changes: 5 additions & 3 deletions connectors/OpenNebula.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
from CloudConnector import CloudConnector
from IM.radl.radl import network, Feature
from IM.config import ConfigOpenNebula
from netaddr import IPNetwork, IPAddress
from IM.config import Config

# Set of classes to parse the XML results of the ONE API
class NIC(XMLObject):
Expand Down Expand Up @@ -203,7 +205,7 @@ def setIPsFromTemplate(self, vm, template):
private_ips = []
for nic in template.NIC:
ip = str(nic.IP)
if network.isPrivateIP(ip):
if any([IPAddress(ip) in IPNetwork(mask) for mask in Config.PRIVATE_NET_MASKS]):
private_ips.append(ip)
else:
public_ips.append(ip)
Expand Down Expand Up @@ -596,8 +598,8 @@ def getONENetworks(self, auth_data):
else:
self.logger.error("Unknown type of network")
continue
is_public = not (network.isPrivateIP(ip))

is_public = not (any([IPAddress(ip) in IPNetwork(mask) for mask in Config.PRIVATE_NET_MASKS]))

res.append((net.NAME, net.ID, is_public))

Expand Down

0 comments on commit 084f6ef

Please sign in to comment.