@@ -22,10 +22,10 @@ import (
2222 "encoding/json"
2323 "fmt"
2424 "strconv"
25-
26- "github.com/pkg/errors"
25+ "time"
2726
2827 "github.com/lacework/go-sdk/lwtime"
28+ "github.com/pkg/errors"
2929)
3030
3131// ResourceGroupsService is the service that interacts with
@@ -48,6 +48,8 @@ type ResourceGroup interface {
4848 ID () string
4949 ResourceGroupType () ResourceGroupType
5050 ResetResourceGUID ()
51+ ResetRGV2Fields ()
52+ IsV2Group () bool
5153}
5254
5355type ResourceGroupType int
@@ -81,41 +83,6 @@ func (i ResourceGroupType) String() string {
8183 return ResourceGroupTypes [i ]
8284}
8385
84- // NewResourceGroup returns an instance of the ResourceGroupData struct with the
85- // provided ResourceGroup type, name and the props field as an interface{}.
86- //
87- // NOTE: This function must be used by any ResourceGroup type.
88- //
89- // Basic usage: Initialize a new ContainerResourceGroup struct, then
90- //
91- // use the new instance to do CRUD operations
92- //
93- // client, err := api.NewClient("account")
94- // if err != nil {
95- // return err
96- // }
97- //
98- // group := api.NewResourceGroup("container resource group",
99- // api.ContainerResourceGroup,
100- // api.ContainerResourceGroupData{
101- // Props: api.ContainerResourceGroupProps{
102- // Description: "all containers,
103- // ContainerLabels: ContainerResourceGroupAllLabels,
104- // ContainerTags: ContainerResourceGroupAllTags,
105- // },
106- // },
107- // )
108- //
109- // client.V2.ResourceGroups.Create(group)
110- func NewResourceGroup (name string , iType ResourceGroupType , props interface {}) ResourceGroupData {
111- return ResourceGroupData {
112- Name : name ,
113- Type : iType .String (),
114- Enabled : 1 ,
115- Props : props ,
116- }
117- }
118-
11986// FindResourceGroupType looks up inside the list of available resource group types
12087// the matching type from the provided string, if none, returns NoneResourceGroup
12188func FindResourceGroupType (resourceGroup string ) (ResourceGroupType , bool ) {
@@ -200,24 +167,6 @@ func castResourceGroupResponse(data resourceGroupWorkaroundData, response interf
200167 return nil
201168}
202169
203- func setResourceGroupResponse (response resourceGroupWorkaroundData ) (ResourceGroupResponse , error ) {
204- isDefault , err := strconv .Atoi (response .IsDefault )
205- if err != nil {
206- return ResourceGroupResponse {}, err
207- }
208- return ResourceGroupResponse {
209- Data : ResourceGroupData {
210- Guid : response .Guid ,
211- IsDefault : isDefault ,
212- ResourceGuid : response .ResourceGuid ,
213- Name : response .Name ,
214- Type : response .Type ,
215- Enabled : response .Enabled ,
216- Props : response .Props ,
217- },
218- }, nil
219- }
220-
221170func setResourceGroupsResponse (workaround resourceGroupsWorkaroundResponse ) (ResourceGroupsResponse , error ) {
222171 var data []ResourceGroupData
223172 for _ , r := range workaround .Data {
@@ -250,9 +199,9 @@ func (svc *ResourceGroupsService) Delete(guid string) error {
250199// To return a more specific Go struct of a Resource Group, use the proper
251200// method such as GetContainerResourceGroup() where the function name is composed by:
252201//
253- // Get<Type>(guid)
202+ // Get<Type>(guid)
254203//
255- // Where <Type> is the Resource Group type.
204+ // Where <Type> is the Resource Group type.
256205func (svc * ResourceGroupsService ) Get (guid string , response interface {}) error {
257206 var rawResponse resourceGroupWorkaroundResponse
258207 err := svc .get (guid , & rawResponse )
@@ -290,11 +239,26 @@ func (group ResourceGroupData) ResourceGroupType() ResourceGroupType {
290239}
291240
292241func (group ResourceGroupData ) ID () string {
293- return group .ResourceGuid
242+ if ! group .IsV2Group () {
243+ return group .ResourceGuid
244+ } else {
245+ return group .ResourceGroupGuid
246+ }
247+ }
248+
249+ func (group * ResourceGroupData ) ResetRGV2Fields () {
250+ group .UpdatedBy = ""
251+ group .UpdatedTime = nil
252+ group .CreatedBy = ""
253+ group .CreatedTime = nil
254+ group .IsDefaultBoolean = nil
255+ group .IsOrg = nil
294256}
295257
296258func (group * ResourceGroupData ) ResetResourceGUID () {
297259 group .ResourceGuid = ""
260+ group .ResourceGroupGuid = ""
261+ group .ResetRGV2Fields ()
298262}
299263
300264func (group ResourceGroupData ) Status () string {
@@ -304,6 +268,10 @@ func (group ResourceGroupData) Status() string {
304268 return "Disabled"
305269}
306270
271+ func (group ResourceGroupData ) IsV2Group () bool {
272+ return group .Query != nil
273+ }
274+
307275type ResourceGroupResponse struct {
308276 Data ResourceGroupData `json:"data"`
309277}
@@ -313,13 +281,26 @@ type ResourceGroupsResponse struct {
313281}
314282
315283type ResourceGroupData struct {
284+ // RGv1 Fields
316285 Guid string `json:"guid,omitempty"`
317286 IsDefault int `json:"isDefault,omitempty"`
318287 ResourceGuid string `json:"resourceGuid,omitempty"`
319- Name string `json:"resourceName"`
288+ Name string `json:"resourceName,omitempty "`
320289 Type string `json:"resourceType"`
321- Enabled int `json:"enabled,omitempty"`
322- Props interface {} `json:"props"`
290+ Enabled int `json:"enabled"`
291+ Props interface {} `json:"props,omitempty"`
292+
293+ // RG v2 Fields. `Enabled` and `Type` fields are the same in RGv1 nd RGv2
294+ NameV2 string `json:"name,omitempty"`
295+ Query * RGQuery `json:"query,omitempty"`
296+ Description string `json:"description,omitempty"`
297+ ResourceGroupGuid string `json:"resourceGroupGuid,omitempty"`
298+ CreatedTime * time.Time `json:"lastUpdated,omitempty"`
299+ CreatedBy string `json:"createdBy,omitempty"`
300+ UpdatedTime * time.Time `json:"updatedTime,omitempty"`
301+ UpdatedBy string `json:"updatedBy,omitempty"`
302+ IsDefaultBoolean * bool `json:"isDefaultBoolean,omitempty"`
303+ IsOrg * bool `json:"isOrg,omitempty"`
323304}
324305
325306// RAIN-21510 workaround
@@ -339,4 +320,15 @@ type resourceGroupWorkaroundData struct {
339320 Type string `json:"resourceType"`
340321 Enabled int `json:"enabled,omitempty"`
341322 Props interface {} `json:"props"`
323+
324+ NameV2 string `json:"name,omitempty"`
325+ Query * RGQuery `json:"query,omitempty"`
326+ Description string `json:"description,omitempty"`
327+ ResourceGroupGuid string `json:"resourceGroupGuid,omitempty"`
328+ CreatedTime * time.Time `json:"lastUpdated,omitempty"`
329+ CreatedBy string `json:"createdBy,omitempty"`
330+ UpdatedTime * time.Time `json:"updatedTime,omitempty"`
331+ UpdatedBy string `json:"updatedBy,omitempty"`
332+ IsDefaultBoolean * bool `json:"isDefaultBoolean,omitempty"`
333+ IsOrg * bool `json:"isOrg,omitempty"`
342334}
0 commit comments