Skip to content

Commit

Permalink
Replace get with post and use form encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
probably-not committed Jan 6, 2025
1 parent 172f280 commit cc0f1e8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
9 changes: 4 additions & 5 deletions lib/flame_ec2/ec2_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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")
]
Expand All @@ -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
Expand Down
18 changes: 8 additions & 10 deletions test/flame_ec2/ec2_api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,25 @@ 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"

assert parsed["TagSpecification.1.ResourceType"] == "instance"
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"])
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit cc0f1e8

Please sign in to comment.