Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type RobotClient interface {
IPGetList() ([]models.IP, error)
RDnsGetList() ([]models.Rdns, error)
RDnsGet(ip string) (*models.Rdns, error)
RDnsSet(ip string, input *models.RdnsSetInput) (*models.Rdns, error)
BootLinuxGet(id int) (*models.Linux, error)
BootLinuxDelete(id int) (*models.Linux, error)
BootLinuxSet(id int, input *models.LinuxSetInput) (*models.Linux, error)
Expand Down
4 changes: 4 additions & 0 deletions models/rdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ type Rdns struct {
IP string `json:"ip"`
Ptr string `json:"ptr"`
}

type RdnsSetInput struct {
Ptr string `json:"ptr"`
}
21 changes: 21 additions & 0 deletions rdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"encoding/json"
"fmt"
neturl "net/url"

"github.com/syself/hrobot-go/models"
)
Expand Down Expand Up @@ -43,3 +44,23 @@ func (c *Client) RDnsGet(ip string) (*models.Rdns, error) {

return &rDnsResp.Rdns, nil
}

func (c *Client) RDnsSet(ip string, input *models.RdnsSetInput) (*models.Rdns, error) {
url := fmt.Sprintf(c.baseURL+"/rdns/%s", ip)

formData := neturl.Values{}
formData.Set("ptr", input.Ptr)

bytes, err := c.doPostFormRequest(url, formData)
if err != nil {
return nil, err
}

var rdnsResp models.RdnsResponse
err = json.Unmarshal(bytes, &rdnsResp)
if err != nil {
return nil, err
}

return &rdnsResp.Rdns, nil
}
81 changes: 81 additions & 0 deletions rdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"

client "github.com/syself/hrobot-go"
"github.com/syself/hrobot-go/models"
. "gopkg.in/check.v1"
)

Expand Down Expand Up @@ -120,3 +121,83 @@ func (s *ClientSuite) TestRDnsGetServerError(c *C) {
_, err := robotClient.RDnsGet(testIP)
c.Assert(err, Not(IsNil))
}

func (s *ClientSuite) TestRDnsSetSuccess(c *C) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reqContentType := r.Header.Get("Content-Type")
c.Assert(reqContentType, Equals, "application/x-www-form-urlencoded")

body, bodyErr := ioutil.ReadAll(r.Body)
c.Assert(bodyErr, IsNil)
c.Assert(string(body), Equals, "ptr=testen.de")

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

pwd, pwdErr := os.Getwd()
c.Assert(pwdErr, IsNil)

data, readErr := ioutil.ReadFile(fmt.Sprintf("%s/test/response/rdns_get.json", pwd))
c.Assert(readErr, IsNil)

_, err := w.Write(data)
c.Assert(err, IsNil)
}))
defer ts.Close()

robotClient := client.NewBasicAuthClient("user", "pass")
robotClient.SetBaseURL(ts.URL)

input := &models.RdnsSetInput{
Ptr: "testen.de",
}

rdns, err := robotClient.RDnsSet(testIP, input)
c.Assert(err, IsNil)
c.Assert(rdns.IP, Equals, testIP)
}

func (s *ClientSuite) TestRDnsSetInvalidResponse(c *C) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reqContentType := r.Header.Get("Content-Type")
c.Assert(reqContentType, Equals, "application/x-www-form-urlencoded")

body, bodyErr := ioutil.ReadAll(r.Body)
c.Assert(bodyErr, IsNil)
c.Assert(string(body), Equals, "ptr=testen.de")

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

_, err := w.Write([]byte("invalid JSON"))
c.Assert(err, IsNil)
}))
defer ts.Close()

robotClient := client.NewBasicAuthClient("user", "pass")
robotClient.SetBaseURL(ts.URL)

input := &models.RdnsSetInput{
Ptr: "testen.de",
}

_, err := robotClient.RDnsSet(testIP, input)
c.Assert(err, Not(IsNil))
}

func (s *ClientSuite) TestRDnsSetServerError(c *C) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer ts.Close()

robotClient := client.NewBasicAuthClient("user", "pass")
robotClient.SetBaseURL(ts.URL)

input := &models.RdnsSetInput{
Ptr: "testen.de",
}

_, err := robotClient.RDnsSet(testIP, input)
c.Assert(err, Not(IsNil))
}