Skip to content

Commit b0413e4

Browse files
sjain05ErikAtSumo
authored andcommitted
SUMO-245407: Add support for list apps v2 as data resource
1 parent 0f95eed commit b0413e4

File tree

3 files changed

+215
-27
lines changed

3 files changed

+215
-27
lines changed

sumologic/data_source_sumologic_apps.go

Lines changed: 83 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ func dataSourceSumoLogicApps() *schema.Resource {
2424
Type: schema.TypeString,
2525
Optional: true,
2626
},
27+
"id": {
28+
Type: schema.TypeString,
29+
Computed: true,
30+
},
2731
"apps": {
2832
Type: schema.TypeList,
2933
Computed: true,
@@ -44,9 +48,33 @@ func dataSourceSumoLogicApps() *schema.Resource {
4448
"installs": {Type: schema.TypeInt, Computed: true},
4549
"app_type": {Type: schema.TypeString, Computed: true},
4650
"attributes": {
47-
Type: schema.TypeMap,
48-
Computed: true,
49-
Elem: &schema.Schema{Type: schema.TypeList, Elem: &schema.Schema{Type: schema.TypeString}},
51+
Type: schema.TypeList,
52+
Optional: true,
53+
Elem: &schema.Resource{
54+
Schema: map[string]*schema.Schema{
55+
"category": {
56+
Type: schema.TypeList,
57+
Optional: true,
58+
Elem: &schema.Schema{
59+
Type: schema.TypeString,
60+
},
61+
},
62+
"use_case": {
63+
Type: schema.TypeList,
64+
Optional: true,
65+
Elem: &schema.Schema{
66+
Type: schema.TypeString,
67+
},
68+
},
69+
"collection": {
70+
Type: schema.TypeList,
71+
Optional: true,
72+
Elem: &schema.Schema{
73+
Type: schema.TypeString,
74+
},
75+
},
76+
},
77+
},
5078
},
5179
"family": {Type: schema.TypeString, Computed: true},
5280
"installable": {Type: schema.TypeBool, Computed: true},
@@ -58,25 +86,29 @@ func dataSourceSumoLogicApps() *schema.Resource {
5886
}
5987
}
6088

61-
func dataSourceSumoLogicAppsRead(d *schema.ResourceData, m interface{}) error {
62-
c := m.(*Client)
89+
func dataSourceSumoLogicAppsRead(d *schema.ResourceData, meta interface{}) error {
90+
c := meta.(*Client)
6391

6492
// Read apps from the API
65-
id, apps, err := c.getApps(d.Get("name").(string), d.Get("author").(string))
93+
id, fapps, err := c.getApps(d.Get("name").(string), d.Get("author").(string))
6694
if err != nil {
6795
return err
6896
}
6997

70-
if err := d.Set("apps", flattenApps(apps)); err != nil {
71-
return err
72-
}
98+
// if err := d.Set("apps", flattenApps(apps)); err != nil {
99+
// return err
100+
// }
73101

102+
//fmt.Printf("%v\n", apps)
74103
d.SetId(id)
104+
fmt.Printf("%v\n", fapps)
105+
d.Set("apps", fapps)
106+
fmt.Printf("%v\n", d.Get("apps"))
75107

76108
return nil
77109
}
78110

79-
func (s *Client) getApps(name string, author string) (string, []App, error) {
111+
func (s *Client) getApps(name string, author string) (string, []interface{}, error) {
80112
// Construct the base URL
81113
baseURL := "v2/apps"
82114

@@ -102,14 +134,15 @@ func (s *Client) getApps(name string, author string) (string, []App, error) {
102134

103135
apps := AppsResponse{}
104136
err = json.Unmarshal(data, &apps)
137+
//fmt.Printf("%v\n", apps)
105138
if err != nil {
106139
return "", nil, err
107140
}
108141

109142
// Generate a unique ID for this data source
110143
id := generateDataSourceId(name, author, apps.Apps)
111144

112-
return id, apps.Apps, nil
145+
return id, flattenApps(apps.Apps), nil
113146
}
114147

115148
func generateDataSourceId(name string, author string, apps []App) string {
@@ -136,6 +169,16 @@ func generateDataSourceId(name string, author string, apps []App) string {
136169
func flattenApps(apps []App) []interface{} {
137170
var flattenedApps []interface{}
138171
for _, app := range apps {
172+
173+
//attributes := make([]interface{}, 1)
174+
internalAttributes := make(map[string]interface{})
175+
internalAttributes["category"] = app.Attributes.Category
176+
internalAttributes["use_case"] = app.Attributes.UseCase
177+
internalAttributes["collection"] = app.Attributes.Collection
178+
attributes := []interface{}{
179+
internalAttributes,
180+
}
181+
139182
flattenedApp := map[string]interface{}{
140183
"uuid": app.UUID,
141184
"name": app.Name,
@@ -147,7 +190,7 @@ func flattenApps(apps []App) []interface{} {
147190
"beta": app.Beta,
148191
"installs": app.Installs,
149192
"app_type": app.AppType,
150-
"attributes": app.Attributes,
193+
"attributes": attributes,
151194
"family": app.Family,
152195
"installable": app.Installable,
153196
"show_on_marketplace": app.ShowOnMarketplace,
@@ -157,23 +200,37 @@ func flattenApps(apps []App) []interface{} {
157200
return flattenedApps
158201
}
159202

203+
// Helper function to convert []string to []interface{}
204+
func convertStringSliceToInterfaceSlice(input []string) []interface{} {
205+
result := make([]interface{}, len(input))
206+
for i, v := range input {
207+
result[i] = v
208+
}
209+
fmt.Printf("%v\n", result)
210+
return result
211+
}
212+
160213
type AppsResponse struct {
161214
Apps []App `json:"apps"`
162215
}
163216

164217
type App struct {
165-
UUID string `json:"uuid"`
166-
Name string `json:"name"`
167-
Description string `json:"description"`
168-
LatestVersion string `json:"latestVersion"`
169-
Icon string `json:"icon"`
170-
Author string `json:"author"`
171-
AccountTypes []string `json:"accountTypes"`
172-
Beta bool `json:"beta"`
173-
Installs int `json:"installs"`
174-
AppType string `json:"appType"`
175-
Attributes map[string]interface{} `json:"attributes"`
176-
Family string `json:"family"`
177-
Installable bool `json:"installable"`
178-
ShowOnMarketplace bool `json:"showOnMarketplace"`
218+
UUID string `json:"uuid"`
219+
Name string `json:"name"`
220+
Description string `json:"description"`
221+
LatestVersion string `json:"latestVersion"`
222+
Icon string `json:"icon"`
223+
Author string `json:"author"`
224+
AccountTypes []string `json:"accountTypes"`
225+
Beta bool `json:"beta"`
226+
Installs int `json:"installs"`
227+
AppType string `json:"appType"`
228+
Attributes struct {
229+
Category []string `json:"category"`
230+
UseCase []string `json:"useCase"`
231+
Collection []string `json:"collection"`
232+
} `json:"attributes"`
233+
Family string `json:"family"`
234+
Installable bool `json:"installable"`
235+
ShowOnMarketplace bool `json:"showOnMarketplace"`
179236
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package sumologic
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
10+
)
11+
12+
func TestAccDataSourceSumoLogicApps_basic(t *testing.T) {
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() { testAccPreCheck(t) },
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccDataSourceSumoLogicAppsConfig_basic,
19+
Check: resource.ComposeTestCheckFunc(
20+
testAccCheckSumoLogicAppsDataSourceID("data.sumologic_apps.test"),
21+
//testAccCheckSumoLogicAppsGreaterThanZero("data.sumologic_apps.test"),
22+
resource.TestCheckResourceAttrSet("data.sumologic_apps.test", "id"),
23+
//resource.TestCheckResourceAttrSet("data.sumologic_apps.test", "apps"),
24+
resource.TestCheckResourceAttrSet("data.sumologic_apps.test", "apps.0.uuid"),
25+
resource.TestCheckResourceAttrSet("data.sumologic_apps.test", "apps.0.name"),
26+
resource.TestCheckResourceAttrSet("data.sumologic_apps.test", "apps.0.description"),
27+
resource.TestCheckResourceAttrSet("data.sumologic_apps.test", "apps.0.latestVersion"),
28+
resource.TestCheckResourceAttrSet("data.sumologic_apps.test", "apps.0.icon"),
29+
resource.TestCheckResourceAttrSet("data.sumologic_apps.test", "apps.0.author"),
30+
resource.TestCheckResourceAttrSet("data.sumologic_apps.test", "apps.0.appType"),
31+
//resource.TestCheckResourceAttrSet("data.sumologic_apps.test", "apps.0.attributes"),
32+
resource.TestCheckResourceAttrSet("data.sumologic_apps.test", "apps.0.installable"),
33+
resource.TestCheckResourceAttrSet("data.sumologic_apps.test", "apps.0.showOnMarketplace"),
34+
),
35+
},
36+
},
37+
})
38+
}
39+
40+
// func TestAccDataSourceSumoLogicApps_filtered(t *testing.T) {
41+
// resource.Test(t, resource.TestCase{
42+
// PreCheck: func() { testAccPreCheck(t) },
43+
// ProviderFactories: providerFactories,
44+
// Steps: []resource.TestStep{
45+
// {
46+
// Config: testAccDataSourceSumoLogicAppsConfig_filtered,
47+
// Check: resource.ComposeTestCheckFunc(
48+
// testAccCheckSumoLogicAppsDataSourceID("data.sumologic_apps.filtered"),
49+
// resource.TestCheckResourceAttr("data.sumologic_apps.filtered", "apps.#", "1"),
50+
// resource.TestCheckResourceAttr("data.sumologic_apps.filtered", "apps.0.name", "AWS CloudTrail"),
51+
// resource.TestCheckResourceAttr("data.sumologic_apps.filtered", "apps.0.author", "Sumo Logic"),
52+
// ),
53+
// },
54+
// },
55+
// })
56+
// }
57+
58+
func testAccCheckSumoLogicAppsGreaterThanZero(n string) resource.TestCheckFunc {
59+
return func(s *terraform.State) error {
60+
rs, ok := s.RootModule().Resources[n]
61+
if !ok {
62+
return fmt.Errorf("Can't find SumoLogic Apps data source: %s", n)
63+
}
64+
65+
appsCount, ok := rs.Primary.Attributes["apps.#"]
66+
if !ok {
67+
return fmt.Errorf("Apps count not found in state")
68+
}
69+
70+
count, err := strconv.Atoi(appsCount)
71+
if err != nil {
72+
return fmt.Errorf("Failed to parse apps count: %v", err)
73+
}
74+
75+
if count <= 0 {
76+
return fmt.Errorf("No apps returned, expected at least one")
77+
}
78+
79+
return nil
80+
}
81+
}
82+
83+
func testAccCheckSumoLogicAppsDataSourceID(n string) resource.TestCheckFunc {
84+
return func(s *terraform.State) error {
85+
86+
rs, ok := s.RootModule().Resources[n]
87+
if !ok {
88+
return fmt.Errorf("Can't find SumoLogic Apps data source: %s", n)
89+
}
90+
91+
fmt.Printf("%v\n", s.RootModule().Resources)
92+
fmt.Printf("%v\n", rs.Primary)
93+
if rs.Primary.ID == "" {
94+
return fmt.Errorf("SumoLogic Apps data source ID not set")
95+
}
96+
return nil
97+
}
98+
}
99+
100+
const testAccDataSourceSumoLogicAppsConfig_basic = `
101+
data "sumologic_apps" "test" {
102+
name = "MySQL - OpenTelemetry"
103+
author = "Sumo Logic"
104+
}
105+
`
106+
107+
const testAccDataSourceSumoLogicAppsConfig_filtered = `
108+
data "sumologic_apps" "filtered" {
109+
name = "AWS CloudTrail"
110+
author = "Sumo Logic"
111+
}
112+
`

sumologic/data_source_sumologic_folder_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
910
)
1011

1112
func TestAccDataSourceFolder_basic(t *testing.T) {
@@ -14,15 +15,33 @@ func TestAccDataSourceFolder_basic(t *testing.T) {
1415
Providers: testAccProviders,
1516
Steps: []resource.TestStep{
1617
{
17-
Config: folderConfig("/Library/Users/[email protected]"),
18+
Config: folderConfig("/Library/Installed Apps"),
1819
Check: resource.ComposeTestCheckFunc(
20+
testAccCheckSumoLogicfDataSourceID("data.sumologic_folder.personal_folder"),
1921
testAccDataSourceFolderCheck("data.sumologic_folder.personal_folder"),
2022
),
2123
},
2224
},
2325
})
2426
}
2527

28+
func testAccCheckSumoLogicfDataSourceID(n string) resource.TestCheckFunc {
29+
return func(s *terraform.State) error {
30+
31+
rs, ok := s.RootModule().Resources[n]
32+
if !ok {
33+
return fmt.Errorf("Can't find SumoLogic Apps data source: %s", n)
34+
}
35+
36+
fmt.Printf("%v\n", s.RootModule().Resources)
37+
fmt.Printf("%v\n", rs.Primary)
38+
if rs.Primary.ID == "" {
39+
return fmt.Errorf("SumoLogic Apps data source ID not set")
40+
}
41+
return nil
42+
}
43+
}
44+
2645
func TestAccDataSourceFolder_folder_does_not_exist(t *testing.T) {
2746
resource.Test(t, resource.TestCase{
2847
PreCheck: func() { testAccPreCheck(t) },

0 commit comments

Comments
 (0)