Skip to content

Handles inconsistent JSON-encoding on query property. #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions gohelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ type Credential struct {
Type string `json:"type"`
Port int `json:"port"`
Hostname string `json:"hostname"`
Query struct {
IsMaster bool `json:"is_master"`
// Split for Credential.Query.IsMaster included to handle inconsistent
// JSON-encoding of `json:"query"`.
Query struct {
IsMaster bool `json:"-"`
}
RawQuery interface{} `json:"query"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be rawQuery so that it's not accessible outside this package. It's an internal implementation detail.

}

type Credentials map[string][]Credential
Expand Down Expand Up @@ -382,6 +385,17 @@ func extractCredentials(relationships string) (Credentials, error) {
return nil, err
}

// Address inconsistent JSON-encoding of `json:"query"`
for k, _ := range rels {
// Convert to map so interface keys are accessible
if mappedRawQuery, ok := rels[k][0].RawQuery.(map[string]interface{}); ok {
if val, ok := mappedRawQuery["is_master"]; ok {
// Handle is_master: bool present case
rels[k][0].Query.IsMaster = val.(bool)
}
}
}

return rels, nil
}

Expand Down
40 changes: 40 additions & 0 deletions gohelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,46 @@ func TestCredentialsForMissingRelationshipErrrors(t *testing.T) {
}
}

func TestCredentialsForRelationshipPropertyQueryStructDefinedReturns(t *testing.T) {
config, err := psh.NewRuntimeConfigReal(helper.RuntimeEnv(psh.EnvList{}), "PLATFORM_")
helper.Ok(t, err)

creds, err := config.Credentials("database")
helper.Ok(t, err)

helper.Equals(t, true, creds.Query.IsMaster)
}

func TestCredentialsForRelationshipPropertyQueryAbsentReturns(t *testing.T) {
config, err := psh.NewRuntimeConfigReal(helper.RuntimeEnv(psh.EnvList{}), "PLATFORM_")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if these tests would be more valid if they took an existing config and mutated it, rather than having them lying around as discrete test data? I'm honestly unsure here, nor do I know how much work that would be.

helper.Ok(t, err)

creds, err := config.Credentials("influxdb")
helper.Ok(t, err)

helper.Equals(t, false, creds.Query.IsMaster)
}

func TestCredentialsForRelationshipPropertyQueryDefinedAsEmptyArrayReturns(t *testing.T) {
config, err := psh.NewRuntimeConfigReal(helper.RuntimeEnv(psh.EnvList{}), "PLATFORM_")
helper.Ok(t, err)

creds, err := config.Credentials("redis")
helper.Ok(t, err)

helper.Equals(t, false, creds.Query.IsMaster)
}

func TestCredentialsForRelationshipPropertyQueryDefinedAsEmptyObjectReturns(t *testing.T) {
config, err := psh.NewRuntimeConfigReal(helper.RuntimeEnv(psh.EnvList{}), "PLATFORM_")
helper.Ok(t, err)

creds, err := config.Credentials("elasticsearch")
helper.Ok(t, err)

helper.Equals(t, false, creds.Query.IsMaster)
}

func TestGetAllRoutesAtRuntimeWorks(t *testing.T) {
config, err := psh.NewRuntimeConfigReal(helper.RuntimeEnv(psh.EnvList{}), "PLATFORM_")
helper.Ok(t, err)
Expand Down
116 changes: 76 additions & 40 deletions testdata/PLATFORM_RELATIONSHIPS.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,78 @@
{
"database" : [
{
"scheme" : "mysql",
"cluster" : "dtsla3sy7euhc-master-7rqtwti",
"service" : "mysql",
"username" : "user",
"password" : "",
"host" : "database.internal",
"path" : "main",
"public" : false,
"fragment" : null,
"ip" : "169.254.81.252",
"query" : {
"is_master" : true
},
"rel" : "mysql",
"type" : "mysql:10.2",
"port" : 3306,
"hostname" : "ihq65cmi2m7nd3svqpcrbjchyy.mysql.service._.us-2.platformsh.site"
}
],
"elasticsearch" : [
{
"hostname" : "bwhgfsnjp7kzqlf7pd35dfg6mm.elasticsearch.service._.us-2.platformsh.site",
"port" : 9200,
"type" : "elasticsearch:5.4",
"rel" : "elasticsearch",
"query" : {},
"ip" : "169.254.250.214",
"path" : null,
"public" : false,
"fragment" : null,
"password" : null,
"host" : "elasticsearch.internal",
"username" : null,
"service" : "elasticsearch",
"cluster" : "dtsla3sy7euhc-master-7rqtwti",
"scheme" : "http"
}
]
"database": [
{
"scheme": "mysql",
"cluster": "dtsla3sy7euhc-master-7rqtwti",
"service": "mysql",
"username": "user",
"password": "",
"host": "database.internal",
"path": "main",
"public": false,
"fragment": null,
"ip": "169.254.81.252",
"query": {
"is_master": true
},
"rel": "mysql",
"type": "mysql:10.2",
"port": 3306,
"hostname": "ihq65cmi2m7nd3svqpcrbjchyy.mysql.service._.us-2.platformsh.site"
}
],
"influxdb": [
{
"service": "influxdb",
"ip": "169.254.70.27",
"hostname": "haz5rys6n2dsnjusqa54os3ii4.influxdb.service._.eu-3.platformsh.site",
"cluster": "rjify4yjcwxaa-master-7rqtwti",
"host": "influxdb.internal",
"rel": "influxdb",
"scheme": "http",
"type": "influxdb:1.3",
"port": 8086
}
],
"redis": [
{
"username": null,
"scheme": "redis",
"service": "redis",
"fragment": null,
"ip": "169.254.39.193",
"hostname": "2wye4dawv22vhvozyec3o5hxfm.redis.service._.eu-3.platformsh.site",
"public": false,
"cluster": "rjify4yjcwxaa-master-7rqtwti",
"host": "redis.internal",
"rel": "redis",
"query": [

],
"path": null,
"password": null,
"type": "redis:5.0",
"port": 6379
}
],
"elasticsearch" : [
{
"username": null,
"scheme": "http",
"service": "elasticsearch",
"fragment": null,
"ip": "169.254.49.124",
"hostname": "j2dkzht3gs2yr66fb2brhoj4zu.elasticsearch.service._.eu-3.platformsh.site",
"public": false,
"cluster": "rjify4yjcwxaa-master-7rqtwti",
"host": "elasticsearch.internal",
"rel": "elasticsearch",
"query": {

},
"path": null,
"password": null,
"type": "elasticsearch:5.4",
"port": 9200
}
]
}