2022-08-25 19:26:29 -06:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/database"
|
2022-09-08 22:14:03 -06:00
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/hub"
|
2022-08-25 19:26:29 -06:00
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/models"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2022-08-30 20:03:24 -06:00
|
|
|
func CreateSource(c *gin.Context) {
|
2022-08-25 19:26:29 -06:00
|
|
|
logger := c.MustGet("LOGGER").(*logrus.Entry)
|
|
|
|
databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository)
|
|
|
|
|
2022-08-30 20:03:24 -06:00
|
|
|
providerCred := models.Source{}
|
2022-08-25 19:26:29 -06:00
|
|
|
if err := c.ShouldBindJSON(&providerCred); err != nil {
|
|
|
|
logger.Errorln("An error occurred while parsing posted provider credential", err)
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-25 22:57:29 -06:00
|
|
|
logger.Infof("Parsed Create Provider Credentials Payload: %v", providerCred)
|
|
|
|
|
2022-08-30 20:03:24 -06:00
|
|
|
err := databaseRepo.CreateSource(c, &providerCred)
|
2022-08-25 19:26:29 -06:00
|
|
|
if err != nil {
|
|
|
|
logger.Errorln("An error occurred while storing provider credential", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
2022-09-08 22:14:03 -06:00
|
|
|
|
|
|
|
// after creating the source, we should do a bulk import
|
|
|
|
sourceClient, err := hub.NewClient(providerCred.ProviderId, nil, logger, providerCred)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorln("An error occurred while initializing hub client using source credential", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = sourceClient.SyncAll(databaseRepo)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorln("An error occurred while bulk import of resources from source", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-25 19:26:29 -06:00
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": providerCred})
|
|
|
|
}
|
2022-08-27 20:34:48 -06:00
|
|
|
|
2022-08-30 20:03:24 -06:00
|
|
|
func ListSource(c *gin.Context) {
|
2022-08-27 20:34:48 -06:00
|
|
|
logger := c.MustGet("LOGGER").(*logrus.Entry)
|
|
|
|
databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository)
|
|
|
|
|
2022-08-30 20:03:24 -06:00
|
|
|
providerCredentials, err := databaseRepo.GetSources(c)
|
2022-08-27 20:34:48 -06:00
|
|
|
if err != nil {
|
|
|
|
logger.Errorln("An error occurred while storing provider credential", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": providerCredentials})
|
|
|
|
}
|
2022-09-08 23:53:54 -06:00
|
|
|
|
|
|
|
func RawRequestSource(c *gin.Context) {
|
|
|
|
logger := c.MustGet("LOGGER").(*logrus.Entry)
|
|
|
|
databaseRepo := c.MustGet("REPOSITORY").(database.DatabaseRepository)
|
|
|
|
|
|
|
|
sources, err := databaseRepo.GetSources(c)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorln("An error occurred while storing provider credential", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var foundSource *models.Source
|
|
|
|
for _, source := range sources {
|
|
|
|
if source.ProviderId == c.Param("sourceType") {
|
|
|
|
foundSource = &source
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if foundSource == nil {
|
|
|
|
logger.Errorf("Did not source credentials for %s", c.Param("sourceType"))
|
|
|
|
c.JSON(http.StatusNotFound, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := hub.NewClient(c.Param("sourceType"), nil, logger, *foundSource)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("Could not initialize source client", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var resp map[string]interface{}
|
|
|
|
err = client.GetRequest(c.Param("path"), &resp)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("Error making raw request", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": resp})
|
|
|
|
}
|