from ucsmsdk.ucsexception import UcsException
import re, sys

# given an array and a string of numbers, make sure they are all in the array:
# 
def check_values(array, csv):
    indexes = csv.split(',')
    for i in indexes:
        try: 
            i = int(i) - 1
        except:
            print "bad value: " + i
            return False
        if i < 0 or i > len(array) - 1:
            return False
    return True


# get the available servers to put in the pool. 
def select_kube_servers(handle):
    from ucsmsdk.mometa.compute.ComputeRackUnit import ComputeRackUnit
    from ucsmsdk.mometa.fabric.FabricComputeSlotEp import FabricComputeSlotEp
    print "Listing Available UCS Servers"
    filter_string = '(presence, "equipped")'
    # get blades
    blades = handle.query_classid("fabricComputeSlotEp", filter_string)
    # get all connected rack mount servers.
    servers = handle.query_classid("computeRackUnit")
    m = blades + servers
    while True:
        for i, s in enumerate(m):
            if type(s) is FabricComputeSlotEp:
                print "[%d]: Blade %s/%s type %s" % (i+1, s.chassis_id, s.rn, s.model)
            if type(s) is ComputeRackUnit:
                print "[%d]: Rack %s type %s" % (i+1, s.rn, s.model)
        vals = raw_input("(E.g.: 2,4,8): ")
        if check_values(m, vals) == True:
            k8servers = [m[int(x)-1] for x in vals.split(',')]
            print "Install Kubernetes on the following servers:"
            for s in k8servers:
                if type(s) is FabricComputeSlotEp:
                    print "\tBlade %s/%s type %s" % (s.chassis_id, s.rn, s.model)
                if type(s) is ComputeRackUnit:
                    print "\tServer %s type %s" % (s.rn, s.model)

            yn = raw_input("Is this correct? [N/y]: ")
            if yn == "y" or yn == "Y":
                return k8servers

    
def createKubeBootPolicy(handle):
    print "Creating Kube Boot Policy"
    from ucsmsdk.mometa.lsboot.LsbootPolicy import LsbootPolicy
    from ucsmsdk.mometa.lsboot.LsbootVirtualMedia import LsbootVirtualMedia
    from ucsmsdk.mometa.lsboot.LsbootStorage import LsbootStorage
    from ucsmsdk.mometa.lsboot.LsbootLocalStorage import LsbootLocalStorage
    from ucsmsdk.mometa.lsboot.LsbootDefaultLocalImage import LsbootDefaultLocalImage

    mo = LsbootPolicy(parent_mo_or_dn="org-root", name="kube", descr="Kuberenetes", reboot_on_update="yes", policy_owner="local", enforce_vnic_name="yes", boot_mode="legacy")
    mo_1 = LsbootVirtualMedia(parent_mo_or_dn=mo, access="read-only-remote-cimc", lun_id="0", order="2")
    mo_2 = LsbootStorage(parent_mo_or_dn=mo, order="1")
    mo_2_1 = LsbootLocalStorage(parent_mo_or_dn=mo_2, )
    mo_2_1_1 = LsbootDefaultLocalImage(parent_mo_or_dn=mo_2_1, order="1")

    handle.add_mo(mo, modify_present=True)
    try: 
        handle.commit()
    except UcsException as err:
        if err.error_code == "103":
            print "\talready exists"

def deleteKubeBootPolicy(handle):
    mo = handle.query_dn("org-root/boot-policy-kube")
    try:
        handle.remove_mo(mo)
        handle.commit()
    except AttributeError:
        print "\talready deleted"

def createKubeLocalDiskPolicy(handle):
    print "Creating Kube Local Disk Policy"
    from ucsmsdk.mometa.storage.StorageLocalDiskConfigPolicy import StorageLocalDiskConfigPolicy

    mo = StorageLocalDiskConfigPolicy(parent_mo_or_dn="org-root", protect_config="no", name="kube", descr="Kubernetes", flex_flash_raid_reporting_state="disable", flex_flash_state="disable", policy_owner="local", mode="raid-mirrored")
    handle.add_mo(mo)
    try: 
        handle.commit()
    except UcsException as err:
        if err.error_code == "103":
            print "\talready exists"

def deleteKubeLocalDiskPolicy(handle):
    print "Deleting Kube Local Disk Policy"
    mo = handle.query_dn("org-root/local-disk-config-kube")
    try:
        handle.remove_mo(mo)
        handle.commit()
    except AttributeError: