295 lines
7.7 KiB
Go
295 lines
7.7 KiB
Go
package houstonClient
|
|
|
|
import (
|
|
"bytes"
|
|
"cybertron/configs"
|
|
"cybertron/pkg/log"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
type HoustonClientInterface interface {
|
|
CreateIncident(payload CreateHoustonRequest) (*HoustonResponse, error)
|
|
GetAllProducts() (*ProductsResponse, error)
|
|
GetReportingAndResponder(productID []string) (*HoustonIncidentResponse, error)
|
|
}
|
|
|
|
type HoustonClient struct {
|
|
baseUrl string
|
|
realmId string
|
|
}
|
|
|
|
type ReportingTeam struct {
|
|
Value int `json:"value"`
|
|
Label string `json:"label"`
|
|
}
|
|
|
|
type Product struct {
|
|
ProductID int `json:"product_id"`
|
|
ProductName string `json:"product_name"`
|
|
}
|
|
|
|
type IncidentData struct {
|
|
ID int `json:"id"`
|
|
Title string `json:"title"`
|
|
Description string `json:"description"`
|
|
Status int `json:"status"`
|
|
StatusName string `json:"statusName"`
|
|
SeverityId int `json:"severityId"`
|
|
SeverityName string `json:"severityName"`
|
|
IncidentName string `json:"incidentName"`
|
|
SlackChannel string `json:"slackChannel"`
|
|
DetectionTime interface{} `json:"detectionTime"`
|
|
StartTime string `json:"startTime"`
|
|
EndTime interface{} `json:"endTime"`
|
|
TeamId int `json:"teamId"`
|
|
TeamName string `json:"teamName"`
|
|
JiraLinks interface{} `json:"jiraLinks"`
|
|
ConfluenceId interface{} `json:"confluenceId"`
|
|
SeverityTat string `json:"severityTat"`
|
|
RemindMeAt interface{} `json:"remindMeAt"`
|
|
EnableReminder bool `json:"enableReminder"`
|
|
CreatedBy string `json:"createdBy"`
|
|
UpdatedBy string `json:"updatedBy"`
|
|
CreatedAt string `json:"createdAt"`
|
|
UpdatedAt string `json:"updatedAt"`
|
|
RcaLink string `json:"rcaLink"`
|
|
ReportingTeam ReportingTeam `json:"reportingTeam"`
|
|
Products []Product `json:"products"`
|
|
}
|
|
|
|
type HoustonResponse struct {
|
|
Data IncidentData `json:"data"`
|
|
Error json.RawMessage `json:"error"`
|
|
StatusCode int `json:"status"`
|
|
}
|
|
|
|
type CreateHoustonRequest struct {
|
|
Title string `json:"title"`
|
|
SeverityId int `json:"severityId"`
|
|
Description string `json:"description"`
|
|
ReportingTeamId int `json:"reportingTeamId"`
|
|
ResponderTeamId int `json:"responderTeamId"`
|
|
ProductIds []int `json:"productIds"`
|
|
CreatedBy string `json:"createdBy"`
|
|
ErrorID string `json:"errorId"`
|
|
}
|
|
|
|
type ProductsResponse struct {
|
|
Data ProductResponseData `json:"data"`
|
|
Error json.RawMessage `json:"error"`
|
|
StatusCode int `json:"status"`
|
|
}
|
|
|
|
type ProductResponseData struct {
|
|
DefaultProduct DefaultProductType `json:"defaultProduct"`
|
|
Products []DefaultProductType `json:"products"`
|
|
}
|
|
|
|
type DefaultProductType struct {
|
|
Label string `json:"label"`
|
|
Value json.Number `json:"value"`
|
|
}
|
|
|
|
type Team struct {
|
|
Value int `json:"value"`
|
|
Label string `json:"label"`
|
|
}
|
|
|
|
type TeamDataResponse struct {
|
|
DefaultTeam *Team `json:"defaultTeam"` // Use a pointer to handle possible `null` value
|
|
Teams []Team `json:"teams"`
|
|
}
|
|
|
|
type IncidentTeamResponse struct {
|
|
ReportingTeam TeamDataResponse `json:"reportingTeam"`
|
|
ResponderTeam TeamDataResponse `json:"responderTeam"`
|
|
}
|
|
|
|
type HoustonIncidentResponse struct {
|
|
Data IncidentTeamResponse `json:"data"`
|
|
Error json.RawMessage `json:"error"`
|
|
StatusCode int `json:"status"`
|
|
}
|
|
|
|
func NewHoustonClient(houstonConfig configs.HoustonClientConfig) *HoustonClient {
|
|
return &HoustonClient{
|
|
baseUrl: houstonConfig.GetHoustonBaseUrl(),
|
|
}
|
|
}
|
|
|
|
var logger = log.Log.GetLog()
|
|
var client = http.Client{}
|
|
|
|
const (
|
|
SessionUrl = "%s/session/%s"
|
|
)
|
|
|
|
func (m *HoustonClient) CreateIncident(payload CreateHoustonRequest) (*HoustonResponse, error) {
|
|
url := m.baseUrl + "/houston/create-incident-v3"
|
|
|
|
fmt.Println("Creating incident with payload:", payload)
|
|
fmt.Println("POST URL:", url)
|
|
|
|
// Marshal the payload to JSON
|
|
jsonPayload, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Create a new POST request
|
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Set the necessary headers
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", "Bearer "+m.realmId)
|
|
|
|
// Use http.Client to send the request
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// Check for unauthorized request
|
|
if resp.StatusCode == http.StatusUnauthorized {
|
|
return nil, errors.New("unauthorized request")
|
|
}
|
|
|
|
// Read the response body
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Print the response body
|
|
fmt.Println("Response body:", string(body))
|
|
|
|
// Decode the response
|
|
var houstonResponse HoustonResponse
|
|
if err := json.Unmarshal(body, &houstonResponse); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
houstonResponse.StatusCode = resp.StatusCode
|
|
fmt.Println("Incident created successfully with status code:", houstonResponse.StatusCode)
|
|
fmt.Println("Response data:", houstonResponse.Data)
|
|
return &houstonResponse, nil
|
|
}
|
|
|
|
func (m *HoustonClient) GetAllProducts() (*ProductsResponse, error) {
|
|
url := m.baseUrl + "/houston/user/products"
|
|
|
|
fmt.Println("GET URL:", url)
|
|
|
|
// Create a new GET request
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Set the necessary headers
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", "Bearer "+m.realmId)
|
|
|
|
// Use http.Client to send the request
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// Check for unauthorized request
|
|
if resp.StatusCode == http.StatusUnauthorized {
|
|
return nil, errors.New("unauthorized request")
|
|
}
|
|
|
|
// Read the response body
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Print the response body
|
|
fmt.Println("Response body:", string(body))
|
|
|
|
// Decode the response
|
|
var productsResponse ProductsResponse
|
|
if err := json.Unmarshal(body, &productsResponse); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
productsResponse.StatusCode = resp.StatusCode
|
|
fmt.Println("Products fetched successfully with status code:", productsResponse.StatusCode)
|
|
fmt.Println("Response data:", productsResponse.Data)
|
|
return &productsResponse, nil
|
|
}
|
|
|
|
func (m *HoustonClient) GetReportingAndResponder(productID []string) (*HoustonIncidentResponse, error) {
|
|
baseURL := m.baseUrl + "/houston/product/reporting-and-responder-teams"
|
|
|
|
// Construct URL with query parameters
|
|
u, err := url.Parse(baseURL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
q := u.Query()
|
|
for _, id := range productID {
|
|
q.Add("productID", id)
|
|
}
|
|
u.RawQuery = q.Encode()
|
|
|
|
fmt.Println("GET URL:", u.String())
|
|
|
|
// Create a new GET request
|
|
req, err := http.NewRequest("GET", u.String(), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Set the necessary headers
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
// Use http.Client to send the request
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
fmt.Println("Error:", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// Check for unauthorized request
|
|
if resp.StatusCode == http.StatusUnauthorized {
|
|
return nil, errors.New("unauthorized request")
|
|
}
|
|
|
|
// Read the response body
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Print the response body
|
|
fmt.Println("Response body:", string(body))
|
|
|
|
// Decode the response
|
|
var productsResponse HoustonIncidentResponse
|
|
if err := json.Unmarshal(body, &productsResponse); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
productsResponse.StatusCode = resp.StatusCode
|
|
fmt.Println("Products fetched successfully with status code:", productsResponse.StatusCode)
|
|
fmt.Println("Response data:", productsResponse.Data)
|
|
return &productsResponse, nil
|
|
|
|
}
|