|
| 1 | +package sumologic |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func (s *Client) GetDashboard(id string) (*Dashboard, error) { |
| 11 | + url := fmt.Sprintf("v2/dashboards/%s", id) |
| 12 | + data, _, err := s.Get(url) |
| 13 | + if err != nil { |
| 14 | + return nil, err |
| 15 | + } |
| 16 | + if data == nil { |
| 17 | + return nil, nil |
| 18 | + } |
| 19 | + |
| 20 | + var dashboard Dashboard |
| 21 | + err = json.Unmarshal(data, &dashboard) |
| 22 | + if err != nil { |
| 23 | + return nil, err |
| 24 | + } |
| 25 | + log.Printf("[GetDashboard] response: %+v\n", dashboard) |
| 26 | + return &dashboard, nil |
| 27 | +} |
| 28 | + |
| 29 | +func (s *Client) GetAppInstance(id string) (*Dashboard, error) { |
| 30 | + url := fmt.Sprintf("v2/apps/instances/%s", id) |
| 31 | + data, _, err := s.Get(url) |
| 32 | + if err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + if data == nil { |
| 36 | + return nil, nil |
| 37 | + } |
| 38 | + |
| 39 | + var dashboard Dashboard |
| 40 | + err = json.Unmarshal(data, &dashboard) |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + log.Printf("[GetDashboard] response: %+v\n", dashboard) |
| 45 | + return &dashboard, nil |
| 46 | +} |
| 47 | + |
| 48 | +func (s *Client) CreateDashboard(dashboardReq Dashboard) (*Dashboard, error) { |
| 49 | + responseBody, err := s.Post("v2/dashboards", dashboardReq) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + |
| 54 | + var dashboard Dashboard |
| 55 | + err = json.Unmarshal(responseBody, &dashboard) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + log.Printf("[CreateDashboard] response: %+v\n", dashboard) |
| 60 | + return &dashboard, nil |
| 61 | +} |
| 62 | + |
| 63 | +func (s *Client) CreateAppInstance(uuid string, appInstallPayload AppInstallPayload) (*AppInstallResponse, error) { |
| 64 | + url := fmt.Sprintf("v2/apps/%s/install", uuid) |
| 65 | + jobId, err := s.Post(url, appInstallPayload) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + |
| 70 | + // Wait for install job to finish |
| 71 | + url = fmt.Sprintf("v2/apps/install/%s/status", jobId) |
| 72 | + _, err = waitForJob(url, time.Minute, s) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + var appInstallResponse AppInstallResponse |
| 78 | + b, _, _ := s.Get(url) |
| 79 | + err = json.Unmarshal(b, &appInstallResponse) |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + log.Printf("[CreateApp] response: %+v\n", appInstallResponse) |
| 84 | + return &appInstallResponse, nil |
| 85 | +} |
| 86 | + |
| 87 | +func (s *Client) DeleteDashboard(id string) error { |
| 88 | + url := fmt.Sprintf("v2/dashboards/%s", id) |
| 89 | + _, err := s.Delete(url) |
| 90 | + return err |
| 91 | +} |
| 92 | + |
| 93 | +func (s *Client) UpdateDashboard(dashboard Dashboard) error { |
| 94 | + url := fmt.Sprintf("v2/dashboards/%s", dashboard.ID) |
| 95 | + _, err := s.Put(url, dashboard) |
| 96 | + return err |
| 97 | +} |
| 98 | + |
| 99 | +type AppInstallPayload struct { |
| 100 | + VERSION string `json:"version"` |
| 101 | + PARAMETERS map[string]string `json:"parameters"` |
| 102 | +} |
| 103 | + |
| 104 | +type AppInstallResponse struct { |
| 105 | + INSTANCEID string `json:"instanceId"` |
| 106 | + PATH string `json:"path"` |
| 107 | + FOLDERID string `json:"folderId"` |
| 108 | +} |
| 109 | + |
| 110 | +type AppInstance struct { |
| 111 | + ID string `json:"id"` |
| 112 | + UUID string `json:"uuid"` |
| 113 | + VERSION string `json:"version"` |
| 114 | + NAME string `json:"name"` |
| 115 | + DESCRIPTION string `json:"description"` |
| 116 | + CONFIGURATIONBLOB string `json:"configurationBlob"` |
| 117 | + PREVIOUSVERSION string `json:"previousVersion"` |
| 118 | + LATESTVERSION string `json:"latestVersion"` |
| 119 | + PATH string `json:"path"` |
| 120 | + MANAGEDOBJECTS []ManagedObject `json:"managedObjects"` |
| 121 | + FOLDERID string `json:"folderId"` |
| 122 | + CREATEDAT string `json:"createdAt"` |
| 123 | + CREATEDBY string `json:"createdBy"` |
| 124 | + MODIFIEDAT string `json:"modifiedAt"` |
| 125 | + MODIFIEDBY string `json:"modifiedBy"` |
| 126 | +} |
| 127 | + |
| 128 | +type ManagedObject struct { |
| 129 | + ID string `json:"id"` |
| 130 | + NAME string `json:"name"` |
| 131 | + TYPE string `json:"type"` |
| 132 | + DESCRIPTION string `json:"description"` |
| 133 | +} |
| 134 | + |
| 135 | +type Dashboard struct { |
| 136 | + ID string `json:"id,omitempty"` |
| 137 | + Title string `json:"title"` |
| 138 | + Description string `json:"description"` |
| 139 | + FolderId string `json:"folderId"` |
| 140 | + TopologyLabelMap *TopologyLabel `json:"topologyLabelMap"` |
| 141 | + Domain string `json:"domain"` |
| 142 | + RefreshInterval int `json:"refreshInterval"` |
| 143 | + TimeRange interface{} `json:"timeRange"` |
| 144 | + Panels []interface{} `json:"panels"` |
| 145 | + Layout interface{} `json:"layout"` |
| 146 | + Variables []Variable `json:"variables"` |
| 147 | + Theme string `json:"theme"` |
| 148 | + ColoringRules []ColoringRule `json:"coloringRules"` |
| 149 | +} |
| 150 | + |
| 151 | +type TopologyLabel struct { |
| 152 | + Data map[string][]string `json:"data"` |
| 153 | +} |
| 154 | + |
| 155 | +// Panel related structs |
| 156 | +type TextPanel struct { |
| 157 | + Id string `json:"id,omitempty"` |
| 158 | + Key string `json:"key"` |
| 159 | + Title string `json:"title"` |
| 160 | + VisualSettings string `json:"visualSettings"` |
| 161 | + KeepVisualSettingsConsistentWithParent bool `json:"keepVisualSettingsConsistentWithParent"` |
| 162 | + PanelType string `json:"panelType"` |
| 163 | + // Text panel related properties |
| 164 | + Text string `json:"text"` |
| 165 | +} |
| 166 | + |
| 167 | +type SumoSearchPanel struct { |
| 168 | + Id string `json:"id,omitempty"` |
| 169 | + Key string `json:"key"` |
| 170 | + Title string `json:"title"` |
| 171 | + VisualSettings string `json:"visualSettings"` |
| 172 | + KeepVisualSettingsConsistentWithParent bool `json:"keepVisualSettingsConsistentWithParent"` |
| 173 | + PanelType string `json:"panelType"` |
| 174 | + // Search panel related properties |
| 175 | + Queries []SearchPanelQuery `json:"queries"` |
| 176 | + Description string `json:"description"` |
| 177 | + TimeRange interface{} `json:"timeRange"` |
| 178 | + ColoringRules []ColoringRule `json:"coloringRules"` |
| 179 | + LinkedDashboards []LinkedDashboard `json:"linkedDashboards"` |
| 180 | +} |
| 181 | + |
| 182 | +type SearchPanelQuery struct { |
| 183 | + QueryString string `json:"queryString"` |
| 184 | + QueryType string `json:"queryType"` |
| 185 | + QueryKey string `json:"queryKey"` |
| 186 | + MetricsQueryMode string `json:"metricsQueryMode,omitempty"` |
| 187 | + MetricsQueryData *MetricsQueryData `json:"metricsQueryData,omitempty"` |
| 188 | +} |
| 189 | + |
| 190 | +type MetricsQueryData struct { |
| 191 | + Metric string `json:"metric"` |
| 192 | + AggregationType string `json:"aggregationType"` |
| 193 | + GroupBy string `json:"groupBy,omitempty"` |
| 194 | + Filters []MetricsQueryFilter `json:"filters"` |
| 195 | + Operators []MetricsQueryOperator `json:"operators"` |
| 196 | +} |
| 197 | + |
| 198 | +type MetricsQueryFilter struct { |
| 199 | + Key string `json:"key"` |
| 200 | + Value string `json:"value"` |
| 201 | + Negation bool `json:"negation,omitempty"` |
| 202 | +} |
| 203 | + |
| 204 | +type MetricsQueryOperator struct { |
| 205 | + Name string `json:"operatorName"` |
| 206 | + Parameters []MetricsQueryOperatorParameter `json:"parameters"` |
| 207 | +} |
| 208 | + |
| 209 | +type MetricsQueryOperatorParameter struct { |
| 210 | + Key string `json:"key"` |
| 211 | + Value string `json:"value"` |
| 212 | +} |
| 213 | + |
| 214 | +// Layout related structs |
| 215 | +type GridLayout struct { |
| 216 | + LayoutType string `json:"layoutType"` |
| 217 | + LayoutStructures []LayoutStructure `json:"layoutStructures"` |
| 218 | +} |
| 219 | + |
| 220 | +type LayoutStructure struct { |
| 221 | + Key string `json:"key"` |
| 222 | + Structure string `json:"structure"` |
| 223 | +} |
| 224 | + |
| 225 | +// Variable related structs |
| 226 | +type Variable struct { |
| 227 | + Id string `json:"id,omitempty"` |
| 228 | + Name string `json:"name"` |
| 229 | + DisplayName string `json:"displayName,omitempty"` |
| 230 | + DefaultValue string `json:"defaultValue,omitempty"` |
| 231 | + SourceDefinition interface{} `json:"sourceDefinition"` |
| 232 | + AllowMultiSelect bool `json:"allowMultiSelect,omitempty"` |
| 233 | + IncludeAllOption bool `json:"includeAllOption"` |
| 234 | + HideFromUI bool `json:"hideFromUI,omitempty"` |
| 235 | +} |
| 236 | + |
| 237 | +type MetadataVariableSourceDefinition struct { |
| 238 | + VariableSourceType string `json:"variableSourceType"` |
| 239 | + Filter string `json:"filter"` |
| 240 | + Key string `json:"key"` |
| 241 | +} |
| 242 | + |
| 243 | +type CsvVariableSourceDefinition struct { |
| 244 | + VariableSourceType string `json:"variableSourceType"` |
| 245 | + Values string `json:"values"` |
| 246 | +} |
| 247 | + |
| 248 | +type LogQueryVariableSourceDefinition struct { |
| 249 | + VariableSourceType string `json:"variableSourceType"` |
| 250 | + Query string `json:"query"` |
| 251 | + Field string `json:"field"` |
| 252 | +} |
| 253 | + |
| 254 | +// Coloring Rule related structs |
| 255 | +type ColoringRule struct { |
| 256 | + Scope string `json:"scope"` |
| 257 | + SingleSeriesAggregateFunction string `json:"singleSeriesAggregateFunction"` |
| 258 | + MultipleSeriesAggregateFunction string `json:"multipleSeriesAggregateFunction"` |
| 259 | + ColorThresholds []ColorThreshold `json:"colorThresholds"` |
| 260 | +} |
| 261 | + |
| 262 | +type ColorThreshold struct { |
| 263 | + Color string `json:"color"` |
| 264 | + Min float64 `json:"min,omitempty"` |
| 265 | + Max float64 `json:"max,omitempty"` |
| 266 | +} |
| 267 | + |
| 268 | +type LinkedDashboard struct { |
| 269 | + Id string `json:"id"` |
| 270 | + RelativePath string `json:"relativePath,omitempty"` |
| 271 | + IncludeTimeRange bool `json:"includeTimeRange"` |
| 272 | + IncludeVariables bool `json:"includeVariables"` |
| 273 | +} |
0 commit comments