Skip to content

Commit

Permalink
Refactored CLI code and added missing commands
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisjbell authored and krutten committed Nov 19, 2015
1 parent 6b3089e commit d41216d
Show file tree
Hide file tree
Showing 19 changed files with 2,136 additions and 106 deletions.
29 changes: 28 additions & 1 deletion api_agent/api_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api_agent
import (
"encoding/json"
"fmt"
"github.com/spf13/viper"
"io"
"io/ioutil"
"net/http"
Expand All @@ -11,7 +12,21 @@ import (
)

func getServerDetails() (string, string, string) {
return "http", "localhost", "8080"
if viper.GetBool("ShieldSSL") {
return "https", viper.GetString("ShieldServer"), viper.GetString("ShieldPort")
} else {
return "http", viper.GetString("ShieldServer"), viper.GetString("ShieldPort")
}
}

type ResponseError struct {
Status int
FullResponse *http.Response
//Notes string
}

func (e ResponseError) Error() string {
return fmt.Sprintf("Status: %d\n %+v", e.Status, e.FullResponse)
}

func makeApiCall(data interface{}, action, uri string, postbody io.Reader) error {
Expand All @@ -21,17 +36,29 @@ func makeApiCall(data interface{}, action, uri string, postbody io.Reader) error

url := fmt.Sprintf("%s://%s:%s/%s", schema, host, port, uri)

if viper.GetBool("Verbose") {
fmt.Println("API Call:", url)
}

req, err := http.NewRequest(action, url, postbody)
httpClient := &http.Client{}

resp, err := httpClient.Do(req)

// FIXME - Make this a --debug flag
//fmt.Println("Req : ", req, "\nResp: ", resp)

if err != nil {
fmt.Println("ERROR: Failed to successfully communicate with host", err)
log.Error("Failed to successfully communicate with host: ", err)
return err
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
return ResponseError{Status: resp.StatusCode, FullResponse: resp}
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("ERROR: Failed to read body", err)
Expand Down
88 changes: 88 additions & 0 deletions api_agent/archives.go
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
}
157 changes: 157 additions & 0 deletions api_agent/jobs.go
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
}
35 changes: 35 additions & 0 deletions api_agent/plugins.go
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
}
Loading

0 comments on commit d41216d

Please sign in to comment.