import subprocess
import os
import sys
import re
from networkCheck import networkCheck

class checkPrivateNetwork(networkCheck):

    def getHumanName(self):
        return "Check configuration of private network"
        
    def runCheck(self,config, debug):
        # confirm static assigned private numbers are not actually active on the net!
        returnCode=0

        # Get the netmask value        
        netmask = config["environment"]["privateNetmask"]
        network = config["environment"]["privateNetwork"]


        # make sure any privateIP addresses fit within this network|netmask
        for server in config['environment']['servers']:
            if "deployed" not in server:
                for vm in server['vms']:
                    ip = vm['privateIP']
                    if ip == '':
                        # this is a problem - every VM should be on the private net
                        self.addFailure('Invalid Private Network IP for vm '+vm['name']+'. All VMs must be on the private network')
                    elif ip != 'dhcp':

                        if not self.checkNetwork(ip, network, netmask):
                            self.addFailure('Invalid static private IP '+ip+' for vm '+vm['name']+' with netmask '+netmask+' and network '+network)
                                
                        else:

                            # then its a static.  make sure its not active...
                            if self.pingIP(ip):
                                # thats a problem!
                                self.addFailure('Assigned static private IP '+ip+' for vm '+vm['name']+' appears to be alive on the network. Please enter a different IP address.')
       
        
        return returnCode
