Skip to content

Commit

Permalink
Small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
EricGhildyal committed Dec 2, 2024
1 parent 6e6cca2 commit 778ccc9
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/adapters/ingresses/apig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl AwsApiGateway {
}

/// Helper function to convert an API Gateway's name to its auto-generated AWS ID
pub async fn get_resource_id_by_name(
pub async fn get_resource_id_by_path(
&self,
api_id: &str,
resource_name: &str,
Expand Down Expand Up @@ -133,7 +133,7 @@ impl Ingress for AwsApiGateway {
let function_code = FunctionCode::builder().zip_file(code).build();
let zip_file = function_code
.zip_file()
.ok_or(miette!("Couldn't zip lambda code"))?;
.ok_or(miette!("Couldn't zip Lambda code"))?;

// Upload it to Lambda and ensure to create a new version
let lambda_res = self
Expand All @@ -149,10 +149,18 @@ impl Ingress for AwsApiGateway {
// NOTE: this ARN includes the new version number appended to the end
let lambda_arn = lambda_res
.function_arn()
.ok_or(miette!("Couldn't get ARN of deployed lambda"))?;
.ok_or(miette!("Couldn't get ARN of deployed Lambda"))?;

// Get the auto-generated API ID and Resource ID
let api = self.get_api_id_by_name(&self.gateway_name).await?;
let api_id = api.id().ok_or(miette!("Couldn't get ID of deployed API"))?;
let api_id = api.id().ok_or(miette!("Couldn't get ID of API Gateway"))?;

let resource = self
.get_resource_id_by_path(api_id, &self.resource_path)
.await?;
let resource_id = resource
.id()
.ok_or(miette!("Couldn't get ID of API Gateway Resource"))?;

// Ensure we add invoke permissions to the new version of the lambda
// NOTE: All calls to invoke the function will fail unless this is explicitly added
Expand All @@ -166,6 +174,7 @@ impl Ingress for AwsApiGateway {
.await
.into_diagnostic()?;

// Update our API Gateway to point at our new lambda version
let patch_op = PatchOperation::builder()
.op(Op::Replace)
.path("/uri")
Expand All @@ -175,14 +184,6 @@ impl Ingress for AwsApiGateway {
))
.build();

let resource = self
.get_resource_id_by_name(api_id, &self.resource_path)
.await?;
let resource_id = resource
.id()
.ok_or(miette!("Couldn't get ID of Resource"))?;

// Next, we need to update our API Gateway to point at our new lambda version
self.apig_client
.update_integration()
.rest_api_id(api_id)
Expand Down

0 comments on commit 778ccc9

Please sign in to comment.