Skip to content

Commit

Permalink
Add subcommand wasmer domain register to register a new domain
Browse files Browse the repository at this point in the history
  • Loading branch information
ayys authored and theduke committed Mar 18, 2024
1 parent f3bfaf4 commit c0c4233
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/backend-api/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3050,8 +3050,8 @@ type RegisterDomainPayload {

input RegisterDomainInput {
name: String!
namespace: String!
importRecords: Boolean = false
namespace: String
importRecords: Boolean = true
clientMutationId: String
}

Expand Down
23 changes: 23 additions & 0 deletions lib/backend-api/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,29 @@ pub async fn get_domain_with_records(
Ok(opt)
}

/// Register a new domain
pub async fn register_domain(
client: &WasmerClient,
name: String,
namespace: Option<String>,
import_records: Option<bool>,
) -> Result<types::DnsDomain, anyhow::Error> {
let vars = types::RegisterDomainVars {
name,
namespace,
import_records,
};
let opt = client
.run_graphql_strict(types::RegisterDomain::build(vars))
.await
.map_err(anyhow::Error::from)?
.register_domain
.context("Domain registration failed")?
.domain
.context("Domain registration failed, no associatede domain found.")?;
Ok(opt)
}

/// Retrieve all DNS records.
///
/// NOTE: this is a privileged operation that requires extra permissions.
Expand Down
20 changes: 20 additions & 0 deletions lib/backend-api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,26 @@ mod queries {
pub get_deploy_app_version: Option<DeployAppVersion>,
}

#[derive(cynic::QueryFragment, Debug)]
pub struct RegisterDomainPayload {
pub success: bool,
pub domain: Option<DnsDomain>,
}

#[derive(cynic::QueryVariables, Debug)]
pub struct RegisterDomainVars {
pub name: String,
pub namespace: Option<String>,
pub import_records: Option<bool>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Mutation", variables = "RegisterDomainVars")]
pub struct RegisterDomain {
#[arguments(input: {name: $name, importRecords: $import_records, namespace: $namespace})]
pub register_domain: Option<RegisterDomainPayload>,
}

#[derive(cynic::QueryVariables, Debug)]
pub struct UpsertDomainFromZoneFileVars {
pub zone_file: String,
Expand Down
5 changes: 5 additions & 0 deletions lib/cli/src/commands/domain/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod get;
pub mod list;
pub mod register;
pub mod zonefile;
use crate::commands::AsyncCliCommand;

Expand All @@ -17,6 +18,9 @@ pub enum CmdDomain {

/// Sync local zone file with remotex
SyncZoneFile(self::zonefile::CmdZoneFileSync),

/// Register new domain
Register(self::register::CmdDomainRegister),
}

#[async_trait::async_trait]
Expand All @@ -29,6 +33,7 @@ impl AsyncCliCommand for CmdDomain {
CmdDomain::Get(cmd) => cmd.run_async().await,
CmdDomain::GetZoneFile(cmd) => cmd.run_async().await,
CmdDomain::SyncZoneFile(cmd) => cmd.run_async().await,
CmdDomain::Register(cmd) => cmd.run_async().await,
}
}
}
45 changes: 45 additions & 0 deletions lib/cli/src/commands/domain/register.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::{
commands::AsyncCliCommand,
opts::{ApiOpts, ItemTableFormatOpts},
};

/// Show a domain
#[derive(clap::Parser, Debug)]
pub struct CmdDomainRegister {
#[clap(flatten)]
fmt: ItemTableFormatOpts,
#[clap(flatten)]
api: ApiOpts,

/// Name of the domain.
name: String,

/// owner under which the domain will live.
#[clap(long, short)]
namespace: Option<String>,

/// auto update DNS records for this domain.
#[clap(long, short)]
import_records: bool,
}

#[async_trait::async_trait]
impl AsyncCliCommand for CmdDomainRegister {
type Output = ();

async fn run_async(self) -> Result<(), anyhow::Error> {
let client = self.api.client()?;
let domain = wasmer_api::query::register_domain(
&client,
self.name,
self.namespace,
Some(self.import_records),
)
.await?;
println!(
"{}: domain registered under owner `{}`",
domain.name, domain.owner.global_name
);
Ok(())
}
}

0 comments on commit c0c4233

Please sign in to comment.