Skip to content

Commit 904aaf3

Browse files
committed
fix: formatting and remove redundant transaction
1 parent e5bc095 commit 904aaf3

File tree

5 files changed

+26
-40
lines changed

5 files changed

+26
-40
lines changed

guard/lib/guard/grpc_servers/service_account_server.ex

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@ defmodule Guard.GrpcServers.ServiceAccountServer do
33

44
require Logger
55

6-
import Guard.Utils, only: [grpc_error!: 2, valid_uuid?: 1, validate_uuid!: 1]
6+
import Guard.Utils, only: [grpc_error!: 2, validate_uuid!: 1]
77
import Guard.GrpcServers.Utils, only: [observe_and_log: 3]
88

99
alias Guard.Store.ServiceAccount
1010
alias Guard.Api.Organization
1111
alias Google.Protobuf.Timestamp
1212
alias InternalApi.ServiceAccount, as: ServiceAccountPB
1313

14-
@user_exchange "user_exchange"
15-
@updated_routing_key "updated"
16-
@deleted_routing_key "deleted"
17-
1814
@spec create(ServiceAccountPB.CreateRequest.t(), GRPC.Server.Stream.t()) ::
1915
ServiceAccountPB.CreateResponse.t()
2016
def create(

guard/lib/guard/service_account/actions.ex

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ defmodule Guard.ServiceAccount.Actions do
99

1010
require Logger
1111

12-
alias Guard.Repo
1312
alias Guard.FrontRepo
1413
alias Guard.Store.ServiceAccount
1514

@@ -20,8 +19,6 @@ defmodule Guard.ServiceAccount.Actions do
2019
creator_id: String.t()
2120
}
2221

