This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
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,69 @@ | ||
"""External CLI.""" | ||
# Third-Party Libraries | ||
import click | ||
|
||
# cisagov Libraries | ||
from dmcli.utils.categories import get_categories | ||
from dmcli.utils.external import ( | ||
categorize_external_domain, | ||
create_external_domain, | ||
delete_external_domain, | ||
get_external_domain_categories, | ||
get_external_domains, | ||
) | ||
from dmcli.utils.message_handling import success_msg, warning_msg | ||
|
||
|
||
@click.group() | ||
def external(): | ||
"""Manage external domains CLI group.""" | ||
pass | ||
|
||
|
||
@external.command("all") | ||
def all(): | ||
"""Get all external domains.""" | ||
return success_msg("\n".join([domain["name"] for domain in get_external_domains()])) | ||
|
||
|
||
@external.command("domain") | ||
@click.option("-d", "--domain", required=True, prompt=True) | ||
def details(domain): | ||
"""Get external domain details including categorization.""" | ||
domain_details = get_external_domains(domain_name=domain)[0] | ||
cat_details = get_external_domain_categories(domain_details["_id"]) | ||
return success_msg( | ||
"\n".join( | ||
f"{key}: {value}" | ||
for key, value in domain_details.items() | ||
if value is not None and key not in ["_id", "updated", "updated_by"] | ||
) | ||
+ "\n" | ||
+ "\n".join(f"{cat['proxy']}: {cat['status']}" for cat in cat_details) | ||
) | ||
|
||
|
||
@external.command("categorize") | ||
@click.option("-d", "--domain", required=True, prompt=True) | ||
@click.option("-e", "--email", required=True, prompt=True) | ||
def categorize(domain, email): | ||
"""Categorize a new external domain.""" | ||
domain = create_external_domain(domain, email) | ||
if domain["message"]: | ||
warning_msg(domain["message"]) | ||
return | ||
categories = get_categories() | ||
category_name = click.prompt( | ||
"Please enter a category", type=click.Choice(categories) | ||
) | ||
resp = categorize_external_domain(domain["_id"], category_name) | ||
return success_msg(resp.get("success")) | ||
|
||
|
||
@external.command("delete") | ||
@click.option("-d", "--domain", required=True, prompt=True) | ||
def delete(domain): | ||
"""Delete an external domain.""" | ||
external_domain = get_external_domains(domain) | ||
delete_external_domain(external_domain[0]["_id"]) | ||
return success_msg(f"{domain} has been deleted.") |
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,34 @@ | ||
"""External methods for API.""" | ||
# cisagov Libraries | ||
from dmcli.utils import api | ||
|
||
|
||
def get_external_domains(domain_name: str = ""): | ||
"""Get all external domains.""" | ||
path = "/api/external-domains/" | ||
if domain_name: | ||
path = f"{path}?name={domain_name}" | ||
return api.get(path) | ||
|
||
|
||
def create_external_domain(domain_name: str, proxy_email: str): | ||
"""Create an external domain.""" | ||
data = {"name": domain_name, "proxy_email": proxy_email} | ||
return api.post("/api/external-domains/", json=data) | ||
|
||
|
||
def categorize_external_domain(domain_id: str, category: str): | ||
"""Categorize an external domain.""" | ||
return api.post( | ||
f"/api/external-domain/{domain_id}/categorize/", json={"category": category} | ||
) | ||
|
||
|
||
def get_external_domain_categories(domain_id: str): | ||
"""Get categorization data on domain categories.""" | ||
return api.get(f"/api/external-domain/{domain_id}/categorize/") | ||
|
||
|
||
def delete_external_domain(domain_id: str): | ||
"""Delete external domain.""" | ||
return api.delete(f"/api/external-domain/{domain_id}/") |