1818package utils
1919
2020import (
21+ "fmt"
22+ "strings"
2123 "testing"
2224 "time"
2325
@@ -26,23 +28,30 @@ import (
2628)
2729
2830type CmdCfg struct {
29- sleep int // minutes to sleep prior to CAI retreval. default: 2
30- assetType string // asset type to retrieve. default: all
31+ sleep int // minutes to sleep prior to CAI retreval. default: 2
32+ assetTypes []string // asset types to retrieve. empty: all
33+ args []string // arguments to pass to call
3134}
3235
3336type cmdOption func (* CmdCfg )
3437
3538// newCmdConfig sets defaults and options
3639func newCmdConfig (opts ... cmdOption ) (* CmdCfg ) {
3740 caiOpts := & CmdCfg {
38- sleep : 2 ,
39- assetType : "" ,
41+ sleep : 2 ,
42+ assetTypes : nil ,
43+ args : nil ,
4044 }
4145
4246 for _ , opt := range opts {
4347 opt (caiOpts )
4448 }
4549
50+ if caiOpts .assetTypes != nil {
51+ caiOpts .args = []string {"--asset-types" , strings .Join (caiOpts .assetTypes , "," )}
52+ }
53+ caiOpts .args = append (caiOpts .args , "--content-type" , "resource" )
54+
4655 return caiOpts
4756}
4857
@@ -53,20 +62,21 @@ func WithSleep(sleep int) cmdOption {
5362 }
5463}
5564
56- // Set asset type
57- func WithAssetType ( assetType string ) cmdOption {
65+ // Set asset types
66+ func WithAssetTypes ( assetTypes [] string ) cmdOption {
5867 return func (f * CmdCfg ) {
59- f .assetType = assetType
68+ f .assetTypes = assetTypes
6069 }
6170}
6271
6372// GetProjectResources returns the cloud asset inventory resources for a project as a gjson.Result
6473func GetProjectResources (t testing.TB , project string , opts ... cmdOption ) gjson.Result {
6574 caiOpts := newCmdConfig (opts ... )
75+
76+ // Cloud Asset Inventory offers best-effort data freshness.
77+ t .Logf ("Sleeping for %d minutes before retrieving Cloud Asset Inventory..." , caiOpts .sleep )
6678 time .Sleep (time .Duration (caiOpts .sleep ) * time .Minute )
67- if caiOpts .assetType != "" {
68- return gcloud .Runf (t , "asset list --project=%s --asset-types=%s --content-type=resource" , project , caiOpts .assetType )
69- } else {
70- return gcloud .Runf (t , "asset list --project=%s --content-type=resource" , project )
71- }
79+
80+ cmd := fmt .Sprintf ("asset list --project %s" , project )
81+ return gcloud .Runf (t , strings .Join (append ([]string {cmd }, caiOpts .args ... ), " " ))
7282}
0 commit comments