Skip to content

Commit

Permalink
Merge pull request #57 from lexara-prime-ai/dev
Browse files Browse the repository at this point in the history
Add Functionality to List All Suspended Services
  • Loading branch information
irfanghat authored Jul 19, 2024
2 parents 99bcfe3 + af01d81 commit 5f0a0b5
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 43 deletions.
82 changes: 43 additions & 39 deletions render_cdk/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(unused)]
// #![deny(missing_docs)]

use render_cdk::environment_management::prelude::*;
use render_cdk::iaas::prelude::*;
Expand All @@ -19,68 +20,71 @@ async fn main() {
/// List all Services.
// let services = ServiceManager::list_all_services("50").await;

/// List all Services by Name and Type.
// List all Services that are suspended/not_suspended.
let services = ServiceManager::list_all_suspended_services("suspended", "50").await;

// List all Services by Name and Type.
// let services = ServiceManager::find_service_by_name_and_type("whoami", "web_service").await;

/// List all Services by Region.
// List all Services by Region.
// let services = ServiceManager::find_service_by_region("oregon", "10").await;

/// List all Services by Environment.
// List all Services by Environment.
// let services = ServiceManager::find_service_by_environment("image", "10").await;
////////////////////////////////////////////////
///
/// 2. Using simple .conf files for resource provisioning.
//
// 2. Using simple .conf files for resource provisioning.
// let config = config::Conf::read_configuration_file().unwrap();
// println!("Sample Configuration: {:?}\n", config);

/// 3. Retrieve a list of authorized 'users'.
let authorized_user = Owner::list_authorized_users("[email protected]", "100")
.await
.unwrap();
// 3. Retrieve a list of authorized 'users'.
// let authorized_user = Owner::list_authorized_users("[email protected]", "100")
// .await
// .unwrap();

////////////////////////////
// [DEBUG] logs.
///////////////////////////
println!("Owner Info.: {:?}\n", authorized_user);
// println!("Owner Info.: {:?}\n", authorized_user);

///////////////////////////
// Retrieving the <owner_id>. This is used to tie a <resource> to the user who created it.
let owner_id = authorized_user
.get(0)
.map(|owner_response| owner_response.owner.id.clone())
.expect("No authorized users found.");
// let owner_id = authorized_user
// .get(0)
// .map(|owner_response| owner_response.owner.id.clone())
// .expect("No authorized users found.");

// /// 4. Creating services.
// The following is a sample deployment configuration.
let deployment_config = template::Template {
type_: "static_site".to_owned(), // Options ->
name: "test_deployment".to_owned(),
owner_id,
repo: "https://github.com/lexara-prime-ai/SAMPLE_STATIC_SITE".to_owned(),
auto_deploy: "yes".to_owned(), // By default, Render automatically deploys your service whenever you update its code or configuration.
branch: None,
image: None,
build_filter: None,
root_dir: Some("./public".to_owned()),
env_vars: vec![],
secret_files: vec![],
service_details: Some(ServiceDetails {
build_command: None, // Render runs this command to build your app before each deploy e.g npm run build, yarn build.
headers: vec![],
publish_path: Some("./".to_owned()), // This will translate to /public/
pull_request_previews_enabled: Some("yes".to_owned()),
routes: vec![],
}),
};
// let deployment_config = template::Template {
// type_: "static_site".to_owned(), // Options ->
// name: "test_deployment".to_owned(),
// owner_id,
// repo: "https://github.com/lexara-prime-ai/SAMPLE_STATIC_SITE".to_owned(),
// auto_deploy: "yes".to_owned(), // By default, Render automatically deploys your service whenever you update its code or configuration.
// branch: None,
// image: None,
// build_filter: None,
// root_dir: Some("./public".to_owned()),
// env_vars: vec![],
// secret_files: vec![],
// service_details: Some(ServiceDetails {
// build_command: None, // Render runs this command to build your app before each deploy e.g npm run build, yarn build.
// headers: vec![],
// publish_path: Some("./".to_owned()), // This will translate to /public/
// pull_request_previews_enabled: Some("yes".to_owned()),
// routes: vec![],
// }),
// };

//////////////////////////
// [DEBUG] logs.
/////////////////////////
LOGGER::INFO(
"Deployment Config. : ",
&deployment_config.to_json_string(),
LogLevel::WARN,
);
// LOGGER::INFO(
// "Deployment Config. : ",
// &deployment_config.to_json_string(),
// LogLevel::WARN,
// );

// let service = ServiceManager::create_service(deployment_config)
// .await
Expand Down
69 changes: 65 additions & 4 deletions render_cdk/src/resource_management/services/service_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ pub trait ServiceManagerOperations {
fn list_all_services(
limit: &str,
) -> impl std::future::Future<Output = Result<String, Error>> + Send;
fn list_all_suspended_services(
service_status: &str,
limit: &str,
) -> impl std::future::Future<Output = Result<String, Error>> + Send;
fn find_service_by_name_and_type(
service_name: &str,
service_type: &str,
Expand All @@ -37,6 +41,7 @@ pub trait ServiceManagerOperations {
service_env: &str,
limit: &str,
) -> impl std::future::Future<Output = Result<String, Error>> + Send;

////////////////////////////////
///////////////////////////////
// /// Creating services.
Expand Down Expand Up @@ -65,7 +70,59 @@ impl ServiceManagerOperations for ServiceManager {
//////////////////////////////
////// [DEBUG] logs. /////////
//////////////////////////////
// LOGGER::INFO("Processing [REQUEST] -> ", &api_url, LogLevel::WARN);
LOGGER::INFO("Processing [REQUEST] -> ", &api_url, LogLevel::WARN);
// LOGGER::INFO("Processing [REQUEST] -> ", &api_key, LogLevel::WARN);
//////////////////////////////

let response = client
.get(api_url)
.header(ACCEPT, "application/json")
.header(AUTHORIZATION, format!("Bearer {}", api_key))
.send()
.await
.context("Error sending request.")?;

//////////////////////////////
if response.status().is_success() {
let results = response.text().await.context("Error parsing response.")?;
LOGGER::INFO("[RESPONSE]", &results, LogLevel::SUCCESS);
Ok(results)
} else {
LOGGER::INFO("[RESPONSE STATUS] -> ", "FAILED", LogLevel::CRITICAL);
Err(anyhow::anyhow!(
"Request failed with status: {}",
response.status()
))
}
}

/// Finding all suspended services.
/// Reqquired arguments: <service_status> i.e suspended/not_suspended.
async fn list_all_suspended_services(
service_status: &str,
limit: &str,
) -> Result<String, Error> {
/*****************************************************
*
curl --request GET \
--url 'https://api.render.com/v1/services?suspended=suspended&limit=20' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer {{render_api_token_goes_here}}'
*****************************************************************/

/////////////////////////////
let client = State::init().await.CLIENT;
let api_key = State::init().await.API_KEY;
let api_url = format!(
"{}{}{}{}{}",
BASE_URL, "/services?suspended=", service_status, "&limit=", limit
);

//////////////////////////////
////// [DEBUG] logs. /////////
//////////////////////////////
LOGGER::INFO("Processing [REQUEST] -> ", &api_url, LogLevel::WARN);
// LOGGER::INFO("Processing [REQUEST] -> ", &api_key, LogLevel::WARN);
//////////////////////////////

Expand All @@ -83,6 +140,7 @@ impl ServiceManagerOperations for ServiceManager {
LOGGER::INFO("[RESPONSE]", &results, LogLevel::SUCCESS);
Ok(results)
} else {
LOGGER::INFO("[RESPONSE STATUS] -> ", "FAILED", LogLevel::CRITICAL);
Err(anyhow::anyhow!(
"Request failed with status: {}",
response.status()
Expand Down Expand Up @@ -116,7 +174,7 @@ impl ServiceManagerOperations for ServiceManager {
//////////////////////////////
////// [DEBUG] logs. /////////
//////////////////////////////
// LOGGER::INFO("Processing [REQUEST] -> ", &api_url, LogLevel::WARN);
LOGGER::INFO("Processing [REQUEST] -> ", &api_url, LogLevel::WARN);
// LOGGER::INFO("Processing [REQUEST] -> ", &api_key, LogLevel::WARN);
//////////////////////////////

Expand All @@ -134,6 +192,7 @@ impl ServiceManagerOperations for ServiceManager {
LOGGER::INFO("[RESPONSE]", &results, LogLevel::SUCCESS);
Ok(results)
} else {
LOGGER::INFO("[RESPONSE STATUS] -> ", "FAILED", LogLevel::CRITICAL);
Err(anyhow::anyhow!(
"Request failed with status: {}",
response.status()
Expand Down Expand Up @@ -163,7 +222,7 @@ impl ServiceManagerOperations for ServiceManager {
//////////////////////////////
////// [DEBUG] logs. /////////
//////////////////////////////
// LOGGER::INFO("Processing [REQUEST] -> ", &api_url, LogLevel::WARN);
LOGGER::INFO("Processing [REQUEST] -> ", &api_url, LogLevel::WARN);
// LOGGER::INFO("Processing [REQUEST] -> ", &api_key, LogLevel::WARN);
//////////////////////////////

Expand All @@ -181,6 +240,7 @@ impl ServiceManagerOperations for ServiceManager {
LOGGER::INFO("[RESPONSE]", &results, LogLevel::SUCCESS);
Ok(results)
} else {
LOGGER::INFO("[RESPONSE STATUS] -> ", "FAILED", LogLevel::CRITICAL);
Err(anyhow::anyhow!(
"Request failed with status: {}",
response.status()
Expand Down Expand Up @@ -210,7 +270,7 @@ impl ServiceManagerOperations for ServiceManager {
//////////////////////////////
////// [DEBUG] logs. /////////
//////////////////////////////
// LOGGER::INFO("Processing [REQUEST] -> ", &api_url, LogLevel::WARN);
LOGGER::INFO("Processing [REQUEST] -> ", &api_url, LogLevel::WARN);
// LOGGER::INFO("Processing [REQUEST] -> ", &api_key, LogLevel::WARN);
//////////////////////////////
let response = client
Expand All @@ -227,6 +287,7 @@ impl ServiceManagerOperations for ServiceManager {
LOGGER::INFO("[RESPONSE]", &results, LogLevel::SUCCESS);
Ok(results)
} else {
LOGGER::INFO("[RESPONSE STATUS] -> ", "FAILED", LogLevel::CRITICAL);
Err(anyhow::anyhow!(
"Request failed with status: {}",
response.status()
Expand Down

0 comments on commit 5f0a0b5

Please sign in to comment.