def checkInstancesInZone(ZONE):
    compute = googleapiclient.discovery.build('compute',
                                              'v1',
                                              credentials=credentials)
    instances = list_instances(compute, PROJECT_ID, ZONE)

    if (instances != None):
        for instance in instances:
            print('Instance name: ' + instance['name'] + "\nInstance ID: " +
                  instance['id'] + '\nZone: ' + ZONE + '\nState: ' +
                  instance['status'])
            machine_type = "".join(
                str(instance['machineType']).split('/')[-1:])
            print('Machine Type:', machine_type)
            network_priv = instance['networkInterfaces']
            for x in network_priv:
                print("Private IP", x['networkIP'])

                # Get subnet details, CIDR and GW
                subnetwork = "".join(str(x['subnetwork']).split('/')[-1:])
                sregion = "".join(str(x['subnetwork']).split('/')[-3])
                request = service.subnetworks().get(project=PROJECT_ID,
                                                    region=sregion,
                                                    subnetwork=subnetwork)
                response = request.execute()
                print("Private Subnet CIDR:", response['ipCidrRange'])
                print("Private Subnet Gateway:", response['gatewayAddress'])
                if 'hostname' in instance:
                    print("Custom FQDN:", instance['hostname'])
                    hostname = instance['hostname']
                else:
                    hostname = ""
                internal_dns = instance[
                    'name'] + "." + ZONE + ".c." + PROJECT_ID + ".internal"
                print("Internal (Zonal) DNS Name:", internal_dns)
                a, *rest = x['accessConfigs']
                if 'natIP' in a:
                    print("Public IP:", a['natIP'])
                    public_ip = a['natIP']
                else:
                    public_ip = ""

            config = PROJECT_NAME + " [" + ZONE + "]"
            # Check if Project/Region configuration in BAM already is present, if not add the Project/Region configuration
            conf = BAM.GetConfiguration(soap_client, config)
            if conf:
                print(
                    BAM.bcolours.GREEN + BAM.bcolours.BOLD +
                    '[Google CloudAtlas] Project/Region Configuration already in BlueCat Address Manager '
                    + BAM.bcolours.ENDC)
            else:
                print(
                    BAM.bcolours.GREEN + BAM.bcolours.BOLD +
                    '[Google CloudAtlas] Project/Region Configuration not found, adding to BlueCat Address Manager '
                    + BAM.bcolours.ENDC)
                BAM.AddGCPConfiguration(soap_client, config)

            # Check if Network Block of VPC is already in the config in BAM, if not add the required Block
            conf = BAM.GetConfiguration(soap_client, config)
            blk = BAM.GetBlockV4(soap_client, conf.id, response['ipCidrRange'])
            if blk:
                print(
                    BAM.bcolours.GREEN + BAM.bcolours.BOLD +
                    '[Google CloudAtlas] Project/Region Block already in BlueCat Address Manager '
                    + BAM.bcolours.ENDC)
            else:
                print(
                    BAM.bcolours.GREEN + BAM.bcolours.BOLD +
                    '[Google CloudAtlas] Adding Project/Region Network Block to BlueCat Address Manager '
                    + BAM.bcolours.ENDC)
                conf = BAM.GetConfiguration(soap_client, config)
                pid = str(conf['id'])
                props = "name=" + response['ipCidrRange']
                blk = BAM.AddBlockV4(soap_client, pid, response['ipCidrRange'],
                                     props)

            # Check if Subnet of VNET is already in the Block in BAM, if not add the required Subnet
            blk = BAM.GetBlockV4(soap_client, conf.id, response['ipCidrRange'])
            subn = BAM.GetNetworkV4(soap_client, blk.id,
                                    response['ipCidrRange'])
            if subn:
                print(
                    BAM.bcolours.GREEN + BAM.bcolours.BOLD +
                    '[Google CloudAtlas] Project/Region Subnet already in BlueCat Address Manager '
                    + BAM.bcolours.ENDC)
            else:
                print(
                    BAM.bcolours.GREEN + BAM.bcolours.BOLD +
                    '[Google CloudAtlas] Adding Project/Region Subnet to BlueCat Address Manager '
                    + BAM.bcolours.ENDC)
                props = "name=" + response['ipCidrRange']
                BAM.AddNetworkV4(soap_client, blk.id,
                                 str(response['ipCidrRange']), props)

            # Check if Instance Device is already added, if not add the required device
            dev = BAM.GetDevice(soap_client, conf.id, instance['name'])
            if dev:
                print(
                    BAM.bcolours.GREEN + BAM.bcolours.BOLD +
                    '[Google CloudAtlas] Google VM Device in BlueCat Address Manager, updating  '
                    + BAM.bcolours.ENDC)
                BAM.DelDevice(soap_client, conf.id, dev.id)
                props = "PrivateDNSName=" + internal_dns + '|' + "PublicDNSName=" + hostname + '|' + "InstanceState=" + instance[
                    'status'] + '|' + "InstanceType=" + machine_type + "|" + "AvailabilityZone=" + ZONE + "|" + "IPv4PublicIP=" + public_ip
                device = soap_client.service.addDevice(
                    str(conf['id']), instance['name'], GCPDevType,
                    GCPInstanceSubType, x['networkIP'], "", props)

            else:
                print(
                    BAM.bcolours.GREEN + BAM.bcolours.BOLD +
                    '[Google CloudAtlas] Google VM Device not found, adding to BlueCat Address Manager '
                    + BAM.bcolours.ENDC)
                props = "PrivateDNSName=" + internal_dns + '|' + "PublicDNSName=" + hostname + '|' + "InstanceState=" + instance[
                    'status'] + '|' + "InstanceType=" + machine_type + "|" + "AvailabilityZone=" + ZONE + "|" + "IPv4PublicIP=" + public_ip
                device = soap_client.service.addDevice(
                    str(conf['id']), instance['name'], GCPDevType,
                    GCPInstanceSubType, x['networkIP'], "", props)
            print("\n")
