Skip to content

Commit

Permalink
add domain detach and info command
Browse files Browse the repository at this point in the history
  • Loading branch information
aliel committed Oct 12, 2023
1 parent a918996 commit 9b77354
Showing 1 changed file with 78 additions and 44 deletions.
122 changes: 78 additions & 44 deletions src/aleph_client/commands/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,28 @@ async def check_domain_records(fqdn, target, owner):
return status


async def attach_resource(account: AccountFromPrivateKey, fqdn: Hostname, item_hash: Optional[str] = None):
async def attach_resource(account: AccountFromPrivateKey, fqdn: Hostname, item_hash: Optional[str] = None, detach: Optional[bool] = False):
domain_info = await get_aggregate_domain_info(account, fqdn)
console = Console()

while not item_hash:
while not item_hash and detach is False:
item_hash = Prompt.ask("Enter Hash reference of the resource to attach")

table = Table(title=f"Attach resource to: {fqdn}")
table.add_column("Current resource", justify="right", style="red", no_wrap=True)
table.add_column("New resource", justify="right", style="green", no_wrap=True)
table.add_column("Resource type", style="magenta")

current_resource = ""
resource_type: TargetType
current_resource = "null"
domain_validator = DomainValidator()

"""
Detect target type on the fly to be able to switch to another type
"""
resource_type = await domain_validator.get_target_type(fqdn)

print(domain_info)
if domain_info is not None:
if domain_info is not None and domain_info.get("info"):
current_resource = domain_info["info"]["message_id"]
resource_type = TargetType(domain_info["info"]["type"].lower())
else:
domain_validator = DomainValidator()
resource_type = await domain_validator.get_target_type(fqdn)

table.add_row(f"{current_resource[:16]}...{current_resource[-16:]}", f"{item_hash[:16]}...{item_hash[-16:]}", resource_type)

Expand All @@ -84,22 +84,30 @@ async def attach_resource(account: AccountFromPrivateKey, fqdn: Hostname, item_h
async with AuthenticatedAlephClient(
account=account, api_server=sdk_settings.API_HOST
) as client:
aggregate_content = {
fqdn: {
if detach:
aggregate_content = {
"message_id": item_hash,
"type": resource_type,
# console page compatibility
"programType": resource_type
}
}
else:
aggregate_content = {
fqdn: {
"message_id": item_hash,
"type": resource_type,
# console page compatibility
"programType": resource_type
}
}

aggregate_message, message_status = await client.create_aggregate(
key="domains",
content=aggregate_content,
channel="ALEPH-CLOUDSOLUTIONS"
)

console.log(f"[green bold]Aleph message created!")
console.log("[green bold]Aleph message created!")
console.log(f"Visualise on: https://explorer.aleph.im/address/ETH/{account.get_address()}/message/AGGREGATE/{aggregate_message.item_hash}")


Expand Down Expand Up @@ -195,8 +203,7 @@ async def attach(
private_key_file: Optional[Path] = typer.Option(
sdk_settings.PRIVATE_KEY_FILE, help=help_strings.PRIVATE_KEY_FILE
),
fqdn: str = typer.Argument(..., help=help_strings.CUSTOM_DOMAIN_NAME
),
fqdn: str = typer.Argument(..., help=help_strings.CUSTOM_DOMAIN_NAME),
item_hash: Optional[str] = typer.Option(None, help=help_strings.CUSTOM_DOMAIN_ITEM_HASH),
):
"""Attach resource to a Custom Domain."""
Expand All @@ -205,39 +212,66 @@ async def attach(
await attach_resource(account, fqdn, item_hash)
raise typer.Exit()

@app.command()
@coro
async def detach(
private_key: Optional[str] = typer.Option(
sdk_settings.PRIVATE_KEY_STRING, help=help_strings.PRIVATE_KEY
),
private_key_file: Optional[Path] = typer.Option(
sdk_settings.PRIVATE_KEY_FILE, help=help_strings.PRIVATE_KEY_FILE
),
fqdn: str = typer.Argument(..., help=help_strings.CUSTOM_DOMAIN_NAME)
):
"""Unlink Custom Domain."""
account: AccountFromPrivateKey = _load_account(private_key, private_key_file)

await attach_resource(account, fqdn, None, True)
raise typer.Exit()


@app.command()
@coro
async def info(
fqdn: str = typer.Argument(..., help=help_strings.CUSTOM_DOMAIN_NAME)
private_key: Optional[str] = typer.Option(
sdk_settings.PRIVATE_KEY_STRING, help=help_strings.PRIVATE_KEY
),
private_key_file: Optional[Path] = typer.Option(
sdk_settings.PRIVATE_KEY_FILE, help=help_strings.PRIVATE_KEY_FILE
),
fqdn: str = typer.Argument(..., help=help_strings.CUSTOM_DOMAIN_NAME)
):
"""Show Custom Domain Details."""
account: AccountFromPrivateKey = _load_account(private_key, private_key_file)

console = Console()
domain_validator = DomainValidator()
target = None

domain_info = await get_aggregate_domain_info(account, fqdn)
if domain_info is None or domain_info.get("info") is None:
console.log(f"Domain: {fqdn} not configured")
raise typer.Exit()

table = Table(title=f"Domain info: {fqdn}")
table.add_column("Resource type", justify="right", style="cyan", no_wrap=True)
table.add_column("Attached resource", justify="right", style="cyan", no_wrap=True)
table.add_column("Final resource", justify="right", style="cyan", no_wrap=True)

resource_type = TargetType(domain_info["info"]["type"])
final_resource = "Unknown"

try:
res = await domain_validator.resolver.query(fqdn, "CNAME")
cname_value = res.cname
if sdk_settings.DNS_IPFS_DOMAIN in cname_value:
target = "ipfs"
elif sdk_settings.DNS_PROGRAM_DOMAIN in cname_value:
target = "program"
elif sdk_settings.DNS_INSTANCE_DOMAIN in cname_value:
target = "instance"
if resource_type == TargetType.IPFS:
final_resource = ""
elif resource_type == TargetType.PROGRAM:
final_resource = domain_info["info"]["message_id"]
if resource_type == TargetType.INSTANCE:
ips = await domain_validator.get_ipv6_addresses(fqdn)
final_resource = ",".join(ips)
except Exception:
typer.echo(f"Domain: {fqdn} not configured")
raise typer.Exit()
pass

print(target)
if target is not None:
try:
status = await domain_validator.check_domain(fqdn, target)
print(status)
if target == "ipfs":
pass
elif target == "program":
pass
if target == "instance":
ipv6 = await domain_validator.get_ipv6_addresses(fqdn)
typer.echo(ipv6)
except Exception:
raise typer.Exit()
else:
raise typer.Exit()
table.add_row(resource_type, domain_info["info"]["message_id"], final_resource)

console.print(table)
raise typer.Exit()

0 comments on commit 9b77354

Please sign in to comment.