Skip to content

Commit

Permalink
Merge pull request #155 from tucksaun/fix/154
Browse files Browse the repository at this point in the history
Append a trailing slash to Elasticsearch URL
  • Loading branch information
fabpot committed Jul 7, 2022
2 parents 35035d3 + 77e4e0f commit a3c1c28
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions envs/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ func (l *Local) dockerServiceToRelationship(client *docker.Client, container typ
"host": host,
"ip": host,
"port": formatDockerPort(p.PublicPort),
"path": "/",
"rel": "elasticsearch",
"scheme": "http",
}
Expand Down
9 changes: 8 additions & 1 deletion envs/envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@ func extractRelationshipsEnvs(env Environment) Envs {
values[fmt.Sprintf("%sNAME", prefix)] = endpoint["path"].(string)
values[fmt.Sprintf("%sDATABASE", prefix)] = endpoint["path"].(string)
} else if rel == "elasticsearch" {
values[fmt.Sprintf("%sURL", prefix)] = fmt.Sprintf("%s://%s:%s", endpoint["scheme"].(string), endpoint["host"].(string), formatInt(endpoint["port"]))
path, hasPath := endpoint["path"]
if !hasPath || path == nil {
path = ""
}
values[fmt.Sprintf("%sURL", prefix)] = fmt.Sprintf("%s://%s:%s%s", endpoint["scheme"].(string), endpoint["host"].(string), formatInt(endpoint["port"]), path)
values[fmt.Sprintf("%sHOST", prefix)] = endpoint["host"].(string)
values[fmt.Sprintf("%sPORT", prefix)] = formatInt(endpoint["port"])
values[fmt.Sprintf("%sSCHEME", prefix)] = endpoint["scheme"].(string)
Expand Down Expand Up @@ -318,6 +322,9 @@ func formatInt(val interface{}) string {
if s, ok := val.(string); ok {
return s
}
if i, ok := val.(int); ok {
return strconv.Itoa(i)
}
return strconv.FormatInt(int64(val.(float64)), 10)
}

Expand Down
57 changes: 57 additions & 0 deletions envs/envs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,60 @@ func (s *ScenvSuite) TestAppID(c *C) {
c.Assert(appID("testdata/project_without_composer"), Equals, "")
c.Assert(appID("testdata/project_with_borked_composer"), Equals, "")
}

type fakeEnv struct {
Rels Relationships
}

func (f fakeEnv) Path() string {
return "/dev/null"
}

func (f fakeEnv) Mailer() Envs {
return nil
}

func (f fakeEnv) Language() string {
return "php"
}

func (f fakeEnv) Relationships() Relationships {
return f.Rels
}

func (f fakeEnv) Extra() Envs {
return nil
}

func (f fakeEnv) Local() bool {
return true
}

func (s *ScenvSuite) TestElasticsearchURLEndsWithTrailingSlash(c *C) {
env := fakeEnv{
Rels: map[string][]map[string]interface{}{
"elasticsearch": {
map[string]interface{}{
"host": "localhost",
"ip": "localhost",
"port": 9200,
"path": "/",
"rel": "elasticsearch",
"scheme": "http",
},
},
},
}

rels := extractRelationshipsEnvs(env)
c.Assert(rels["ELASTICSEARCH_URL"], Equals, "http://localhost:9200/")

// We want to stay backward compatible with Platform.sh/SymfonyCloud
env.Rels["elasticsearch"][0]["path"] = nil
rels = extractRelationshipsEnvs(env)
c.Assert(rels["ELASTICSEARCH_URL"], Equals, "http://localhost:9200")

delete(env.Rels["elasticsearch"][0], "path")
rels = extractRelationshipsEnvs(env)
c.Assert(rels["ELASTICSEARCH_URL"], Equals, "http://localhost:9200")
}

0 comments on commit a3c1c28

Please sign in to comment.