Skip to content

Commit

Permalink
Merge pull request #441 from symfony-cli/vhost-amqp
Browse files Browse the repository at this point in the history
Add vhost to the RabbitMQ URL when available
  • Loading branch information
fabpot committed Feb 15, 2024
2 parents 0ae097d + b0736f0 commit 2f90d57
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
9 changes: 7 additions & 2 deletions envs/envs.go
Expand Up @@ -251,8 +251,13 @@ func extractRelationshipsEnvs(env Environment) Envs {
values[fmt.Sprintf("%sUSERNAME", prefix)] = endpoint["username"].(string)
values[fmt.Sprintf("%sPASSWORD", prefix)] = endpoint["password"].(string)
} else if scheme == "amqp" {
values[fmt.Sprintf("%sURL", prefix)] = fmt.Sprintf("%s://%s:%s@%s:%s", endpoint["scheme"].(string), endpoint["username"].(string), endpoint["password"].(string), endpoint["host"].(string), formatInt(endpoint["port"]))
values[fmt.Sprintf("%sDSN", prefix)] = fmt.Sprintf("%s://%s:%s@%s:%s", endpoint["scheme"].(string), endpoint["username"].(string), endpoint["password"].(string), endpoint["host"].(string), formatInt(endpoint["port"]))
vhost := ""
if v, ok := endpoint["vhost"]; ok && v != nil {
values[fmt.Sprintf("%sVHOST", prefix)] = endpoint["vhost"].(string)
vhost = "/" + endpoint["vhost"].(string)
}
values[fmt.Sprintf("%sURL", prefix)] = fmt.Sprintf("%s://%s:%s@%s:%s%s", endpoint["scheme"].(string), endpoint["username"].(string), endpoint["password"].(string), endpoint["host"].(string), formatInt(endpoint["port"]), vhost)
values[fmt.Sprintf("%sDSN", prefix)] = fmt.Sprintf("%s://%s:%s@%s:%s%s", endpoint["scheme"].(string), endpoint["username"].(string), endpoint["password"].(string), endpoint["host"].(string), formatInt(endpoint["port"]), vhost)
values[fmt.Sprintf("%sSERVER", prefix)] = formatServer(endpoint)
values[fmt.Sprintf("%sHOST", prefix)] = endpoint["host"].(string)
values[fmt.Sprintf("%sPORT", prefix)] = formatInt(endpoint["port"])
Expand Down
39 changes: 37 additions & 2 deletions envs/remote_test.go
Expand Up @@ -404,6 +404,8 @@ func (s *RemoteSuite) TestRelationships(c *C) {
"DATABASE_DATABASE": "main",
"DATABASE_NAME": "main",
})
os.Unsetenv("DATABASE_VERSION")
os.Unsetenv("DATABASE_CHARSET")
}

func (s *RemoteSuite) TestMySQLReadReplicaForDedicated(c *C) {
Expand All @@ -422,7 +424,7 @@ func (s *RemoteSuite) TestMySQLReadReplicaForDedicated(c *C) {
c.Assert("mysql://mysql:xxx@db.internal:3306/main?sslmode=disable&charset=utf8mb4&serverVersion=10.6.0-MariaDB", DeepEquals, e["DB_URL"])
}

func (s *RemoteSuite) TestSomeBugForDedicated(c *C) {
func (s *RemoteSuite) TestNoIPsForDedicated(c *C) {
r := &Remote{}
value, err := os.ReadFile("testdata/dedicated/no_ips_for_dedicated.json")
if err != nil {
Expand All @@ -433,5 +435,38 @@ func (s *RemoteSuite) TestSomeBugForDedicated(c *C) {
}

rels := extractRelationshipsEnvs(r)
c.Assert(rels, DeepEquals, Envs{"DATABASE_DATABASE": "x_stg", "DATABASE_DRIVER": "mysql", "DATABASE_HOST": "127.0.0.1", "DATABASE_NAME": "x_stg", "DATABASE_PASSWORD": "x", "DATABASE_PORT": "3306", "DATABASE_SERVER": "mysql://127.0.0.1:3306", "DATABASE_URL": "mysql://xstg:x@127.0.0.1:3306/x_stg?sslmode=disable&charset=utf8", "DATABASE_USER": "xstg", "DATABASE_USERNAME": "xstg", "RABBITMQ_DSN": "amqp://x_stg:x@localhost:5672", "RABBITMQ_HOST": "localhost", "RABBITMQ_MANAGEMENT_HOST": "localhost", "RABBITMQ_MANAGEMENT_PASSWORD": "x", "RABBITMQ_MANAGEMENT_PORT": "15672", "RABBITMQ_MANAGEMENT_SCHEME": "http", "RABBITMQ_MANAGEMENT_SERVER": "http://localhost:15672", "RABBITMQ_MANAGEMENT_URL": "http://x_stg:x@localhost:15672", "RABBITMQ_MANAGEMENT_USER": "x_stg", "RABBITMQ_MANAGEMENT_USERNAME": "x_stg", "RABBITMQ_PASSWORD": "x", "RABBITMQ_PORT": "5672", "RABBITMQ_SCHEME": "amqp", "RABBITMQ_SERVER": "amqp://localhost:5672", "RABBITMQ_URL": "amqp://x_stg:x@localhost:5672", "RABBITMQ_USER": "x_stg", "RABBITMQ_USERNAME": "x_stg", "REDISCACHE_HOST": "localhost", "REDISCACHE_PORT": "6379", "REDISCACHE_SCHEME": "redis", "REDISCACHE_URL": "redis://localhost:6379"})
c.Assert(rels, DeepEquals, Envs{
"DATABASE_DATABASE": "x_stg",
"DATABASE_DRIVER": "mysql",
"DATABASE_HOST": "127.0.0.1",
"DATABASE_NAME": "x_stg",
"DATABASE_PASSWORD": "x",
"DATABASE_PORT": "3306",
"DATABASE_SERVER": "mysql://127.0.0.1:3306",
"DATABASE_URL": "mysql://xstg:x@127.0.0.1:3306/x_stg?sslmode=disable&charset=utf8mb4",
"DATABASE_USER": "xstg",
"DATABASE_USERNAME": "xstg",
"RABBITMQ_VHOST": "x_stg",
"RABBITMQ_DSN": "amqp://x_stg:x@localhost:5672/x_stg",
"RABBITMQ_HOST": "localhost",
"RABBITMQ_MANAGEMENT_HOST": "localhost",
"RABBITMQ_MANAGEMENT_PASSWORD": "x",
"RABBITMQ_MANAGEMENT_PORT": "15672",
"RABBITMQ_MANAGEMENT_SCHEME": "http",
"RABBITMQ_MANAGEMENT_SERVER": "http://localhost:15672",
"RABBITMQ_MANAGEMENT_URL": "http://x_stg:x@localhost:15672",
"RABBITMQ_MANAGEMENT_USER": "x_stg",
"RABBITMQ_MANAGEMENT_USERNAME": "x_stg",
"RABBITMQ_PASSWORD": "x",
"RABBITMQ_PORT": "5672",
"RABBITMQ_SCHEME": "amqp",
"RABBITMQ_SERVER": "amqp://localhost:5672",
"RABBITMQ_URL": "amqp://x_stg:x@localhost:5672/x_stg",
"RABBITMQ_USER": "x_stg",
"RABBITMQ_USERNAME": "x_stg",
"REDISCACHE_HOST": "localhost",
"REDISCACHE_PORT": "6379",
"REDISCACHE_SCHEME": "redis",
"REDISCACHE_URL": "redis://localhost:6379",
})
}

0 comments on commit 2f90d57

Please sign in to comment.