Skip to content

Commit

Permalink
Bumped crate version. (#98)
Browse files Browse the repository at this point in the history
## Changes

- Improved error handling.
- Added method for `delete_postgres_instance` method.
- Added `create_<method>_request` macros(Reduce boilerplate).
  • Loading branch information
lexara-prime-ai authored Aug 18, 2024
2 parents 14f15ac + b2ec095 commit 1fb3ebe
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 76 deletions.
2 changes: 1 addition & 1 deletion render_cdk/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion render_cdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "render_cdk"
version = "0.0.18"
version = "0.0.19"
edition = "2021"
authors = ["Irfan Ghat"]
description = "This crate provides a streamlined interface for interacting with Render, a platform that allows you to build, deploy, and scale your apps with ease."
Expand Down
2 changes: 1 addition & 1 deletion render_cdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Add `render_cdk` to your `Cargo.toml`:

```toml
[dependencies]
render_cdk = "0.0.18"
render_cdk = "0.0.19"
```

* _Alternatively_, running at the `cargo add render_cdk` **root** of your project will also add **render_cdk** to your project.
Expand Down
2 changes: 1 addition & 1 deletion render_cdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
//! ```toml
//! [dependencies]
//! tokio = { version = "1.37.0", features = ["full"] }
//! render_cdk = "0.0.18"
//! render_cdk = "0.0.19"
//! ```
//!
//! ## Examples
Expand Down
4 changes: 3 additions & 1 deletion render_cdk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ async fn main() {
// let services = ServiceManager::find_service_by_environment("image", "50").await;

// let databases = ServiceManager::list_postgres_instances(true, "50").await;
// let databases = ServiceManager::find_postgres_instance_by_name("agilecomet", true, "100").await;
// let databases =
// ServiceManager::find_postgres_instance_by_name("fluentcomet", true, "100").await;
// let databases =
// ServiceManager::find_postgres_instance_with_status("suspended", true, "50").await;

Expand Down Expand Up @@ -118,6 +119,7 @@ async fn main() {
// sleep(Duration::from_secs(150)).await;
// ServiceManager::delete_service("test_static", "static").await;
// ServiceManager::delete_service("test_web", "web_service").await;
// ServiceManager::delete_postgres_instance("xenialterra").await;
}

/// Mandatory Regression Tests.
Expand Down
131 changes: 62 additions & 69 deletions render_cdk/src/resource_management/services/service_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ use crate::utils::request_manager;
// [DEBUG] utils.
use crate::logger::prelude::*;
use crate::utils::stringify::Stringify;
use crate::{create_request, create_request_with_body, LOGGER};
use crate::{
create_delete_request, create_get_request, create_post_request, handle_response, LOGGER,
};
use colored::Colorize;

// Predefined [CONSTANTS].
Expand Down Expand Up @@ -102,6 +104,11 @@ pub trait ServiceManagerOperations {
service_name: &str,
service_type: &str,
) -> impl std::future::Future<Output = Result<Value, Error>> + Send;

/// Delete postgres instance.
fn delete_postgres_instance(
name: &str,
) -> impl std::future::Future<Output = Result<Value, Error>> + Send;
}

impl ServiceManagerOperations for ServiceManager {
Expand All @@ -123,7 +130,7 @@ impl ServiceManagerOperations for ServiceManager {
// [DEBUG] logs.
LOGGER!("\nProcessing <request> -> ", &api_url, LogLevel::WARN);

let response = create_request!(client, api_url, api_key)?;
let response = create_get_request!(client, api_url, api_key)?;

// Validate <response> status.
if response.status().is_success() {
Expand Down Expand Up @@ -179,7 +186,7 @@ impl ServiceManagerOperations for ServiceManager {
// [DEBUG] logs.
LOGGER!("\nProcessing <request> -> ", &api_url, LogLevel::WARN);

let response = create_request!(client, api_url, api_key)?;
let response = create_get_request!(client, api_url, api_key)?;

// Validate response.
if response.status().is_success() {
Expand Down Expand Up @@ -245,7 +252,7 @@ impl ServiceManagerOperations for ServiceManager {
// [DEBUG] logs.
LOGGER!("\nProcessing <request> -> ", &api_url, LogLevel::WARN);

let response = create_request!(client, api_url, api_key)?;
let response = create_get_request!(client, api_url, api_key)?;

// Validate response.
if response.status().is_success() {
Expand Down Expand Up @@ -311,7 +318,7 @@ impl ServiceManagerOperations for ServiceManager {
// [DEBUG] logs.
LOGGER!("\nProcessing <request> -> ", &api_url, LogLevel::WARN);

let response = create_request!(client, api_url, api_key)?;
let response = create_get_request!(client, api_url, api_key)?;

// Validate response.
if response.status().is_success() {
Expand Down Expand Up @@ -368,7 +375,7 @@ impl ServiceManagerOperations for ServiceManager {
// [DEBUG] logs.
LOGGER!("\nProcessing <request> -> ", &api_url, LogLevel::WARN);

let response = create_request!(client, api_url, api_key)?;
let response = create_get_request!(client, api_url, api_key)?;

// Validate response status.
if response.status().is_success() {
Expand Down Expand Up @@ -429,7 +436,7 @@ impl ServiceManagerOperations for ServiceManager {
// [DEBUG] logs.
LOGGER!("\nProcessing <request> -> ", &api_url, LogLevel::WARN);

let response = create_request!(client, api_url, api_key)?;
let response = create_get_request!(client, api_url, api_key)?;

// Validate response status.
if response.status().is_success() {
Expand Down Expand Up @@ -486,7 +493,7 @@ impl ServiceManagerOperations for ServiceManager {
// [DEBUG] logs.
LOGGER!("\nProcessing <request> -> ", &api_url, LogLevel::WARN);

let response = create_request!(client, api_url, api_key)?;
let response = create_get_request!(client, api_url, api_key)?;

// Validate response status.
if response.status().is_success() {
Expand Down Expand Up @@ -543,7 +550,7 @@ impl ServiceManagerOperations for ServiceManager {
// [DEBUG] logs.
LOGGER!("\nProcessing <request> -> ", &api_url, LogLevel::WARN);

let response = create_request!(client, api_url, api_key)?;
let response = create_get_request!(client, api_url, api_key)?;

// Validate response status.
if response.status().is_success() {
Expand Down Expand Up @@ -634,32 +641,9 @@ impl ServiceManagerOperations for ServiceManager {
LOGGER!("\nProcessing <request> -> ", &api_url, LogLevel::WARN);
LOGGER!("[PAYLOAD] -> ", &payload, LogLevel::WARN);

let response =
create_request_with_body!(client, api_url, api_key, payload, "<create_service>")?;
let response = create_post_request!(client, api_url, api_key, payload, "<create_service>")?;

// Validate response status.
if response.status().is_success() {
let result = response.text().await.context("Error parsing response.")?;
let data: Value = serde_json::from_str(&result)?;

LOGGER!("<response> -> ", format!("{:#?}", data), LogLevel::SUCCESS);

Ok(data)
} else {
let result = response.text().await.context("Error parsing response.")?;
let data: Value = serde_json::from_str(&result)?;
let message = data["message"]
.as_str()
.unwrap_or("An error occured :: Process -> <create_service>");

LOGGER!(
"<response status> -> ",
format!("{:#?}", message),
LogLevel::CRITICAL
);

Err(anyhow::anyhow!("<Error>: {:#?}", data))
}
handle_response!(response, "<create_service>")
}

async fn deploy_configuration(config_path: &str) -> Result<String, Error> {
Expand Down Expand Up @@ -698,8 +682,7 @@ impl ServiceManagerOperations for ServiceManager {

LOGGER!("[PAYLOAD] :: -> ", &payload, LogLevel::WARN);

let response =
create_request_with_body!(client, api_url, api_key, payload, "<postgres>")?;
let response = create_post_request!(client, api_url, api_key, payload, "<postgres>")?;

if response.status().is_success() {
let result = response.text().await.context("Error parsing response.")?;
Expand Down Expand Up @@ -760,7 +743,7 @@ impl ServiceManagerOperations for ServiceManager {

LOGGER!("[PAYLOAD] :: -> ", &payload, LogLevel::WARN);

let response = create_request_with_body!(client, api_url, api_key, payload, "<redis>")?;
let response = create_post_request!(client, api_url, api_key, payload, "<redis>")?;

if response.status().is_success() {
let result = response.text().await.context("Error parsing response.")?;
Expand Down Expand Up @@ -833,46 +816,56 @@ impl ServiceManagerOperations for ServiceManager {
let api_key = State::init().await.API_KEY;
let service_url = format!("{}{}{}", BASE_URL, "/services/", id);

// // [DEBUG] logs.
// [DEBUG] logs.
LOGGER!(
"\nProcessing <request> :: <delete> -> ",
&service_url,
LogLevel::WARN
);

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

// Validate response status.
if response.status().is_success() {
let result = response.text().await.context("Error parsing response.")?;
let data: Value = serde_json::from_str(&result)?;

LOGGER!("<response> -> ", format!("{:#?}", data), LogLevel::SUCCESS);

Ok(data)
} else {
let result = response.text().await.context("Error parsing response.")?;
let data: Value = serde_json::from_str(&result)?;
let message = data["message"]
.as_str()
.unwrap_or("An error occured :: Process -> <create_service>");

LOGGER!(
"<response status> -> ",
format!("{:#?}", message),
LogLevel::CRITICAL
);

Err(anyhow::anyhow!("<Error>: {:#?}", data))
}
let response = create_delete_request!(client, service_url, api_key)?;

handle_response!(response, "<delete_service>")
}
None => Err(anyhow::anyhow!("Service Id not found.")),
}
}

/// Deleting postgres instances.
async fn delete_postgres_instance(name: &str) -> Result<Value, Error> {
/*****************************************************
*
curl --request DELETE \
--url https://api.render.com/v1/postgres/postgresId \
--header 'accept: application/json' \
--header 'Authorization: Bearer {{render_api_token_goes_here}}'
*****************************************************************/

let postgres_instance =
ServiceManager::find_postgres_instance_by_name(name, true, "100").await?;

// Retrieve <postgres_id>.
let postgres_id = postgres_instance[0]["postgres"]["id"].as_str();

match postgres_id {
Some(id) => {
let client = State::init().await.CLIENT;
let api_key = State::init().await.API_KEY;
let postgres_url = format!("{}{}{}", BASE_URL, "/postgres/", id);

// [DEBUG] logs.
LOGGER!(
"\nProcessing <request> :: <delete> -> ",
&postgres_url,
LogLevel::WARN
);

let response = create_delete_request!(client, postgres_url, api_key)?;

handle_response!(response, "<delete_postgres_instance>")
}
None => Err(anyhow::anyhow!("Postgres Id not found.")),
}
}
}
Loading

0 comments on commit 1fb3ebe

Please sign in to comment.