Skip to content

Commit 9cc0c0e

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

File tree

2 files changed

+48
-78
lines changed

2 files changed

+48
-78
lines changed

sumologic/data_source_sumologic_apps.go

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,21 @@ func dataSourceSumoLogicAppsRead(d *schema.ResourceData, meta interface{}) error
9090
c := meta.(*Client)
9191

9292
// Read apps from the API
93-
id, fapps, err := c.getApps(d.Get("name").(string), d.Get("author").(string))
93+
id, apps, err := c.getApps(d.Get("name").(string), d.Get("author").(string))
9494
if err != nil {
9595
return err
9696
}
9797

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

102-
//fmt.Printf("%v\n", apps)
103102
d.SetId(id)
104-
fmt.Printf("%v\n", fapps)
105-
d.Set("apps", fapps)
106-
fmt.Printf("%v\n", d.Get("apps"))
107103

108104
return nil
109105
}
110106

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

@@ -134,15 +130,15 @@ func (s *Client) getApps(name string, author string) (string, []interface{}, err
134130

135131
apps := AppsResponse{}
136132
err = json.Unmarshal(data, &apps)
137-
//fmt.Printf("%v\n", apps)
133+
138134
if err != nil {
139135
return "", nil, err
140136
}
141137

142138
// Generate a unique ID for this data source
143139
id := generateDataSourceId(name, author, apps.Apps)
144140

145-
return id, flattenApps(apps.Apps), nil
141+
return id, apps.Apps, nil
146142
}
147143

148144
func generateDataSourceId(name string, author string, apps []App) string {
@@ -170,7 +166,6 @@ func flattenApps(apps []App) []interface{} {
170166
var flattenedApps []interface{}
171167
for _, app := range apps {
172168

173-
//attributes := make([]interface{}, 1)
174169
internalAttributes := make(map[string]interface{})
175170
internalAttributes["category"] = app.Attributes.Category
176171
internalAttributes["use_case"] = app.Attributes.UseCase
@@ -200,16 +195,6 @@ func flattenApps(apps []App) []interface{} {
200195
return flattenedApps
201196
}
202197

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-
213198
type AppsResponse struct {
214199
Apps []App `json:"apps"`
215200
}

sumologic/data_source_sumologic_apps_test.go

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,95 +18,80 @@ func TestAccDataSourceSumoLogicApps_basic(t *testing.T) {
1818
Config: testAccDataSourceSumoLogicAppsConfig_basic,
1919
Check: resource.ComposeTestCheckFunc(
2020
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"),
21+
resource.TestCheckResourceAttrSet("data.sumologic_apps.test", "apps.#"),
22+
checkResourceAttrGreaterThanZero("data.sumologic_apps.test", "apps.#"),
3423
),
3524
},
3625
},
3726
})
3827
}
3928

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-
// }
29+
func TestAccDataSourceSumoLogicApps_filtered(t *testing.T) {
30+
resource.Test(t, resource.TestCase{
31+
PreCheck: func() { testAccPreCheck(t) },
32+
Providers: testAccProviders,
33+
Steps: []resource.TestStep{
34+
{
35+
Config: testAccDataSourceSumoLogicAppsConfig_filtered,
36+
Check: resource.ComposeTestCheckFunc(
37+
testAccCheckSumoLogicAppsDataSourceID("data.sumologic_apps.filtered"),
38+
resource.TestCheckResourceAttr("data.sumologic_apps.filtered", "apps.#", "1"),
39+
resource.TestCheckResourceAttr("data.sumologic_apps.filtered", "apps.0.name", "MySQL - OpenTelemetry"),
40+
resource.TestCheckResourceAttr("data.sumologic_apps.filtered", "apps.0.author", "Sumo Logic"),
41+
),
42+
},
43+
},
44+
})
45+
}
5746

58-
func testAccCheckSumoLogicAppsGreaterThanZero(n string) resource.TestCheckFunc {
47+
func testAccCheckSumoLogicAppsDataSourceID(n string) resource.TestCheckFunc {
5948
return func(s *terraform.State) error {
6049
rs, ok := s.RootModule().Resources[n]
6150
if !ok {
6251
return fmt.Errorf("Can't find SumoLogic Apps data source: %s", n)
6352
}
6453

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")
54+
if rs.Primary.ID == "" {
55+
return fmt.Errorf("SumoLogic Apps data source ID not set")
7756
}
78-
7957
return nil
8058
}
8159
}
8260

83-
func testAccCheckSumoLogicAppsDataSourceID(n string) resource.TestCheckFunc {
61+
func checkResourceAttrGreaterThanZero(resourceName, attributeName string) resource.TestCheckFunc {
8462
return func(s *terraform.State) error {
63+
rs, ok := s.RootModule().Resources[resourceName]
64+
if !ok {
65+
return fmt.Errorf("Not found: %s", resourceName)
66+
}
8567

86-
rs, ok := s.RootModule().Resources[n]
68+
attrValue, ok := rs.Primary.Attributes[attributeName]
8769
if !ok {
88-
return fmt.Errorf("Can't find SumoLogic Apps data source: %s", n)
70+
return fmt.Errorf("Attribute not found: %s", attributeName)
8971
}
9072

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")
73+
// Convert the attribute value to an integer
74+
count, err := strconv.Atoi(attrValue)
75+
if err != nil {
76+
return fmt.Errorf("Error converting attribute value to integer: %s", err)
9577
}
78+
79+
// Check if count is greater than 0
80+
if count <= 0 {
81+
return fmt.Errorf("Expected %s to be greater than 0, got %d", attributeName, count)
82+
}
83+
9684
return nil
9785
}
9886
}
9987

10088
const testAccDataSourceSumoLogicAppsConfig_basic = `
101-
data "sumologic_apps" "test" {
102-
name = "MySQL - OpenTelemetry"
103-
author = "Sumo Logic"
104-
}
89+
data "sumologic_apps" "test" {}
10590
`
10691

10792
const testAccDataSourceSumoLogicAppsConfig_filtered = `
10893
data "sumologic_apps" "filtered" {
109-
name = "AWS CloudTrail"
94+
name = "MySQL - OpenTelemetry"
11095
author = "Sumo Logic"
11196
}
11297
`

0 commit comments

Comments
 (0)