Skip to content

Commit

Permalink
feat: Add List support
Browse files Browse the repository at this point in the history
  • Loading branch information
zekisherif committed Aug 2, 2023
1 parent 967b3e8 commit 7765084
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 80 deletions.
35 changes: 0 additions & 35 deletions api/resource_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,41 +81,6 @@ func (i ResourceGroupType) String() string {
return ResourceGroupTypes[i]
}

// NewResourceGroup returns an instance of the ResourceGroupData struct with the
// provided ResourceGroup type, name and the props field as an interface{}.
//
// NOTE: This function must be used by any ResourceGroup type.
//
// Basic usage: Initialize a new ContainerResourceGroup struct, then
// use the new instance to do CRUD operations
//
// client, err := api.NewClient("account")
// if err != nil {
// return err
// }
//
// group := api.NewResourceGroup("container resource group",
// api.ContainerResourceGroup,
// api.ContainerResourceGroupData{
// Props: api.ContainerResourceGroupProps{
// Description: "all containers,
// ContainerLabels: ContainerResourceGroupAllLabels,
// ContainerTags: ContainerResourceGroupAllTags,
// },
// },
// )
//
// client.V2.ResourceGroups.Create(group)
//
func NewResourceGroup(name string, iType ResourceGroupType, props interface{}) ResourceGroupData {
return ResourceGroupData{
Name: name,
Type: iType.String(),
Enabled: 1,
Props: props,
}
}