def get_azure_vms():
    print(BAM.bcolours.GREEN + BAM.bcolours.BOLD +
          '[Azure CloudAtlas] get_azure_vms()' + BAM.bcolours.ENDC)
    for vm in compute_client.virtual_machines.list_all():
        vmd = compute_client.virtual_machines.get(GROUP_NAME,
                                                  vm.name,
                                                  expand='instanceView')
        hardware = vmd.hardware_profile
        print(BAM.bcolours.GREEN + BAM.bcolours.BOLD +
              '[Azure CloudAtlas] Virtual Machine Discovered' +
              BAM.bcolours.ENDC)
        for stat in vmd.instance_view.statuses:
            cur_status = stat.display_status
        for interface in vm.network_profile.network_interfaces:
            name = " ".join(interface.id.split('/')[-1:])
            sub = "".join(interface.id.split('/')[4])
            ipconfs = network_client.network_interfaces.get(
                sub, name).ip_configurations
            for i in ipconfs:
                vnet = "".join(i.subnet.id.split('/')[-3])
                b = network_client.virtual_networks.get(GROUP_NAME, vnet)
                vnet_name = b.name
                address_space = str(
                    b.address_space.address_prefixes).split("'")[1].strip()
                sn = "".join(i.subnet.id.split('/')[-1:])
                sub = network_client.subnets.get(GROUP_NAME, vnet, sn)
                public_ip = network_client.public_ip_addresses.get(
                    GROUP_NAME, vm.name + "-ip")
                if public_ip.dns_settings is None:
                    pubdns = "NA"
                else:
                    pubdns = public_ip.dns_settings.fqdn

        config = GROUP_NAME + " [" + vnet + "]"

        # Check if VNET configuration in BAM already is present, if not add the VNET configuration
        conf = BAM.GetConfiguration(soap_client, config)
        if conf:
            print(
                BAM.bcolours.GREEN + BAM.bcolours.BOLD +
                '[Azure CloudAtlas] VNET Configuration already in BlueCat Address Manager '
                + BAM.bcolours.ENDC)
        else:
            print(
                BAM.bcolours.GREEN + BAM.bcolours.BOLD +
                '[Azure CloudAtlas] VNET Configuration not found, adding to BlueCat Address Manager '
                + BAM.bcolours.ENDC)
            conf = vnet
            BAM.AddAzureConfiguration(soap_client, config)

        # Check if Network Block of VPC is already in the config in BAM, if not add the required Block
        conf = BAM.GetConfiguration(soap_client, config)
        blk = BAM.GetBlockV4(soap_client, conf.id, str(address_space))
        if blk:
            print(
                BAM.bcolours.GREEN + BAM.bcolours.BOLD +
                '[Azure CloudAtlas] VNET Network Block already in BlueCat Address Manager '
                + BAM.bcolours.ENDC)
        else:
            print(
                BAM.bcolours.GREEN + BAM.bcolours.BOLD +
                '[Azure CloudAtlas] Adding VNET Network Block to BlueCat Address Manager '
                + BAM.bcolours.ENDC)
            conf = BAM.GetConfiguration(soap_client, config)
            pid = str(conf['id'])
            props = "name=" + vnet_name
            blk = BAM.AddBlockV4(soap_client, pid, address_space, props)

        # Check if Subnet of VNET is already in the Block in BAM, if not add the required Subnet
        blk = BAM.GetBlockV4(soap_client, conf.id, str(address_space))
        subn = BAM.GetNetworkV4(soap_client, blk.id, str(sub.address_prefix))
        if subn:
            print(
                BAM.bcolours.GREEN + BAM.bcolours.BOLD +
                '[Azure CloudAtlas] VNET Subnet already in BlueCat Address Manager '
                + BAM.bcolours.ENDC)
        else:
            print(
                BAM.bcolours.GREEN + BAM.bcolours.BOLD +
                '[Azure CloudAtlas] Adding VNET Subnet to BlueCat Address Manager '
                + BAM.bcolours.ENDC)
            props = "name=" + sn
            BAM.AddNetworkV4(soap_client, blk.id, str(sub.address_prefix),
                             props)

        # Check if Instance Device is already added, if not add the required device
        dev = BAM.GetDevice(soap_client, conf.id, vm.name)
        if dev:
            print(
                BAM.bcolours.GREEN + BAM.bcolours.BOLD +
                '[Azure CloudAtlas] Azure VM Device in BlueCat Address Manager, updating  '
                + BAM.bcolours.ENDC)
            BAM.DelDevice(soap_client, conf.id, dev.id)
            props = "PrivateDNSName=Not Applicable" + '|' + "PublicDNSName=" + pubdns + '|' + "InstanceState=" + cur_status + '|' + "InstanceType=" + hardware.vm_size + "|" + "AvailabilityZone=" + vm.location + "|" + "IPv4PublicIP=" + str(
                public_ip.ip_address)
            device = soap_client.service.addDevice(str(conf['id']), vm.name,
                                                   AzureDevType,
                                                   AzureInsanceSubType,
                                                   i.private_ip_address, "",
                                                   props)

        else:
            print(
                BAM.bcolours.GREEN + BAM.bcolours.BOLD +
                '[Azure CloudAtlas] Azure VM Device not found, adding to BlueCat Address Manager '
                + BAM.bcolours.ENDC)
            props = "PrivateDNSName=Not Applicable" + '|' + "PublicDNSName=" + pubdns + '|' + "InstanceState=" + cur_status + '|' + "InstanceType=" + hardware.vm_size + "|" + "AvailabilityZone=" + vm.location + "|" + "IPv4PublicIP=" + str(
                public_ip.ip_address)
            device = soap_client.service.addDevice(str(conf['id']), vm.name,
                                                   AzureDevType,
                                                   AzureInsanceSubType,
                                                   i.private_ip_address, "",
                                                   props)

        print("")
		conf = BAM.GetConfiguration(soap_client,instance.vpc_id)
		pid = str(conf['id'])
		props="name=" + instance.vpc_id
		blk = BAM.AddBlockV4(soap_client,pid,vpc.cidr_block,props)

	# Check if Subnet of VPC is already in the Block in BAM, if not add the required Subnet

	blk = BAM.GetBlockV4(soap_client,conf.id,str(vpc.cidr_block))
	sub = BAM.GetNetworkV4(soap_client,blk.id,str(subnet.cidr_block))
	if sub:
		print (BAM.bcolours.GREEN + BAM.bcolours.BOLD + '[AWS CloudAtlas] VPC Subnet already in BlueCat Address Manager ' + BAM.bcolours.ENDC )
	else:
		blk = BAM.GetBlockV4(soap_client,conf.id,str(vpc.cidr_block))
		print (BAM.bcolours.GREEN + BAM.bcolours.BOLD + '[AWS CloudAtlas] Adding VPC Subnet to BlueCat Address Manager ' + BAM.bcolours.ENDC )
		props="name=" + instance.subnet_id
		BAM.AddNetworkV4(soap_client,blk.id,subnet.cidr_block,props)

	# Check if Instance Device is already added, if not add the required device
	dev = BAM.GetDevice(soap_client,conf.id,instance.id)
	if dev:
		print (BAM.bcolours.GREEN + BAM.bcolours.BOLD + '[AWS CloudAtlas] EC2 Instance Device in BlueCat Address Manager, updating  ' + BAM.bcolours.ENDC )
		BAM.DelDevice(soap_client,conf.id,dev.id)
		props="PrivateDNSName="+instance.private_dns_name + '|' + "PublicDNSName=" + instance.public_dns_name + '|' + "InstanceState="+instance.state['Name'] + '|' + "InstanceType="+instance.instance_type + "|" + "AvailabilityZone=" + instance.placement['AvailabilityZone'] + "|" + "IPv4PublicIP=" + str(instance.public_ip_address)
		device = soap_client.service.addDevice(str(conf['id']),instance.id,AWSDevType,AWSInsanceSubType,instance.private_ip_address,"",props)
	#	device = BAM.AssignIP4Address(soap_client,str(conf['id']),instance.private_ip_address, mac_addr)

	else:
		print (BAM.bcolours.GREEN + BAM.bcolours.BOLD + '[AWS CloudAtlas] EC2 Instance Device not found, adding to BlueCat Address Manager ' + BAM.bcolours.ENDC )
		props="PrivateDNSName="+instance.private_dns_name + '|' + "PublicDNSName=" + instance.public_dns_name + '|' + "InstanceState="+instance.state['Name'] + '|' + "InstanceType="+instance.instance_type + "|" + "AvailabilityZone=" + instance.placement['AvailabilityZone'] + "|" + "IPv4PublicIP=" + str(instance.public_ip_address)
		device = soap_client.service.addDevice(str(conf['id']),instance.id,AWSDevType,AWSInsanceSubType,instance.private_ip_address,"",props)
	#	device = BAM.AssignIP4Address(soap_client,str(conf['id']),instance.private_ip_address, mac_addr)