up
This commit is contained in:
@@ -199,11 +199,19 @@ func (c *HostingerClient) GetVPSByID(vmID int) (*HostingerVPS, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
// Intentar primero como objeto directo
|
||||||
var vps HostingerVPS
|
var vps HostingerVPS
|
||||||
if err := json.Unmarshal(body, &vps); err != nil {
|
if err := json.Unmarshal(body, &vps); err == nil && vps.ID != 0 {
|
||||||
return nil, fmt.Errorf("hostinger: no se pudo interpretar respuesta VPS: %w", err)
|
return &vps, nil
|
||||||
}
|
}
|
||||||
return &vps, nil
|
// Fallback: respuesta envuelta en {"data": {...}}
|
||||||
|
var wrapped struct {
|
||||||
|
Data HostingerVPS `json:"data"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(body, &wrapped); err == nil && wrapped.Data.ID != 0 {
|
||||||
|
return &wrapped.Data, nil
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("hostinger: no se pudo interpretar respuesta VPS (body: %s)", string(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetVPSList obtiene la lista de VPS/VMs.
|
// GetVPSList obtiene la lista de VPS/VMs.
|
||||||
|
|||||||
@@ -165,7 +165,22 @@ func UpdateServidor(c *fiber.Ctx) error {
|
|||||||
app.Http.Database.DB.First(&actual, uid)
|
app.Http.Database.DB.First(&actual, uid)
|
||||||
nombreAnterior := actual.Nombre
|
nombreAnterior := actual.Nombre
|
||||||
|
|
||||||
if err := models.UpdateServidor(m); err != nil {
|
// Usar map para Updates y así no perder campos con valor cero (incluido hostinger_vps_id=nil)
|
||||||
|
updateMap := map[string]interface{}{
|
||||||
|
"nombre": m.Nombre,
|
||||||
|
"ip_servidor": m.IpServidor,
|
||||||
|
"so": m.So,
|
||||||
|
"vencimiento": m.Vencimiento,
|
||||||
|
"ram": m.Ram,
|
||||||
|
"nucleos": m.Nucleos,
|
||||||
|
"disco": m.Disco,
|
||||||
|
"ultimo_ping": m.UltimoPing,
|
||||||
|
"prov_servidor_id": m.ProvServidorID,
|
||||||
|
"tipo_servidor_id": m.TipoServidorID,
|
||||||
|
"hostinger_vps_id": m.HostingerVpsID,
|
||||||
|
"hostinger_subscription_id": m.HostingerSubscriptionID,
|
||||||
|
}
|
||||||
|
if err := app.Http.Database.DB.Model(&models.Servidor{}).Where("id = ?", uid).Updates(updateMap).Error; err != nil {
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"message": "Error al actualizar el servidor",
|
"message": "Error al actualizar el servidor",
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
@@ -206,6 +221,9 @@ func SyncServidorFromHostinger(c *fiber.Ctx) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{"error": err.Error()})
|
return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{"error": err.Error()})
|
||||||
}
|
}
|
||||||
|
if vps.ID == 0 {
|
||||||
|
return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{"error": "Hostinger no devolvio datos para ese VPS ID"})
|
||||||
|
}
|
||||||
|
|
||||||
updates := map[string]interface{}{
|
updates := map[string]interface{}{
|
||||||
"hostinger_state": vps.State,
|
"hostinger_state": vps.State,
|
||||||
@@ -275,11 +293,11 @@ func SyncServidorFromHostinger(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := db.Model(&servidor).Updates(updates).Error; err != nil {
|
if err := db.Model(&servidor).Updates(updates).Error; err != nil {
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "no se pudo guardar"})
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "no se pudo guardar", "detalle": err.Error()})
|
||||||
}
|
}
|
||||||
|
|
||||||
db.First(&servidor, id)
|
db.First(&servidor, id)
|
||||||
return c.JSON(fiber.Map{"ok": true, "servidor": servidor})
|
return c.JSON(fiber.Map{"ok": true, "servidor": servidor, "campos_actualizados": updates})
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteServidor(c *fiber.Ctx) error {
|
func DeleteServidor(c *fiber.Ctx) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user