Skip to content

Commit

Permalink
Add helper method for apig name -> apig id
Browse files Browse the repository at this point in the history
  • Loading branch information
EricGhildyal authored and RobbieMcKinstry committed Nov 26, 2024
1 parent 0dbd4dd commit 823cb8b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
*.zip
29 changes: 26 additions & 3 deletions src/adapters/ingresses/apig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::WholePercent;

use super::Ingress;
use async_trait::async_trait;
use aws_sdk_apigateway::types::{Op, PatchOperation};
use aws_sdk_apigateway::types::{Op, PatchOperation, RestApi};
use miette::miette;
use miette::{IntoDiagnostic, Result};
use tokio::{fs::File, io::AsyncReadExt};
Expand Down Expand Up @@ -117,12 +117,35 @@ impl AwsApiGateway {
Ok(())
}

pub async fn get_api_id_by_name(&self, api_name: &str) -> Result<RestApi> {
// Given an API Gateway's name, return its auto-generated AWS ID
let all_apis = self
.apig_client
.get_rest_apis()
.send()
.await
.into_diagnostic()?;

let api = all_apis
.items()
.iter()
.find(|api| api.name.clone().unwrap() == api_name)
.ok_or(miette!(
"Could not find an API Gateway with the name: {}",
api_name
))?;

Ok(api.clone())
}

pub async fn update_canary_traffic(
&self,
api_id: &str,
api_name: &str,
stage_name: &str,
traffic_percentage: WholePercent,
) -> Result<()> {
let api = self.get_api_id_by_name(api_name).await?;

let patch_op = PatchOperation::builder()
.op(Op::Replace)
.path("/canarySettings/percentTraffic")
Expand All @@ -131,7 +154,7 @@ impl AwsApiGateway {

self.apig_client
.update_stage()
.rest_api_id(api_id)
.rest_api_id(api.id.unwrap_or_default())
.stage_name(stage_name)
.patch_operations(patch_op)
.send()
Expand Down

0 comments on commit 823cb8b

Please sign in to comment.