-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add optional argument - add confirmation (y/n)
- Loading branch information
1 parent
344d7a0
commit 01849da
Showing
8 changed files
with
198 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package retrieval | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/therealpaulgg/ssh-sync/pkg/dto" | ||
"github.com/therealpaulgg/ssh-sync/pkg/models" | ||
"github.com/therealpaulgg/ssh-sync/pkg/utils" | ||
) | ||
|
||
func GetMachines(profile *models.Profile) ([]dto.MachineDto, error) { | ||
url := profile.ServerUrl | ||
url.Path = "/api/v1/machines/" | ||
req, err := http.NewRequest("GET", url.String(), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
token, err := utils.GetToken() | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Set("Authorization", "Bearer "+token) | ||
req.Header.Set("Content-Type", "application/json") | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
} | ||
machines := []dto.MachineDto{} | ||
err = json.NewDecoder(resp.Body).Decode(&machines) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return machines, nil | ||
} | ||
|
||
func DeleteMachine(profile *models.Profile, machineName string) error { | ||
buf := new(bytes.Buffer) | ||
if err := json.NewEncoder(buf).Encode(dto.MachineDto{ | ||
Name: machineName, | ||
}); err != nil { | ||
return err | ||
} | ||
url := profile.ServerUrl | ||
url.Path = "/api/v1/machines/" | ||
req, err := http.NewRequest("DELETE", url.String(), buf) | ||
if err != nil { | ||
return err | ||
} | ||
token, err := utils.GetToken() | ||
if err != nil { | ||
return err | ||
} | ||
req.Header.Set("Authorization", "Bearer "+token) | ||
req.Header.Set("Content-Type", "application/json") | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package retrieval | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/therealpaulgg/ssh-sync/pkg/dto" | ||
"github.com/therealpaulgg/ssh-sync/pkg/models" | ||
) | ||
|
||
func TestGetMachines(t *testing.T) { | ||
// Arrange | ||
profile := &models.Profile{} | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
json.NewEncoder(w).Encode([]dto.MachineDto{ | ||
{ | ||
Name: "test", | ||
}, | ||
}) | ||
})) | ||
url, _ := url.Parse(server.URL) | ||
profile.ServerUrl = *url | ||
// Act | ||
machines, err := GetMachines(profile) | ||
// Assert | ||
assert.Nil(t, err) | ||
assert.Equal(t, 1, len(machines)) | ||
assert.Equal(t, "test", machines[0].Name) | ||
} | ||
|
||
func TestDeleteMachine(t *testing.T) { | ||
// Arrange | ||
profile := &models.Profile{} | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
url, _ := url.Parse(server.URL) | ||
profile.ServerUrl = *url | ||
// Act | ||
err := DeleteMachine(profile, "test") | ||
// Assert | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestDeleteMachineDoesNotExist(t *testing.T) { | ||
// Arrange | ||
profile := &models.Profile{} | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusNotFound) | ||
})) | ||
url, _ := url.Parse(server.URL) | ||
profile.ServerUrl = *url | ||
// Act | ||
err := DeleteMachine(profile, "test") | ||
// Assert | ||
assert.NotNil(t, err) | ||
} |
This file was deleted.
Oops, something went wrong.