Skip to content

Commit 0f95eed

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

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package sumologic
2+
3+
import (
4+
"crypto/sha256"
5+
"encoding/hex"
6+
"encoding/json"
7+
"fmt"
8+
"net/url"
9+
"sort"
10+
"strings"
11+
12+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
13+
)
14+
15+
func dataSourceSumoLogicApps() *schema.Resource {
16+
return &schema.Resource{
17+
Read: dataSourceSumoLogicAppsRead,
18+
Schema: map[string]*schema.Schema{
19+
"name": {
20+
Type: schema.TypeString,
21+
Optional: true,
22+
},
23+
"author": {
24+
Type: schema.TypeString,
25+
Optional: true,
26+
},
27+
"apps": {
28+
Type: schema.TypeList,
29+
Computed: true,
30+
Elem: &schema.Resource{
31+
Schema: map[string]*schema.Schema{
32+
"uuid": {Type: schema.TypeString, Computed: true},
33+
"name": {Type: schema.TypeString, Computed: true},
34+
"description": {Type: schema.TypeString, Computed: true},
35+
"latest_version": {Type: schema.TypeString, Computed: true},
36+
"icon": {Type: schema.TypeString, Computed: true},
37+
"author": {Type: schema.TypeString, Computed: true},
38+
"account_types": {
39+
Type: schema.TypeList,
40+
Computed: true,
41+
Elem: &schema.Schema{Type: schema.TypeString},
42+
},
43+
"beta": {Type: schema.TypeBool, Computed: true},
44+
"installs": {Type: schema.TypeInt, Computed: true},
45+
"app_type": {Type: schema.TypeString, Computed: true},
46+
"attributes": {
47+
Type: schema.TypeMap,
48+
Computed: true,
49+
Elem: &schema.Schema{Type: schema.TypeList, Elem: &schema.Schema{Type: schema.TypeString}},
50+
},
51+
"family": {Type: schema.TypeString, Computed: true},
52+
"installable": {Type: schema.TypeBool, Computed: true},
53+
"show_on_marketplace": {Type: schema.TypeBool, Computed: true},
54+
},
55+
},
56+
},
57+
},
58+
}
59+
}
60+
61+
func dataSourceSumoLogicAppsRead(d *schema.ResourceData, m interface{}) error {
62+
c := m.(*Client)
63+
64+
// Read apps from the API
65+
id, apps, err := c.getApps(d.Get("name").(string), d.Get("author").(string))
66+
if err != nil {
67+
return err
68+
}
69+
70+
if err := d.Set("apps", flattenApps(apps)); err != nil {
71+
return err
72+
}
73+
74+
d.SetId(id)
75+
76+
return nil
77+
}
78+
79+
func (s *Client) getApps(name string, author string) (string, []App, error) {
80+
// Construct the base URL
81+
baseURL := "v2/apps"
82+
83+
// Create url.Values to hold the query parameters
84+
params := url.Values{}
85+
if name != "" {
86+
params.Add("name", name)
87+
}
88+
if author != "" {
89+
params.Add("author", author)
90+
}
91+
92+
// Construct the full URL string
93+
fullURL := baseURL
94+
if len(params) > 0 {
95+
fullURL += "?" + params.Encode()
96+
}
97+
98+
data, _, err := s.Get(fullURL)
99+
if err != nil {
100+
return "", nil, err
101+
}
102+
103+
apps := AppsResponse{}
104+
err = json.Unmarshal(data, &apps)
105+
if err != nil {
106+
return "", nil, err
107+
}
108+
109+
// Generate a unique ID for this data source
110+
id := generateDataSourceId(name, author, apps.Apps)
111+
112+
return id, apps.Apps, nil
113+
}
114+
115+
func generateDataSourceId(name string, author string, apps []App) string {
116+
// Start with the filter parameters
117+
idParts := []string{
118+
fmt.Sprintf("name:%s", name),
119+
fmt.Sprintf("author:%s", author),
120+
}
121+
122+
// Add a sorted list of app UUIDs
123+
var uuids []string
124+
for _, app := range apps {
125+
uuids = append(uuids, app.UUID)
126+
}
127+
sort.Strings(uuids)
128+
idParts = append(idParts, fmt.Sprintf("apps:%s", strings.Join(uuids, ",")))
129+
130+
// Join all parts and create a hash
131+
idString := strings.Join(idParts, "|")
132+
hash := sha256.Sum256([]byte(idString))
133+
return hex.EncodeToString(hash[:])
134+
}
135+
136+
func flattenApps(apps []App) []interface{} {
137+
var flattenedApps []interface{}
138+
for _, app := range apps {
139+
flattenedApp := map[string]interface{}{
140+
"uuid": app.UUID,
141+
"name": app.Name,
142+
"description": app.Description,
143+
"latest_version": app.LatestVersion,
144+
"icon": app.Icon,
145+
"author": app.Author,
146+
"account_types": app.AccountTypes,
147+
"beta": app.Beta,
148+
"installs": app.Installs,
149+
"app_type": app.AppType,
150+
"attributes": app.Attributes,
151+
"family": app.Family,
152+
"installable": app.Installable,
153+
"show_on_marketplace": app.ShowOnMarketplace,
154+
}
155+
flattenedApps = append(flattenedApps, flattenedApp)
156+
}
157+
return flattenedApps
158+
}
159+
160+
type AppsResponse struct {
161+
Apps []App `json:"apps"`
162+
}
163+
164+
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"`
179+
}

sumologic/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ func Provider() terraform.ResourceProvider {
137137
"sumologic_role": dataSourceSumologicRole(),
138138
"sumologic_role_v2": dataSourceSumologicRoleV2(),
139139
"sumologic_user": dataSourceSumologicUser(),
140+
"sumologic_apps": dataSourceSumoLogicApps(),
140141
},
141142
ConfigureFunc: providerConfigure,
142143
}

0 commit comments

Comments
 (0)