Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new rover cloud config validate subcommand #2055

Merged
merged 9 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
509 changes: 234 additions & 275 deletions Cargo.lock
loshz marked this conversation as resolved.
Show resolved Hide resolved

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/rover-client/src/operations/cloud/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod fetch;
pub mod types;
pub mod update;
pub mod validate;

pub use fetch::run;
pub use types::*;
29 changes: 29 additions & 0 deletions crates/rover-client/src/operations/cloud/config/types.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use crate::operations::cloud::config::fetch::cloud_config_fetch_query;
use crate::operations::cloud::config::update::cloud_config_update_query;
use crate::operations::cloud::config::validate::cloud_config_validate_query::{
self, RouterConfigInput,
};
use crate::shared::GraphRef;

type FetchQueryVariables = cloud_config_fetch_query::Variables;
type UpdateQueryVariables = cloud_config_update_query::Variables;
type ValidateQueryVariables = cloud_config_validate_query::Variables;

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct CloudConfigFetchInput {
Expand Down Expand Up @@ -40,3 +44,28 @@ impl From<CloudConfigUpdateInput> for UpdateQueryVariables {
}
}
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct CloudConfigValidateInput {
pub graph_ref: GraphRef,
pub config: String,
}

impl From<CloudConfigValidateInput> for ValidateQueryVariables {
fn from(input: CloudConfigValidateInput) -> Self {
Self {
ref_: input.graph_ref.to_string(),
config: RouterConfigInput {
gcus: None,
graph_composition_id: None,
router_config: Some(input.config),
router_version: None,
},
}
}
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CloudConfigValidateResponse {
pub msg: String,
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mutation CloudConfigUpdateQuery($graph_id: ID!, $variant: String!, $config: Stri
variant(name: $variant) {
upsertRouterConfig(configuration: $config) {
__typename
... on RouterUpsertFailure {
... on RouterUpsertFailure {
message
}
... on GraphVariant {
Expand Down
61 changes: 61 additions & 0 deletions crates/rover-client/src/operations/cloud/config/validate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use super::types::{CloudConfigValidateInput, CloudConfigValidateResponse};

use graphql_client::*;

use crate::blocking::StudioClient;
use crate::operations::cloud::config::validate::cloud_config_validate_query::{
CloudConfigValidateQueryVariant::GraphVariant,
CloudConfigValidateQueryVariantOnGraphVariantValidateRouter::{
CloudValidationSuccess, InternalServerError, InvalidInputErrors,
},
};
use crate::shared::GraphRef;
use crate::RoverClientError;

#[derive(GraphQLQuery, Debug)]
// The paths are relative to the directory where your `Cargo.toml` is located.
// Both json and the GraphQL schema language are supported as sources for the schema
#[graphql(
query_path = "src/operations/cloud/config/validate_query.graphql",
schema_path = ".schema/schema.graphql",
response_derives = "Eq, PartialEq, Debug, Serialize, Deserialize",
deprecated = "warn"
)]
pub struct CloudConfigValidateQuery;

pub async fn run(
input: CloudConfigValidateInput,
client: &StudioClient,
) -> Result<CloudConfigValidateResponse, RoverClientError> {
let graph_ref = input.graph_ref.clone();
let data = client
.post::<CloudConfigValidateQuery>(input.into())
.await?;
build_response(graph_ref, data)
}

fn build_response(
graph_ref: GraphRef,
data: cloud_config_validate_query::ResponseData,
) -> Result<CloudConfigValidateResponse, RoverClientError> {
let typename = data
.variant
.ok_or_else(|| RoverClientError::GraphNotFound {
graph_ref: graph_ref.clone(),
})?;

let graph_variant = match typename {
GraphVariant(gv) => gv,
_ => {
return Err(RoverClientError::GraphNotFound {
graph_ref: graph_ref.clone(),
})
}
};
loshz marked this conversation as resolved.
Show resolved Hide resolved

match graph_variant.validate_router {
CloudValidationSuccess(res) => Ok(CloudConfigValidateResponse { msg: res.message }),
InvalidInputErrors(e) => Err(RoverClientError::InvalidRouterConfig { msg: e.message }),
InternalServerError(e) => Err(RoverClientError::ClientError { msg: e.message }),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
query CloudConfigValidateQuery($config: RouterConfigInput!, $ref: ID!) {
variant(ref: $ref) {
__typename
... on GraphVariant {
validateRouter(config: $config) {
__typename
... on CloudValidationSuccess {
message
}
... on InvalidInputErrors {
errors {
argument
location
reason
}
message
}
... on InternalServerError {
message
}
}
}
}
}
47 changes: 40 additions & 7 deletions src/command/cloud/config.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use clap::Parser;
use rover_client::operations::cloud::config::CloudConfigUpdateInput;
use serde::Serialize;

use crate::options::{FileOpt, GraphRefOpt, ProfileOpt};
use crate::utils::client::StudioClientConfig;
use crate::{RoverOutput, RoverResult};

use rover_client::blocking::StudioClient;
use rover_client::operations::cloud::config::{fetch, types::CloudConfigFetchInput, update};
use rover_client::operations::cloud::config::{
fetch,
types::{CloudConfigFetchInput, CloudConfigUpdateInput, CloudConfigValidateInput},
update, validate,
};

#[derive(Debug, Serialize, Parser)]
pub struct Config {
Expand All @@ -17,11 +20,14 @@ pub struct Config {

#[derive(Debug, Serialize, Parser)]
pub enum Command {
/// Get current config for a given graph ref
/// Get current router config for a given graph ref
Fetch(Fetch),

/// Update current config for a given graph ref
/// Update current router config for a given graph ref
Update(Update),

/// Validate a router config for a given graph ref
Validate(Update),
}

#[derive(Debug, Serialize, Parser)]
Expand Down Expand Up @@ -57,6 +63,10 @@ impl Config {
let client = client_config.get_authenticated_client(&args.profile)?;
self.update(client, &args.graph, &args.file).await
}
Command::Validate(args) => {
let client = client_config.get_authenticated_client(&args.profile)?;
self.validate(client, &args.graph, &args.file).await
}
}
}

Expand All @@ -65,7 +75,7 @@ impl Config {
client: StudioClient,
graph: &GraphRefOpt,
) -> RoverResult<RoverOutput> {
eprintln!("Fetching cloud config for: {}", graph.graph_ref);
println!("Fetching cloud router config for: {}", graph.graph_ref);
loshz marked this conversation as resolved.
Show resolved Hide resolved

let cloud_config = fetch::run(
CloudConfigFetchInput {
Expand All @@ -76,7 +86,6 @@ impl Config {
.await?;

Ok(RoverOutput::CloudConfigFetchResponse {
graph_ref: cloud_config.graph_ref,
config: cloud_config.config,
})
}
Expand All @@ -87,7 +96,7 @@ impl Config {
graph: &GraphRefOpt,
file: &FileOpt,
) -> RoverResult<RoverOutput> {
println!("Updating cloud config for: {}", graph.graph_ref);
println!("Updating cloud router config for: {}", graph.graph_ref);

let config = file.read_file_descriptor("Cloud Router config", &mut std::io::stdin())?;

Expand All @@ -102,4 +111,28 @@ impl Config {

Ok(RoverOutput::EmptySuccess)
}

pub async fn validate(
&self,
client: StudioClient,
graph: &GraphRefOpt,
file: &FileOpt,
) -> RoverResult<RoverOutput> {
println!("Validating cloud router config for: {}", graph.graph_ref);

let config = file.read_file_descriptor("Cloud Router config", &mut std::io::stdin())?;

let validation = validate::run(
CloudConfigValidateInput {
graph_ref: graph.graph_ref.clone(),
config,
},
&client,
)
.await?;

Ok(RoverOutput::MessageResponse {
msg: validation.msg,
})
}
}
18 changes: 9 additions & 9 deletions src/command/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ pub enum RoverOutput {
},
EmptySuccess,
CloudConfigFetchResponse {
graph_ref: GraphRef,
config: String,
},
MessageResponse {
msg: String,
},
}

impl RoverOutput {
Expand Down Expand Up @@ -447,10 +449,8 @@ impl RoverOutput {
Some(jwt.to_string())
}
RoverOutput::EmptySuccess => None,
RoverOutput::CloudConfigFetchResponse {
graph_ref: _,
config,
} => Some(config.to_string()),
RoverOutput::CloudConfigFetchResponse { config } => Some(config.to_string()),
RoverOutput::MessageResponse { msg } => Some(msg.into()),
})
}

Expand Down Expand Up @@ -569,12 +569,12 @@ impl RoverOutput {
RoverOutput::LicenseResponse { jwt, .. } => {
json!({"jwt": jwt })
}
RoverOutput::CloudConfigFetchResponse {
graph_ref: _,
config,
} => {
RoverOutput::CloudConfigFetchResponse { config } => {
json!({ "config": config })
}
RoverOutput::MessageResponse { msg } => {
json!({ "message": msg })
}
}
}

Expand Down
Loading