Skip to content

Commit

Permalink
Merge pull request #77 from ddymko/create-ipv4-change
Browse files Browse the repository at this point in the history
AddIPv4 returns change
  • Loading branch information
ddymko authored Aug 25, 2020
2 parents f0aaa9b + a34ed58 commit 83ab8ff
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
28 changes: 17 additions & 11 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type ServerService interface {
SetUserData(ctx context.Context, instanceID, userData string) error
IPV4Info(ctx context.Context, instanceID string, public bool) ([]IPV4, error)
IPV6Info(ctx context.Context, instanceID string) ([]IPV6, error)
AddIPV4(ctx context.Context, instanceID string) error
AddIPV4(ctx context.Context, instanceID, reboot string) (*IP, error)
DestroyIPV4(ctx context.Context, instanceID, ip string) error
EnableIPV6(ctx context.Context, instanceID string) error
Bandwidth(ctx context.Context, instanceID string) ([]map[string]string, error)
Expand Down Expand Up @@ -186,6 +186,10 @@ type ServerOptions struct {
FirewallGroupID string
}

type IP struct {
IPv4 string `json:"ipv4"`
}

// ChangeApp changes the VPS to a different application.
func (s *ServerServiceHandler) ChangeApp(ctx context.Context, instanceID, appID string) error {

Expand Down Expand Up @@ -904,27 +908,28 @@ func (s *ServerServiceHandler) IPV6Info(ctx context.Context, instanceID string)
}

// AddIPV4 will add a new IPv4 address to a server.
func (s *ServerServiceHandler) AddIPV4(ctx context.Context, instanceID string) error {

// The server will be rebooted unless you specify otherwise. You must reboot the server before the IPv4 address can be configured.
func (s *ServerServiceHandler) AddIPV4(ctx context.Context, instanceID, reboot string) (*IP, error) {
uri := "/v1/server/create_ipv4"

values := url.Values{
"SUBID": {instanceID},
}

req, err := s.client.NewRequest(ctx, http.MethodPost, uri, values)
if reboot == "no" {
values.Add("reboot", "no")
}

req, err := s.client.NewRequest(ctx, http.MethodPost, uri, values)
if err != nil {
return err
return nil, err
}

err = s.client.DoWithContext(ctx, req, nil)

if err != nil {
return err
ip := new(IP)
if err = s.client.DoWithContext(ctx, req, ip); err != nil {
return nil, err
}

return nil
return ip, nil
}

// DestroyIPV4 removes a secondary IPv4 address from a server.
Expand Down Expand Up @@ -1466,6 +1471,7 @@ func (s *ServerServiceHandler) GetServer(ctx context.Context, instanceID string)

return server, nil
}

// EnableDDOS for a specific server.
func (s *ServerServiceHandler) EnableDDOS(ctx context.Context, instanceID string) error {
uri := "/v1/server/ddos_protection_enable"
Expand Down
10 changes: 8 additions & 2 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,14 +545,20 @@ func TestServerServiceHandler_AddIPV4(t *testing.T) {
defer teardown()

mux.HandleFunc("/v1/server/create_ipv4", func(writer http.ResponseWriter, request *http.Request) {
fmt.Fprint(writer)
response := `{"ipv4": "123.123.123.124"}`
fmt.Fprint(writer, response)
})

err := client.Server.AddIPV4(ctx, "1234")
ip, err := client.Server.AddIPV4(ctx, "1234", "no")

if err != nil {
t.Errorf("Server.AddIPV4 returned %+v", err)
}

expectedIP := &IP{IPv4: "123.123.123.124"}
if !reflect.DeepEqual(ip, expectedIP) {
t.Errorf("Server.AddIPV4 returned %+v, expected %+v", ip, expectedIP)
}
}

func TestServerServiceHandler_DestroyIPV4(t *testing.T) {
Expand Down

0 comments on commit 83ab8ff

Please # to comment.