Skip to content

Commit

Permalink
Merge pull request #75 from ddymko/server_ddos
Browse files Browse the repository at this point in the history
Add DDOS functionality
  • Loading branch information
ddymko authored Aug 24, 2020
2 parents db4f7d8 + 4c0ba8d commit f0aaa9b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
38 changes: 38 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type ServerService interface {
ListByMainIP(ctx context.Context, mainIP string) ([]Server, error)
ListByTag(ctx context.Context, tag string) ([]Server, error)
GetServer(ctx context.Context, instanceID string) (*Server, error)
EnableDDOS(ctx context.Context, instanceID string) error
DisableDDOS(ctx context.Context, instanceID string) error
}

// ServerServiceHandler handles interaction with the server methods for the Vultr API
Expand Down Expand Up @@ -1463,5 +1465,41 @@ 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"
values := url.Values{
"SUBID": {instanceID},
}

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

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

return nil
}

// DisableDDOS protection for a specific server.
func (s *ServerServiceHandler) DisableDDOS(ctx context.Context, instanceID string) error {
uri := "/v1/server/ddos_protection_disable"
values := url.Values{
"SUBID": {instanceID},
}

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

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

return nil
}
30 changes: 30 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,3 +1091,33 @@ func TestServerServiceHandler_GetServer(t *testing.T) {
t.Errorf("Server.GetServer returned %+v, expected %+v", server, expected)
}
}

func TestServerServiceHandler_EnableDDOS(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/v1/server/ddos_protection_enable", func(writer http.ResponseWriter, request *http.Request) {
fmt.Fprint(writer)
})

err := client.Server.EnableDDOS(ctx, "1234")

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

func TestServerServiceHandler_DisableDDOS(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/v1/server/ddos_protection_disable", func(writer http.ResponseWriter, request *http.Request) {
fmt.Fprint(writer)
})

err := client.Server.DisableDDOS(ctx, "1234")

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

0 comments on commit f0aaa9b

Please # to comment.