Skip to content

Commit

Permalink
Merge pull request #505 from zhang-hua/bug-93578300
Browse files Browse the repository at this point in the history
Reduce API calls when CRU operations of service keys
  • Loading branch information
Simon Leung committed Jul 21, 2015
2 parents c17dd49 + 44a2e7f commit 76a82e9
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 89 deletions.
33 changes: 14 additions & 19 deletions cf/commands/servicekey/create_service_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ import (
)

type CreateServiceKey struct {
ui terminal.UI
config core_config.Reader
serviceRepo api.ServiceRepository
serviceKeyRepo api.ServiceKeyRepository
ui terminal.UI
config core_config.Reader
serviceRepo api.ServiceRepository
serviceKeyRepo api.ServiceKeyRepository
serviceInstanceRequirement requirements.ServiceInstanceRequirement
}

func NewCreateServiceKey(ui terminal.UI, config core_config.Reader, serviceRepo api.ServiceRepository, serviceKeyRepo api.ServiceKeyRepository) (cmd CreateServiceKey) {
return CreateServiceKey{
func NewCreateServiceKey(ui terminal.UI, config core_config.Reader, serviceRepo api.ServiceRepository, serviceKeyRepo api.ServiceKeyRepository) (cmd *CreateServiceKey) {
return &CreateServiceKey{
ui: ui,
config: config,
serviceRepo: serviceRepo,
serviceKeyRepo: serviceKeyRepo,
}
}

func (cmd CreateServiceKey) Metadata() command_metadata.CommandMetadata {
func (cmd *CreateServiceKey) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "create-service-key",
ShortName: "csk",
Expand All @@ -57,22 +58,22 @@ EXAMPLE:
}
}

func (cmd CreateServiceKey) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
func (cmd *CreateServiceKey) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
if len(c.Args()) != 2 {
cmd.ui.FailWithUsage(c)
}

loginRequirement := requirementsFactory.NewLoginRequirement()
serviceInstanceRequirement := requirementsFactory.NewServiceInstanceRequirement(c.Args()[0])
cmd.serviceInstanceRequirement = requirementsFactory.NewServiceInstanceRequirement(c.Args()[0])
targetSpaceRequirement := requirementsFactory.NewTargetedSpaceRequirement()

reqs = []requirements.Requirement{loginRequirement, serviceInstanceRequirement, targetSpaceRequirement}
reqs = []requirements.Requirement{loginRequirement, cmd.serviceInstanceRequirement, targetSpaceRequirement}

return reqs, nil
}

