Skip to content

Commit

Permalink
fixed malformed auth issue due to http to https redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
pi3ch committed Nov 12, 2023
1 parent 7663778 commit c566744
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions pkg/apiv1/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,23 @@ func NewMachineAPI(httpClient *hreq.Client, endpoint string) *MachineAPI {

func (a *MachineAPI) LockMachine(app string, id string, timeout int) (*MachineLease, error) {
var res MachineLease
_, err := a.httpClient.R().SetResult(&res).Post(fmt.Sprintf("http://%s/v1/apps/%s/machines/%s/lease/?ttl=%d", a.endpoint, app, id, timeout))
_, err := a.httpClient.R().SetResult(&res).Post(fmt.Sprintf("https://%s/v1/apps/%s/machines/%s/lease/?ttl=%d", a.endpoint, app, id, timeout))
if err != nil {
return nil, err
}
return &res, nil
}

func (a *MachineAPI) ReleaseMachine(lease MachineLease, app string, id string) error {
_, err := a.httpClient.R().SetHeader(NonceHeader, lease.Data.Nonce).Delete(fmt.Sprintf("http://%s/v1/apps/%s/machines/%s/lease", a.endpoint, app, id))
_, err := a.httpClient.R().SetHeader(NonceHeader, lease.Data.Nonce).Delete(fmt.Sprintf("https://%s/v1/apps/%s/machines/%s/lease", a.endpoint, app, id))
if err != nil {
return err
}
return nil
}

func (a *MachineAPI) WaitForMachine(app string, id string, instanceID string) error {
_, err := a.httpClient.R().Get(fmt.Sprintf("http://%s/v1/apps/%s/machines/%s/wait?instance_id=%s", a.endpoint, app, id, instanceID))
_, err := a.httpClient.R().Get(fmt.Sprintf("https://%s/v1/apps/%s/machines/%s/wait?instance_id=%s", a.endpoint, app, id, instanceID))
return err
}

Expand All @@ -150,7 +150,7 @@ func (a *MachineAPI) CreateMachine(req MachineCreateOrUpdateRequest, app string,
if req.Config.Guest.MemoryMb == 0 {
req.Config.Guest.MemoryMb = 256
}
createResponse, err := a.httpClient.R().SetBody(req).SetResult(res).Post(fmt.Sprintf("http://%s/v1/apps/%s/machines", a.endpoint, app))
createResponse, err := a.httpClient.R().SetBody(req).SetResult(res).Post(fmt.Sprintf("https://%s/v1/apps/%s/machines", a.endpoint, app))

if err != nil {
return err
Expand Down Expand Up @@ -178,7 +178,7 @@ func (a *MachineAPI) UpdateMachine(req MachineCreateOrUpdateRequest, app string,
if err != nil {
return err
}
reqRes, err := a.httpClient.R().SetBody(req).SetResult(res).SetHeader(NonceHeader, lease.Data.Nonce).Post(fmt.Sprintf("http://%s/v1/apps/%s/machines/%s", a.endpoint, app, id))
reqRes, err := a.httpClient.R().SetBody(req).SetResult(res).SetHeader(NonceHeader, lease.Data.Nonce).Post(fmt.Sprintf("https://%s/v1/apps/%s/machines/%s", a.endpoint, app, id))
if err != nil {
return err
}
Expand All @@ -193,27 +193,27 @@ func (a *MachineAPI) UpdateMachine(req MachineCreateOrUpdateRequest, app string,
}

func (a *MachineAPI) ReadMachine(app string, id string, res *MachineResponse) (*hreq.Response, error) {
return a.httpClient.R().SetResult(res).Get(fmt.Sprintf("http://%s/v1/apps/%s/machines/%s", a.endpoint, app, id))
return a.httpClient.R().SetResult(res).Get(fmt.Sprintf("https://%s/v1/apps/%s/machines/%s", a.endpoint, app, id))
}

func (a *MachineAPI) DeleteMachine(app string, id string, maxRetries int) error {
deleted := false
for i := 0; i < maxRetries; i++ {
var machine MachineResponse
readResponse, err := a.httpClient.R().SetResult(&machine).Get(fmt.Sprintf("http://%s/v1/apps/%s/machines/%s", a.endpoint, app, id))
readResponse, err := a.httpClient.R().SetResult(&machine).Get(fmt.Sprintf("https://%s/v1/apps/%s/machines/%s", a.endpoint, app, id))
if err != nil {
return err
}

if readResponse.StatusCode == 200 {
if machine.State == "started" || machine.State == "starting" || machine.State == "replacing" {
_, _ = a.httpClient.R().Post(fmt.Sprintf("http://%s/v1/apps/%s/machines/%s/stop", a.endpoint, app, id))
_, _ = a.httpClient.R().Post(fmt.Sprintf("https://%s/v1/apps/%s/machines/%s/stop", a.endpoint, app, id))
}
if machine.State == "stopping" || machine.State == "destroying" {
time.Sleep(5 * time.Second)
}
if machine.State == "stopped" || machine.State == "replaced" {
_, err = a.httpClient.R().Delete(fmt.Sprintf("http://%s/v1/apps/%s/machines/%s", a.endpoint, app, id))
_, err = a.httpClient.R().Delete(fmt.Sprintf("https://%s/v1/apps/%s/machines/%s", a.endpoint, app, id))
if err != nil {
return err
}
Expand Down

0 comments on commit c566744

Please sign in to comment.