Skip to content

Commit

Permalink
Instance : add optional fields for reinstall call (#181)
Browse files Browse the repository at this point in the history
* instance reinstall was missing the optional request body that can be used

* added omitempty to request and returning instance response
  • Loading branch information
ddymko committed Nov 12, 2021
1 parent 5b92620 commit 9d28fea
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 8 deletions.
19 changes: 14 additions & 5 deletions instance.go
Expand Up @@ -22,7 +22,7 @@ type InstanceService interface {
Start(ctx context.Context, instanceID string) error
Halt(ctx context.Context, instanceID string) error
Reboot(ctx context.Context, instanceID string) error
Reinstall(ctx context.Context, instanceID string) error
Reinstall(ctx context.Context, instanceID string, reinstallReq *ReinstallReq) (*Instance, error)

MassStart(ctx context.Context, instanceList []string) error
MassHalt(ctx context.Context, instanceList []string) error
Expand Down Expand Up @@ -250,6 +250,11 @@ type InstanceUpdateReq struct {
FirewallGroupID string `json:"firewall_group_id,omitempty"`
}

// ReinstallReq struct used to allow changes during a reinstall
type ReinstallReq struct {
Hostname string `json:"hostname,omitempty"`
}

// Create will create the server with the given parameters
func (i *InstanceServiceHandler) Create(ctx context.Context, instanceReq *InstanceCreateReq) (*Instance, error) {
uri := fmt.Sprintf("%s", instancePath)
Expand Down Expand Up @@ -367,15 +372,19 @@ func (i *InstanceServiceHandler) Reboot(ctx context.Context, instanceID string)
}

// Reinstall an instance.
func (i *InstanceServiceHandler) Reinstall(ctx context.Context, instanceID string) error {
func (i *InstanceServiceHandler) Reinstall(ctx context.Context, instanceID string, reinstallReq *ReinstallReq) (*Instance, error) {
uri := fmt.Sprintf("%s/%s/reinstall", instancePath, instanceID)

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

return i.client.DoWithContext(ctx, req, nil)
instance := new(instanceBase)
if err := i.client.DoWithContext(ctx, req, instance); err != nil {
return nil, err
}
return instance.Instance, nil
}

// MassStart will start a list of instances the machine is already running, it will be restarted.
Expand Down
72 changes: 69 additions & 3 deletions instance_test.go
Expand Up @@ -456,14 +456,80 @@ func TestServerServiceHandler_Reinstall(t *testing.T) {
defer teardown()

mux.HandleFunc("/v2/instances/14b3e7d6-ffb5-4994-8502-57fcd9db3b33/reinstall", func(writer http.ResponseWriter, request *http.Request) {
fmt.Fprint(writer)
response := `{
"instance": {
"id": "14b3e7d6-ffb5-4994-8502-57fcd9db3b33",
"os": "CentOS SELinux 8 x64",
"ram": 2048,
"disk": 60,
"main_ip": "123.123.123.123",
"vcpu_count": 2,
"region": "ewr",
"plan": "vc2-1c-2gb",
"date_created": "2013-12-19 14:45:41",
"status": "active",
"allowed_bandwidth": 2000,
"netmask_v4": "255.255.255.248",
"gateway_v4": "123.123.123.1",
"power_status": "running",
"server_status": "ok",
"v6_network": "2001:DB8:1000::",
"v6_main_ip": "fd11:1111:1112:1c02:0200:00ff:fe00:0000",
"v6_network_size": 64,
"label": "my new server",
"internal_ip": "10.99.0.10",
"kvm": "https://my.vultr.com/subs/novnc/api.php?data=eawxFVZw2mXnhGUV",
"default_password" : "nreqnusibni",
"tag": "tagger",
"os_id": 362,
"app_id": 0,
"firewall_group_id": "1234",
"features": [
"auto_backups", "ipv6"
]
}
}`
fmt.Fprint(writer, response)
})

err := client.Instance.Reinstall(ctx, "14b3e7d6-ffb5-4994-8502-57fcd9db3b33")

instanceRes, err := client.Instance.Reinstall(ctx, "14b3e7d6-ffb5-4994-8502-57fcd9db3b33", nil)
if err != nil {
t.Errorf("Instance.Reinstall returned %+v", err)
}

expected := &Instance{
ID: "14b3e7d6-ffb5-4994-8502-57fcd9db3b33",
Os: "CentOS SELinux 8 x64",
OsID: 362,
RAM: 2048,
Disk: 60,
MainIP: "123.123.123.123",
VCPUCount: 2,
Region: "ewr",
DefaultPassword: "nreqnusibni",
DateCreated: "2013-12-19 14:45:41",
Status: "active",
AllowedBandwidth: 2000,
NetmaskV4: "255.255.255.248",
GatewayV4: "123.123.123.1",
PowerStatus: "running",
ServerStatus: "ok",
Plan: "vc2-1c-2gb",
V6Network: "2001:DB8:1000::",
V6MainIP: "fd11:1111:1112:1c02:0200:00ff:fe00:0000",
V6NetworkSize: 64,
Label: "my new server",
InternalIP: "10.99.0.10",
KVM: "https://my.vultr.com/subs/novnc/api.php?data=eawxFVZw2mXnhGUV",
Tag: "tagger",
AppID: 0,
FirewallGroupID: "1234",
Features: []string{"auto_backups", "ipv6"},
}

if !reflect.DeepEqual(instanceRes, expected) {
t.Errorf("Instance.Create returned %+v, expected %+v", server, expected)
}
}

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

0 comments on commit 9d28fea

Please sign in to comment.