From 835b677660b1be83510392e49813e6a575910467 Mon Sep 17 00:00:00 2001 From: Dr Nic Williams Date: Wed, 26 Aug 2015 11:23:10 -0700 Subject: [PATCH] Add GetTaskStatusesWithLimit(limit) --- api/director_task_status.go | 19 ++++++++++++++ example/bosh-lite-example.go | 10 +++++++- example/current_target.go | 50 ------------------------------------ 3 files changed, 28 insertions(+), 51 deletions(-) delete mode 100644 example/current_target.go diff --git a/api/director_task_status.go b/api/director_task_status.go index dd81edd..8ce43b1 100644 --- a/api/director_task_status.go +++ b/api/director_task_status.go @@ -7,6 +7,7 @@ import ( "github.com/cloudfoundry-community/gogobosh/net" ) +// GetTaskStatuses returns a list of most recent task statuses func (repo BoshDirectorRepository) GetTaskStatuses() (tasks []models.TaskStatus, apiResponse net.ApiResponse) { taskResponses := []taskStatusResponse{} @@ -23,6 +24,24 @@ func (repo BoshDirectorRepository) GetTaskStatuses() (tasks []models.TaskStatus, return } +// GetTaskStatusesWithLimit returns a max of 'limit' task statuses +func (repo BoshDirectorRepository) GetTaskStatusesWithLimit(limit int) (tasks []models.TaskStatus, apiResponse net.ApiResponse) { + taskResponses := []taskStatusResponse{} + + path := fmt.Sprintf("/tasks?limit=%d", limit) + apiResponse = repo.gateway.GetResource(repo.config.TargetURL+path, repo.config.Username, repo.config.Password, &taskResponses) + if apiResponse.IsNotSuccessful() { + return + } + + for _, resource := range taskResponses { + tasks = append(tasks, resource.ToModel()) + } + + return +} + +// GetTaskStatus returns details of a specific task to allow polling for state change func (repo BoshDirectorRepository) GetTaskStatus(taskID int) (task models.TaskStatus, apiResponse net.ApiResponse) { taskResponse := taskStatusResponse{} diff --git a/example/bosh-lite-example.go b/example/bosh-lite-example.go index 75a2b1e..de058cd 100644 --- a/example/bosh-lite-example.go +++ b/example/bosh-lite-example.go @@ -82,7 +82,15 @@ func main() { fmt.Println("") } - task, apiResponse := repo.GetTaskStatus(1) + tasks, apiResponse := repo.GetTaskStatusesWithLimit(3) + if apiResponse.IsNotSuccessful() { + fmt.Println("Could not fetch tasks") + return + } else { + fmt.Printf("%#v\n", tasks) + } + + task, apiResponse := repo.GetTaskStatus(tasks[0].ID) if apiResponse.IsNotSuccessful() { fmt.Println("Could not fetch BOSH task 1") return diff --git a/example/current_target.go b/example/current_target.go deleted file mode 100644 index 7858008..0000000 --- a/example/current_target.go +++ /dev/null @@ -1,50 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/cloudfoundry-community/gogobosh" - "github.com/cloudfoundry-community/gogobosh/api" - "github.com/cloudfoundry-community/gogobosh/local" - "github.com/cloudfoundry-community/gogobosh/net" - "github.com/cloudfoundry-community/gogobosh/utils" -) - -func main() { - utils.Logger = utils.NewLogger() - - configPath, err := local.DefaultBoshConfigPath() - if err != nil { - fmt.Println(err) - return - } - config, err := local.LoadBoshConfig(configPath) - if err != nil { - fmt.Println(err) - return - } - target, username, password, err := config.CurrentBoshTarget() - if err != nil { - fmt.Println(err) - return - } - fmt.Printf("Targeting %s with user %s...\n", target, username) - - director := gogobosh.NewDirector(target, username, password) - repo := api.NewBoshDirectorRepository(&director, net.NewDirectorGateway()) - - info, apiResponse := repo.GetInfo() - if apiResponse.IsNotSuccessful() { - fmt.Println("Could not fetch BOSH info") - return - } - - fmt.Println("Director") - fmt.Printf(" Name %s\n", info.Name) - fmt.Printf(" URL %s\n", info.URL) - fmt.Printf(" Version %s\n", info.Version) - fmt.Printf(" User %s\n", info.User) - fmt.Printf(" UUID %s\n", info.UUID) - fmt.Printf(" CPI %s\n", info.CPI) - -}