Skip to content

Commit

Permalink
User upserting for size profile
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Jul 18, 2024
1 parent 77718bb commit 21a4661
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/uplink/packages.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ defmodule Uplink.Packages do
defdelegate get_or_create_project_name(client, metadata),
to: Metadata.Manager

defdelegate get_or_create_size_profile(cient, metadata),
defdelegate get_or_upsert_size_profile(cient, metadata),
to: Metadata.Manager

defdelegate profile_name(metadata),
Expand Down
2 changes: 1 addition & 1 deletion lib/uplink/packages/instance/bootstrap.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ defmodule Uplink.Packages.Instance.Bootstrap do
profile_name = Packages.profile_name(metadata)

size_profile_name =
Packages.get_or_create_size_profile(client, metadata)
Packages.get_or_upsert_size_profile(client, metadata)

lxd_project_name = Packages.get_or_create_project_name(client, metadata)

Expand Down
2 changes: 1 addition & 1 deletion lib/uplink/packages/instance/upgrade.ex
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ defmodule Uplink.Packages.Instance.Upgrade do

defp handle_update_config(client, instance, metadata) do
profile_name = Packages.profile_name(metadata)
size_profile_name = Packages.get_or_create_size_profile(client, metadata)
size_profile_name = Packages.get_or_upsert_size_profile(client, metadata)

profiles = [profile_name, "default"]

Expand Down
57 changes: 38 additions & 19 deletions lib/uplink/packages/metadata/manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ defmodule Uplink.Packages.Metadata.Manager do
end
end

def get_or_create_size_profile(_client, %Metadata{package_size: nil}), do: nil
def get_or_upsert_size_profile(_client, %Metadata{package_size: nil}), do: nil

def get_or_create_size_profile(
def get_or_upsert_size_profile(
client,
%Metadata{package_size: %Metadata.Size{}} = metadata
) do
Expand All @@ -43,8 +43,8 @@ defmodule Uplink.Packages.Metadata.Manager do
client
|> Lexdee.get_profile(size_profile)
|> case do
{:ok, %{body: %{"name" => name}}} ->
name
{:ok, %{body: %{"name" => _name}}} ->
update_size_profile(client, metadata)

{:error, %{"error_code" => 404}} ->
create_size_profile(client, metadata)
Expand Down Expand Up @@ -77,10 +77,39 @@ defmodule Uplink.Packages.Metadata.Manager do
end
end

defp create_size_profile(
client,
%Metadata{package_size: package_size} = metadata
) do
defp create_size_profile(client, %Metadata{} = metadata) do
profile_params = build_size_config(metadata)
profile_name = profile_params["name"]

client
|> Lexdee.create_profile(profile_params)
|> case do
{:ok, %{body: nil}} ->
profile_name

{:error, %{"error" => _message}} ->
nil
end
end

defp update_size_profile(client, %Metadata{} = metadata) do
profile_params = build_size_config(metadata)
profile_name = profile_params["name"]

profile_params = Map.delete(profile_params, "name")

client
|> Lexdee.update_profile(profile_name, profile_params)
|> case do
{:ok, %{body: _body}} ->
profile_name

{:error, %{"error" => _message}} ->
nil
end
end

defp build_size_config(%Metadata{package_size: package_size} = metadata) do
profile_name = size_profile_name(metadata)

config = %{
Expand Down Expand Up @@ -108,22 +137,12 @@ defmodule Uplink.Packages.Metadata.Manager do
config
end

params = %{
%{
"name" => profile_name,
"config" => config,
"description" =>
"Size profile for #{metadata.channel.package.organization.slug}/#{metadata.channel.package.slug}"
}

client
|> Lexdee.create_profile(params)
|> case do
{:ok, %{body: nil}} ->
profile_name

{:error, %{"error" => _message}} ->
nil
end
end

defp project_name(%Metadata{channel: channel}) do
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/lxd/profiles/show_size_profile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"type": "sync",
"status": "Success",
"status_code": 200,
"operation": "",
"error_code": 0,
"error": "",
"metadata": {
"name": "example.medium",
"description": "",
"config": {
"limits.cpu": "1",
"limits.memory": "1GiB"
},
"devices": {},
"used_by": []
}
}
Loading

0 comments on commit 21a4661

Please sign in to comment.