Skip to content

Commit

Permalink
Fix race in get resource commands (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitarPetrov authored Mar 25, 2020
1 parent 4e8f70f commit b7476e0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions internal/cmd/binding/get_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package binding

import (
"fmt"
"strings"

"github.com/Peripli/service-manager-cli/pkg/types"

Expand Down Expand Up @@ -59,6 +60,10 @@ func (gb *GetBindingCmd) Run() error {
for _, binding := range bindings.ServiceBindings {
bd, err := gb.Client.GetBindingByID(binding.ID, &gb.Parameters)
if err != nil {
// The binding could be deleted after List and before Get
if strings.Contains(err.Error(), "StatusCode: 404") {
continue
}
return err
}
instance, err := gb.Client.GetInstanceByID(bd.ServiceInstanceID, &query.Parameters{})
Expand All @@ -69,6 +74,11 @@ func (gb *GetBindingCmd) Run() error {
resultBindings.ServiceBindings = append(resultBindings.ServiceBindings, *bd)
}

if len(resultBindings.ServiceBindings) < 1 {
output.PrintMessage(gb.Output, "No binding found with name: %s", gb.bindingName)
return nil
}

output.PrintServiceManagerObject(gb.Output, gb.outputFormat, resultBindings)
output.Println(gb.Output)

Expand Down
6 changes: 6 additions & 0 deletions internal/cmd/broker/get_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package broker

import (
"fmt"
"strings"

"github.com/Peripli/service-manager-cli/pkg/query"

Expand Down Expand Up @@ -58,6 +59,11 @@ func (gb *GetBrokerCmd) Run() error {
id := brokers.Brokers[0].ID
broker, err := gb.Client.GetBrokerByID(id, &gb.Parameters)
if err != nil {
// The broker could be deleted after List and before Get
if strings.Contains(err.Error(), "StatusCode: 404") {
output.PrintMessage(gb.Output, "No broker found with name: %s", gb.name)
return nil
}
return err
}
output.PrintServiceManagerObject(gb.Output, gb.outputFormat, broker)
Expand Down
10 changes: 10 additions & 0 deletions internal/cmd/instance/get_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package instance

import (
"fmt"
"strings"

"github.com/Peripli/service-manager-cli/pkg/types"

Expand Down Expand Up @@ -59,11 +60,20 @@ func (gb *GetInstanceCmd) Run() error {
for _, instance := range instances.ServiceInstances {
inst, err := gb.Client.GetInstanceByID(instance.ID, &gb.Parameters)
if err != nil {
// The instance could be deleted after List and before Get
if strings.Contains(err.Error(), "StatusCode: 404") {
continue
}
return err
}
resultInstances.ServiceInstances = append(resultInstances.ServiceInstances, *inst)
}

if len(resultInstances.ServiceInstances) < 1 {
output.PrintMessage(gb.Output, "No instance found with name: %s", gb.instanceName)
return nil
}

output.PrintServiceManagerObject(gb.Output, gb.outputFormat, resultInstances)
output.Println(gb.Output)
return nil
Expand Down

0 comments on commit b7476e0

Please sign in to comment.