-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored CLI code and added missing commands
- Loading branch information
1 parent
6b3089e
commit d41216d
Showing
19 changed files
with
2,136 additions
and
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package api_agent | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/starkandwayne/shield/db" | ||
) | ||
|
||
func FetchListArchives(plugin, unused string) (*[]db.AnnotatedArchive, error) { | ||
|
||
// Data to be returned of proper type | ||
data := &[]db.AnnotatedArchive{} | ||
|
||
// Make uri based on options | ||
uri := fmt.Sprintf("v1/archives") | ||
joiner := "?" | ||
if plugin != "" { | ||
uri = fmt.Sprintf("%s%splugin=%s", uri, joiner, plugin) | ||
joiner = "&" | ||
} | ||
if unused != "" { | ||
uri = fmt.Sprintf("%s%sunused=%s", uri, joiner, unused) | ||
joiner = "&" | ||
} | ||
|
||
// Call generic API request | ||
err := makeApiCall(data, `GET`, uri, nil) | ||
return data, err | ||
} | ||
|
||
func GetArchive(uuid string) (*db.AnnotatedArchive, error) { | ||
|
||
// Data to be returned of proper type | ||
data := &db.AnnotatedArchive{} | ||
|
||
// Make uri based on options | ||
uri := fmt.Sprintf("v1/archive/%s", uuid) | ||
|
||
// Call generic API request | ||
err := makeApiCall(data, `GET`, uri, nil) | ||
return data, err | ||
} | ||
|
||
func RestoreArchive(uuid, target string) error { | ||
|
||
data := struct { | ||
Status string `json:"ok"` | ||
}{} | ||
|
||
buf := bytes.NewBufferString(target) | ||
|
||
uri := fmt.Sprintf("v1/archive/%s/restore", uuid) | ||
|
||
err := makeApiCall(&data, `POST`, uri, buf) | ||
|
||
return err | ||
} | ||
|
||
func UpdateArchive(uuid string, content string) (*db.AnnotatedArchive, error) { | ||
|
||
data := struct { | ||
Status string `json:"ok"` | ||
}{} | ||
|
||
buf := bytes.NewBufferString(content) | ||
|
||
uri := fmt.Sprintf("v1/archive/%s", uuid) | ||
|
||
err := makeApiCall(&data, `PUT`, uri, buf) | ||
|
||
if err == nil { | ||
return GetArchive(uuid) | ||
} | ||
return nil, err | ||
} | ||
|
||
func DeleteArchive(uuid string) error { | ||
|
||
uri := fmt.Sprintf("v1/archive/%s", uuid) | ||
|
||
data := struct { | ||
Status string `json:"ok"` | ||
}{} | ||
|
||
err := makeApiCall(&data, `DELETE`, uri, nil) | ||
|
||
return err | ||
} |
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,157 @@ | ||
package api_agent | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/starkandwayne/shield/db" | ||
) | ||
|
||
func FetchListJobs(target, store, schedule, retention, paused string) (*[]db.AnnotatedJob, error) { | ||
|
||
// Data to be returned of proper type | ||
data := &[]db.AnnotatedJob{} | ||
|
||
// Make uri based on options | ||
uri := fmt.Sprintf("v1/jobs") | ||
joiner := "?" | ||
if target != "" { | ||
uri = fmt.Sprintf("%s%starget=%s", uri, joiner, target) | ||
joiner = "&" | ||
} | ||
if store != "" { | ||
uri = fmt.Sprintf("%s%sstore=%s", uri, joiner, store) | ||
joiner = "&" | ||
} | ||
if schedule != "" { | ||
uri = fmt.Sprintf("%s%sschedule=%s", uri, joiner, schedule) | ||
joiner = "&" | ||
} | ||
if retention != "" { | ||
uri = fmt.Sprintf("%s%sretention=%s", uri, joiner, retention) | ||
joiner = "&" | ||
} | ||
if paused != "" { | ||
uri = fmt.Sprintf("%s%spaused=%s", uri, joiner, paused) | ||
joiner = "&" | ||
} | ||
|
||
// Call generic API request | ||
err := makeApiCall(data, `GET`, uri, nil) | ||
return data, err | ||
} | ||
|
||
func GetJob(uuid string) (*db.AnnotatedJob, error) { | ||
|
||
// Data to be returned of proper type | ||
data := &db.AnnotatedJob{} | ||
|
||
// Make uri based on options | ||
uri := fmt.Sprintf("v1/job/%s", uuid) | ||
|
||
// Call generic API request | ||
err := makeApiCall(data, `GET`, uri, nil) | ||
return data, err | ||
} | ||
|
||
func IsPausedJob(uuid string) (bool, error) { | ||
// UUID validation can be handled by GetJob | ||
data, err := GetJob(uuid) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return data.Paused, err | ||
} | ||
|
||
func CreateJob(content string) (*db.AnnotatedJob, error) { | ||
|
||
data := struct { | ||
Status string `json:"ok"` | ||
UUID string `json:"uuid"` | ||
}{} | ||
|
||
buf := bytes.NewBufferString(content) | ||
|
||
err := makeApiCall(&data, `POST`, `v1/jobs`, buf) | ||
|
||
if err == nil { | ||
return GetJob(data.UUID) | ||
} | ||
return nil, err | ||
} | ||
|
||
func UpdateJob(uuid string, content string) (*db.AnnotatedJob, error) { | ||
|
||
data := struct { | ||
Status string `json:"ok"` | ||
}{} | ||
|
||
buf := bytes.NewBufferString(content) | ||
|
||
uri := fmt.Sprintf("v1/job/%s", uuid) | ||
|
||
err := makeApiCall(&data, `PUT`, uri, buf) | ||
|
||
if err == nil { | ||
return GetJob(uuid) | ||
} | ||
return nil, err | ||
} | ||
|
||
func DeleteJob(uuid string) error { | ||
|
||
uri := fmt.Sprintf("v1/job/%s", uuid) | ||
|
||
data := struct { | ||
Status string `json:"ok"` | ||
}{} | ||
|
||
err := makeApiCall(&data, `DELETE`, uri, nil) | ||
|
||
return err | ||
} | ||
|
||
func PauseJob(uuid string) error { | ||
|
||
data := struct { | ||
Status string `json:"ok"` | ||
}{} | ||
|
||
buf := bytes.NewBufferString("") | ||
|
||
uri := fmt.Sprintf("v1/job/%s/pause", uuid) | ||
|
||
err := makeApiCall(&data, `POST`, uri, buf) | ||
|
||
return err | ||
} | ||
|
||
func UnpauseJob(uuid string) error { | ||
|
||
data := struct { | ||
Status string `json:"ok"` | ||
}{} | ||
|
||
buf := bytes.NewBufferString("") | ||
|
||
uri := fmt.Sprintf("v1/job/%s/unpause", uuid) | ||
|
||
err := makeApiCall(&data, `POST`, uri, buf) | ||
|
||
return err | ||
} | ||
|
||
func RunJob(uuid, owner string) error { | ||
|
||
data := struct { | ||
Status string `json:"ok"` | ||
}{} | ||
|
||
buf := bytes.NewBufferString(owner) | ||
|
||
uri := fmt.Sprintf("v1/job/%s/run", uuid) | ||
|
||
err := makeApiCall(&data, `POST`, uri, buf) | ||
|
||
return err | ||
} |
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,35 @@ | ||
package api_agent | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// FIXME - Placeholder until plugin's schema is defined | ||
type AnnotatedPlugin struct { | ||
Name string `json:"name"` | ||
} | ||
|
||
func FetchListPlugins() (*[]AnnotatedPlugin, error) { | ||
|
||
// Data to be returned of proper type | ||
data := &[]AnnotatedPlugin{} | ||
|
||
// Make uri based on options | ||
uri := fmt.Sprintf("v1/plugins") | ||
|
||
// Call generic API request | ||
err := makeApiCall(data, `GET`, uri, nil) | ||
return data, err | ||
} | ||
|
||
func GetPlugin(uuid string) (*AnnotatedPlugin, error) { | ||
// Data to be returned of proper type | ||
data := &AnnotatedPlugin{} | ||
|
||
// Make uri based on options | ||
uri := fmt.Sprintf("v1/plugin/%s", uuid) | ||
|
||
// Call generic API request | ||
err := makeApiCall(data, `GET`, uri, nil) | ||
return data, err | ||
} |
Oops, something went wrong.