Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaadF committed Apr 13, 2021
1 parent 19cda1a commit dd52f93
Show file tree
Hide file tree
Showing 9 changed files with 416 additions and 355 deletions.
Expand Up @@ -2,12 +2,12 @@ module ApiCalls exposing (..)

import DataTypes exposing (Model, Msg(..), Secret)
import Http exposing (emptyBody, expectJson, jsonBody, request, send)
import JsonDecoder exposing (decodeDeleteSecret, decodeGetAllSecrets, decodeOneSecret)
import JsonDecoder exposing (decodeSecretsApi)
import JsonEncoder exposing (encodeSecret)

getUrl: DataTypes.Model -> String -> String
getUrl m parameter =
m.contextPath ++ "/secure/api/latest/secret" ++ parameter
getUrl m url =
m.contextPath ++ "/secure/api" ++ url

getSecret : Model -> String -> Cmd Msg
getSecret model secretName =
Expand All @@ -16,9 +16,9 @@ getSecret model secretName =
request
{ method = "GET"
, headers = []
, url = getUrl model secretName
, url = getUrl model ("/secret/" ++ secretName)
, body = emptyBody
, expect = expectJson decodeOneSecret
, expect = expectJson decodeSecretsApi
, timeout = Nothing
, withCredentials = False
}
Expand All @@ -32,9 +32,9 @@ getAllSecrets model =
request
{ method = "GET"
, headers = []
, url = getUrl model ""
, url = getUrl model "/secret"
, body = emptyBody
, expect = expectJson decodeGetAllSecrets
, expect = expectJson decodeSecretsApi
, timeout = Nothing
, withCredentials = False
}
Expand All @@ -48,9 +48,9 @@ deleteSecret secretName model =
request
{ method = "DELETE"
, headers = []
, url = getUrl model secretName
, url = getUrl model ("/secret/" ++ secretName)
, body = emptyBody
, expect = expectJson decodeDeleteSecret
, expect = expectJson decodeSecretsApi
, timeout = Nothing
, withCredentials = False
}
Expand All @@ -64,9 +64,9 @@ addSecret model secret =
request
{ method = "PUT"
, headers = []
, url = getUrl model ""
, url = getUrl model "/secret"
, body = jsonBody (encodeSecret secret)
, expect = expectJson decodeOneSecret
, expect = expectJson decodeSecretsApi
, timeout = Nothing
, withCredentials = False
}
Expand All @@ -80,9 +80,9 @@ updateSecret model secret =
request
{ method = "POST"
, headers = []
, url = getUrl model ""
, url = getUrl model "/secret"
, body = jsonBody (encodeSecret secret)
, expect = expectJson decodeOneSecret
, expect = expectJson decodeSecretsApi
, timeout = Nothing
, withCredentials = False
}
Expand Down
@@ -1,50 +1,63 @@
module DataTypes exposing(..)

import Http exposing (Error)
import Toasty
import Toasty.Defaults

