From 3311ef262edc058f29f0f151d938c3e76ab15f05 Mon Sep 17 00:00:00 2001 From: Darren <75614232+dmurray-lacework@users.noreply.github.com> Date: Thu, 9 Sep 2021 15:42:51 +0100 Subject: [PATCH] fix(cli): Output correct resource group json (#543) Signed-off-by: Darren Murray --- cli/cmd/resource_group_aws.go | 17 +++++- cli/cmd/resource_group_azure.go | 22 +++++-- cli/cmd/resource_group_container.go | 22 +++++-- cli/cmd/resource_group_gcp.go | 22 +++++-- cli/cmd/resource_group_lw_account.go | 17 +++++- cli/cmd/resource_group_machine.go | 18 ++++-- cli/cmd/resource_groups.go | 90 ++++++++++++++++++++-------- 7 files changed, 156 insertions(+), 52 deletions(-) diff --git a/cli/cmd/resource_group_aws.go b/cli/cmd/resource_group_aws.go index 6c8c14b26..dcdafc69c 100644 --- a/cli/cmd/resource_group_aws.go +++ b/cli/cmd/resource_group_aws.go @@ -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 +} diff --git a/cli/cmd/resource_group_azure.go b/cli/cmd/resource_group_azure.go index cbac46411..945bd9f15 100644 --- a/cli/cmd/resource_group_azure.go +++ b/cli/cmd/resource_group_azure.go @@ -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{} } @@ -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 +} diff --git a/cli/cmd/resource_group_container.go b/cli/cmd/resource_group_container.go index cb52fe149..e1ce514bf 100644 --- a/cli/cmd/resource_group_container.go +++ b/cli/cmd/resource_group_container.go @@ -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{} } @@ -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 +} diff --git a/cli/cmd/resource_group_gcp.go b/cli/cmd/resource_group_gcp.go index e62c31efb..7549b1874 100644 --- a/cli/cmd/resource_group_gcp.go +++ b/cli/cmd/resource_group_gcp.go @@ -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{} } @@ -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 +} diff --git a/cli/cmd/resource_group_lw_account.go b/cli/cmd/resource_group_lw_account.go index 6ca0b3946..252572a27 100644 --- a/cli/cmd/resource_group_lw_account.go +++ b/cli/cmd/resource_group_lw_account.go @@ -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 +} diff --git a/cli/cmd/resource_group_machine.go b/cli/cmd/resource_group_machine.go index c6194fd8e..78083d0f5 100644 --- a/cli/cmd/resource_group_machine.go +++ b/cli/cmd/resource_group_machine.go @@ -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{} } @@ -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 +} diff --git a/cli/cmd/resource_groups.go b/cli/cmd/resource_groups.go index 3b2a8c560..96c426df2 100644 --- a/cli/cmd/resource_groups.go +++ b/cli/cmd/resource_groups.go @@ -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, }) } @@ -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 }, } @@ -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() { @@ -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)) @@ -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 = "" @@ -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(): @@ -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{} } @@ -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"` }