Skip to content

Commit

Permalink
feat: omit cloud for ls-availale and add-schedule
Browse files Browse the repository at this point in the history
closes: #396

Change-Id: Iec2088c936f759bb0520148cbf3a2a4eae53cacb
  • Loading branch information
dominikvagner committed Oct 14, 2022
1 parent 11a264f commit e0367b5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
61 changes: 47 additions & 14 deletions quads/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,17 @@ def action_available(self):
"There was something wrong constructing the parameters for the query."
)

omit_cloud = ""
if self.cli_args["omitcloud"]:
omit_cloud = Cloud.objects(name=self.cli_args["omitcloud"]).first()
if not omit_cloud:
raise CliException("Omit cloud not found")
for host in all_hosts:
if Schedule.is_host_available(host=host["name"], start=_start, end=_end):
if Schedule.current_schedule(host=host):
current.append(host["name"])
current_schedule = Schedule.current_schedule(host=host)
if current_schedule:
if host.default_cloud.name == conf["spare_pool_name"] and current_schedule[0].cloud != omit_cloud:
current.append(host["name"])
else:
if host.default_cloud.name == conf["spare_pool_name"]:
available.append(host["name"])
Expand Down Expand Up @@ -1215,19 +1222,33 @@ def action_add_schedule(self):
if self.cli_args["host"] is None and self.cli_args["host_list"] is None:
raise CliException("Missing option. --host or --host-list required.")

omitted_cloud_id = None
if self.cli_args["omitcloud"]:
clouds = json.loads(Cloud.objects().all().to_json())
omitted_cloud = [c for c in clouds if c.get("name") == self.cli_args["omitcloud"]]
if len(omitted_cloud) == 0:
self.logger.warning(f"No cloud named {self.cli_args['omitcloud']} found.")
omitted_cloud_id = omitted_cloud[0].get("_id").get("$oid")

if self.cli_args["host"]:
data = {
"cloud": self.cli_args["schedcloud"],
"host": self.cli_args["host"],
"start": self.cli_args["schedstart"],
"end": self.cli_args["schedend"],
}
try:
self.logger.info(self.quads.insert_schedule(data)["result"][0])
except ConnectionError:
raise CliException(
"Could not connect to the quads-server, verify service is up and running."
)
if self.cli_args["omitcloud"] and omitted_cloud_id:
host_obj = Host.objects(name=self.cli_args["host"]).first()
host_json = json.loads(host_obj.to_json())
if host_json.get("cloud").get("$oid") == omitted_cloud_id:
self.logger.info("Host is in part of the cloud specified with --omit-cloud. Nothing has been done.")
else:
data = {
"cloud": self.cli_args["schedcloud"],
"host": self.cli_args["host"],
"start": self.cli_args["schedstart"],
"end": self.cli_args["schedend"],
}
try:
self.logger.info(self.quads.insert_schedule(data)["result"][0])
except ConnectionError:
raise CliException(
"Could not connect to the quads-server, verify service is up and running."
)

elif self.cli_args["host_list"]:
try:
Expand All @@ -1243,6 +1264,18 @@ def action_add_schedule(self):
)
_sched_end = datetime.strptime(self.cli_args["schedend"], "%Y-%m-%d %H:%M")

if self.cli_args["omitcloud"] and omitted_cloud_id:
self.logger.info(f"INFO - All hosts from {self.cli_args['omitcloud']} will be omitted.")
omitted = []
for host in host_list:
host_obj = Host.objects(name=host).first()
host_json = json.loads(host_obj.to_json())
if host_json.get("cloud").get("$oid") == omitted_cloud_id:
omitted.append(host)
for host in omitted:
host_list.remove(host)
self.logger.info(f"{host} will be omitted.")

for host in host_list:
is_available = Schedule.is_host_available(
host=host,
Expand Down
7 changes: 7 additions & 0 deletions quads/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,13 @@
default=None,
help="Schedule end date/time",
)
parser.add_argument(
"--omit-cloud",
dest="omitcloud",
type=str,
default="",
help="Specify a cloud from which hosts should be omitted when adding schedule or when listing available.",
)
parser.add_argument(
"--check",
dest="check",
Expand Down

0 comments on commit e0367b5

Please sign in to comment.