Skip to content

Commit

Permalink
Merge pull request #440 from symfony-cli/ip-on-dedicated-fix
Browse files Browse the repository at this point in the history
Fix env vars when IP is not available
  • Loading branch information
fabpot committed Feb 15, 2024
2 parents 57dc589 + a468b49 commit 0ae097d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
24 changes: 18 additions & 6 deletions envs/envs.go
Expand Up @@ -263,21 +263,29 @@ func extractRelationshipsEnvs(env Environment) Envs {
} else if scheme == "memcached" {
values[fmt.Sprintf("%sHOST", prefix)] = endpoint["host"].(string)
values[fmt.Sprintf("%sPORT", prefix)] = formatInt(endpoint["port"])
values[fmt.Sprintf("%sIP", prefix)] = endpoint["ip"].(string)
if v, ok := endpoint["ip"]; ok && v != nil {
values[fmt.Sprintf("%sIP", prefix)] = v.(string)
}
} else if rel == "influxdb" {
values[fmt.Sprintf("%sSCHEME", prefix)] = endpoint["scheme"].(string)
values[fmt.Sprintf("%sHOST", prefix)] = endpoint["host"].(string)
values[fmt.Sprintf("%sPORT", prefix)] = formatInt(endpoint["port"])
values[fmt.Sprintf("%sIP", prefix)] = endpoint["ip"].(string)
if v, ok := endpoint["ip"]; ok && v != nil {
values[fmt.Sprintf("%sIP", prefix)] = v.(string)
}
} else if scheme == "kafka" {
values[fmt.Sprintf("%sURL", prefix)] = fmt.Sprintf("%s://%s:%s", endpoint["scheme"].(string), endpoint["host"].(string), formatInt(endpoint["port"]))
values[fmt.Sprintf("%sSCHEME", prefix)] = endpoint["scheme"].(string)
values[fmt.Sprintf("%sHOST", prefix)] = endpoint["host"].(string)
values[fmt.Sprintf("%sPORT", prefix)] = formatInt(endpoint["port"])
values[fmt.Sprintf("%sIP", prefix)] = endpoint["ip"].(string)
if v, ok := endpoint["ip"]; ok && v != nil {
values[fmt.Sprintf("%sIP", prefix)] = v.(string)
}
} else if scheme == "tcp" {
values[fmt.Sprintf("%sURL", prefix)] = formatServer(endpoint)
values[fmt.Sprintf("%sIP", prefix)] = endpoint["ip"].(string)
if v, ok := endpoint["ip"]; ok && v != nil {
values[fmt.Sprintf("%sIP", prefix)] = v.(string)
}
values[fmt.Sprintf("%sPORT", prefix)] = formatInt(endpoint["port"])
values[fmt.Sprintf("%sSCHEME", prefix)] = endpoint["scheme"].(string)
values[fmt.Sprintf("%sHOST", prefix)] = endpoint["host"].(string)
Expand All @@ -296,7 +304,9 @@ func extractRelationshipsEnvs(env Environment) Envs {
values[fmt.Sprintf("%sURL", prefix)] = fmt.Sprintf("%s://%s:%s", endpoint["scheme"].(string), endpoint["host"].(string), formatInt(endpoint["port"]))
}
values[fmt.Sprintf("%sSERVER", prefix)] = formatServer(endpoint)
values[fmt.Sprintf("%sIP", prefix)] = endpoint["ip"].(string)
if v, ok := endpoint["ip"]; ok && v != nil {
values[fmt.Sprintf("%sIP", prefix)] = v.(string)
}
values[fmt.Sprintf("%sPORT", prefix)] = formatInt(endpoint["port"])
values[fmt.Sprintf("%sSCHEME", prefix)] = endpoint["scheme"].(string)
values[fmt.Sprintf("%sHOST", prefix)] = endpoint["host"].(string)
Expand All @@ -323,7 +333,9 @@ func extractRelationshipsEnvs(env Environment) Envs {
// for Symfony Mailer, use a MAILER prefix
values[fmt.Sprintf("%sDSN", prefix)] = fmt.Sprintf("%s://%s:%s", endpoint["scheme"].(string), endpoint["host"].(string), formatInt(endpoint["port"]))
} else if rel == "simple" {
values[fmt.Sprintf("%sIP", prefix)] = endpoint["ip"].(string)
if v, ok := endpoint["ip"]; ok && v != nil {
values[fmt.Sprintf("%sIP", prefix)] = v.(string)
}
values[fmt.Sprintf("%sPORT", prefix)] = formatInt(endpoint["port"])
values[fmt.Sprintf("%sHOST", prefix)] = endpoint["host"].(string)
}
Expand Down
14 changes: 14 additions & 0 deletions envs/remote_test.go
Expand Up @@ -421,3 +421,17 @@ func (s *RemoteSuite) TestMySQLReadReplicaForDedicated(c *C) {
c.Assert("mysql://mysql:xxx@dbread.internal:3306/main?sslmode=disable&charset=utf8mb4&serverVersion=10.6.0-MariaDB", DeepEquals, e["DBREAD_URL"])
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) {
r := &Remote{}
value, err := os.ReadFile("testdata/dedicated/no_ips_for_dedicated.json")
if err != nil {
panic(err)
}
if err := os.Setenv("PLATFORM_RELATIONSHIPS", base64.StdEncoding.EncodeToString(value)); err != nil {
panic(err)
}

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"})
}
33 changes: 33 additions & 0 deletions envs/testdata/dedicated/no_ips_for_dedicated.json
@@ -0,0 +1,33 @@
{
"database" : [
{
"host" : "127.0.0.1",
"password" : "x",
"path" : "x_stg",
"port" : "3306",
"query" : {
"compression" : true,
"is_master" : true
},
"scheme" : "mysql",
"username" : "xstg"
}
],
"rabbitmq" : [
{
"host" : "localhost",
"password" : "x",
"port" : "5672",
"scheme" : "amqp",
"username" : "x_stg",
"vhost" : "x_stg"
}
],
"rediscache" : [
{
"host" : "localhost",
"port" : "6379",
"scheme" : "redis"
}
]
}

0 comments on commit 0ae097d

Please sign in to comment.