Skip to content

Commit

Permalink
Merge pull request #224 from platformsh/simplified-rels
Browse files Browse the repository at this point in the history
Use simplified relationships for all services & vendors
  • Loading branch information
akalipetis authored Jun 14, 2024
2 parents f1c5e52 + 7408c19 commit d84ff2c
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 41 deletions.
12 changes: 7 additions & 5 deletions internal/question/models/answer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package models

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -155,21 +154,24 @@ func getStack(answersStack Stack) platformifier.Stack {
}
}

// getRelationships returns a map of service names to their relationship names.
func getRelationships(services []Service) map[string]string {
// getRelationships returns a map of service names to their endpoint names.
func getRelationships(services []Service) map[string]platformifier.Relationship {
endpointRemap := map[string]string{
"mariadb": "mysql",
"oracle-mysql": "mysql",
"chrome-headless": "http",
"redis-persistent": "redis",
}
relationships := make(map[string]string)
relationships := make(map[string]platformifier.Relationship)
for _, service := range services {
endpoint := strings.Split(service.Type.Name, ":")[0]
if remappedEndpoint, ok := endpointRemap[endpoint]; ok {
endpoint = remappedEndpoint
}
relationships[service.Name] = fmt.Sprintf("%s:%s", service.Name, endpoint)
relationships[service.Name] = platformifier.Relationship{
Service: service.Name,
Endpoint: endpoint,
}
}
return relationships
}
18 changes: 10 additions & 8 deletions internal/question/models/answer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package models
import (
"reflect"
"testing"

"github.com/platformsh/platformify/platformifier"
)

func Test_getRelationships(t *testing.T) {
Expand All @@ -12,7 +14,7 @@ func Test_getRelationships(t *testing.T) {
tests := []struct {
name string
args args
want map[string]string
want map[string]platformifier.Relationship
}{
{
name: "Simple",
Expand All @@ -27,8 +29,8 @@ func Test_getRelationships(t *testing.T) {
},
},
},
want: map[string]string{
"mysql": "mysql:mysql",
want: map[string]platformifier.Relationship{
"mysql": {Service: "mysql", Endpoint: "mysql"},
},
},
{
Expand Down Expand Up @@ -65,11 +67,11 @@ func Test_getRelationships(t *testing.T) {
},
},
},
want: map[string]string{
"mariadb": "mariadb:mysql",
"oracle-mysql": "oracle-mysql:mysql",
"chrome-headless": "chrome-headless:http",
"redis-persistent": "redis-persistent:redis",
want: map[string]platformifier.Relationship{
"mariadb": {Service: "mariadb", Endpoint: "mysql"},
"oracle-mysql": {Service: "oracle-mysql", Endpoint: "mysql"},
"chrome-headless": {Service: "chrome-headless", Endpoint: "http"},
"redis-persistent": {Service: "redis-persistent", Endpoint: "redis"},
},
},
}
Expand Down
7 changes: 6 additions & 1 deletion platformifier/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func (s Stack) Name() string {
}
}

type Relationship struct {
Service string
Endpoint string
}

// UserInput contains the configuration from user input.
type UserInput struct {
Stack Stack
Expand All @@ -71,7 +76,7 @@ type UserInput struct {
Disk string
Mounts map[string]map[string]string
Services []Service
Relationships map[string]string
Relationships map[string]Relationship
WorkingDirectory string
HasGit bool
}
Expand Down
15 changes: 6 additions & 9 deletions platformifier/platformifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package platformifier
import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"reflect"
Expand Down Expand Up @@ -73,9 +72,9 @@ var (
"source_path": "pip",
},
},
Relationships: map[string]string{
"db": "db:postgresql",
"mysql": "mysql:mysql",
Relationships: map[string]Relationship{
"db": {Service: "db", Endpoint: "postgresql"},
"mysql": {Service: "mysql", Endpoint: "mysql"},
},
HasGit: true,
Services: []Service{
Expand Down Expand Up @@ -124,8 +123,8 @@ var (
"source_path": "maven",
},
},
Relationships: map[string]string{
"mysql": "mysql:mysql",
Relationships: map[string]Relationship{
"mysql": {Service: "mysql", Endpoint: "mysql"},
},
HasGit: true,
Services: []Service{
Expand Down Expand Up @@ -158,7 +157,7 @@ var (
Dependencies: map[string]map[string]string{},
Disk: "",
Mounts: map[string]map[string]string{},
Relationships: map[string]string{},
Relationships: map[string]Relationship{},
HasGit: false,
Services: []Service{},
}
Expand Down Expand Up @@ -574,8 +573,6 @@ func TestPlatformifier_Upsunify(t *testing.T) {
if err := validator.ValidateConfig(dir, "upsun"); (err != nil) != tt.wantErr {
t.Errorf("Platformifier.Platformify() validation error = %v, wantErr %v", err, tt.wantErr)
}
out, _ := os.ReadFile(dir + "/.upsun/config.yaml")
fmt.Println(string(out))
})
}
}
4 changes: 2 additions & 2 deletions platformifier/templates/generic/.platform.app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ type: "{{ .Type }}"
{{ if .Relationships -}}
relationships:
{{ range $key, $value := .Relationships }}
{{- $key }}: "{{ $value }}"
{{- $key }}:
{{ end -}}
{{ else }}
# relationships:
# database: "db:postgresql"
# db:
{{ end }}

