From b62ce5758e6ab8d171e13aa77c8ffe10f59956c4 Mon Sep 17 00:00:00 2001 From: Simon Johansson Date: Thu, 1 Dec 2016 17:13:04 +0100 Subject: [PATCH] JSON: allow port to be provided as int or string Signed-off-by: Andreas Fleig --- cfmysql/cf_api_client.go | 22 ++++++++++++++- cfmysql/cf_api_client_test.go | 4 ++- cfmysql/plugin.go | 2 +- cfmysql/plugin_test.go | 2 +- cfmysql/resources/resources.go | 8 ++++-- cfmysql/resources/resources_test.go | 13 +++++++-- cfmysql/test_resources/service_bindings.json | 29 +++++++++++++++++++- 7 files changed, 71 insertions(+), 9 deletions(-) diff --git a/cfmysql/cf_api_client.go b/cfmysql/cf_api_client.go index 77a5d97..aae9cc1 100644 --- a/cfmysql/cf_api_client.go +++ b/cfmysql/cf_api_client.go @@ -8,6 +8,7 @@ import ( "encoding/json" pluginResources "github.com/andreasf/cf-mysql-plugin/cfmysql/resources" . "code.cloudfoundry.org/cli/plugin/models" + "strconv" ) //go:generate counterfeiter . ApiClient @@ -60,7 +61,7 @@ func (self *SdkApiClient) GetMysqlServices(cliConnection plugin.CliConnection) ( bindingResult := <- bindingChan if bindingResult.Err != nil { - return nil, err + return nil, bindingResult.Err } return getAvailableServices(bindingResult.Bindings, instances), nil @@ -97,6 +98,25 @@ func deserializeBindings(bindingResponse []byte) (*pluginResources.PaginatedServ return nil, fmt.Errorf("Unable to deserialize service bindings: %s", err) } + // port might be int or string, we don't know upfront. + // use index because range would create copies + for i := range paginatedResources.Resources { + credentials := &paginatedResources.Resources[i].Entity.Credentials + + var portInt int + var portString string + + err = json.Unmarshal(credentials.RawPort, &portString) + if err != nil { + err = json.Unmarshal(credentials.RawPort, &portInt) + if err != nil { + return nil, err + } + portString = strconv.Itoa(portInt) + } + credentials.Port = portString + } + return paginatedResources, nil } diff --git a/cfmysql/cf_api_client_test.go b/cfmysql/cf_api_client_test.go index 0dfe53d..41c1c07 100644 --- a/cfmysql/cf_api_client_test.go +++ b/cfmysql/cf_api_client_test.go @@ -52,7 +52,9 @@ var _ = Describe("CfSdkClient", func() { paginatedResources, err := apiClient.GetServiceBindings(cliConnection) Expect(err).To(BeNil()) - Expect(paginatedResources.Resources).To(HaveLen(3)) + Expect(paginatedResources.Resources).To(HaveLen(4)) + Expect(paginatedResources.Resources[0].Entity.Credentials.Port).To(Equal("3306")) + Expect(paginatedResources.Resources[3].Entity.Credentials.Port).To(Equal("54321")) Expect(cliConnection.AccessTokenCallCount()).To(Equal(1)) Expect(cliConnection.ApiEndpointCallCount()).To(Equal(1)) diff --git a/cfmysql/plugin.go b/cfmysql/plugin.go index 307f47f..4d56332 100644 --- a/cfmysql/plugin.go +++ b/cfmysql/plugin.go @@ -24,7 +24,7 @@ func (self *MysqlPlugin) GetMetadata() plugin.PluginMetadata { Version: plugin.VersionType{ Major: 1, Minor: 3, - Build: 1, + Build: 2, }, MinCliVersion: plugin.VersionType{ Major: 6, diff --git a/cfmysql/plugin_test.go b/cfmysql/plugin_test.go index 4101dc3..fd6bdf1 100644 --- a/cfmysql/plugin_test.go +++ b/cfmysql/plugin_test.go @@ -21,7 +21,7 @@ var _ = Describe("Plugin", func() { Expect(mysqlPlugin.GetMetadata().Version).To(Equal(plugin.VersionType{ Major: 1, Minor: 3, - Build: 1, + Build: 2, })) }) }) diff --git a/cfmysql/resources/resources.go b/cfmysql/resources/resources.go index db54cb8..ac8cace 100644 --- a/cfmysql/resources/resources.go +++ b/cfmysql/resources/resources.go @@ -1,6 +1,9 @@ package resources -import "code.cloudfoundry.org/cli/cf/api/resources" +import ( + "code.cloudfoundry.org/cli/cf/api/resources" + "encoding/json" +) type PaginatedServiceBindingResources struct { TotalResults int `json:"total_results"` @@ -22,7 +25,8 @@ type MysqlCredentials struct { Uri string `json:"uri"` DbName string `json:"name"` Hostname string `json:"hostname"` - Port string `json:"port"` + Port string + RawPort json.RawMessage `json:"port"` Username string `json:"username"` Password string `json:"password"` } diff --git a/cfmysql/resources/resources_test.go b/cfmysql/resources/resources_test.go index 5159289..9fd938f 100644 --- a/cfmysql/resources/resources_test.go +++ b/cfmysql/resources/resources_test.go @@ -35,17 +35,26 @@ var _ = Describe("Resources", func() { err := json.Unmarshal(test_resources.LoadResource("../test_resources/service_bindings.json"), paginatedResources) Expect(err).To(BeNil()) - Expect(paginatedResources.Resources).To(HaveLen(3)) + Expect(paginatedResources.Resources).To(HaveLen(4)) Expect(paginatedResources.Resources[0].Entity.ServiceInstanceGUID).To(Equal("service-instance-guid-a")) Expect(paginatedResources.Resources[0].Entity.Credentials.Uri).To(Equal("mysql://username-a:password-a@database-a.host:3306/dbname-a?reconnect=true")) Expect(paginatedResources.Resources[0].Entity.Credentials.DbName).To(Equal("dbname-a")) Expect(paginatedResources.Resources[0].Entity.Credentials.Hostname).To(Equal("database-a.host")) - Expect(paginatedResources.Resources[0].Entity.Credentials.Port).To(Equal("3306")) Expect(paginatedResources.Resources[0].Entity.Credentials.Username).To(Equal("username-a")) Expect(paginatedResources.Resources[0].Entity.Credentials.Password).To(Equal("password-a")) Expect(paginatedResources.Resources[1].Entity.ServiceInstanceGUID).To(Equal("service-instance-guid-b")) + + var portString string + err = json.Unmarshal(paginatedResources.Resources[0].Entity.Credentials.RawPort, &portString) + Expect(err).To(BeNil()) + Expect(portString).To(Equal("3306")) + + var portInt int + err = json.Unmarshal(paginatedResources.Resources[3].Entity.Credentials.RawPort, &portInt) + Expect(err).To(BeNil()) + Expect(portInt).To(Equal(54321)) }) }) }) diff --git a/cfmysql/test_resources/service_bindings.json b/cfmysql/test_resources/service_bindings.json index 1bdd8f5..ea20845 100644 --- a/cfmysql/test_resources/service_bindings.json +++ b/cfmysql/test_resources/service_bindings.json @@ -83,7 +83,34 @@ "app_url": "/v2/apps/app-guid-b", "service_instance_url": "/v2/service_instances/service-instance-guid-d" } + }, + { + "metadata": { + "guid": "guid-service-binding-e", + "url": "/v2/service_bindings/guid-service-binding-e", + "created_at": "2016-11-15T09:22:15Z", + "updated_at": null + }, + "entity": { + "app_guid": "app-guid-e", + "service_instance_guid": "service-instance-guid-e", + "credentials": { + "jdbcUrl": "jdbc:mysql://database-e.host/dbname-e?user=username-e&password=password-e", + "uri": "mysql://username-e:password-e@database-e.host:3306/dbname-e?reconnect=true", + "name": "dbname-e", + "hostname": "database-e.host", + "port": 54321, + "username": "username-e", + "password": "password-e" + }, + "binding_options": {}, + "gateway_data": null, + "gateway_name": "", + "syslog_drain_url": null, + "volume_mounts": [], + "app_url": "/v2/apps/app-guid-e", + "service_instance_url": "/v2/service_instances/service-instance-guid-b" + } } - ] }