Skip to content

Commit

Permalink
fix(cli): Output correct resource group json (#543)
Browse files Browse the repository at this point in the history
Signed-off-by: Darren Murray <[email protected]>
  • Loading branch information
dmurray-lacework committed Sep 9, 2021
1 parent 13fb167 commit 3311ef2
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 52 deletions.
17 changes: 14 additions & 3 deletions cli/cmd/resource_group_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,23 @@ func createAwsResourceGroup() error {
return err
}

func setAwsProps(group string) []string {
var awsProps api.AwsResourceGroupProps
err := json.Unmarshal([]byte(group), &awsProps)
func setAwsProps(group []byte) []string {
awsProps, err := unmarshallAwsProps(group)
if err != nil {
return []string{}
}

return []string{"ACCOUNT IDS", strings.Join(awsProps.AccountIDs, ",")}
}

func unmarshallAwsPropString(group []byte) (props api.AwsResourceGroupProps, err error) {
var rawProps api.AwsResourceJsonStringGroupProps
err = json.Unmarshal(group, &rawProps)
props = api.AwsResourceGroupProps(rawProps)
return
}

func unmarshallAwsProps(group []byte) (props api.AwsResourceGroupProps, err error) {
err = json.Unmarshal(group, &props)
return
}
22 changes: 16 additions & 6 deletions cli/cmd/resource_group_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,10 @@ func createAzureResourceGroup() error {
return err
}

func setAzureProps(group string) [][]string {
var (
azProps api.AzureResourceGroupProps
details [][]string
)
err := json.Unmarshal([]byte(group), &azProps)
func setAzureProps(group []byte) [][]string {
var details [][]string

azProps, err := unmarshallAzureProps(group)
if err != nil {
return [][]string{}
}
Expand All @@ -94,3 +92,15 @@ func setAzureProps(group string) [][]string {
details = append(details, []string{"SUBSCRIPTIONS", strings.Join(azProps.Subscriptions, ",")})
return details
}

func unmarshallAzurePropString(group []byte) (props api.AzureResourceGroupProps, err error) {
var rawProps api.AzureResourceJsonStringGroupProps
err = json.Unmarshal(group, &rawProps)
props = api.AzureResourceGroupProps(rawProps)
return
}

func unmarshallAzureProps(group []byte) (props api.AzureResourceGroupProps, err error) {
err = json.Unmarshal(group, &props)
return
}
22 changes: 17 additions & 5 deletions cli/cmd/resource_group_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ func createContainerResourceGroup() error {
return err
}

func setContainerProps(group string) [][]string {
func setContainerProps(group []byte) [][]string {
var (
ctrProps api.ContainerResourceGroupProps
labels []string
details [][]string
details [][]string
labels []string
)
err := json.Unmarshal([]byte(group), &ctrProps)

ctrProps, err := unmarshallContainerProps(group)
if err != nil {
return [][]string{}
}
Expand All @@ -100,3 +100,15 @@ func setContainerProps(group string) [][]string {
details = append(details, []string{"CONTAINER TAGS", strings.Join(ctrProps.ContainerTags, ",")})
return details
}

func unmarshallContainerPropString(group []byte) (props api.ContainerResourceGroupProps, err error) {
var rawProps api.ContainerResourceJsonStringGroupProps
err = json.Unmarshal(group, &rawProps)
props = api.ContainerResourceGroupProps(rawProps)
return
}

func unmarshallContainerProps(group []byte) (props api.ContainerResourceGroupProps, err error) {
err = json.Unmarshal(group, &props)
return
}
22 changes: 16 additions & 6 deletions cli/cmd/resource_group_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,10 @@ func createGcpResourceGroup() error {
return err
}

func setGcpProps(group string) [][]string {
var (
gcpProps api.GcpResourceGroupProps
details [][]string
)
err := json.Unmarshal([]byte(group), &gcpProps)
func setGcpProps(group []byte) [][]string {
var details [][]string

gcpProps, err := unmarshallGcpProps(group)
if err != nil {
return [][]string{}
}
Expand All @@ -93,3 +91,15 @@ func setGcpProps(group string) [][]string {
details = append(details, []string{"PROJECTS", strings.Join(gcpProps.Projects, ",")})
return details
}

func unmarshallGcpPropString(group []byte) (props api.GcpResourceGroupProps, err error) {
var rawProps api.GcpResourceGroupJsonStringProps
err = json.Unmarshal(group, &rawProps)
props = api.GcpResourceGroupProps(rawProps)
return
}

func unmarshallGcpProps(group []byte) (props api.GcpResourceGroupProps, err error) {
err = json.Unmarshal(group, &props)
return
}
17 changes: 14 additions & 3 deletions cli/cmd/resource_group_lw_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,23 @@ func createLwAccountResourceGroup() error {
return err
}

func setLwAccountProps(group string) []string {
var lwProps api.LwAccountResourceGroupProps
err := json.Unmarshal([]byte(group), &lwProps)
func setLwAccountProps(group []byte) []string {
lwProps, err := unmarshallLwAccountProps(group)
if err != nil {
return []string{}
}

return []string{"LW ACCOUNTS", strings.Join(lwProps.LwAccounts, ",")}
}

func unmarshallLwAccountPropString(group []byte) (props api.LwAccountResourceGroupProps, err error) {
var rawProps api.LwAccountResourceGroupJsonStringProps
err = json.Unmarshal(group, &rawProps)
props = api.LwAccountResourceGroupProps(rawProps)
return
}

func unmarshallLwAccountProps(group []byte) (props api.LwAccountResourceGroupProps, err error) {
err = json.Unmarshal(group, &props)
return
}
18 changes: 14 additions & 4 deletions cli/cmd/resource_group_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@ func createMachineResourceGroup() error {
return err
}

func setMachineProps(group string) []string {
var machineProps api.MachineResourceGroupProps

err := json.Unmarshal([]byte(group), &machineProps)
func setMachineProps(group []byte) []string {
machineProps, err := unmarshallMachineProps(group)
if err != nil {
return []string{}
}
Expand All @@ -90,3 +88,15 @@ func setMachineProps(group string) []string {
}
return []string{"MACHINE TAGS", strings.Join(tags, ",")}
}

func unmarshallMachinePropString(group []byte) (props api.MachineResourceGroupProps, err error) {
var rawProps api.MachineResourceGroupJsonStringProps
err = json.Unmarshal(group, &rawProps)
props = api.MachineResourceGroupProps(rawProps)
return
}

func unmarshallMachineProps(group []byte) (props api.MachineResourceGroupProps, err error) {
err = json.Unmarshal(group, &props)
return
}
90 changes: 65 additions & 25 deletions cli/cmd/resource_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ Then navigate to Settings > Resource Groups.
for _, g := range resourceGroups.Data {

groups = append(groups, resourceGroup{
Id: g.ResourceGuid,
ResType: g.Type,
Name: g.Name,
State: g.Status(),
Id: g.ResourceGuid,
ResType: g.Type,
Name: g.Name,
status: g.Status(),
Enabled: g.Enabled,
IsDefault: g.IsDefault,
})
}

Expand All @@ -88,10 +90,10 @@ Then navigate to Settings > Resource Groups.

rows := [][]string{}
for _, g := range groups {
rows = append(rows, []string{g.Id, g.ResType, g.Name, g.State})
rows = append(rows, []string{g.Id, g.ResType, g.Name, g.status, IsDefault(g.IsDefault)})
}

cli.OutputHuman(renderSimpleTable([]string{"RESOURCE GUID", "TYPE", "NAME", "STATE"}, rows))
cli.OutputHuman(renderSimpleTable([]string{"RESOURCE GUID", "TYPE", "NAME", "STATUS", "DEFAULT"}, rows))
return nil
},
}
Expand All @@ -108,12 +110,16 @@ Then navigate to Settings > Resource Groups.
return errors.Wrap(err, "unable to get resource group")
}

props, _ := parsePropsType(response)

group := resourceGroup{
Id: response.Data.ResourceGuid,
ResType: response.Data.Type,
Name: response.Data.Name,
State: response.Data.Status(),
Props: response.Data.Props,
Id: response.Data.ResourceGuid,
ResType: response.Data.Type,
Name: response.Data.Name,
status: response.Data.Status(),
Props: props,
Enabled: response.Data.Enabled,
IsDefault: response.Data.IsDefault,
}

if cli.JSONOutput() {
Expand All @@ -123,10 +129,10 @@ Then navigate to Settings > Resource Groups.
return cli.OutputJSON(jsonOut)
}

groupCommon := [][]string{}
groupCommon = append(groupCommon, []string{group.Id, group.ResType, group.Name, group.State})
var groupCommon [][]string
groupCommon = append(groupCommon, []string{group.Id, group.ResType, group.Name, group.status, IsDefault(group.IsDefault)})

cli.OutputHuman(renderSimpleTable([]string{"RESOURCE ID", "TYPE", "NAME", "STATE"}, groupCommon))
cli.OutputHuman(renderSimpleTable([]string{"RESOURCE ID", "TYPE", "NAME", "STATE", "DEFAULT"}, groupCommon))
cli.OutputHuman("\n")
cli.OutputHuman(buildResourceGroupPropsTable(group))

Expand Down Expand Up @@ -170,6 +176,28 @@ Then navigate to Settings > Resource Groups.
}
)

// parsePropsType converts props json string to interface of resource group props type
func parsePropsType(response api.ResourceGroupResponse) (interface{}, error) {
propsString := response.Data.Props.(string)

switch response.Data.Type {
case api.AwsResourceGroup.String():
return unmarshallAwsPropString([]byte(propsString))
case api.AzureResourceGroup.String():
return unmarshallAzurePropString([]byte(propsString))
case api.ContainerResourceGroup.String():
return unmarshallContainerPropString([]byte(propsString))
case api.GcpResourceGroup.String():
return unmarshallGcpPropString([]byte(propsString))
case api.LwAccountResourceGroup.String():
return unmarshallLwAccountPropString([]byte(propsString))
case api.MachineResourceGroup.String():
return unmarshallMachinePropString([]byte(propsString))
}
return nil, errors.New("Unable to determine resource group props type")

}

func promptCreateResourceGroup() error {
var (
group = ""
Expand Down Expand Up @@ -228,8 +256,11 @@ func buildResourceGroupPropsTable(group resourceGroup) string {
}

func determineResourceGroupProps(resType string, props interface{}) [][]string {
details := setBaseProps(props)
propsString := props.(string)
propsString, err := json.Marshal(props)
if err != nil {
return [][]string{}
}
details := setBaseProps(propsString)

switch resType {
case api.AwsResourceGroup.String():
Expand All @@ -249,13 +280,13 @@ func determineResourceGroupProps(resType string, props interface{}) [][]string {
return details
}

func setBaseProps(props interface{}) [][]string {
func setBaseProps(props []byte) [][]string {
var (
baseProps resourceGroupPropsBase
details [][]string
)

err := json.Unmarshal([]byte(props.(string)), &baseProps)
err := json.Unmarshal(props, &baseProps)
if err != nil {
return [][]string{}
}
Expand All @@ -277,16 +308,25 @@ func init() {
resourceGroupsCommand.AddCommand(resourceGroupsDeleteCommand)
}

func IsDefault(isDefault int) string {
if isDefault == 1 {
return "True"
}
return "False"
}

type resourceGroup struct {
Id string `json:"resource_guid"`
ResType string `json:"type"`
Name string `json:"name"`
State string `json:"state"`
Props interface{} `json:"props"`
Id string `json:"resource_guid"`
ResType string `json:"type"`
Name string `json:"name"`
Props interface{} `json:"props"`
Enabled int `json:"enabled"`
IsDefault int `json:"isDefault"`
status string
}

type resourceGroupPropsBase struct {
Description string `json:"description"`
UpdatedBy string `json:"UPDATED_BY,omitempty"`
LastUpdated int `json:"LAST_UPDATED,omitempty"`
UpdatedBy string `json:"updatedBy,omitempty"`
LastUpdated int `json:"lastUpdated,omitempty"`
}

0 comments on commit 3311ef2

Please sign in to comment.