// FindResourceGroupType looks up inside the list of available resource group types
// the matching type from the provided string, if none, returns NoneResourceGroup
func FindResourceGroupType(resourceGroup string) (ResourceGroupType, bool) {
Expand Down
9 changes: 0 additions & 9 deletions api/resource_groups_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ import (
"github.com/pkg/errors"
)

func NewResourceGroupV2(name string, iType ResourceGroupType, query *RGQuery) ResourceGroupV2Data {
return ResourceGroupV2Data{
Name: name,
Type: iType.String(),
Enabled: 1,
Query: query,
}
}

func (svc *ResourceGroupsV2Service) List() (response ResourceGroupsResponse, err error) {
var rawResponse ResourceGroupsV2Response
err = svc.client.RequestDecoder("GET", apiV2ResourceGroups, nil, &rawResponse)
Expand Down
136 changes: 106 additions & 30 deletions api/resource_groups_version_service.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,71 @@
package api
import (

"encoding/json"
"github.com/lacework/go-sdk/lwtime"
"github.com/pkg/errors"
import (
"encoding/json"
"github.com/lacework/go-sdk/lwtime"
"github.com/pkg/errors"
)



type ResourceGroupsVersionService struct {
client *Client
v1ResourceGroupService *ResourceGroupsService
v2ResourceGroupService *ResourceGroupsV2Service
featureFlagService *FeatureFlagsService
client *Client
v1ResourceGroupService *ResourceGroupsService
v2ResourceGroupService *ResourceGroupsV2Service
featureFlagService *FeatureFlagsService
}

func NewResourceGroupsVersionService(c *Client) *ResourceGroupsVersionService {
return &ResourceGroupsVersionService {
return &ResourceGroupsVersionService{
c,
&ResourceGroupsService{c},
&ResourceGroupsV2Service{c},
&FeatureFlagsService{c},
}
}

// NewResourceGroup returns an instance of the ResourceGroupData struct with the
// provided ResourceGroup type, name and the props field as an interface{}.
//
// NOTE: This function must be used by any ResourceGroup type.
//
// Basic usage: Initialize a new ContainerResourceGroup struct, then
// use the new instance to do CRUD operations
//
// client, err := api.NewClient("account")
// if err != nil {
// return err
// }
//
// group := api.NewResourceGroup("container resource group",
// api.ContainerResourceGroup,
// api.ContainerResourceGroupData{
// Props: api.ContainerResourceGroupProps{
// Description: "all containers,
// ContainerLabels: ContainerResourceGroupAllLabels,
// ContainerTags: ContainerResourceGroupAllTags,
// },
// },
// )
//
// client.V2.ResourceGroups.Create(group)
//
func NewResourceGroup(name string, iType ResourceGroupType, props interface{}) ResourceGroupVersionUnawareData {
return ResourceGroupVersionUnawareData{
Name: name,
Type: iType.String(),
Enabled: 1,
Props: props,
}
}

func NewResourceGroupWithQuery(name string, iType ResourceGroupType, query *RGQuery) ResourceGroupVersionUnawareData {
return ResourceGroupVersionUnawareData{
Name: name,
Type: iType.String(),
Enabled: 1,
Query: query,
}
}

func isRGV2FlagEnabled(featureFlagService *FeatureFlagsService) (bool, error) {
response, err := featureFlagService.GetFeatureFlagsMatchingPrefix("PUBLIC.rgv2.cli")

Expand All @@ -38,6 +80,10 @@ type ResourceGroupVersionUnawareResponse struct {
Data ResourceGroupVersionUnawareData `json:"data"`
}

type ResourceGroupsVersionUnawareResponse struct {
Data []ResourceGroupVersionUnawareData `json:"data"`
}

type ResourceGroupVersionUnawareData struct {
// RGv1 Fields
Guid string `json:"guid,omitempty"`
Expand All @@ -49,7 +95,7 @@ type ResourceGroupVersionUnawareData struct {
Props interface{} `json:"props,omitempty"`

// RG v2 Fields. `Enabled` and `Type` fields are the same in RGv1 nd RGv2
NameV2 string `json:"name,omitempty"`
NameV2 string `json:"name,omitempty"`
Query *RGQuery `json:"query,omitempty"`
Description string `json:"description,omitempty"`
ResourceGroupGuid string `json:"resourceGroupGuid,omitempty"`
Expand All @@ -62,7 +108,7 @@ type ResourceGroupVersionUnawareData struct {
}

func castResourceGroupV1Response(data ResourceGroupResponse, response interface{}) error {
group := ResourceGroupVersionUnawareResponse {
group := ResourceGroupVersionUnawareResponse{
Data: ResourceGroupVersionUnawareData{
Guid: data.Data.Guid,
IsDefault: data.Data.IsDefault,
Expand All @@ -75,9 +121,9 @@ func castResourceGroupV1Response(data ResourceGroupResponse, response interface{
}

j, err := json.Marshal(group)
if err != nil {
return err
}
if err != nil {
return err
}

err = json.Unmarshal(j, &response)
if err != nil {
Expand All @@ -88,20 +134,20 @@ func castResourceGroupV1Response(data ResourceGroupResponse, response interface{
}

func castResourceGroupV2Response(data ResourceGroupV2Response, response interface{}) error {
group := ResourceGroupVersionUnawareResponse {
group := ResourceGroupVersionUnawareResponse{
Data: ResourceGroupVersionUnawareData{
Type: data.Data.Type,
Enabled: data.Data.Enabled,
NameV2: data.Data.Name,
Query: data.Data.Query,
Description: data.Data.Description,
Type: data.Data.Type,
Enabled: data.Data.Enabled,
NameV2: data.Data.Name,
Query: data.Data.Query,
Description: data.Data.Description,
ResourceGroupGuid: data.Data.ResourceGroupGuid,
CreatedTime: data.Data.CreatedTime,
CreatedBy: data.Data.CreatedBy,
UpdatedTime: data.Data.UpdatedTime,
UpdatedBy: data.Data.UpdatedBy,
IsDefaultBoolean: data.Data.IsDefaultBoolean,
IsOrg: data.Data.IsOrg,
CreatedTime: data.Data.CreatedTime,
CreatedBy: data.Data.CreatedBy,
UpdatedTime: data.Data.UpdatedTime,
UpdatedBy: data.Data.UpdatedBy,
IsDefaultBoolean: data.Data.IsDefaultBoolean,
IsOrg: data.Data.IsOrg,
},
}

Expand Down Expand Up @@ -272,7 +318,6 @@ func (svc *ResourceGroupsVersionService) Update(group ResourceGroupVersionUnawar
return
}


err = errors.New("Unable to update resource group")
return
}
Expand All @@ -296,4 +341,35 @@ func (svc *ResourceGroupsVersionService) Delete(guid string) error {
}

return nil
}
}

// ListV1 returns a list of Resource Groups v1
// We need this temporary function to only get v1 RGs for alerting. RGv2 not supported in AlertRules/ReportRules yet.
func (svc *ResourceGroupsVersionService) ListV1() (response ResourceGroupsResponse, err error) {
var allGroupsResponse ResourceGroupsVersionUnawareResponse
err = svc.client.RequestDecoder("GET", apiV2ResourceGroups, nil, &allGroupsResponse)

if err != nil {
return
}

var v1ResourceGroups []ResourceGroupData

for _, group := range allGroupsResponse.Data {
if group.Query == nil {
var resourceGroupV1Data ResourceGroupData
err = transformToResourceGroupVersionAwareObject(group, &resourceGroupV1Data)
if err != nil {
return
}
v1ResourceGroups = append(v1ResourceGroups, resourceGroupV1Data)
}
}

response.Data = v1ResourceGroups
return
}
func (svc *ResourceGroupsVersionService) List() (response ResourceGroupsVersionUnawareResponse, err error) {
err = svc.client.RequestDecoder("GET", apiV2ResourceGroups, nil, &response)
return
}
6 changes: 3 additions & 3 deletions api/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type V2Endpoints struct {
ContainerRegistries *ContainerRegistriesService
Configs *v2ConfigService
FeatureFlags *FeatureFlagsService
ResourceGroups *ResourceGroupsService
ResourceGroups *ResourceGroupsVersionService
AgentAccessTokens *AgentAccessTokensService
AgentInfo *AgentInfoService
Inventory *InventoryService
Expand Down Expand Up @@ -77,7 +77,7 @@ func NewV2Endpoints(c *Client) *V2Endpoints {
&ContainerRegistriesService{c},
NewV2ConfigService(c),
&FeatureFlagsService{c},
&ResourceGroupsService{c},
NewResourceGroupsVersionService(c),
&AgentAccessTokensService{c},
&AgentInfoService{c},
&InventoryService{c},
Expand Down Expand Up @@ -113,7 +113,7 @@ func NewV2Endpoints(c *Client) *V2Endpoints {
AlertRules: &AlertRulesService{c},
CloudAccounts: &CloudAccountsService{c},
ContainerRegistries: &ContainerRegistriesService{c},
ResourceGroups: NewResourceGroupsVersionService(c),
ResourceGroups: &ResourceGroupsService{c},
TeamMembers: &TeamMembersService{c},
ReportRules: &ReportRulesService{c},
VulnerabilityExceptions: &VulnerabilityExceptionsService{c},
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/alert_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func getAlertChannels() ([]string, map[string]string) {
func getResourceGroups() ([]string, map[string]string) {
cli.StartProgress("")
defer cli.StopProgress()
response, err := cli.LwApi.V2.ResourceGroups.List()
response, err := cli.LwApi.V2.ResourceGroups.ListV1()

if err != nil {
return nil, nil
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/report_distributions_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func promptReportDistributionScope(distribution *api.ReportDistribution, subRepo

func promptReportDistributionResourceGroup(distribution *api.ReportDistribution) error {
cli.StartProgress("Fetching list of resource groups...")
resourceGroups, err := cli.LwApi.V2.ResourceGroups.List()
resourceGroups, err := cli.LwApi.V2.ResourceGroups.ListV1()
cli.StopProgress()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/report_distributions_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func promptUpdateReportDistributionResourceGroup(
distribution *api.ReportDistributionUpdate, existing api.ReportDistribution,
) error {
cli.StartProgress("Fetching list of resource groups...")
resourceGroups, err := cli.LwApi.V2.ResourceGroups.List()
resourceGroups, err := cli.LwApi.V2.ResourceGroups.ListV1()
cli.StopProgress()
if err != nil {
return err
Expand Down

0 comments on commit 7765084

Please sign in to comment.