forked from RedHatInsights/ros-ocp-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #RHIROS-1214 - Sources integration (RedHatInsights#103)
* Fixes #RHIROS-1214 - Sources integration
- Loading branch information
1 parent
6b78c8e
commit 89d5c97
Showing
9 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package services | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"strconv" | ||
|
||
k "github.com/confluentinc/confluent-kafka-go/v2/kafka" | ||
"github.com/labstack/gommon/log" | ||
|
||
"github.com/redhatinsights/ros-ocp-backend/internal/config" | ||
"github.com/redhatinsights/ros-ocp-backend/internal/kafka" | ||
"github.com/redhatinsights/ros-ocp-backend/internal/logging" | ||
"github.com/redhatinsights/ros-ocp-backend/internal/model" | ||
"github.com/redhatinsights/ros-ocp-backend/internal/types" | ||
"github.com/redhatinsights/ros-ocp-backend/internal/utils/sources" | ||
) | ||
|
||
var cost_app_id int | ||
|
||
func StartHouseKeeperService() { | ||
log := logging.GetLogger() | ||
cfg := config.GetConfig() | ||
var err error | ||
cost_app_id, err = sources.GetCostApplicationID() | ||
if err != nil { | ||
log.Error("Unable to get cost application id") | ||
os.Exit(1) | ||
} | ||
|
||
kafka.StartConsumer(cfg.SourcesEventTopic, sourcesListener) | ||
|
||
} | ||
|
||
func sourcesListener(msg *k.Message) { | ||
headers := msg.Headers | ||
for _, v := range headers { | ||
if v.Key == "event_type" && string(v.Value) == "Application.destroy" { | ||
var data types.SourcesEvent | ||
if !json.Valid([]byte(msg.Value)) { | ||
log.Errorf("Received message on kafka topic is not vaild JSON: %s", msg.Value) | ||
return | ||
} | ||
if err := json.Unmarshal(msg.Value, &data); err != nil { | ||
log.Errorf("Unable to decode kafka message: %s", msg.Value) | ||
return | ||
} | ||
if data.Application_type_id == cost_app_id { | ||
cluster := model.Cluster{ | ||
SourceId: strconv.Itoa(data.Source_id), | ||
} | ||
if err := cluster.DeleteCluster(); err != nil { | ||
log.Errorf("unable to delete record from clusters table: %v. Error: %v", cluster, err) | ||
} else { | ||
log.Infof("Successfully deleted the cluster with Source_id: %v.", cluster.SourceId) | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package types | ||
|
||
type SourcesEvent struct { | ||
Id int `validate:"required"` | ||
Source_id int `validate:"required"` | ||
Application_type_id int `validate:"required"` | ||
Tenant string `validate:"required"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package sources | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/redhatinsights/ros-ocp-backend/internal/config" | ||
) | ||
|
||
var cfg *config.Config = config.GetConfig() | ||
|
||
func GetCostApplicationID() (int, error) { | ||
url := cfg.SourceApiBaseUrl + cfg.SourceApiPrefix + "/application_types?filter[name][eq]=/insights/platform/cost-management" | ||
res, err := http.Get(url) | ||
if err != nil { | ||
return 0, fmt.Errorf("error while calling sources API: %v", err) | ||
} | ||
defer res.Body.Close() | ||
body, _ := io.ReadAll(res.Body) | ||
if res.StatusCode != 200 { | ||
return 0, fmt.Errorf("%v", res) | ||
} | ||
payload := map[string]interface{}{} | ||
if err := json.Unmarshal(body, &payload); err != nil { | ||
return 0, fmt.Errorf("unable to unmarshal response of sources /application_types API %v", err) | ||
} | ||
data := payload["data"].([]interface{}) | ||
app := data[0].(map[string]interface{}) | ||
cost_app_id, _ := strconv.Atoi(app["id"].(string)) | ||
return cost_app_id, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters