-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add scheduler and launcher basic support (#7)
US54114 alertlogic-cli: scheduler, launcher basic support * Add scheduler commands to list/scan hosts * Add launcher commands to show deployment status
- Loading branch information
Alexei Osin
authored and
Anton Benkevich
committed
Jun 13, 2017
1 parent
174a1e3
commit ce67f58
Showing
12 changed files
with
1,748 additions
and
46 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# This module defines commands to manipulate scheduler queues for a given environment | ||
|
||
from alertlogiccli.commands import CLICommand | ||
from alertlogiccli.commands import InvalidHTTPResponse, InvalidParameter, InvalidServiceResponse | ||
import requests | ||
import json | ||
|
||
class ListDeployedResourcesCommand(CLICommand): | ||
"""Command to list security infrastructure resources deployed to a given environment""" | ||
command = "list_deployed_resources" | ||
def __init__(self, services): | ||
CLICommand.__init__(self, services) | ||
|
||
@classmethod | ||
def get_parser(cls, subparsers): | ||
cmd_help = "lists security infrastructure resources deployed to a given environment" | ||
parser = subparsers.add_parser(cls.command, help=cmd_help) | ||
|
||
def execute(self, environment_id, **kwargs): | ||
try: | ||
response = self.services.launcher.getawsresourcesbyenvironment(environment_id=environment_id) | ||
response.raise_for_status() | ||
except requests.exceptions.HTTPError as e: | ||
raise InvalidHTTPResponse(self.command, e.message) | ||
content = response.json() | ||
return json.dumps(content, sort_keys=True, indent=4) | ||
|
||
class GetDeploymentStatusCommand(CLICommand): | ||
"""Command to get deployment status for a given environment""" | ||
command = "get_deployment_status" | ||
def __init__(self, services): | ||
CLICommand.__init__(self, services) | ||
|
||
@classmethod | ||
def get_parser(cls, subparsers): | ||
cmd_help = "gets deployment status for a given environment" | ||
parser = subparsers.add_parser(cls.command, help=cmd_help) | ||
|
||
def execute(self, environment_id, **kwargs): | ||
try: | ||
response = self.services.launcher.getdeploymentstatus(environment_id=environment_id) | ||
response.raise_for_status() | ||
except requests.exceptions.HTTPError as e: | ||
raise InvalidHTTPResponse(self.command, e.message) | ||
content = response.json() | ||
return json.dumps(content, sort_keys=True, indent=4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# This module defines commands to manipulate scheduler queues for a given environment | ||
|
||
from alertlogiccli.commands import CLICommand | ||
from alertlogiccli.commands import InvalidHTTPResponse, InvalidParameter, InvalidServiceResponse | ||
import requests | ||
import json | ||
|
||
class ListScanQueuesCommand(CLICommand): | ||
"""List hosts in scan queues for a given environment""" | ||
command = "list_scan_queues" | ||
def __init__(self, services): | ||
CLICommand.__init__(self, services) | ||
|
||
@classmethod | ||
def get_parser(cls, subparsers): | ||
cmd_help = "lists hosts in scan queues for a given environment" | ||
parser = subparsers.add_parser(cls.command, help=cmd_help) | ||
parser.add_argument("--vpc_key", help="filter hosts for a given VPC") | ||
|
||
def execute(self, environment_id, vpc_key=None, **kwargs): | ||
try: | ||
response = self.services.scan_scheduler.listscanassets(environment_id=environment_id) | ||
response.raise_for_status() | ||
except requests.exceptions.HTTPError as e: | ||
raise InvalidHTTPResponse(self.command, e.message) | ||
content = response.json() | ||
regular = group_by_vpc(content["assets"], vpc_key) | ||
immediate = group_by_vpc(content["immediate"], vpc_key) | ||
result = {"regular": regular, "immediate": immediate} | ||
return json.dumps(result, sort_keys=True, indent=4) | ||
|
||
class ScanHostCommand(CLICommand): | ||
"""Puts a host to the immediate scan queue""" | ||
command = "scan_host" | ||
def __init__(self, services): | ||
CLICommand.__init__(self, services) | ||
|
||
@classmethod | ||
def get_parser(cls, subparsers): | ||
cmd_help = "puts a host in the immediate scan queue" | ||
parser = subparsers.add_parser(cls.command, help=cmd_help) | ||
parser.add_argument("--host_key", required=True, help="a host key to put in the queue") | ||
|
||
def execute(self, environment_id, host_key, **kwargs): | ||
try: | ||
response = self.services.scan_scheduler.scanasset(environment_id=environment_id, asset_key=host_key) | ||
response.raise_for_status() | ||
except requests.exceptions.HTTPError as e: | ||
raise InvalidHTTPResponse(self.command, e.message) | ||
return "ok" | ||
|
||
def group_by_vpc(assets, filter_vpc_key): | ||
acc = {} | ||
for asset in assets: | ||
vpc_key = asset["vpc"] | ||
if not filter_vpc_key or filter_vpc_key == vpc_key: | ||
group = acc.get(vpc_key, {"vpc": vpc_key, "hosts": []}) | ||
group["hosts"].append(asset) | ||
acc[vpc_key] = group | ||
return acc.values() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.