happyDomain/admin/db-zone.go

167 lines
4.9 KiB
Go
Raw Permalink Normal View History

2023-12-24 10:18:08 +00:00
// This file is part of the happyDomain (R) project.
// Copyright (c) 2020-2024 happyDomain
// Authors: Pierre-Olivier Mercier, et al.
2020-06-08 23:25:15 +00:00
//
2023-12-24 10:18:08 +00:00
// This program is offered under a commercial and under the AGPL license.
// For commercial licensing, contact us at <contact@happydomain.org>.
2020-06-08 23:25:15 +00:00
//
2023-12-24 10:18:08 +00:00
// For AGPL licensing:
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
2020-06-08 23:25:15 +00:00
//
2023-12-24 10:18:08 +00:00
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
2020-06-08 23:25:15 +00:00
//
2023-12-24 10:18:08 +00:00
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-06-08 23:25:15 +00:00
package admin
import (
2020-06-10 23:18:33 +00:00
"encoding/base64"
2020-06-08 23:25:15 +00:00
"fmt"
"net/http"
2021-05-05 01:48:16 +00:00
"github.com/gin-gonic/gin"
2020-06-08 23:25:15 +00:00
2023-09-07 09:37:18 +00:00
"git.happydns.org/happyDomain/api"
"git.happydns.org/happyDomain/config"
"git.happydns.org/happyDomain/model"
"git.happydns.org/happyDomain/storage"
2020-06-08 23:25:15 +00:00
)
2021-05-05 01:48:16 +00:00
func declareZonesRoutes(opts *config.Options, router *gin.RouterGroup) {
router.GET("/zones", getUserDomainZones)
router.PUT("/zones", updateUserDomainZones)
router.POST("/zones", newUserDomainZone)
2020-06-08 23:25:15 +00:00
2021-05-05 01:48:16 +00:00
router.DELETE("/zones/:zoneid", deleteZone)
2020-06-08 23:25:15 +00:00
2021-05-05 01:48:16 +00:00
apiZonesRoutes := router.Group("/zones/:zoneid")
apiZonesRoutes.Use(api.ZoneHandler)
2020-06-10 23:18:33 +00:00
2021-05-05 01:48:16 +00:00
apiZonesRoutes.GET("", api.GetZone)
apiZonesRoutes.PUT("", updateZone)
apiZonesRoutes.PATCH("", patchZoneService)
apiZonesRoutes.GET("/*serviceid", getZoneService)
apiZonesRoutes.PUT("/*serviceid", updateZoneService)
2020-06-08 23:25:15 +00:00
}
2021-05-05 01:48:16 +00:00
func getUserDomainZones(c *gin.Context) {
domain := c.MustGet("domain").(*happydns.Domain)
c.JSON(http.StatusOK, domain.ZoneHistory)
2020-06-08 23:25:15 +00:00
}
2021-05-05 01:48:16 +00:00
func updateUserDomainZones(c *gin.Context) {
domain := c.MustGet("domain").(*happydns.Domain)
err := c.ShouldBindJSON(&domain.ZoneHistory)
2020-06-08 23:25:15 +00:00
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Something is wrong in received data: %s", err.Error())})
2021-05-05 01:48:16 +00:00
return
2020-06-08 23:25:15 +00:00
}
2021-05-05 01:48:16 +00:00
ApiResponse(c, domain, storage.MainStore.UpdateDomain(domain))
2020-06-08 23:25:15 +00:00
}
2021-05-05 01:48:16 +00:00
func newUserDomainZone(c *gin.Context) {
2020-06-08 23:25:15 +00:00
uz := &happydns.Zone{}
2021-05-05 01:48:16 +00:00
err := c.ShouldBindJSON(&uz)
2020-06-08 23:25:15 +00:00
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Something is wrong in received data: %s", err.Error())})
2021-05-05 01:48:16 +00:00
return
2020-06-08 23:25:15 +00:00
}
2022-10-25 16:16:34 +00:00
uz.Id = nil
2020-06-08 23:25:15 +00:00
2021-05-05 01:48:16 +00:00
ApiResponse(c, uz, storage.MainStore.CreateZone(uz))
2020-06-08 23:25:15 +00:00
}
2021-05-05 01:48:16 +00:00
func updateZone(c *gin.Context) {
zone := c.MustGet("zone").(*happydns.Zone)
2020-06-08 23:25:15 +00:00
uz := &happydns.Zone{}
2021-05-05 01:48:16 +00:00
err := c.ShouldBindJSON(&uz)
2020-06-08 23:25:15 +00:00
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Something is wrong in received data: %s", err.Error())})
2021-05-05 01:48:16 +00:00
return
2020-06-08 23:25:15 +00:00
}
uz.Id = zone.Id
2021-05-05 01:48:16 +00:00
ApiResponse(c, uz, storage.MainStore.UpdateZone(uz))
2020-06-08 23:25:15 +00:00
}
2021-05-05 01:48:16 +00:00
func getZoneService(c *gin.Context) {
zone := c.MustGet("zone").(*happydns.Zone)
serviceid, err := base64.StdEncoding.DecodeString(c.Param("serviceid")[1:])
2020-06-10 23:18:33 +00:00
if err != nil {
2021-05-05 01:48:16 +00:00
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": err.Error()})
return
2020-06-10 23:18:33 +00:00
}
2020-06-14 20:58:28 +00:00
_, svc := zone.FindService(serviceid)
2021-05-05 01:48:16 +00:00
c.JSON(http.StatusOK, svc)
2020-06-10 23:18:33 +00:00
}
2021-05-05 01:48:16 +00:00
func updateZoneService(c *gin.Context) {
domain := c.MustGet("domain").(*happydns.Domain)
zone := c.MustGet("zone").(*happydns.Zone)
serviceid, err := base64.StdEncoding.DecodeString(c.Param("serviceid")[1:])
2020-06-10 23:18:33 +00:00
if err != nil {
2021-05-05 01:48:16 +00:00
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": err.Error()})
return
2020-06-10 23:18:33 +00:00
}
usc := &happydns.ServiceCombined{}
2021-05-05 01:48:16 +00:00
err = c.ShouldBindJSON(&usc)
2020-06-10 23:18:33 +00:00
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Something is wrong in received data: %s", err.Error())})
2021-05-05 01:48:16 +00:00
return
2020-06-10 23:18:33 +00:00
}
2020-06-14 20:58:28 +00:00
err = zone.EraseService(usc.Domain, domain.DomainName, serviceid, usc)
2020-06-10 23:18:33 +00:00
if err != nil {
2021-05-05 01:48:16 +00:00
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
2020-06-10 23:18:33 +00:00
}
2021-05-05 01:48:16 +00:00
ApiResponse(c, zone.Services, storage.MainStore.UpdateZone(zone))
2020-06-10 23:18:33 +00:00
}
2021-05-05 01:48:16 +00:00
func patchZoneService(c *gin.Context) {
domain := c.MustGet("domain").(*happydns.Domain)
zone := c.MustGet("zone").(*happydns.Zone)
2020-06-10 23:18:33 +00:00
usc := &happydns.ServiceCombined{}
2021-05-05 01:48:16 +00:00
err := c.ShouldBindJSON(&usc)
2020-06-10 23:18:33 +00:00
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Something is wrong in received data: %s", err.Error())})
2021-05-05 01:48:16 +00:00
return
2020-06-10 23:18:33 +00:00
}
2020-06-14 20:58:28 +00:00
err = zone.EraseService(usc.Domain, domain.DomainName, usc.Id, usc)
2020-06-10 23:18:33 +00:00
if err != nil {
2021-05-05 01:48:16 +00:00
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
2023-09-15 09:15:04 +00:00
return
2020-06-10 23:18:33 +00:00
}
2021-05-05 01:48:16 +00:00
ApiResponse(c, zone.Services, storage.MainStore.UpdateZone(zone))
2020-06-10 23:18:33 +00:00
}
2021-05-05 01:48:16 +00:00
func deleteZone(c *gin.Context) {
2022-10-25 16:16:34 +00:00
zoneid, err := happydns.NewIdentifierFromString(c.Param("zoneid"))
2020-06-08 23:25:15 +00:00
if err != nil {
2021-05-05 01:48:16 +00:00
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": err.Error()})
2020-06-08 23:25:15 +00:00
} else {
2021-05-05 01:48:16 +00:00
ApiResponse(c, true, storage.MainStore.DeleteZone(&happydns.Zone{ZoneMeta: happydns.ZoneMeta{Id: zoneid}}))
2020-06-08 23:25:15 +00:00
}
}