use crate::errors::{self, Error, Result};
use aws_sdk_autoscaling::{operation::set_instance_health::SetInstanceHealthError, Client};
use aws_smithy_client::SdkError;
use aws_types::SdkConfig as AwsSdkConfig;
#[derive(Debug, Clone)]
pub struct Manager {
pub region: String,
pub cli: Client,
}
impl Manager {
pub fn new(shared_config: &AwsSdkConfig) -> Self {
Self {
region: shared_config.region().unwrap().to_string(),
cli: Client::new(shared_config),
}
}
pub async fn set_instance_health(&self, instance_id: &str, status: &str) -> Result<()> {
log::info!(
"setting instance health for '{instance_id}' with '{status}' in region '{}'",
self.region
);
let ret = self
.cli
.set_instance_health()
.instance_id(instance_id)
.health_status(status)
.send()
.await;
let resp = match ret {
Ok(v) => v,
Err(e) => {
return Err(Error::API {
message: format!("failed set_instance_health {:?}", e),
retryable: errors::is_sdk_err_retryable(&e)
|| is_err_retryable_set_instance_health(&e),
});
}
};
log::info!(
"successfully set instance health for '{}' with {} (output: {:?})",
instance_id,
status,
resp
);
Ok(())
}
}
#[inline]
fn is_err_retryable_set_instance_health(
e: &SdkError<
SetInstanceHealthError,
aws_smithy_runtime_api::client::orchestrator::HttpResponse,
>,
) -> bool {
match e {
SdkError::ServiceError(err) => err.err().is_resource_contention_fault(),
_ => false,
}
}