-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): split list of Google project id and alias (#377)
We are implementing a workaround into the Lacework CLI to split the ID and alias from the list of Google projects when a user runs the command: ``` lacework compliance google list-projects <org_id> ``` Ultimately we will do this in the Lacework backend, but for now, we are going to adopt this workaround until we start using APIv2. Example of the new JSON and human-readable outputs: ``` ➜ lacework comp gcp list 123456789012 -p mini PROJECT ID PROJECT ALIAS -----------------------+---------------- abc-demo-project-123 demo-project ➜ lacework comp gcp list 123456789012 -p mini --json { "organization": { "alias": "", "id": "123456789012" }, "projects": [ { "alias": "demo-project", "id": "abc-demo-project-123" } ] } ``` Signed-off-by: Salim Afiune Maya <[email protected]>
- Loading branch information
Showing
2 changed files
with
183 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// | ||
// Author:: Salim Afiune Maya (<[email protected]>) | ||
// Copyright:: Copyright 2021, Lacework Inc. | ||
// License:: Apache License, Version 2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/lacework/go-sdk/api" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSplitIDAndAlias(t *testing.T) { | ||
cases := []struct { | ||
subjectText string | ||
expectedID string | ||
expectedAlias string | ||
}{ | ||
// empty text will return empty id and alias | ||
{"", "", ""}, | ||
// alias should not be empty | ||
{"()", "", ""}, | ||
// minimum text that can be splitted | ||
{"a (b)", "a", "b"}, | ||
// if we couldn't get the alias from the provided text | ||
// it means that the entire text is the id | ||
{"1234567890", "1234567890", ""}, | ||
// other common test cases | ||
{"1234567890 (alias-example)", "1234567890", "alias-example"}, | ||
{"proj-id-with-numbers (alias with spaces)", "proj-id-with-numbers", "alias with spaces"}, | ||
{"only-project-id-123", "only-project-id-123", ""}, | ||
// seriously, we should never have only the alias in the response ;-) | ||
{"(this should never happen)", "", "this should never happen"}, | ||
} | ||
for i, kase := range cases { | ||
t.Run(fmt.Sprintf("test case %d", i), func(t *testing.T) { | ||
actualID, actualAlias := splitIDAndAlias(kase.subjectText) | ||
assert.Equalf(t, kase.expectedID, actualID, "wrong id") | ||
assert.Equalf(t, kase.expectedAlias, actualAlias, "wrong alias") | ||
}) | ||
} | ||
} | ||
|
||
func TestFixGcpProjectsApiResponse(t *testing.T) { | ||
cases := []struct { | ||
subject api.CompGcpProjects | ||
expected cliComplianceGcpInfo | ||
}{ | ||
// empty projects will return empty cli info | ||
{ | ||
api.CompGcpProjects{}, | ||
cliComplianceGcpInfo{Projects: make([]cliComplianceIDAlias, 0)}, | ||
}, | ||
// real test case with NO alias | ||
{ | ||
api.CompGcpProjects{ | ||
Organization: "1234567890123", | ||
Projects: []string{"project-id-1", "project-id-2", "project-id-3", "project-id-4"}, | ||
}, | ||
cliComplianceGcpInfo{ | ||
Organization: cliComplianceIDAlias{"1234567890123", ""}, | ||
Projects: []cliComplianceIDAlias{ | ||
cliComplianceIDAlias{"project-id-1", ""}, | ||
cliComplianceIDAlias{"project-id-2", ""}, | ||
cliComplianceIDAlias{"project-id-3", ""}, | ||
cliComplianceIDAlias{"project-id-4", ""}, | ||
}, | ||
}, | ||
}, | ||
// real test case with alias | ||
{ | ||
api.CompGcpProjects{ | ||
Organization: "1234567890123 (cool.org.alias.example.com)", | ||
Projects: []string{ | ||
"id-1 (a test project)", | ||
"xmen-project (serious alias)", | ||
"disney-movies (Maybe Production)", | ||
"foo (bar)", | ||
}, | ||
}, | ||
cliComplianceGcpInfo{ | ||
Organization: cliComplianceIDAlias{"1234567890123", "cool.org.alias.example.com"}, | ||
Projects: []cliComplianceIDAlias{ | ||
cliComplianceIDAlias{"id-1", "a test project"}, | ||
cliComplianceIDAlias{"xmen-project", "serious alias"}, | ||
cliComplianceIDAlias{"disney-movies", "Maybe Production"}, | ||
cliComplianceIDAlias{"foo", "bar"}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for i, kase := range cases { | ||
t.Run(fmt.Sprintf("test case %d", i), func(t *testing.T) { | ||
assert.Equalf(t, | ||
kase.expected, splitGcpProjectsApiResponse(kase.subject), | ||
"there is a problem with this test case, please check", | ||
) | ||
}) | ||
} | ||
} |