From 8c829ba6564cdc4c135a965a3074340ffebb5278 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Thu, 11 Jun 2020 01:18:33 +0200 Subject: [PATCH] Handle service modifications --- admin/db-zone.go | 49 +++++++++++++++++++ api/zones.go | 31 ++++++++++++ htdocs/src/components/hDomainService.vue | 28 ++++++++++- htdocs/src/components/hResourceValue.vue | 2 +- .../src/components/hResourceValueInputRaw.vue | 2 + .../src/components/hResourceValueObject.vue | 10 ++-- htdocs/src/components/hResourceValueTable.vue | 14 ++++-- htdocs/src/components/hSubdomainItem.vue | 2 +- htdocs/src/components/hSubdomainList.vue | 6 ++- htdocs/src/services/ZoneApi.js | 4 ++ 10 files changed, 137 insertions(+), 11 deletions(-) diff --git a/admin/db-zone.go b/admin/db-zone.go index aec0037..175951d 100644 --- a/admin/db-zone.go +++ b/admin/db-zone.go @@ -32,6 +32,7 @@ package admin import ( + "encoding/base64" "encoding/json" "fmt" "io" @@ -58,6 +59,10 @@ func init() { router.GET("/api/zones/:zoneid", api.ApiHandler(zoneHandler(getZone))) router.PUT("/api/zones/:zoneid", api.ApiHandler(zoneHandler(updateZone))) router.DELETE("/api/zones/:zoneid", api.ApiHandler(deleteZone)) + + router.GET("/api/users/:userid/domains/:domain/zones/:zoneid/:serviceid", api.ApiHandler(zoneHandler(getZoneService))) + router.PUT("/api/users/:userid/domains/:domain/zones/:zoneid/:serviceid", api.ApiHandler(zoneHandler(updateZoneService))) + router.PATCH("/api/users/:userid/domains/:domain/zones/:zoneid", api.ApiHandler(zoneHandler(patchZoneService))) } func getUserDomainZones(_ *config.Options, domain *happydns.Domain, _ httprouter.Params, _ io.Reader) api.Response { @@ -115,6 +120,50 @@ func updateZone(_ *config.Options, zone *happydns.Zone, _ httprouter.Params, bod return api.NewAPIResponse(uz, storage.MainStore.UpdateZone(uz)) } +func getZoneService(_ *config.Options, zone *happydns.Zone, ps httprouter.Params, body io.Reader) api.Response { + serviceid, err := base64.StdEncoding.DecodeString(ps.ByName("serviceid")) + if err != nil { + return api.NewAPIErrorResponse(http.StatusBadRequest, err) + } + + return api.NewAPIResponse(zone.FindService(serviceid), nil) +} + +func updateZoneService(_ *config.Options, zone *happydns.Zone, ps httprouter.Params, body io.Reader) api.Response { + serviceid, err := base64.StdEncoding.DecodeString(ps.ByName("serviceid")) + if err != nil { + return api.NewAPIErrorResponse(http.StatusBadRequest, err) + } + + usc := &happydns.ServiceCombined{} + err = json.NewDecoder(body).Decode(&usc) + if err != nil { + return api.NewAPIErrorResponse(http.StatusBadRequest, fmt.Errorf("Something is wrong in received data: %w", err)) + } + + err = zone.EraseService(usc.Domain, serviceid, usc) + if err != nil { + return api.NewAPIErrorResponse(http.StatusBadRequest, err) + } + + return api.NewAPIResponse(zone.Services, storage.MainStore.UpdateZone(zone)) +} + +func patchZoneService(_ *config.Options, zone *happydns.Zone, _ httprouter.Params, body io.Reader) api.Response { + usc := &happydns.ServiceCombined{} + err := json.NewDecoder(body).Decode(&usc) + if err != nil { + return api.NewAPIErrorResponse(http.StatusBadRequest, fmt.Errorf("Something is wrong in received data: %w", err)) + } + + err = zone.EraseService(usc.Domain, usc.Id, usc) + if err != nil { + return api.NewAPIErrorResponse(http.StatusBadRequest, err) + } + + return api.NewAPIResponse(zone.Services, storage.MainStore.UpdateZone(zone)) +} + func deleteZone(opts *config.Options, ps httprouter.Params, body io.Reader) api.Response { zoneid, err := strconv.ParseInt(ps.ByName("zoneid"), 10, 64) if err != nil { diff --git a/api/zones.go b/api/zones.go index 969aeea..6416255 100644 --- a/api/zones.go +++ b/api/zones.go @@ -32,7 +32,9 @@ package api import ( + "encoding/json" "errors" + "fmt" "io" "net/http" "strconv" @@ -47,6 +49,7 @@ import ( func init() { router.GET("/api/domains/:domain/zone/:zoneid", apiAuthHandler(zoneHandler(getZone))) + router.PATCH("/api/domains/:domain/zone/:zoneid", apiAuthHandler(zoneHandler(updateZoneService))) router.POST("/api/domains/:domain/import_zone", apiAuthHandler(domainHandler(importZone))) } @@ -146,3 +149,31 @@ func importZone(opts *config.Options, domain *happydns.Domain, body io.Reader) R response: myZone.Id, } } + +func updateZoneService(opts *config.Options, domain *happydns.Domain, zone *happydns.Zone, body io.Reader) Response { + usc := &happydns.ServiceCombined{} + err := json.NewDecoder(body).Decode(&usc) + if err != nil { + return APIErrorResponse{ + err: fmt.Errorf("Something is wrong in received data: %w", err), + } + } + + err = zone.EraseService(usc.Domain, usc.Id, usc) + if err != nil { + return APIErrorResponse{ + err: err, + } + } + + err = storage.MainStore.UpdateZone(zone) + if err != nil { + return APIErrorResponse{ + err: err, + } + } + + return APIResponse{ + response: zone, + } +} diff --git a/htdocs/src/components/hDomainService.vue b/htdocs/src/components/hDomainService.vue index 0627cf7..10194f6 100644 --- a/htdocs/src/components/hDomainService.vue +++ b/htdocs/src/components/hDomainService.vue @@ -44,13 +44,14 @@ - +