23-
@exchange_name "user_exchange"
24-
2522
@doc """
2623
Create a new service account.
2724
@@ -38,7 +35,8 @@ defmodule Guard.ServiceAccount.Actions do
3835
@spec create(service_account_params()) ::
3936
{:ok, %{service_account: map(), api_token: String.t()}} | {:error, atom() | list()}
4037
def create(
41-
%{org_id: org_id, name: name, description: description, creator_id: creator_id} = params
38+
%{org_id: _org_id, name: _name, description: _description, creator_id: _creator_id} =
39+
params
4240
) do
4341
case _create(params) do
4442
{:ok, {service_account, api_token}} ->
@@ -127,7 +125,7 @@ defmodule Guard.ServiceAccount.Actions do
127125
# Use nested transactions because FrontRepo and Repo are different databases
128126
FrontRepo.transaction(fn ->
129127
with {:ok, result} <- ServiceAccount.create(params),
130-
{:ok, rbac_user} <- create_rbac_user_in_transaction(result.service_account) do
128+
{:ok, _rbac_user} <- create_rbac_user_in_transaction(result.service_account) do
131129
# Return the service account data and API token
132130
{result.service_account, result.api_token}
133131
else
@@ -139,24 +137,19 @@ defmodule Guard.ServiceAccount.Actions do
139137

140138
defp create_rbac_user_in_transaction(service_account) do
141139
# RBAC operations use Guard.Repo (different database from FrontRepo)
142-
case Repo.transaction(fn ->
143-
case Guard.Store.RbacUser.create(
144-
service_account.id,
145-
service_account.email,
146-
service_account.name
147-
) do
148-
:ok ->
149-
case Guard.Store.RbacUser.fetch(service_account.id) do
150-
nil -> Repo.rollback(:rbac_user_not_found)
151-
rbac_user -> rbac_user
152-
end
153-
154-
:error ->
155-
Repo.rollback(:rbac_user_creation_failed)
156-
end
157-
end) do
158-
{:ok, rbac_user} -> {:ok, rbac_user}
159-
{:error, error} -> {:error, error}
140+
case Guard.Store.RbacUser.create(
141+
service_account.id,
142+
service_account.email,
143+
service_account.name
144+
) do
145+
:ok ->
146+
case Guard.Store.RbacUser.fetch(service_account.id) do
147+
nil -> {:error, :rbac_user_not_found}
148+
rbac_user -> {:ok, rbac_user}
149+
end
150+
151+
:error ->
152+
{:error, :rbac_user_creation_failed}
160153
end
161154
end
162155
end

guard/lib/guard/store/service_account.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ defmodule Guard.Store.ServiceAccount do
1212
import Guard.Utils, only: [valid_uuid?: 1]
1313

1414
alias Guard.FrontRepo
15-
alias Guard.FrontRepo.{User, ServiceAccount, Organization}
15+
alias Guard.FrontRepo.{User, ServiceAccount}
1616
alias Guard.AuthenticationToken
1717
alias Ecto.Changeset
1818

@@ -119,7 +119,7 @@ defmodule Guard.Store.ServiceAccount do
119119
def update(service_account_id, params) when is_binary(service_account_id) do
120120
if valid_uuid?(service_account_id) do
121121
FrontRepo.transaction(fn ->
122-
with {:ok, current_data} <- find(service_account_id),
122+
with {:ok, _current_data} <- find(service_account_id),
123123
{:ok, updated_user} <- update_user_record(service_account_id, params),
124124
{:ok, updated_service_account} <-
125125
update_service_account_record(service_account_id, params) do
@@ -150,7 +150,7 @@ defmodule Guard.Store.ServiceAccount do
150150
def delete(service_account_id) when is_binary(service_account_id) do
151151
if valid_uuid?(service_account_id) do
152152
case FrontRepo.transaction(fn ->
153-
with {:ok, current_data} <- find(service_account_id),
153+
with {:ok, _current_data} <- find(service_account_id),
154154
{:ok, _updated_user} <- deactivate_user_record(service_account_id) do
155155
:deleted
156156
else
@@ -187,7 +187,7 @@ defmodule Guard.Store.ServiceAccount do
187187
def regenerate_token(service_account_id) when is_binary(service_account_id) do
188188
if valid_uuid?(service_account_id) do
189189
FrontRepo.transaction(fn ->
190-
with {:ok, current_data} <- find(service_account_id),
190+
with {:ok, _current_data} <- find(service_account_id),
191191
{:ok, {plain_token, hashed_token}} <- generate_api_token(),
192192
{:ok, _updated_user} <- update_user_token(service_account_id, hashed_token) do
193193
plain_token

guard/test/guard/user/actions_test.exs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ defmodule Guard.User.ActionsTest do
148148
test "should handle service account user in update operations" do
149149
with_mock Guard.Events.UserCreated, publish: fn _, _ -> :ok end do
150150
# Create a service account using the factory
151-
{:ok, %{service_account: service_account, user: service_account_user}} =
151+
{:ok, %{service_account: _service_account, user: service_account_user}} =
152152
Support.Factories.ServiceAccountFactory.insert(name: "Original SA Name")
153153

154154
# Try to update the service account user via User.Actions
@@ -168,12 +168,9 @@ defmodule Guard.User.ActionsTest do
168168
test "should prevent email changes for service account users" do
169169
with_mock Guard.Events.UserCreated, publish: fn _, _ -> :ok end do
170170
# Create a service account
171-
{:ok, %{service_account: service_account, user: service_account_user}} =
171+
{:ok, %{service_account: _service_account, user: service_account_user}} =
172172
Support.Factories.ServiceAccountFactory.insert()
173173

174-
original_email = service_account_user.email
175-
176-
# Try to change the email of a service account user
177174
update_params = %{
178175
179176
}
@@ -190,7 +187,7 @@ defmodule Guard.User.ActionsTest do
190187
test "should maintain service account properties during update" do
191188
with_mock Guard.Events.UserCreated, publish: fn _, _ -> :ok end do
192189
# Create a service account
193-
{:ok, %{service_account: service_account, user: service_account_user}} =
190+
{:ok, %{service_account: _service_account, user: service_account_user}} =
194191
Support.Factories.ServiceAccountFactory.insert()
195192

196193
# Update with various parameters
@@ -211,7 +208,7 @@ defmodule Guard.User.ActionsTest do
211208
test "should not create repository providers for service account users" do
212209
with_mock Guard.Events.UserCreated, publish: fn _, _ -> :ok end do
213210
# Create a service account
214-
{:ok, %{service_account: service_account, user: service_account_user}} =
211+
{:ok, %{service_account: _service_account, user: service_account_user}} =
215212
Support.Factories.ServiceAccountFactory.insert()
216213

217214
# Try to add repository providers (should not work or should be ignored)

guard/test/support/factories/service_account_factory.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ defmodule Support.Factories.ServiceAccountFactory do
105105
defp get_description(nil), do: ""
106106
defp get_description(description), do: description
107107

108-
defp generate_synthetic_email(name, org_id) do
108+
defp generate_synthetic_email(name, _org_id) do
109109
sanitized_name = String.downcase(name) |> String.replace(~r/[^a-z0-9\-]/, "-")
110110
"#{sanitized_name}@sa.test-org.#{Application.fetch_env!(:guard, :base_domain)}"
111111
end

0 commit comments

Comments
 (0)