def getAvailHostAssets(session_id, site_id=-1): """Returns a list of assets that could be used for a new host Returns a list of assets that are assigned to a site and could be used for a new host. If the site_id is -1 then a list of assets in stock that could be used for a new host are returned. """ session = getSessionE(session_id) if site_id==-1: # Return generic list of available host assets at site 'stock' sql = "SELECT s.asset_id, s.description FROM site_host_assets_avail " \ "s, asset a WHERE a.asset_id=s.asset_id AND s.site_id IN " \ "(SELECT site_id FROM asset_stock_location) " \ "AND s.asset_id NOT IN (SELECT asset_id FROM host WHERE " \ "asset_id IS NOT NULL) AND a.asset_type_id IN " \ "(SELECT asset_type_id FROM asset_type_map WHERE " \ "asset_function='host')" p = () else: # Verify site_id is OK validateSiteId(session_id, site_id) # Return list of host assets assigned to site but not yet to a host sql = "SELECT asset_id, description FROM site_host_assets_avail " \ "WHERE site_id=%s" p = (site_id) res = session.query(sql, p) return res
def validateHost(session_id, details, required=False): """Validates the details are valid. This routine does not check for the existance of required data, it only verifies that the fields in the supplied dictionary are valid. """ session = getSessionE(session_id) if "host_name" in details.keys(): if len(details["host_name"])<=0: raise ccs_host_error("Hostname must be greater than 0 " \ "characters long!") if len(details["host_name"]) > 64: raise ccs_host_error("Hostname must be less than 64 " \ "characters long!") if details["host_name"].find(" ") != -1: raise ccs_host_error("Hostname must not contain spaces") elif required: raise ccs_host_error("Hostname is a required field") if "distribution_id" in details.keys(): validateDistributionId(session_id, details["distribution_id"]) elif required: raise ccs_host_error("Distribution is a required field") if "kernel_id" in details.keys(): validateKernelId(session_id, details["kernel_id"]) elif required: raise ccs_host_error("Kernel is a required field") if "host_active" in details.keys(): if details["host_active"] != "t" and details["host_active"] != "f": raise ccs_host_error("Invalid host status value. Must be 't' " \ "or 'f'") elif required: raise ccs_host_error("Host status is a required field") if "is_gateway" in details.keys(): if details["is_gateway"] != "t" and details["is_gateway"] != "f": raise ccs_host_error("Invalid is gateway value. Must be 't' " \ "or 'f'") elif required: raise ccs_host_error("Gateway is a required field") # XXX: Check ip_address field # Site if "site_id" in details.keys() and int(details["site_id"])!=-1: validateSiteId(session_id, details["site_id"]) # Asset if "asset_id" in details.keys() and int(details["asset_id"])!=-1: asset = ccs_asset(session_id, details["asset_id"]) # Check asset is of the correct type if not asset.supportsFunction("host"): raise ccs_host_error("Specified asset is not of the " \ "appropriate type to use as a host!") # Check asset isn't assigned elsewhere if "site_id" in details.keys(): site_id = details["site_id"] else: site_id = -1 if not asset.availableForHost(site_id): raise ccs_host_error("Specified asset already in use or " \ "not available!") return