# The size of the persistent disk of the application (in MB). Minimum value is 128.
Expand Down
6 changes: 1 addition & 5 deletions platformifier/templates/upsun/.upsun/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@ applications:
relationships:
{{ range $key, $value := .Relationships }}
{{- $key }}:
service: "{{ $key }}"
endpoint: "{{ $value }}"
{{ end -}}
{{ else }}
# relationships:
# database:
# service: db
# endpoint: postgresql
# db:
{{ end }}

# Mounts define directories that are writable after the build is complete.
Expand Down
66 changes: 61 additions & 5 deletions validator/schema/platformsh.application.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,25 @@
"relationships": {
"type": "object",
"additionalProperties": {
"type": "string"
"oneOf": [
{
"type": "string"
},
{
"type": "null"
},
{
"type": "object",
"properties": {
"service": {
"type": "string"
},
"endpoint": {
"type": "string"
}
}
}
]
},
"title": "The relationships of the application to defined services.",
"default": {}
Expand Down Expand Up @@ -373,9 +391,28 @@
"relationships": {
"type": "object",
"additionalProperties": {
"type": "string"
"oneOf": [
{
"type": "string"
},
{
"type": "null"
},
{
"type": "object",
"properties": {
"service": {
"type": "string"
},
"endpoint": {
"type": "string"
}
}
}
]
},
"title": "The relationships of the application to defined services."
"title": "The relationships of the application to defined services.",
"default": {}
},
"access": {
"type": "object",
Expand Down Expand Up @@ -762,9 +799,28 @@
"relationships": {
"type": "object",
"additionalProperties": {
"type": "string"
"oneOf": [
{
"type": "string"
},
{
"type": "null"
},
{
"type": "object",
"properties": {
"service": {
"type": "string"
},
"endpoint": {
"type": "string"
}
}
}
]
},
"title": "The relationships of the application to defined services."
"title": "The relationships of the application to defined services.",
"default": {}
},
"access": {
"type": "object",
Expand Down
55 changes: 49 additions & 6 deletions validator/schema/upsun.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
{
"type": "string"
},
{
"type": "null"
},
{
"type": "object",
"properties": {
Expand All @@ -57,8 +60,7 @@
"endpoint": {
"type": "string"
}
},
"nullable": true
}
}
]
},
Expand Down Expand Up @@ -384,9 +386,28 @@
"relationships": {
"type": "object",
"additionalProperties": {
"type": "string"
"oneOf": [
{
"type": "string"
},
{
"type": "null"
},
{
"type": "object",
"properties": {
"service": {
"type": "string"
},
"endpoint": {
"type": "string"
}
}
}
]
},
"title": "The relationships of the application to defined services."
"title": "The relationships of the application to defined services.",
"default": {}
},
"access": {
"type": "object",
Expand Down Expand Up @@ -769,9 +790,28 @@
"relationships": {
"type": "object",
"additionalProperties": {
"type": "string"
"oneOf": [
{
"type": "null"
},
{
"type": "string"
},
{
"type": "object",
"properties": {
"service": {
"type": "string"
},
"endpoint": {
"type": "string"
}
}
}
]
},
"title": "The relationships of the application to defined services."
"title": "The relationships of the application to defined services.",
"default": {}
},
"access": {
"type": "object",
Expand Down Expand Up @@ -1278,6 +1318,9 @@
{
"type": "string"
},
{
"type": "null"
},
{
"type": "object",
"properties": {
Expand Down

0 comments on commit d84ff2c

Please sign in to comment.