type alias Secret =
{ name : String
{ info : SecretInfo
, value : String
}

type alias SecretInfo =
{ name : String
, description : String
}


type alias Model =
{ contextPath : String
, secrets : List Secret
, focusOn : Maybe Secret
, newSecretInput : Maybe Secret
, isOpenCreateModal : Bool
, isOpenEditModal : Bool
, stateInputs : List StateInput
{ contextPath : String
, secrets : List SecretInfo
, focusOn : Maybe SecretInfo
, newSecretInput : Maybe Secret
, openModalMode : Mode
, stateInputs : List StateInput
, openedDescription : List String
, sortOn : (Sorting, Column)
, filteredSecrets : Maybe (List SecretInfo)
}

type WriteAction
type Mode
= Edit
| Add
| Delete
| Read

type StateInput
= InvalidName
| InvalidValue
| InvalidDescription
= EmptyInputName
| EmptyInputValue
| EmptyInputDescription

type Sorting
= ASC
| DESC
| NONE

type Column
= Name
| Description

type Msg
= GetAllSecrets (Result Error (List Secret))
| GetSecret (Result Error Secret)
| AddSecret (Result Error Secret)
| DeleteSecret (Result Error String)
| UpdateSecret (Result Error Secret)
= GetAllSecrets (Result Error (List SecretInfo))
| GetSecret (Result Error (List SecretInfo))
| AddSecret (Result Error (List SecretInfo))
| DeleteSecret (Result Error (List SecretInfo))
| UpdateSecret (Result Error (List SecretInfo))
| CallApi (Model -> Cmd Msg)
| OpenCreateModal
| OpenEditModal Secret
| OpenModal Mode (Maybe SecretInfo)
| CloseModal
| InputName String
| InputValue String
| InputDescription String
| SubmitSecret WriteAction
| OpenDescription Secret
--
-- -- NOTIFICATIONS
--| ToastyMsg (Toasty.Msg Toasty.Defaults.Toast)
--| Notification (Toasty.Msg Toasty.Defaults.Toast)
| SubmitSecret Mode
| OpenDescription SecretInfo
| ChangeSorting Column
| FilterSecrets String
111 changes: 8 additions & 103 deletions webapp/sources/rudder/rudder-web/src/main/elm/secret/sources/Init.elm
@@ -1,112 +1,17 @@
module Init exposing (..)

------------------------------
-- SUBSCRIPTIONS
------------------------------

import ApiCalls exposing (getAllSecrets)
import DataTypes exposing (Model, Msg(..), StateInput(..))
import Html.Attributes exposing (style)
import Http
import Toasty
import Toasty.Defaults
import DataTypes exposing (Column(..), Mode(..), Model, Msg(..), Sorting(..), StateInput(..))

subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none

------------------------------
-- Init and main --
------------------------------

Sub.none

init : { contextPath : String } -> ( Model, Cmd Msg )
init flags =
let
--secrets = [(DataTypes.Secret "toto" "value1"), (DataTypes.Secret "tata" "value2"),( DataTypes.Secret "cqjdiueq" "value2"), (DataTypes.Secret "tv2rv2revwrata" "value2") ]

initModel = Model flags.contextPath [] Nothing Nothing False False [] []
in
( initModel
, getAllSecrets initModel
)

------------------------------
-- NOTIFICATIONS --
------------------------------

getErrorMessage : Http.Error -> String
getErrorMessage e =
let
errMessage =
case e of
Http.BadStatus b ->
let
status =
b.status

message =
status.message
in
"Code " ++ String.fromInt status.code ++ " : " ++ message

Http.BadUrl str ->
"Invalid API url"

Http.Timeout ->
"It took too long to get a response"

Http.NetworkError ->
"Network error"

Http.BadPayload str rstr ->
str
in
errMessage

--
--defaultConfig : Toasty.Config Msg
--defaultConfig =
-- Toasty.Defaults.config
-- |> Toasty.delay 999999999
-- |> Toasty.containerAttrs
-- [ style "position" "fixed"
-- , style "top" "50px"
-- , style "right" "30px"
-- , style "width" "100%"
-- , style "max-width" "500px"
-- , style "list-style-type" "none"
-- , style "padding" "0"
-- , style "margin" "0"
-- , style "z-index" "9999"
-- ]
--
--
--tempConfig : Toasty.Config Msg
--tempConfig =
-- defaultConfig |> Toasty.delay 3000
--
--
--addTempToast : Toasty.Defaults.Toast -> ( Model, Cmd Msg ) -> ( Model, Cmd Msg )
--addTempToast toast ( model, cmd ) =
-- Toasty.addToast tempConfig ToastyMsg toast ( model, cmd )
--
--
--addToast : Toasty.Defaults.Toast -> ( Model, Cmd Msg ) -> ( Model, Cmd Msg )
--addToast toast ( model, cmd ) =
-- Toasty.addToast defaultConfig ToastyMsg toast ( model, cmd )
--
--
--createSuccessNotification : String -> ( Model, Cmd Msg ) -> ( Model, Cmd Msg )
--createSuccessNotification message =
-- addTempToast (Toasty.Defaults.Success "Success!" message)
--
--
--createErrorNotification : String -> Http.Error -> ( Model, Cmd Msg ) -> ( Model, Cmd Msg )
--createErrorNotification message e =
-- addToast (Toasty.Defaults.Error "Error..." (message ++ " (" ++ getErrorMessage e ++ ")"))
--
--
--createDecodeErrorNotification : String -> String -> ( Model, Cmd Msg ) -> ( Model, Cmd Msg )
--createDecodeErrorNotification message e =
-- addToast (Toasty.Defaults.Error "Error..." (message ++ " (" ++ e ++ ")"))
let
initModel = Model flags.contextPath [] Nothing Nothing Read [] [] (NONE, Name) Nothing
in
( initModel
, getAllSecrets initModel
)
@@ -1,23 +1,15 @@
module JsonDecoder exposing (..)

import DataTypes exposing (Secret)
import DataTypes exposing (Secret, SecretInfo)
import Json.Decode as D exposing (Decoder)
import Json.Decode.Pipeline exposing (required)
import Json.Decode.Pipeline exposing (hardcoded, optional, required)

decodeGetAllSecrets : Decoder (List Secret)
decodeGetAllSecrets =
D.at [ "data" ] ( D.at [ "secrets" ] (D.list <| decodeSecret) )
decodeSecretsApi : Decoder (List SecretInfo)
decodeSecretsApi =
D.at [ "data" ] ( D.at [ "secrets" ] (D.list <| decodeSecretInfo) )

decodeOneSecret : Decoder Secret
decodeOneSecret =
D.at [ "data" ] ( D.at [ "secret" ] decodeSecret)

decodeDeleteSecret : Decoder String
decodeDeleteSecret =
D.at [ "data" ] ( D.at [ "secretName" ] D.string )

decodeSecret : Decoder Secret
decodeSecret =
D.succeed Secret
decodeSecretInfo : Decoder SecretInfo
decodeSecretInfo =
D.succeed SecretInfo
|> required "name" D.string
|> required "value" D.string
|> required "description" D.string
Expand Up @@ -6,6 +6,7 @@ import Json.Encode exposing (Value, object, string)
encodeSecret: Secret -> Value
encodeSecret secret =
object
[ ("name", string secret.name)
[ ("name", string secret.info.name)
, ("value", string secret.value)
, ("description", string secret.info.description)
]

0 comments on commit dd52f93

Please sign in to comment.