|
| 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 | + "id": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Computed: true, |
| 30 | + }, |
| 31 | + "apps": { |
| 32 | + Type: schema.TypeList, |
| 33 | + Computed: true, |
| 34 | + Elem: &schema.Resource{ |
| 35 | + Schema: map[string]*schema.Schema{ |
| 36 | + "uuid": {Type: schema.TypeString, Computed: true}, |
| 37 | + "name": {Type: schema.TypeString, Computed: true}, |
| 38 | + "description": {Type: schema.TypeString, Computed: true}, |
| 39 | + "latest_version": {Type: schema.TypeString, Computed: true}, |
| 40 | + "icon": {Type: schema.TypeString, Computed: true}, |
| 41 | + "author": {Type: schema.TypeString, Computed: true}, |
| 42 | + "account_types": { |
| 43 | + Type: schema.TypeList, |
| 44 | + Computed: true, |
| 45 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 46 | + }, |
| 47 | + "beta": {Type: schema.TypeBool, Computed: true}, |
| 48 | + "installs": {Type: schema.TypeInt, Computed: true}, |
| 49 | + "app_type": {Type: schema.TypeString, Computed: true}, |
| 50 | + "attributes": { |
| 51 | + Type: schema.TypeList, |
| 52 | + MaxItems: 1, |
| 53 | + Optional: true, |
| 54 | + Elem: &schema.Resource{ |
| 55 | + Schema: map[string]*schema.Schema{ |
| 56 | + "category": { |
| 57 | + Type: schema.TypeList, |
| 58 | + Optional: true, |
| 59 | + Elem: &schema.Schema{ |
| 60 | + Type: schema.TypeString, |
| 61 | + }, |
| 62 | + }, |
| 63 | + "use_case": { |
| 64 | + Type: schema.TypeList, |
| 65 | + Optional: true, |
| 66 | + Elem: &schema.Schema{ |
| 67 | + Type: schema.TypeString, |
| 68 | + }, |
| 69 | + }, |
| 70 | + "collection": { |
| 71 | + Type: schema.TypeList, |
| 72 | + Optional: true, |
| 73 | + Elem: &schema.Schema{ |
| 74 | + Type: schema.TypeString, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + "family": {Type: schema.TypeString, Computed: true}, |
| 81 | + "installable": {Type: schema.TypeBool, Computed: true}, |
| 82 | + "show_on_marketplace": {Type: schema.TypeBool, Computed: true}, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func dataSourceSumoLogicAppsRead(d *schema.ResourceData, meta interface{}) error { |
| 91 | + c := meta.(*Client) |
| 92 | + |
| 93 | + // Read apps from the API |
| 94 | + id, apps, err := c.getApps(d.Get("name").(string), d.Get("author").(string)) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + if err := d.Set("apps", flattenApps(apps)); err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + d.SetId(id) |
| 104 | + |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func (s *Client) getApps(name string, author string) (string, []App, error) { |
| 109 | + // Construct the base URL |
| 110 | + baseURL := "v2/apps" |
| 111 | + |
| 112 | + // Create url.Values to hold the query parameters |
| 113 | + params := url.Values{} |
| 114 | + if name != "" { |
| 115 | + params.Add("name", name) |
| 116 | + } |
| 117 | + if author != "" { |
| 118 | + params.Add("author", author) |
| 119 | + } |
| 120 | + |
| 121 | + // Construct the full URL string |
| 122 | + fullURL := baseURL |
| 123 | + if len(params) > 0 { |
| 124 | + fullURL += "?" + params.Encode() |
| 125 | + } |
| 126 | + |
| 127 | + data, err := s.Get(fullURL) |
| 128 | + if err != nil { |
| 129 | + return "", nil, err |
| 130 | + } |
| 131 | + |
| 132 | + apps := AppsResponse{} |
| 133 | + err = json.Unmarshal(data, &apps) |
| 134 | + |
| 135 | + if err != nil { |
| 136 | + return "", nil, err |
| 137 | + } |
| 138 | + |
| 139 | + // Generate a unique ID for this data source |
| 140 | + id := generateDataSourceId(name, author, apps.Apps) |
| 141 | + |
| 142 | + return id, apps.Apps, nil |
| 143 | +} |
| 144 | + |
| 145 | +func generateDataSourceId(name string, author string, apps []App) string { |
| 146 | + // Start with the filter parameters |
| 147 | + idParts := []string{ |
| 148 | + fmt.Sprintf("name:%s", name), |
| 149 | + fmt.Sprintf("author:%s", author), |
| 150 | + } |
| 151 | + |
| 152 | + // Add a sorted list of app UUIDs |
| 153 | + var uuids []string |
| 154 | + for _, app := range apps { |
| 155 | + uuids = append(uuids, app.UUID) |
| 156 | + } |
| 157 | + sort.Strings(uuids) |
| 158 | + idParts = append(idParts, fmt.Sprintf("apps:%s", strings.Join(uuids, ","))) |
| 159 | + |
| 160 | + // Join all parts and create a hash |
| 161 | + idString := strings.Join(idParts, "|") |
| 162 | + hash := sha256.Sum256([]byte(idString)) |
| 163 | + return hex.EncodeToString(hash[:]) |
| 164 | +} |
| 165 | + |
| 166 | +func flattenApps(apps []App) []interface{} { |
| 167 | + var flattenedApps []interface{} |
| 168 | + for _, app := range apps { |
| 169 | + |
| 170 | + internalAttributes := make(map[string]interface{}) |
| 171 | + internalAttributes["category"] = app.Attributes.Category |
| 172 | + internalAttributes["use_case"] = app.Attributes.UseCase |
| 173 | + internalAttributes["collection"] = app.Attributes.Collection |
| 174 | + attributes := []interface{}{ |
| 175 | + internalAttributes, |
| 176 | + } |
| 177 | + |
| 178 | + flattenedApp := map[string]interface{}{ |
| 179 | + "uuid": app.UUID, |
| 180 | + "name": app.Name, |
| 181 | + "description": app.Description, |
| 182 | + "latest_version": app.LatestVersion, |
| 183 | + "icon": app.Icon, |
| 184 | + "author": app.Author, |
| 185 | + "account_types": app.AccountTypes, |
| 186 | + "beta": app.Beta, |
| 187 | + "installs": app.Installs, |
| 188 | + "app_type": app.AppType, |
| 189 | + "attributes": attributes, |
| 190 | + "family": app.Family, |
| 191 | + "installable": app.Installable, |
| 192 | + "show_on_marketplace": app.ShowOnMarketplace, |
| 193 | + } |
| 194 | + flattenedApps = append(flattenedApps, flattenedApp) |
| 195 | + } |
| 196 | + return flattenedApps |
| 197 | +} |
| 198 | + |
| 199 | +type AppsResponse struct { |
| 200 | + Apps []App `json:"apps"` |
| 201 | +} |
| 202 | + |
| 203 | +type App struct { |
| 204 | + UUID string `json:"uuid"` |
| 205 | + Name string `json:"name"` |
| 206 | + Description string `json:"description"` |
| 207 | + LatestVersion string `json:"latestVersion"` |
| 208 | + Icon string `json:"icon"` |
| 209 | + Author string `json:"author"` |
| 210 | + AccountTypes []string `json:"accountTypes"` |
| 211 | + Beta bool `json:"beta"` |
| 212 | + Installs int `json:"installs"` |
| 213 | + AppType string `json:"appType"` |
| 214 | + Attributes struct { |
| 215 | + Category []string `json:"category"` |
| 216 | + UseCase []string `json:"useCase"` |
| 217 | + Collection []string `json:"collection"` |
| 218 | + } `json:"attributes"` |
| 219 | + Family string `json:"family"` |
| 220 | + Installable bool `json:"installable"` |
| 221 | + ShowOnMarketplace bool `json:"showOnMarketplace"` |
| 222 | +} |
0 commit comments