Skip to content

Commit

Permalink
Json is interpreted properly when create/updating user service instance.
Browse files Browse the repository at this point in the history
Fixes issue cloudfoundry#193
[Finishes #73971288]
  • Loading branch information
Anand Gaitonde and Christopher Piraino authored and Anand Gaitonde and Christopher Piraino committed Jul 10, 2014
1 parent 57c77b9 commit 4302431
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 22 deletions.
4 changes: 2 additions & 2 deletions cf/api/fakes/fake_user_provided_service_instance_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import "github.com/cloudfoundry/cli/cf/models"
type FakeUserProvidedServiceInstanceRepo struct {
CreateName string
CreateDrainUrl string
CreateParams map[string]string
CreateParams map[string]interface{}

UpdateServiceInstance models.ServiceInstanceFields
}

func (repo *FakeUserProvidedServiceInstanceRepo) Create(name, drainUrl string, params map[string]string) (apiErr error) {
func (repo *FakeUserProvidedServiceInstanceRepo) Create(name, drainUrl string, params map[string]interface{}) (apiErr error) {
repo.CreateName = name
repo.CreateDrainUrl = drainUrl
repo.CreateParams = params
Expand Down
17 changes: 9 additions & 8 deletions cf/api/user_provided_service_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"bytes"
"encoding/json"
"fmt"

"github.com/cloudfoundry/cli/cf/configuration"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/net"
)

type UserProvidedServiceInstanceRepository interface {
Create(name, drainUrl string, params map[string]string) (apiErr error)
Create(name, drainUrl string, params map[string]interface{}) (apiErr error)
Update(serviceInstanceFields models.ServiceInstanceFields) (apiErr error)
}

Expand All @@ -26,14 +27,14 @@ func NewCCUserProvidedServiceInstanceRepository(config configuration.Reader, gat
return
}

func (repo CCUserProvidedServiceInstanceRepository) Create(name, drainUrl string, params map[string]string) (apiErr error) {
func (repo CCUserProvidedServiceInstanceRepository) Create(name, drainUrl string, params map[string]interface{}) (apiErr error) {
path := fmt.Sprintf("%s/v2/user_provided_service_instances", repo.config.ApiEndpoint())

type RequestBody struct {
Name string `json:"name"`
Credentials map[string]string `json:"credentials"`
SpaceGuid string `json:"space_guid"`
SysLogDrainUrl string `json:"syslog_drain_url"`
Name string `json:"name"`
Credentials map[string]interface{} `json:"credentials"`
SpaceGuid string `json:"space_guid"`
SysLogDrainUrl string `json:"syslog_drain_url"`
}

jsonBytes, err := json.Marshal(RequestBody{
Expand All @@ -55,8 +56,8 @@ func (repo CCUserProvidedServiceInstanceRepository) Update(serviceInstanceFields
path := fmt.Sprintf("%s/v2/user_provided_service_instances/%s", repo.config.ApiEndpoint(), serviceInstanceFields.Guid)

type RequestBody struct {
Credentials map[string]string `json:"credentials,omitempty"`
SysLogDrainUrl string `json:"syslog_drain_url,omitempty"`
Credentials map[string]interface{} `json:"credentials,omitempty"`
SysLogDrainUrl string `json:"syslog_drain_url,omitempty"`
}

reqBody := RequestBody{serviceInstanceFields.Params, serviceInstanceFields.SysLogDrainUrl}
Expand Down
6 changes: 3 additions & 3 deletions cf/api/user_provided_service_instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var _ = Describe("UserProvidedServiceRepository", func() {
ts, handler, repo := createUserProvidedServiceInstanceRepo(req)
defer ts.Close()

apiErr := repo.Create("my-custom-service", "", map[string]string{
apiErr := repo.Create("my-custom-service", "", map[string]interface{}{
"host": "example.com",
"user": "me",
"password": "secret",
Expand All @@ -49,7 +49,7 @@ var _ = Describe("UserProvidedServiceRepository", func() {
ts, handler, repo := createUserProvidedServiceInstanceRepo(req)
defer ts.Close()

apiErr := repo.Create("my-custom-service", "syslog://example.com", map[string]string{
apiErr := repo.Create("my-custom-service", "syslog://example.com", map[string]interface{}{
"host": "example.com",
"user": "me",
"password": "secret",
Expand All @@ -69,7 +69,7 @@ var _ = Describe("UserProvidedServiceRepository", func() {
ts, handler, repo := createUserProvidedServiceInstanceRepo(req)
defer ts.Close()

params := map[string]string{
params := map[string]interface{}{
"host": "example.com",
"user": "me",
"password": "secret",
Expand Down
4 changes: 2 additions & 2 deletions cf/commands/service/create_user_provided_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (cmd CreateUserProvidedService) Run(c *cli.Context) {

params := c.String("p")
params = strings.Trim(params, `"`)
paramsMap := make(map[string]string)
paramsMap := make(map[string]interface{})

err := json.Unmarshal([]byte(params), &paramsMap)
if err != nil && params != "" {
Expand All @@ -90,7 +90,7 @@ func (cmd CreateUserProvidedService) Run(c *cli.Context) {
cmd.ui.Ok()
}

func (cmd CreateUserProvidedService) mapValuesFromPrompt(params string, paramsMap map[string]string) map[string]string {
func (cmd CreateUserProvidedService) mapValuesFromPrompt(params string, paramsMap map[string]interface{}) map[string]interface{} {
for _, param := range strings.Split(params, ",") {
param = strings.Trim(param, " ")
paramsMap[param] = cmd.ui.Ask("%s", param)
Expand Down
8 changes: 4 additions & 4 deletions cf/commands/service/create_user_provided_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var _ = Describe("create-user-provided-service command", func() {
))

Expect(repo.CreateName).To(Equal("my-custom-service"))
Expect(repo.CreateParams).To(Equal(map[string]string{
Expect(repo.CreateParams).To(Equal(map[string]interface{}{
"foo": "foo value",
"bar": "bar value",
"baz": "baz value",
Expand All @@ -71,15 +71,15 @@ var _ = Describe("create-user-provided-service command", func() {
})

It("accepts service parameters as JSON without prompting", func() {
args := []string{"-p", `{"foo": "foo value", "bar": "bar value", "baz": "baz value"}`, "my-custom-service"}
args := []string{"-p", `{"foo": "foo value", "bar": "bar value", "baz": 4}`, "my-custom-service"}
testcmd.RunCommand(cmd, args, requirementsFactory)

Expect(ui.Prompts).To(BeEmpty())
Expect(repo.CreateName).To(Equal("my-custom-service"))
Expect(repo.CreateParams).To(Equal(map[string]string{
Expect(repo.CreateParams).To(Equal(map[string]interface{}{
"foo": "foo value",
"bar": "bar value",
"baz": "baz value",
"baz": float64(4),
}))

Expect(ui.Outputs).To(ContainSubstrings(
Expand Down
2 changes: 1 addition & 1 deletion cf/commands/service/update_user_provided_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (cmd *UpdateUserProvidedService) Run(c *cli.Context) {
drainUrl := c.String("l")
params := c.String("p")

paramsMap := make(map[string]string)
paramsMap := make(map[string]interface{})
if params != "" {

err := json.Unmarshal([]byte(params), &paramsMap)
Expand Down
2 changes: 1 addition & 1 deletion cf/commands/service/update_user_provided_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var _ = Describe("update-user-provided-service test", func() {
[]string{"TIP"},
))
Expect(serviceRepo.UpdateServiceInstance.Name).To(Equal("service-name"))
Expect(serviceRepo.UpdateServiceInstance.Params).To(Equal(map[string]string{"foo": "bar"}))
Expect(serviceRepo.UpdateServiceInstance.Params).To(Equal(map[string]interface{}{"foo": "bar"}))
Expect(serviceRepo.UpdateServiceInstance.SysLogDrainUrl).To(Equal("syslog://example.com"))
})
})
Expand Down
2 changes: 1 addition & 1 deletion cf/models/service_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type ServiceInstanceFields struct {
Name string
SysLogDrainUrl string
ApplicationNames []string
Params map[string]string
Params map[string]interface{}
}

type ServiceInstance struct {
Expand Down

0 comments on commit 4302431

Please sign in to comment.