Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cli): improved LQL service func names #438

Merged
merged 8 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions api/lql.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ type LQLService struct {
client *Client
}

func (svc *LQLService) CreateQuery(query string) (
response LQLQuery,
func (svc *LQLService) Create(query string) (
response LQLQueryResponse,
err error,
) {
lqlQuery := LQLQuery{QueryBlob: query}
Expand All @@ -197,8 +197,8 @@ func (svc *LQLService) CreateQuery(query string) (
return
}

func (svc *LQLService) UpdateQuery(query string) (
response LQLQuery,
func (svc *LQLService) Update(query string) (
response LQLQueryResponse,
err error,
) {
lqlQuery := LQLQuery{QueryBlob: query}
Expand All @@ -211,10 +211,10 @@ func (svc *LQLService) UpdateQuery(query string) (
}

func (svc *LQLService) GetQueries() (LQLQueryResponse, error) {
return svc.GetQueryByID("")
return svc.GetByID("")
}

func (svc *LQLService) GetQueryByID(queryID string) (
func (svc *LQLService) GetByID(queryID string) (
response LQLQueryResponse,
err error,
) {
Expand All @@ -228,7 +228,7 @@ func (svc *LQLService) GetQueryByID(queryID string) (
return
}

func (svc *LQLService) RunQuery(query, start, end string) (
func (svc *LQLService) Run(query, start, end string) (
response map[string]interface{},
err error,
) {
Expand Down
2 changes: 1 addition & 1 deletion api/lql_compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type LQLCompileResponse struct {
Message string `json:"message"`
}

func (svc *LQLService) CompileQuery(query string) (
func (svc *LQLService) Compile(query string) (
response LQLCompileResponse,
err error,
) {
Expand Down
8 changes: 4 additions & 4 deletions api/lql_compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestLQLCompileMethod(t *testing.T) {
)
assert.Nil(t, err)

_, err = c.LQL.CompileQuery(lqlQueryStr)
_, err = c.LQL.Compile(lqlQueryStr)
assert.Nil(t, err)
}

Expand All @@ -116,7 +116,7 @@ func TestLQLCompileBadInput(t *testing.T) {
)
assert.Nil(t, err)

_, err = c.LQL.CompileQuery("")
_, err = c.LQL.Compile("")
assert.Equal(t, api.LQLQueryTranslateError, err.Error())
}

Expand All @@ -142,7 +142,7 @@ func TestLQLCompileOK(t *testing.T) {
_ = json.Unmarshal([]byte(mockResponse), &compileExpected)

var compileActual api.LQLCompileResponse
compileActual, err = c.LQL.CompileQuery(lqlQueryStr)
compileActual, err = c.LQL.Compile(lqlQueryStr)
assert.Nil(t, err)
assert.Equal(t, compileExpected, compileActual)
}
Expand All @@ -163,6 +163,6 @@ func TestLQLCompileError(t *testing.T) {
)
assert.Nil(t, err)

_, err = c.LQL.CompileQuery(lqlQueryStr)
_, err = c.LQL.Compile(lqlQueryStr)
assert.NotNil(t, err)
}
2 changes: 1 addition & 1 deletion api/lql_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type LQLDeleteMessage struct {
ID string `json:"lqlDeleted"`
}

func (svc *LQLService) DeleteQuery(queryID string) (
func (svc *LQLService) Delete(queryID string) (
response LQLDeleteResponse,
err error,
) {
Expand Down
13 changes: 5 additions & 8 deletions api/lql_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestLQLDeleteMethod(t *testing.T) {
)
assert.Nil(t, err)

_, err = c.LQL.DeleteQuery(lqlQueryID)
_, err = c.LQL.Delete(lqlQueryID)
assert.Nil(t, err)
}

Expand All @@ -72,15 +72,12 @@ func TestLQLDeleteBadInput(t *testing.T) {
)
assert.Nil(t, err)

_, err = c.LQL.DeleteQuery("")
_, err = c.LQL.Delete("")
assert.NotNil(t, err)
}

func TestLQLDeleteOK(t *testing.T) {
mockResponse := mockLQLMessageResponse(
fmt.Sprintf(`"lqlDeleted": "%s"`, lqlQueryID),
"true",
)
mockResponse := mockLQLMessageResponse(fmt.Sprintf(`"lqlDeleted": "%s"`, lqlQueryID))

fakeServer := lacework.MockServer()
fakeServer.MockAPI(
Expand All @@ -101,7 +98,7 @@ func TestLQLDeleteOK(t *testing.T) {
_ = json.Unmarshal([]byte(mockResponse), &deleteExpected)

var deleteActual api.LQLDeleteResponse
deleteActual, err = c.LQL.DeleteQuery(lqlQueryID)
deleteActual, err = c.LQL.Delete(lqlQueryID)
assert.Nil(t, err)

assert.Equal(t, deleteExpected, deleteActual)
Expand All @@ -123,6 +120,6 @@ func TestLQLDeleteNotFound(t *testing.T) {
)
assert.Nil(t, err)

_, err = c.LQL.DeleteQuery(lqlQueryID)
_, err = c.LQL.Delete(lqlQueryID)
assert.NotNil(t, err)
}
73 changes: 41 additions & 32 deletions api/lql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ import (
)

var (
lqlQueryID = "my_lql"
lqlQueryStr = "my_lql { source { CloudTrailRawEvents } return { INSERT_ID } }"
lqlCreateResponse = `{
lqlQueryID = "my_lql"
lqlQueryStr = "my_lql { source { CloudTrailRawEvents } return { INSERT_ID } }"
lqlCreateData = `[{
"lql_id": "my_lql",
"query_text": "my_lql { source { CloudTrailRawEvents } return { INSERT_ID } }"
}`
}]`
lqlRunData = `[
{
"INSERT_ID": "35308423"
Expand All @@ -46,8 +46,8 @@ var (
lqlErrorReponse = mockLQLDataResponse(
`{ "message": "Error Serving Request" }`,
)
lqlUnableResponse = mockLQLMessageResponse(
`"message": "{\"error\":\"Error: Unable to locate lql query NoSuchQuery, please double check the query exists and has not already been updated.\"}"`,
lqlUnableResponse = mockLQLErrorResponse(
`"{\"error\":\"Error: Unable to locate lql query NoSuchQuery, please double check the query exists and has not already been updated.\"}"`,
"false",
)
)
Expand Down Expand Up @@ -394,9 +394,16 @@ func mockLQLDataResponse(data string) string {
}`
}

func mockLQLMessageResponse(message string, ok string) string {
func mockLQLErrorResponse(message string, ok string) string {
return `{
"ok": ` + ok + `,
"data": { "message": {` + message + `} }
}`
}

func mockLQLMessageResponse(message string) string {
return `{
"ok": ` + ok + `,
"ok": true,
"message": {` + message + `
}
}`
Expand All @@ -419,7 +426,7 @@ func TestLQLCreateMethod(t *testing.T) {
)
assert.Nil(t, err)

_, err = c.LQL.CreateQuery(lqlQueryStr)
_, err = c.LQL.Create(lqlQueryStr)
assert.Nil(t, err)
}

Expand All @@ -439,16 +446,18 @@ func TestLQLCreateBadInput(t *testing.T) {
)
assert.Nil(t, err)

_, err = c.LQL.CreateQuery("")
_, err = c.LQL.Create("")
assert.Equal(t, api.LQLQueryTranslateError, err.Error())
}

func TestLQLCreateOK(t *testing.T) {
mockResponse := mockLQLDataResponse(lqlCreateData)

fakeServer := lacework.MockServer()
fakeServer.MockAPI(
"external/lql",
func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, lqlCreateResponse)
fmt.Fprint(w, mockResponse)
},
)
defer fakeServer.Close()
Expand All @@ -459,11 +468,11 @@ func TestLQLCreateOK(t *testing.T) {
)
assert.Nil(t, err)

createExpected := api.LQLQuery{}
_ = json.Unmarshal([]byte(lqlCreateResponse), &createExpected)
createExpected := api.LQLQueryResponse{}
_ = json.Unmarshal([]byte(mockResponse), &createExpected)

var createActual api.LQLQuery
createActual, err = c.LQL.CreateQuery(lqlQueryStr)
var createActual api.LQLQueryResponse
createActual, err = c.LQL.Create(lqlQueryStr)
assert.Nil(t, err)

assert.Equal(t, createExpected, createActual)
Expand All @@ -485,7 +494,7 @@ func TestLQLCreateError(t *testing.T) {
)
assert.Nil(t, err)

_, err = c.LQL.CreateQuery(lqlQueryStr)
_, err = c.LQL.Create(lqlQueryStr)
assert.NotNil(t, err)
}

Expand Down Expand Up @@ -517,7 +526,7 @@ func TestLQLGetQueriesMethod(t *testing.T) {
}

func TestLQLGetQueryByIDOK(t *testing.T) {
mockResponse := mockLQLDataResponse("[" + lqlCreateResponse + "]")
mockResponse := mockLQLDataResponse(lqlCreateData)

fakeServer := lacework.MockServer()
fakeServer.MockAPI(
Expand All @@ -534,17 +543,17 @@ func TestLQLGetQueryByIDOK(t *testing.T) {
)
assert.Nil(t, err)

createExpected := api.LQLQueryResponse{}
_ = json.Unmarshal([]byte(mockResponse), &createExpected)
getExpected := api.LQLQueryResponse{}
_ = json.Unmarshal([]byte(mockResponse), &getExpected)

var createActual api.LQLQueryResponse
createActual, err = c.LQL.GetQueryByID(lqlQueryStr)
var getActual api.LQLQueryResponse
getActual, err = c.LQL.GetByID(lqlQueryID)
assert.Nil(t, err)

assert.Equal(t, createExpected, createActual)
assert.Equal(t, getExpected, getActual)
}

func TestLQLGetQueryByIDNotFound(t *testing.T) {
func TestLQLGetByIDNotFound(t *testing.T) {
fakeServer := lacework.MockServer()
fakeServer.MockAPI(
"external/lql",
Expand All @@ -560,11 +569,11 @@ func TestLQLGetQueryByIDNotFound(t *testing.T) {
)
assert.Nil(t, err)

_, err = c.LQL.GetQueryByID("NoSuchQuery")
_, err = c.LQL.GetByID("NoSuchQuery")
assert.NotNil(t, err)
}

func TestLQLRunQueryMethod(t *testing.T) {
func TestLQLRunMethod(t *testing.T) {
fakeServer := lacework.MockServer()
fakeServer.MockAPI(
"external/lql/query",
Expand All @@ -581,11 +590,11 @@ func TestLQLRunQueryMethod(t *testing.T) {
)
assert.Nil(t, err)

_, err = c.LQL.RunQuery(lqlQueryStr, "0", "1")
_, err = c.LQL.Run(lqlQueryStr, "0", "1")
assert.Nil(t, err)
}

func TestLQLRunQueryBadInput(t *testing.T) {
func TestLQLRunBadInput(t *testing.T) {
fakeServer := lacework.MockServer()
fakeServer.MockAPI(
"external/lql/query",
Expand All @@ -601,11 +610,11 @@ func TestLQLRunQueryBadInput(t *testing.T) {
)
assert.Nil(t, err)

_, err = c.LQL.RunQuery("", "", "")
_, err = c.LQL.Run("", "", "")
assert.Equal(t, api.LQLQueryTranslateError, err.Error())
}

func TestLQLRunQueryOK(t *testing.T) {
func TestLQLRunOK(t *testing.T) {
mockResponse := mockLQLDataResponse(lqlRunData)

fakeServer := lacework.MockServer()
Expand All @@ -627,13 +636,13 @@ func TestLQLRunQueryOK(t *testing.T) {
_ = json.Unmarshal([]byte(mockResponse), &runExpected)

var runActual map[string]interface{}
runActual, err = c.LQL.RunQuery(lqlQueryStr, "0", "1")
runActual, err = c.LQL.Run(lqlQueryStr, "0", "1")
assert.Nil(t, err)

assert.Equal(t, runExpected, runActual)
}

func TestLQLRunQueryError(t *testing.T) {
func TestLQLRunError(t *testing.T) {
fakeServer := lacework.MockServer()
fakeServer.MockAPI(
"external/lql",
Expand All @@ -649,6 +658,6 @@ func TestLQLRunQueryError(t *testing.T) {
)
assert.Nil(t, err)

_, err = c.LQL.CreateQuery(lqlQueryStr)
_, err = c.LQL.Create(lqlQueryStr)
assert.NotNil(t, err)
}
Loading