@@ -3,7 +3,7 @@ defmodule Core.Services.Shell.Demo do
33 alias Core.Schema . { User , DemoProject }
44 alias Core.Services . { Locks , Users , Upgrades , Repositories }
55 alias GoogleApi.CloudResourceManager.V3.Api . { Projects , Operations }
6- alias GoogleApi.CloudResourceManager.V3.Model . { Project , Operation , SetIamPolicyRequest }
6+ alias GoogleApi.CloudResourceManager.V3.Model . { Project , Operation , SetIamPolicyRequest , MoveProjectRequest }
77 alias GoogleApi.CloudResourceManager.V3.Connection , as: ProjectsConnection
88 alias GoogleApi.IAM.V1.Connection , as: IAMConnection
99 alias GoogleApi.IAM.V1.Api.Projects , as: IAMProjects
@@ -18,13 +18,17 @@ defmodule Core.Services.Shell.Demo do
1818
1919
2020 @ type error :: { :error , term }
21+ @ type demo_resp :: { :ok , DemoProject . t } | error
2122
2223 @ lock "demo-projects"
2324 @ max_count 3
2425
2526 @ spec get_demo_project ( binary ) :: DemoProject . t | nil
2627 def get_demo_project ( id ) , do: Core.Repo . get ( DemoProject , id )
2728
29+ @ spec get_by_user_id ( binary ) :: DemoProject . t | nil
30+ def get_by_user_id ( id ) , do: Core.Repo . get_by ( DemoProject , user_id: id )
31+
2832 @ doc """
2933 Returns at most `limit` demo projects for deletion. This operation should be atomic, and will mark each project with
3034 a heartbeat ts to track status in the deletion pipeline.
@@ -93,6 +97,33 @@ defmodule Core.Services.Shell.Demo do
9397 |> execute ( extract: :final )
9498 end
9599
100+ @ doc """
101+ Transfers a demo project to another organization
102+ """
103+ @ spec transfer_demo_project ( binary , User . t ) :: demo_resp
104+ def transfer_demo_project ( org_id , % User { id: user_id } ) do
105+ demo = get_by_user_id ( user_id )
106+ projs = projects_conn ( )
107+
108+ start_transaction ( )
109+ |> add_operation ( :move , fn _ ->
110+ Projects . cloudresourcemanager_projects_move ( projs , demo . project_id , body: % MoveProjectRequest {
111+ destinationParent: "organizations/#{ org_id } "
112+ } )
113+ |> IO . inspect ( ) # useful for testing
114+ end )
115+ |> add_operation ( :shell , fn _ ->
116+ with % { } = shell <- Core.Services.Shell . get_shell ( user_id ) do
117+ Ecto.Changeset . change ( shell , % { demo_id: nil } )
118+ |> Core.Repo . update ( )
119+ else
120+ _ -> { :ok , nil }
121+ end
122+ end )
123+ |> add_operation ( :delete , fn _ -> Core.Repo . delete ( demo ) end )
124+ |> execute ( extract: :delete )
125+ end
126+
96127 @ doc """
97128 Deletes the demo project and its associated gcp project
98129 """
0 commit comments