Skip to content

Commit

Permalink
fix restore feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed Apr 1, 2024
1 parent 20de911 commit 896bd5a
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 10 deletions.
6 changes: 3 additions & 3 deletions example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ terraform {
}

backend "http" {
username = "qy8aq4jz"
password = "edsP4XhtDVc4"
username = "hs2d21pk"
password = "pO(BwDTjs5ND"
address = "http://localhost:4000/client/clivern/monitoring/prod/state"
lock_address = "http://localhost:4000/client/clivern/monitoring/prod/lock"
unlock_address = "http://localhost:4000/client/clivern/monitoring/prod/unlock"
Expand All @@ -22,7 +22,7 @@ terraform {
provider "local" {}

resource "local_file" "hello_world" {
content = "Hello, World!"
content = "Hello, World! v3"
filename = "${path.module}/hello.txt"
}

Expand Down
25 changes: 25 additions & 0 deletions lib/lynx/module/permission_module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Lynx.Module.PermissionModule do

alias Lynx.Context.ProjectContext
alias Lynx.Context.SnapshotContext
alias Lynx.Context.EnvironmentContext
alias Lynx.Module.TeamModule

def can_access_project_id(:project, :anonymous, _id, _user_id) do
Expand Down Expand Up @@ -83,6 +84,30 @@ defmodule Lynx.Module.PermissionModule do
end
end

def can_access_environment_uuid(:environment, :super, _uuid, _user_id) do
true
end

def can_access_environment_uuid(:environment, :anonymous, _uuid, _user_id) do
false
end

def can_access_environment_uuid(:environment, :regular, uuid, user_id) do
case EnvironmentContext.get_env_by_uuid(uuid) do
nil ->
false

env ->
case ProjectContext.get_project_by_id_teams(env.project_id, get_user_teams_ids(user_id)) do
nil ->
false

_ ->
true
end
end
end

defp get_user_teams_ids(user_id) do
user_teams = TeamModule.get_user_teams(user_id)

Expand Down
27 changes: 27 additions & 0 deletions lib/lynx/module/state_module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,33 @@ defmodule Lynx.Module.StateModule do
end
end


@doc """
Get latest state by environment uuid
"""
def get_latest_state_by_env_uuid(uuid) do
case EnvironmentContext.get_env_by_uuid(uuid) do
nil ->
nil

env ->
case StateContext.get_latest_state_by_environment_id(env.id) do
nil ->
nil

state ->
state
end
end
end

@doc """
Get state by uuid
"""
def get_state_by_uuid(uuid) do
StateContext.get_state_by_uuid(uuid)
end

@doc """
Count environment states
"""
Expand Down
81 changes: 74 additions & 7 deletions lib/lynx_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule LynxWeb.PageController do
alias Lynx.Service.AuthService
alias Lynx.Module.SettingsModule
alias Lynx.Module.PermissionModule
alias Lynx.Module.StateModule

@doc """
Login Page
Expand Down Expand Up @@ -142,7 +143,7 @@ defmodule LynxWeb.PageController do
case conn.assigns[:is_logged] do
false ->
conn
|> redirect(to: "/")
|> redirect(to: "/login")

true ->
conn
Expand Down Expand Up @@ -170,7 +171,7 @@ defmodule LynxWeb.PageController do
case conn.assigns[:is_super] do
false ->
conn
|> redirect(to: "/")
|> redirect(to: "/login")

true ->
conn
Expand All @@ -197,7 +198,7 @@ defmodule LynxWeb.PageController do
case conn.assigns[:is_super] do
false ->
conn
|> redirect(to: "/")
|> redirect(to: "/login")

true ->
conn
Expand All @@ -224,7 +225,7 @@ defmodule LynxWeb.PageController do
case conn.assigns[:is_super] do
false ->
conn
|> redirect(to: "/")
|> redirect(to: "/login")

true ->
conn
Expand Down Expand Up @@ -252,7 +253,7 @@ defmodule LynxWeb.PageController do
case conn.assigns[:is_logged] do
false ->
conn
|> redirect(to: "/")
|> redirect(to: "/login")

true ->
conn
Expand All @@ -279,7 +280,7 @@ defmodule LynxWeb.PageController do
case conn.assigns[:is_logged] do
false ->
conn
|> redirect(to: "/")
|> redirect(to: "/login")

true ->
conn
Expand All @@ -306,7 +307,7 @@ defmodule LynxWeb.PageController do
case conn.assigns[:is_logged] do
false ->
conn
|> redirect(to: "/")
|> redirect(to: "/login")

true ->
if not PermissionModule.can_access_project_uuid(
Expand Down Expand Up @@ -337,6 +338,72 @@ defmodule LynxWeb.PageController do
end
end

@doc """
State Download Page
"""
def state(conn, %{"uuid" => uuid}) do
case conn.assigns[:is_logged] do
false ->
conn
|> redirect(to: "/login")

true ->
if not PermissionModule.can_access_snapshot_uuid(
:snapshot,
conn.assigns[:user_role],
uuid,
conn.assigns[:user_id]
) do
conn
|> redirect(to: "/404")
else
case StateModule.get_state_by_uuid(uuid) do
nil ->
conn
|> redirect(to: "/404")
state ->
conn
|> put_resp_content_type("application/octet-stream")
|> put_resp_header("content-disposition", "attachment; filename=\"state.#{uuid}.json\"")
|> send_resp(200, state.value)
end
end
end
end

@doc """
Environment State Download Page
"""
def environment(conn, %{"uuid" => uuid}) do
case conn.assigns[:is_logged] do
false ->
conn
|> redirect(to: "/login")

true ->
if not PermissionModule.can_access_environment_uuid(
:environment,
conn.assigns[:user_role],
uuid,
conn.assigns[:user_id]
) do
conn
|> redirect(to: "/404")
else
case StateModule.get_latest_state_by_env_uuid(uuid) do
nil ->
conn
|> redirect(to: "/404")
state ->
conn
|> put_resp_content_type("application/octet-stream")
|> put_resp_header("content-disposition", "attachment; filename=\"state.#{state.uuid}.json\"")
|> send_resp(200, state.value)
end
end
end
end

defp get_gavatar(nil) do
""
end
Expand Down
2 changes: 2 additions & 0 deletions lib/lynx_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ defmodule LynxWeb.Router do
get "/admin/projects", PageController, :projects
get "/admin/projects/:uuid", PageController, :project
get "/admin/settings", PageController, :settings
get "/admin/state/download/:uuid", PageController, :state
get "/admin/environment/download/:uuid", PageController, :environment
end

scope "/", LynxWeb do
Expand Down
4 changes: 4 additions & 0 deletions lib/lynx_web/templates/page/project.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
delete_environment_alert: '<%= gettext "You are trying to delete an environment! are you sure?" %>',
delete_environment_endpoint: '<%= Routes.environment_path(@conn, :delete, @data.uuid, "UUID") %>',
get_environment_endpoint: '<%= Routes.environment_path(@conn, :index, @data.uuid, "UUID") %>',
download_environment_state_endpoint: '<%= Routes.page_path(@conn, :environment, "UUID") %>',
get_project_endpoint: '<%= Routes.project_path(@conn, :index, "UUID") %>',
project_uuid: '<%= @data.uuid %>'
}
Expand Down Expand Up @@ -306,6 +307,9 @@
<td style="text-align: center">${formatDatetime(environment.createdAt)}</td>
<td style="text-align: center">
<button @click="viewEnvironmentAction(environment.id)" class="btn btn-dashed btn-sm text-warning-100 border-warning-100 hp-hover-text-color-warning-80 hp-hover-border-color-warning-80"><%= gettext "View" %></button>
<template v-if="environment.stateVersion != 'v0'">
<a target="_blank" @click="downloadEnvironmentStateAction(environment.id)" class="btn btn-dashed btn-sm text-warning-100 border-warning-100 hp-hover-text-color-warning-80 hp-hover-border-color-warning-80"><%= gettext "Download State" %></a>
</template>
<!--<button @click="editEnvironmentAction(environment.id)" class="btn btn-dashed btn-sm text-black-100 border-black-100 hp-hover-text-color-black-80 hp-hover-border-color-black-80"><%= gettext "Edit" %></button>-->
<button @click="deleteEnvironmentAction(environment.id)" class="btn btn-dashed btn-sm text-danger border-danger hp-hover-text-color-danger-2 hp-hover-border-color-danger-2"><%= gettext "Delete" %></button>
</td>
Expand Down
4 changes: 4 additions & 0 deletions priv/static/theme/app/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,10 @@ lynx_app.environments_list = (Vue, axios, $) => {
return format_datetime(datatime);
},

downloadEnvironmentStateAction(id) {
window.location.href = i18n_globals.download_environment_state_endpoint.replace("UUID", id);
},

viewEnvironmentAction(id) {
let data = $("#proto_env_data").text();
let env_endpoint = i18n_globals.get_environment_endpoint.replaceAll("UUID", id);
Expand Down

0 comments on commit 896bd5a

Please sign in to comment.