Skip to content

Commit

Permalink
incident_api: intial add comment function
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaelFangel committed Apr 26, 2024
1 parent f96f818 commit b53a788
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions lib/incident_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,26 @@ defmodule LogpointApi.IncidentApi do
defstruct [:incident_obj_id, :incident_id]
end

@spec get_incidents(String.t(), Credential.t(), TimeRange.t()) :: {:ok, map()} | {:error, String.t()}
defmodule IncidentComment do
defstruct _id: "", comments: []
end

defmodule IncidentCommentData do
defstruct version: "0.1", states: [%IncidentComment{}]
end

@spec get_incidents(String.t(), Credential.t(), TimeRange.t()) ::
{:ok, map()} | {:error, String.t()}
def get_incidents(ip, credential, %TimeRange{} = time_range),
do: get_incident_information(ip, "/incidents", credential, time_range)

@spec get_data_from_incident(String.t(), Credential.t(), Incident.t()) :: {:ok, map()} | {:error, String.t()}
@spec get_data_from_incident(String.t(), Credential.t(), Incident.t()) ::
{:ok, map()} | {:error, String.t()}
def get_data_from_incident(ip, credential, %Incident{} = incident),
do: get_incident_information(ip, "/get_data_from_incident", credential, incident)

@spec get_incident_states(String.t(), Credential.t(), TimeRange.t()) :: {:ok, map()} | {:error, String.t()}
@spec get_incident_states(String.t(), Credential.t(), TimeRange.t()) ::
{:ok, map()} | {:error, String.t()}
def get_incident_states(ip, credential, %TimeRange{} = time_range),
do: get_incident_information(ip, "/incident_states", credential, time_range)

Expand All @@ -33,7 +44,19 @@ defmodule LogpointApi.IncidentApi do
make_request(ip, "/get_users", params)
end

@spec get_incident_information(String.t(), String.t(), Credential.t(), map()) :: {:ok, map()} | {:error, String.t()}
@spec add_comments(String.t(), Credential.t(), IncidentCommentData.t()) :: {:ok, map()} | {:error, String.t()}
def add_comments(ip, %Credential{} = credential, %IncidentCommentData{} = request_data) do
payload = %{
"username" => credential.username,
"secret_key" => credential.secret_key,
"requestData" => request_data
} |> Jason.encode!()

post_request(ip, "/add_incident_comment", payload)
end

@spec get_incident_information(String.t(), String.t(), Credential.t(), map()) ::
{:ok, map()} | {:error, String.t()}
defp get_incident_information(ip, path, %Credential{} = credential, request_data) do
params = %{
"username" => credential.username,
Expand Down Expand Up @@ -64,6 +87,25 @@ defmodule LogpointApi.IncidentApi do
end
end

@spec post_request(String.t(), String.t(), String.t()) :: {:ok, map()} | {:error, String.t()}
defp post_request(ip, path, payload) do
url = build_url(ip, path)
headers = [{"Content-Type", "application/json"}]
# On-prem uses self signed certificates and we thus need to disable the verification.
options = [ssl: [{:verify, :verify_none}]]

case HTTPoison.post(url, payload, headers, options) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
{:ok, Jason.decode!(body)}

{:ok, %HTTPoison.Response{status_code: status_code}} ->
{:error, "Received response with status code #{status_code}"}

{:error, %HTTPoison.Error{reason: reason}} ->
{:error, "HTTP request failed with reason: #{reason}"}
end
end

@spec build_url(String.t(), String.t()) :: String.t()
defp build_url(ip, path), do: "https://" <> ip <> path
end

0 comments on commit b53a788

Please sign in to comment.