diff options
| -rw-r--r-- | client.go | 60 | ||||
| -rw-r--r-- | httpd.go | 111 |
2 files changed, 164 insertions, 7 deletions
@@ -32,6 +32,66 @@ func NewClient(serverUrl string, insecure bool) (cl *Client) { return cl } +// BlockdDisable disable specific hosts on block.d. +func (cl *Client) BlockdDisable(blockdName string) (an interface{}, err error) { + var ( + logp = "BlockdDisable" + res = libhttp.EndpointResponse{} + params = url.Values{} + + hb *hostsBlock + resb []byte + ) + + params.Set(paramNameName, blockdName) + + _, resb, err = cl.PostForm(apiBlockdDisable, nil, params) + if err != nil { + return nil, fmt.Errorf("%s: %w", logp, err) + } + + res.Data = &hb + err = json.Unmarshal(resb, &res) + if err != nil { + return nil, fmt.Errorf("%s: %w", logp, err) + } + if res.Code != http.StatusOK { + return nil, fmt.Errorf("%s: %d %s", logp, res.Code, res.Message) + } + + return hb, nil +} + +// BlockdEnable enable specific hosts on block.d. +func (cl *Client) BlockdEnable(blockdName string) (an interface{}, err error) { + var ( + logp = "BlockdEnable" + res = libhttp.EndpointResponse{} + params = url.Values{} + + hb *hostsBlock + resb []byte + ) + + params.Set(paramNameName, blockdName) + + _, resb, err = cl.PostForm(apiBlockdEnable, nil, params) + if err != nil { + return nil, fmt.Errorf("%s: %w", logp, err) + } + + res.Data = &hb + err = json.Unmarshal(resb, &res) + if err != nil { + return nil, fmt.Errorf("%s: %w", logp, err) + } + if res.Code != http.StatusOK { + return nil, fmt.Errorf("%s: %d %s", logp, res.Code, res.Message) + } + + return hb, nil +} + // BlockdUpdate fetch the latest hosts file from the hosts block // provider based on registered URL. func (cl *Client) BlockdUpdate(blockdName string) (an interface{}, err error) { @@ -25,15 +25,22 @@ const ( paramNameQuery = "query" paramNameType = "type" paramNameValue = "value" - apiBlockd = "/api/block.d" - apiBlockdUpdate = "/api/block.d/update" + + apiBlockd = "/api/block.d" + apiBlockdDisable = "/api/block.d/disable" + apiBlockdEnable = "/api/block.d/enable" + apiBlockdUpdate = "/api/block.d/update" + apiCaches = "/api/caches" apiCachesSearch = "/api/caches/search" - apiEnvironment = "/api/environment" - apiHostsDir = "/api/hosts.d/:name" - apiHostsDirRR = "/api/hosts.d/:name/rr" - apiZone = "/api/zone.d/:name" - apiZoneRRType = "/api/zone.d/:name/rr/:type" + + apiEnvironment = "/api/environment" + + apiHostsDir = "/api/hosts.d/:name" + apiHostsDirRR = "/api/hosts.d/:name/rr" + + apiZone = "/api/zone.d/:name" + apiZoneRRType = "/api/zone.d/:name/rr/:type" ) func (srv *Server) httpdInit() (err error) { @@ -123,6 +130,28 @@ func (srv *Server) httpdRegisterEndpoints() (err error) { err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{ Method: libhttp.RequestMethodPost, + Path: apiBlockdDisable, + RequestType: libhttp.RequestTypeForm, + ResponseType: libhttp.ResponseTypeJSON, + Call: srv.httpApiBlockdDisable, + }) + if err != nil { + return err + } + + err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{ + Method: libhttp.RequestMethodPost, + Path: apiBlockdEnable, + RequestType: libhttp.RequestTypeForm, + ResponseType: libhttp.ResponseTypeJSON, + Call: srv.httpApiBlockdEnable, + }) + if err != nil { + return err + } + + err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{ + Method: libhttp.RequestMethodPost, Path: apiBlockdUpdate, RequestType: libhttp.RequestTypeJSON, ResponseType: libhttp.ResponseTypeJSON, @@ -250,6 +279,74 @@ func (srv *Server) httpdRun() { } } +// httpApiBlockdDisable disable the hosts block.d. +func (srv *Server) httpApiBlockdDisable(epr *libhttp.EndpointRequest) (resBody []byte, err error) { + var ( + res = libhttp.EndpointResponse{} + + hb *hostsBlock + hbName string + ) + + hbName = strings.ToLower(epr.HttpRequest.Form.Get(paramNameName)) + + hb = srv.env.HostsBlocks[hbName] + if hb == nil { + res.Code = http.StatusBadRequest + res.Message = fmt.Sprintf("hosts block.d name not found: %s", hbName) + return nil, &res + } + + if hb.IsEnabled { + err = hb.disable() + if err != nil { + res.Code = http.StatusInternalServerError + res.Message = err.Error() + return nil, &res + } + } + + res.Code = http.StatusOK + res.Message = fmt.Sprintf("hosts block.d %s has succesfully disabled", hbName) + res.Data = hb + + return json.Marshal(&res) +} + +// httpApiBlockdEnable enable the hosts block.d. +func (srv *Server) httpApiBlockdEnable(epr *libhttp.EndpointRequest) (resBody []byte, err error) { + var ( + res = libhttp.EndpointResponse{} + + hb *hostsBlock + hbName string + ) + + hbName = strings.ToLower(epr.HttpRequest.Form.Get(paramNameName)) + + hb = srv.env.HostsBlocks[hbName] + if hb == nil { + res.Code = http.StatusBadRequest + res.Message = fmt.Sprintf("hosts block.d name not found: %s", hbName) + return nil, &res + } + + if !hb.IsEnabled { + err = hb.enable() + if err != nil { + res.Code = http.StatusInternalServerError + res.Message = err.Error() + return nil, &res + } + } + + res.Code = http.StatusOK + res.Message = fmt.Sprintf("hosts block.d %s has succesfully enabled", hbName) + res.Data = hb + + return json.Marshal(&res) +} + // httpApiBlockdUpdate fetch the latest hosts file from the hosts block // provider based on registered URL. // |