func (cmd CreateServiceKey) Run(c *cli.Context) {
serviceInstanceName := c.Args()[0]
func (cmd *CreateServiceKey) Run(c *cli.Context) {
serviceInstance := cmd.serviceInstanceRequirement.GetServiceInstance()
serviceKeyName := c.Args()[1]
params := c.String("c")

Expand All @@ -83,17 +84,11 @@ func (cmd CreateServiceKey) Run(c *cli.Context) {

cmd.ui.Say(T("Creating service key {{.ServiceKeyName}} for service instance {{.ServiceInstanceName}} as {{.CurrentUser}}...",
map[string]interface{}{
"ServiceInstanceName": terminal.EntityNameColor(serviceInstanceName),
"ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name),
"ServiceKeyName": terminal.EntityNameColor(serviceKeyName),
"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
}))

serviceInstance, err := cmd.serviceRepo.FindInstanceByName(serviceInstanceName)
if err != nil {
cmd.ui.Failed(err.Error())
return
}

err = cmd.serviceKeyRepo.CreateServiceKey(serviceInstance.Guid, serviceKeyName, paramsMap)
switch err.(type) {
case nil:
Expand Down
4 changes: 3 additions & 1 deletion cf/commands/servicekey/create_service_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var _ = Describe("create-service-key command", func() {
var (
ui *testterm.FakeUI
config core_config.Repository
cmd CreateServiceKey
cmd *CreateServiceKey
requirementsFactory *testreq.FakeReqFactory
serviceRepo *testapi.FakeServiceRepo
serviceKeyRepo *testapi.FakeServiceKeyRepo
Expand All @@ -38,11 +38,13 @@ var _ = Describe("create-service-key command", func() {
serviceRepo = &testapi.FakeServiceRepo{}
serviceInstance := models.ServiceInstance{}
serviceInstance.Guid = "fake-instance-guid"
serviceInstance.Name = "fake-service-instance"
serviceRepo.FindInstanceByNameMap = generic.NewMap()
serviceRepo.FindInstanceByNameMap.Set("fake-service-instance", serviceInstance)
serviceKeyRepo = testapi.NewFakeServiceKeyRepo()
cmd = NewCreateServiceKey(ui, config, serviceRepo, serviceKeyRepo)
requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: true, ServiceInstanceNotFound: false}
requirementsFactory.ServiceInstance = serviceInstance
})

var callCreateService = func(args []string) bool {
Expand Down
13 changes: 6 additions & 7 deletions cf/commands/servicekey/delete_service_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ type DeleteServiceKey struct {
serviceKeyRepo api.ServiceKeyRepository
}

func NewDeleteServiceKey(ui terminal.UI, config core_config.Reader, serviceRepo api.ServiceRepository, serviceKeyRepo api.ServiceKeyRepository) (cmd DeleteServiceKey) {
return DeleteServiceKey{
func NewDeleteServiceKey(ui terminal.UI, config core_config.Reader, serviceRepo api.ServiceRepository, serviceKeyRepo api.ServiceKeyRepository) (cmd *DeleteServiceKey) {
return &DeleteServiceKey{
ui: ui,
config: config,
serviceRepo: serviceRepo,
serviceKeyRepo: serviceKeyRepo,
}
}

func (cmd DeleteServiceKey) Metadata() command_metadata.CommandMetadata {
func (cmd *DeleteServiceKey) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "delete-service-key",
ShortName: "dsk",
Expand All @@ -42,21 +42,20 @@ EXAMPLE:
}
}

func (cmd DeleteServiceKey) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
func (cmd *DeleteServiceKey) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
if len(c.Args()) != 2 {
cmd.ui.FailWithUsage(c)
}

loginRequirement := requirementsFactory.NewLoginRequirement()
serviceInstanceRequirement := requirementsFactory.NewServiceInstanceRequirement(c.Args()[0])
targetSpaceRequirement := requirementsFactory.NewTargetedSpaceRequirement()

reqs = []requirements.Requirement{loginRequirement, serviceInstanceRequirement, targetSpaceRequirement}
reqs = []requirements.Requirement{loginRequirement, targetSpaceRequirement}

return reqs, nil
}

func (cmd DeleteServiceKey) Run(c *cli.Context) {
func (cmd *DeleteServiceKey) Run(c *cli.Context) {
serviceInstanceName := c.Args()[0]
serviceKeyName := c.Args()[1]

Expand Down
15 changes: 5 additions & 10 deletions cf/commands/servicekey/delete_service_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var _ = Describe("delete-service-key command", func() {
var (
ui *testterm.FakeUI
config core_config.Repository
cmd DeleteServiceKey
cmd *DeleteServiceKey
requirementsFactory *testreq.FakeReqFactory
serviceRepo *testapi.FakeServiceRepo
serviceKeyRepo *testapi.FakeServiceKeyRepo
Expand All @@ -39,7 +39,7 @@ var _ = Describe("delete-service-key command", func() {
serviceRepo.FindInstanceByNameMap.Set("fake-service-instance", serviceInstance)
serviceKeyRepo = testapi.NewFakeServiceKeyRepo()
cmd = NewDeleteServiceKey(ui, config, serviceRepo, serviceKeyRepo)
requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: true, ServiceInstanceNotFound: false}
requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: true}
})

var callDeleteServiceKey = func(args []string) bool {
Expand All @@ -58,11 +58,6 @@ var _ = Describe("delete-service-key command", func() {
Expect(callDeleteServiceKey([]string{"fake-arg-one", "fake-arg-two", "fake-arg-three"})).To(BeFalse())
})

It("fails when service instance is not found", func() {
requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, ServiceInstanceNotFound: true}
Expect(callDeleteServiceKey([]string{"non-exist-service-instance"})).To(BeFalse())
})

It("fails when space is not targetted", func() {
requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: false}
Expect(callDeleteServiceKey([]string{"fake-service-instance", "fake-service-key"})).To(BeFalse())
Expand Down Expand Up @@ -92,7 +87,7 @@ var _ = Describe("delete-service-key command", func() {
})

It("deletes service key successfully when '-f' option is provided", func() {
requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, ServiceInstanceNotFound: false, TargetedSpaceSuccess: true}
requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: true}

Expect(callDeleteServiceKey([]string{"fake-service-instance", "fake-service-key", "-f"})).To(BeTrue())
Expect(ui.Outputs).To(ContainSubstrings(
Expand All @@ -101,7 +96,7 @@ var _ = Describe("delete-service-key command", func() {
})

It("deletes service key successfully when '-f' option is not provided and confirmed 'yes'", func() {
requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, ServiceInstanceNotFound: false, TargetedSpaceSuccess: true}
requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: true}
ui.Inputs = append(ui.Inputs, "yes")

Expect(callDeleteServiceKey([]string{"fake-service-instance", "fake-service-key"})).To(BeTrue())
Expand All @@ -112,7 +107,7 @@ var _ = Describe("delete-service-key command", func() {
})

It("skips to delete service key when '-f' option is not provided and confirmed 'no'", func() {
requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, ServiceInstanceNotFound: false, TargetedSpaceSuccess: true}
requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: true}
ui.Inputs = append(ui.Inputs, "no")

Expect(callDeleteServiceKey([]string{"fake-service-instance", "fake-service-key"})).To(BeTrue())
Expand Down
35 changes: 15 additions & 20 deletions cf/commands/servicekey/service_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,23 @@ import (
)

type ServiceKey struct {
ui terminal.UI
config core_config.Reader
serviceRepo api.ServiceRepository
serviceKeyRepo api.ServiceKeyRepository
ui terminal.UI
config core_config.Reader
serviceRepo api.ServiceRepository
serviceKeyRepo api.ServiceKeyRepository
serviceInstanceRequirement requirements.ServiceInstanceRequirement
}

func NewGetServiceKey(ui terminal.UI, config core_config.Reader, serviceRepo api.ServiceRepository, serviceKeyRepo api.ServiceKeyRepository) (cmd ServiceKey) {
return ServiceKey{
func NewGetServiceKey(ui terminal.UI, config core_config.Reader, serviceRepo api.ServiceRepository, serviceKeyRepo api.ServiceKeyRepository) (cmd *ServiceKey) {
return &ServiceKey{
ui: ui,
config: config,
serviceRepo: serviceRepo,
serviceKeyRepo: serviceKeyRepo,
}
}

func (cmd ServiceKey) Metadata() command_metadata.CommandMetadata {
func (cmd *ServiceKey) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "service-key",
Description: T("Show service key info"),
Expand All @@ -43,30 +44,24 @@ EXAMPLE:
}
}

func (cmd ServiceKey) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
func (cmd *ServiceKey) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
if len(c.Args()) != 2 {
cmd.ui.FailWithUsage(c)
}

loginRequirement := requirementsFactory.NewLoginRequirement()
serviceInstanceRequirement := requirementsFactory.NewServiceInstanceRequirement(c.Args()[0])
cmd.serviceInstanceRequirement = requirementsFactory.NewServiceInstanceRequirement(c.Args()[0])
targetSpaceRequirement := requirementsFactory.NewTargetedSpaceRequirement()

reqs = []requirements.Requirement{loginRequirement, serviceInstanceRequirement, targetSpaceRequirement}
reqs = []requirements.Requirement{loginRequirement, cmd.serviceInstanceRequirement, targetSpaceRequirement}

return reqs, nil
}

func (cmd ServiceKey) Run(c *cli.Context) {
serviceInstanceName := c.Args()[0]
func (cmd *ServiceKey) Run(c *cli.Context) {
serviceInstance := cmd.serviceInstanceRequirement.GetServiceInstance()
serviceKeyName := c.Args()[1]

serviceInstance, err := cmd.serviceRepo.FindInstanceByName(serviceInstanceName)
if err != nil {
cmd.ui.Failed(err.Error())
return
}

serviceKey, err := cmd.serviceKeyRepo.GetServiceKey(serviceInstance.Guid, serviceKeyName)
if err != nil {
cmd.ui.Failed(err.Error())
Expand All @@ -79,15 +74,15 @@ func (cmd ServiceKey) Run(c *cli.Context) {
cmd.ui.Say(T("Getting key {{.ServiceKeyName}} for service instance {{.ServiceInstanceName}} as {{.CurrentUser}}...",
map[string]interface{}{
"ServiceKeyName": terminal.EntityNameColor(serviceKeyName),
"ServiceInstanceName": terminal.EntityNameColor(serviceInstanceName),
"ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name),
"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
}))

if serviceKey.Fields.Name == "" {
cmd.ui.Say(T("No service key {{.ServiceKeyName}} found for service instance {{.ServiceInstanceName}}",
map[string]interface{}{
"ServiceKeyName": terminal.EntityNameColor(serviceKeyName),
"ServiceInstanceName": terminal.EntityNameColor(serviceInstanceName)}))
"ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name)}))
return
}

Expand Down
4 changes: 3 additions & 1 deletion cf/commands/servicekey/service_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var _ = Describe("service-key command", func() {
var (
ui *testterm.FakeUI
config core_config.Repository
cmd ServiceKey
cmd *ServiceKey
requirementsFactory *testreq.FakeReqFactory
serviceRepo *testapi.FakeServiceRepo
serviceKeyRepo *testapi.FakeServiceKeyRepo
Expand All @@ -34,11 +34,13 @@ var _ = Describe("service-key command", func() {
serviceRepo = &testapi.FakeServiceRepo{}
serviceInstance := models.ServiceInstance{}
serviceInstance.Guid = "fake-service-instance-guid"
serviceInstance.Name = "fake-service-instance"
serviceRepo.FindInstanceByNameMap = generic.NewMap()
serviceRepo.FindInstanceByNameMap.Set("fake-service-instance", serviceInstance)
serviceKeyRepo = testapi.NewFakeServiceKeyRepo()
cmd = NewGetServiceKey(ui, config, serviceRepo, serviceKeyRepo)
requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: true, ServiceInstanceNotFound: false}
requirementsFactory.ServiceInstance = serviceInstance
})

var callGetServiceKey = func(args []string) bool {
Expand Down
35 changes: 15 additions & 20 deletions cf/commands/servicekey/service_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@ import (
)

type ServiceKeys struct {
ui terminal.UI
config core_config.Reader
serviceRepo api.ServiceRepository
serviceKeyRepo api.ServiceKeyRepository
ui terminal.UI
config core_config.Reader
serviceRepo api.ServiceRepository
serviceKeyRepo api.ServiceKeyRepository
serviceInstanceRequirement requirements.ServiceInstanceRequirement
}

func NewListServiceKeys(ui terminal.UI, config core_config.Reader, serviceRepo api.ServiceRepository, serviceKeyRepo api.ServiceKeyRepository) (cmd ServiceKeys) {
return ServiceKeys{
func NewListServiceKeys(ui terminal.UI, config core_config.Reader, serviceRepo api.ServiceRepository, serviceKeyRepo api.ServiceKeyRepository) (cmd *ServiceKeys) {
return &ServiceKeys{
ui: ui,
config: config,
serviceRepo: serviceRepo,
serviceKeyRepo: serviceKeyRepo,
}
}

func (cmd ServiceKeys) Metadata() command_metadata.CommandMetadata {
func (cmd *ServiceKeys) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "service-keys",
ShortName: "sk",
Expand All @@ -39,35 +40,29 @@ EXAMPLE:
}
}

func (cmd ServiceKeys) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
func (cmd *ServiceKeys) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
if len(c.Args()) != 1 {
cmd.ui.FailWithUsage(c)
}

loginRequirement := requirementsFactory.NewLoginRequirement()
serviceInstanceRequirement := requirementsFactory.NewServiceInstanceRequirement(c.Args()[0])
cmd.serviceInstanceRequirement = requirementsFactory.NewServiceInstanceRequirement(c.Args()[0])
targetSpaceRequirement := requirementsFactory.NewTargetedSpaceRequirement()

reqs = []requirements.Requirement{loginRequirement, serviceInstanceRequirement, targetSpaceRequirement}
reqs = []requirements.Requirement{loginRequirement, cmd.serviceInstanceRequirement, targetSpaceRequirement}

return reqs, nil
}

func (cmd ServiceKeys) Run(c *cli.Context) {
serviceInstanceName := c.Args()[0]
func (cmd *ServiceKeys) Run(c *cli.Context) {
serviceInstance := cmd.serviceInstanceRequirement.GetServiceInstance()

cmd.ui.Say(T("Getting keys for service instance {{.ServiceInstanceName}} as {{.CurrentUser}}...",
map[string]interface{}{
"ServiceInstanceName": terminal.EntityNameColor(serviceInstanceName),
"ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name),
"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
}))

serviceInstance, err := cmd.serviceRepo.FindInstanceByName(serviceInstanceName)
if err != nil {
cmd.ui.Failed(err.Error())
return
}

serviceKeys, err := cmd.serviceKeyRepo.ListServiceKeys(serviceInstance.Guid)
if err != nil {
cmd.ui.Failed(err.Error())
Expand All @@ -82,7 +77,7 @@ func (cmd ServiceKeys) Run(c *cli.Context) {

if len(serviceKeys) == 0 {
cmd.ui.Say(T("No service key for service instance {{.ServiceInstanceName}}",
map[string]interface{}{"ServiceInstanceName": terminal.EntityNameColor(serviceInstanceName)}))
map[string]interface{}{"ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name)}))
return
} else {
cmd.ui.Say("")
Expand Down
Loading

0 comments on commit 76a82e9

Please sign in to comment.