From cc0f1e8c953dbf1b870b3304619a47d6c026b979 Mon Sep 17 00:00:00 2001 From: Coby Benveniste Date: Mon, 6 Jan 2025 15:05:54 +0200 Subject: [PATCH] Replace get with post and use form encoding --- lib/flame_ec2/ec2_api.ex | 9 ++++----- test/flame_ec2/ec2_api_test.exs | 18 ++++++++---------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/flame_ec2/ec2_api.ex b/lib/flame_ec2/ec2_api.ex index a3f7f20..b6fa781 100644 --- a/lib/flame_ec2/ec2_api.ex +++ b/lib/flame_ec2/ec2_api.ex @@ -10,12 +10,11 @@ defmodule FlameEC2.EC2Api do require Logger def run_instances!(%BackendState{} = state) do - params = build_query_from_state(state) + params = build_params_from_state(state) uri = state.config.ec2_service_endpoint |> URI.new!() - |> URI.append_query(params) |> URI.to_string() credentials = :aws_credentials.get_credentials() @@ -27,7 +26,8 @@ defmodule FlameEC2.EC2Api do %{} -> [ url: uri, - method: :get, + method: :post, + form: params, headers: [{:accept, "application/json"}], aws_sigv4: Map.put_new(credentials, :service, "ec2") ] @@ -40,14 +40,13 @@ defmodule FlameEC2.EC2Api do end end - def build_query_from_state(%BackendState{} = state) do + def build_params_from_state(%BackendState{} = state) do state.config |> params_from_config() |> Map.merge(instance_tags(state)) |> Map.put("Action", "RunInstances") |> flatten_json_object() |> Map.filter(fn {_k, v} -> not is_nil(v) end) - |> URI.encode_query() end defp instance_tags(%BackendState{} = state) do diff --git a/test/flame_ec2/ec2_api_test.exs b/test/flame_ec2/ec2_api_test.exs index 3abd0e9..94c6a64 100644 --- a/test/flame_ec2/ec2_api_test.exs +++ b/test/flame_ec2/ec2_api_test.exs @@ -23,18 +23,17 @@ defmodule FlameEC2.EC2ApiTest do config = FlameEC2.QuickConfigs.simple_valid_config() state = FlameEC2.BackendState.new(config, []) - query = FlameEC2.EC2Api.build_query_from_state(state) - parsed = URI.decode_query(query) + parsed = FlameEC2.EC2Api.build_params_from_state(state) assert parsed["Action"] == "RunInstances" assert parsed["ImageId"] == "ami-123" assert parsed["InstanceType"] == "t3.nano" - assert parsed["MaxCount"] == "1" - assert parsed["MinCount"] == "1" + assert parsed["MaxCount"] == 1 + assert parsed["MinCount"] == 1 - assert parsed["NetworkInterface.1.AssociatePublicIpAddress"] == "false" - assert parsed["NetworkInterface.1.DeleteOnTermination"] == "true" - assert parsed["NetworkInterface.1.DeviceIndex"] == "0" + assert parsed["NetworkInterface.1.AssociatePublicIpAddress"] == false + assert parsed["NetworkInterface.1.DeleteOnTermination"] == true + assert parsed["NetworkInterface.1.DeviceIndex"] == 0 assert parsed["NetworkInterface.1.SubnetId"] == "subnet-123" assert parsed["NetworkInterface.1.SecurityGroupId.1"] == "sg-123" @@ -42,7 +41,7 @@ defmodule FlameEC2.EC2ApiTest do assert parsed["TagSpecification.1.Tag.1.Key"] == "FLAME_PARENT_IP" assert parsed["TagSpecification.1.Tag.1.Value"] == context[:local_ipv4] assert parsed["TagSpecification.1.Tag.2.Key"] == "FLAME_PARENT_APP" - assert parsed["TagSpecification.1.Tag.2.Value"] == "flame_ec2" + assert parsed["TagSpecification.1.Tag.2.Value"] == :flame_ec2 assert parsed["UserData"] {:ok, decoded} = Base.decode64(parsed["UserData"]) @@ -72,8 +71,7 @@ defmodule FlameEC2.EC2ApiTest do state = FlameEC2.BackendState.new(config, []) - query = FlameEC2.EC2Api.build_query_from_state(state) - parsed = URI.decode_query(query) + parsed = FlameEC2.EC2Api.build_params_from_state(state) assert parsed["LaunchTemplate.LaunchTemplateId"] == "lt-123" assert parsed["LaunchTemplate.Version"] == "1"