Skip to content

Commit

Permalink
JSON: allow port to be provided as int or string
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Fleig <[email protected]>
  • Loading branch information
Simon Johansson authored and andreasf committed Dec 1, 2016
1 parent 26d607a commit b62ce57
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 9 deletions.
22 changes: 21 additions & 1 deletion cfmysql/cf_api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 3 additions & 1 deletion cfmysql/cf_api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion cfmysql/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cfmysql/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var _ = Describe("Plugin", func() {
Expect(mysqlPlugin.GetMetadata().Version).To(Equal(plugin.VersionType{
Major: 1,
Minor: 3,
Build: 1,
Build: 2,
}))
})
})
Expand Down
8 changes: 6 additions & 2 deletions cfmysql/resources/resources.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand All @@ -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"`
}
13 changes: 11 additions & 2 deletions cfmysql/resources/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]: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))
})
})
})
Expand Down
29 changes: 28 additions & 1 deletion cfmysql/test_resources/service_bindings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]: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"
}
}

]
}

0 comments on commit b62ce57

Please sign in to comment.