diff --git a/pkg/apis/database/v1alpha1/database_types.go b/pkg/apis/database/v1alpha1/database_types.go deleted file mode 100644 index 30bf428..0000000 --- a/pkg/apis/database/v1alpha1/database_types.go +++ /dev/null @@ -1,167 +0,0 @@ -/* -Copyright 2024 zncdatadev. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package v1alpha1 - -import ( - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// DatabaseSpec defines the desired connection info of Database -type DatabaseSpec struct { - - //+kubebuilder:validation:Required - DatabaseName string `json:"databaseName,omitempty"` - - // Name of DatabaseConnection CR to use for this database. - //+kubebuilder:validation:Required - Reference string `json:"reference,omitempty"` - - // Credential is the credential for the database. - // It contains Username and Password, or ExistSecret. - //+kubebuilder:validation:Required - Credential *DatabaseCredentialSpec `json:"credential,omitempty"` -} - -type DatabaseStatus struct { - // +kubebuilder:validation:Optional - Conditions []metav1.Condition `json:"condition,omitempty"` -} - -//+kubebuilder:object:root=true -//+kubebuilder:subresource:status - -// Database is the Schema for the databases API -type Database struct { - metav1.TypeMeta `json:",inline"` - metav1.ObjectMeta `json:"metadata,omitempty"` - - Spec DatabaseSpec `json:"spec,omitempty"` - Status DatabaseStatus `json:"status,omitempty"` -} - -//+kubebuilder:object:root=true - -// DatabaseList contains a list of Database -type DatabaseList struct { - metav1.TypeMeta `json:",inline"` - metav1.ListMeta `json:"metadata,omitempty"` - Items []Database `json:"items"` -} - -// DatabaseConnectionSpec defines the desired state of DatabaseConnection -type DatabaseConnectionSpec struct { - // +kubebuilder:validation:Required - Provider *DatabaseConnectionProvider `json:"provider,omitempty"` - - // +kubebuilder:validation:Optional - Default bool `json:"default,omitempty"` -} - -// DatabaseCredentialSpec include: Username and Password or ExistSecret. -type DatabaseCredentialSpec struct { - // ExistSecret is a Secret name, created by user. - // It includes Username and Password, it is encrypted by base64. - // If ExistSecret is not empty, Username and Password will be ignored. - // +kubebuilder:validation:Optional - ExistSecret string `json:"existingSecret,omitempty"` - - // Username is the username for the database. - // +kubebuilder:validation:Optional - Username string `json:"username,omitempty"` - - // Password is the password for the database. - // +kubebuilder:validation:Optional - Password string `json:"password,omitempty"` -} - -type DatabaseConnectionStatus struct { - // +kubebuilder:validation:Optional - Conditions []metav1.Condition `json:"condition,omitempty"` -} - -//+kubebuilder:object:root=true -//+kubebuilder:subresource:status - -// DatabaseConnection is the Schema for the databaseconnections API -type DatabaseConnection struct { - metav1.TypeMeta `json:",inline"` - metav1.ObjectMeta `json:"metadata,omitempty"` - - Spec DatabaseConnectionSpec `json:"spec,omitempty"` - Status DatabaseConnectionStatus `json:"status,omitempty"` -} - -//+kubebuilder:object:root=true - -// DatabaseConnectionList contains a list of DatabaseConnection -type DatabaseConnectionList struct { - metav1.TypeMeta `json:",inline"` - metav1.ListMeta `json:"metadata,omitempty"` - Items []DatabaseConnection `json:"items"` -} - -// DatabaseConnectionProvider defines the enum provider for DataConnection. -// You can choose one of mysql, postgres, redis, and provider is required. -type DatabaseConnectionProvider struct { - // +kubebuilder:validation:Optional - Mysql *MysqlProvider `json:"mysql,omitempty"` - // +kubebuilder:validation:Optional - Postgres *PostgresProvider `json:"postgres,omitempty"` - // +kubebuilder:validation:Optional - Redis *RedisProvider `json:"redis,omitempty"` -} - -// MysqlProvider defines the desired connection info of Mysql -type MysqlProvider struct { - // +kubebuilder:default=mysql - // +kubebuilder:validation:Required - Driver string `json:"driver,omitempty"` - // +kubebuilder:validation:Required - Host string `json:"host,omitempty"` - // +kubebuilder:validation:Required - Port int `json:"port,omitempty"` - // +kubebuilder:validation:Required - SSL bool `json:"ssl,omitempty"` - // +kubebuilder:validation:Required - Credential *DatabaseCredentialSpec `json:"credential,omitempty"` -} - -// PostgresProvider defines the desired connection info of Postgres -type PostgresProvider struct { - // +kubebuilder:default=org.postgresql.Driver - Driver string `json:"driver,omitempty"` - // +kubebuilder:validation:Required - Host string `json:"host,omitempty"` - // +kubebuilder:validation:Required - Port int `json:"port,omitempty"` - // +kubebuilder:validation:Required - SSL bool `json:"ssl,omitempty"` - // +kubebuilder:validation:Required - Credential *DatabaseCredentialSpec `json:"credential,omitempty"` -} - -// RedisProvider defines the desired connection info of Redis -type RedisProvider struct { - // +kubebuilder:validation:Required - Host string `json:"host,omitempty"` - // +kubebuilder:validation:Required - Port string `json:"port,omitempty"` -} - -func init() { - SchemeBuilder.Register(&Database{}, &DatabaseList{}, &DatabaseConnection{}, &DatabaseConnectionList{}) -} diff --git a/pkg/apis/database/v1alpha1/groupversion_info.go b/pkg/apis/database/v1alpha1/groupversion_info.go deleted file mode 100644 index 8200d2a..0000000 --- a/pkg/apis/database/v1alpha1/groupversion_info.go +++ /dev/null @@ -1,36 +0,0 @@ -/* -Copyright 2023 zncdatadev. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -// Package v1alpha1 contains API Schema definitions for the database v1alpha1 API group -// +kubebuilder:object:generate=true -// +groupName=database.zncdata.dev -package v1alpha1 - -import ( - "k8s.io/apimachinery/pkg/runtime/schema" - "sigs.k8s.io/controller-runtime/pkg/scheme" -) - -var ( - // GroupVersion is group version used to register these objects - GroupVersion = schema.GroupVersion{Group: "database.zncdata.dev", Version: "v1alpha1"} - - // SchemeBuilder is used to add go types to the GroupVersionKind scheme - SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion} - - // AddToScheme adds the types in this group-version to the given scheme. - AddToScheme = SchemeBuilder.AddToScheme -) diff --git a/pkg/apis/database/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/database/v1alpha1/zz_generated.deepcopy.go deleted file mode 100644 index 127ed76..0000000 --- a/pkg/apis/database/v1alpha1/zz_generated.deepcopy.go +++ /dev/null @@ -1,328 +0,0 @@ -//go:build !ignore_autogenerated - -/* -Copyright 2024 zncdatadev. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -// Code generated by controller-gen. DO NOT EDIT. - -package v1alpha1 - -import ( - "k8s.io/apimachinery/pkg/apis/meta/v1" - runtime "k8s.io/apimachinery/pkg/runtime" -) - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *Database) DeepCopyInto(out *Database) { - *out = *in - out.TypeMeta = in.TypeMeta - in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) - in.Spec.DeepCopyInto(&out.Spec) - in.Status.DeepCopyInto(&out.Status) -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Database. -func (in *Database) DeepCopy() *Database { - if in == nil { - return nil - } - out := new(Database) - in.DeepCopyInto(out) - return out -} - -// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *Database) DeepCopyObject() runtime.Object { - if c := in.DeepCopy(); c != nil { - return c - } - return nil -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *DatabaseConnection) DeepCopyInto(out *DatabaseConnection) { - *out = *in - out.TypeMeta = in.TypeMeta - in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) - in.Spec.DeepCopyInto(&out.Spec) - in.Status.DeepCopyInto(&out.Status) -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DatabaseConnection. -func (in *DatabaseConnection) DeepCopy() *DatabaseConnection { - if in == nil { - return nil - } - out := new(DatabaseConnection) - in.DeepCopyInto(out) - return out -} - -// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *DatabaseConnection) DeepCopyObject() runtime.Object { - if c := in.DeepCopy(); c != nil { - return c - } - return nil -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *DatabaseConnectionList) DeepCopyInto(out *DatabaseConnectionList) { - *out = *in - out.TypeMeta = in.TypeMeta - in.ListMeta.DeepCopyInto(&out.ListMeta) - if in.Items != nil { - in, out := &in.Items, &out.Items - *out = make([]DatabaseConnection, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DatabaseConnectionList. -func (in *DatabaseConnectionList) DeepCopy() *DatabaseConnectionList { - if in == nil { - return nil - } - out := new(DatabaseConnectionList) - in.DeepCopyInto(out) - return out -} - -// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *DatabaseConnectionList) DeepCopyObject() runtime.Object { - if c := in.DeepCopy(); c != nil { - return c - } - return nil -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *DatabaseConnectionProvider) DeepCopyInto(out *DatabaseConnectionProvider) { - *out = *in - if in.Mysql != nil { - in, out := &in.Mysql, &out.Mysql - *out = new(MysqlProvider) - (*in).DeepCopyInto(*out) - } - if in.Postgres != nil { - in, out := &in.Postgres, &out.Postgres - *out = new(PostgresProvider) - (*in).DeepCopyInto(*out) - } - if in.Redis != nil { - in, out := &in.Redis, &out.Redis - *out = new(RedisProvider) - **out = **in - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DatabaseConnectionProvider. -func (in *DatabaseConnectionProvider) DeepCopy() *DatabaseConnectionProvider { - if in == nil { - return nil - } - out := new(DatabaseConnectionProvider) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *DatabaseConnectionSpec) DeepCopyInto(out *DatabaseConnectionSpec) { - *out = *in - if in.Provider != nil { - in, out := &in.Provider, &out.Provider - *out = new(DatabaseConnectionProvider) - (*in).DeepCopyInto(*out) - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DatabaseConnectionSpec. -func (in *DatabaseConnectionSpec) DeepCopy() *DatabaseConnectionSpec { - if in == nil { - return nil - } - out := new(DatabaseConnectionSpec) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *DatabaseConnectionStatus) DeepCopyInto(out *DatabaseConnectionStatus) { - *out = *in - if in.Conditions != nil { - in, out := &in.Conditions, &out.Conditions - *out = make([]v1.Condition, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DatabaseConnectionStatus. -func (in *DatabaseConnectionStatus) DeepCopy() *DatabaseConnectionStatus { - if in == nil { - return nil - } - out := new(DatabaseConnectionStatus) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *DatabaseCredentialSpec) DeepCopyInto(out *DatabaseCredentialSpec) { - *out = *in -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DatabaseCredentialSpec. -func (in *DatabaseCredentialSpec) DeepCopy() *DatabaseCredentialSpec { - if in == nil { - return nil - } - out := new(DatabaseCredentialSpec) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *DatabaseList) DeepCopyInto(out *DatabaseList) { - *out = *in - out.TypeMeta = in.TypeMeta - in.ListMeta.DeepCopyInto(&out.ListMeta) - if in.Items != nil { - in, out := &in.Items, &out.Items - *out = make([]Database, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DatabaseList. -func (in *DatabaseList) DeepCopy() *DatabaseList { - if in == nil { - return nil - } - out := new(DatabaseList) - in.DeepCopyInto(out) - return out -} - -// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *DatabaseList) DeepCopyObject() runtime.Object { - if c := in.DeepCopy(); c != nil { - return c - } - return nil -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *DatabaseSpec) DeepCopyInto(out *DatabaseSpec) { - *out = *in - if in.Credential != nil { - in, out := &in.Credential, &out.Credential - *out = new(DatabaseCredentialSpec) - **out = **in - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DatabaseSpec. -func (in *DatabaseSpec) DeepCopy() *DatabaseSpec { - if in == nil { - return nil - } - out := new(DatabaseSpec) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *DatabaseStatus) DeepCopyInto(out *DatabaseStatus) { - *out = *in - if in.Conditions != nil { - in, out := &in.Conditions, &out.Conditions - *out = make([]v1.Condition, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DatabaseStatus. -func (in *DatabaseStatus) DeepCopy() *DatabaseStatus { - if in == nil { - return nil - } - out := new(DatabaseStatus) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *MysqlProvider) DeepCopyInto(out *MysqlProvider) { - *out = *in - if in.Credential != nil { - in, out := &in.Credential, &out.Credential - *out = new(DatabaseCredentialSpec) - **out = **in - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new MysqlProvider. -func (in *MysqlProvider) DeepCopy() *MysqlProvider { - if in == nil { - return nil - } - out := new(MysqlProvider) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *PostgresProvider) DeepCopyInto(out *PostgresProvider) { - *out = *in - if in.Credential != nil { - in, out := &in.Credential, &out.Credential - *out = new(DatabaseCredentialSpec) - **out = **in - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new PostgresProvider. -func (in *PostgresProvider) DeepCopy() *PostgresProvider { - if in == nil { - return nil - } - out := new(PostgresProvider) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *RedisProvider) DeepCopyInto(out *RedisProvider) { - *out = *in -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RedisProvider. -func (in *RedisProvider) DeepCopy() *RedisProvider { - if in == nil { - return nil - } - out := new(RedisProvider) - in.DeepCopyInto(out) - return out -} diff --git a/pkg/client/database.go b/pkg/client/database.go deleted file mode 100644 index 275a62b..0000000 --- a/pkg/client/database.go +++ /dev/null @@ -1,284 +0,0 @@ -package client - -import ( - "context" - "fmt" - "strings" - - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - dbv1alpha1 "github.com/zncdatadev/operator-go/pkg/apis/database/v1alpha1" -) - -type DataBaseType string - -const ( - Postgres DataBaseType = "postgresql" - Mysql DataBaseType = "mysql" - Derby DataBaseType = "derby" - Unknown DataBaseType = "unknown" -) - -const ( - DbUsernameName = "USERNAME" - DbPasswordName = "PASSWORD" -) - -type DatabaseCredential struct { - Username string `json:"USERNAME"` - Password string `json:"PASSWORD"` -} - -type DatabaseParams struct { - DbType DataBaseType - Driver string - Username string - Password string - Host string - Port int32 - DbName string -} - -func NewDatabaseParams( - Driver string, - Username string, - Password string, - Host string, - Port int32, - DbName string) *DatabaseParams { - var dbType DataBaseType - if strings.Contains(Driver, "postgresql") { - dbType = Postgres - } - if strings.Contains(Driver, "mysql") { - dbType = Mysql - } - if strings.Contains(Driver, "derby") { - dbType = Derby - } - if Driver == "" { - dbType = Unknown - } - return &DatabaseParams{ - DbType: dbType, - Driver: Driver, - Username: Username, - Password: Password, - Host: Host, - Port: Port, - DbName: DbName, - } -} - -// DatabaseConfiguration is a struct that holds the configuration for a database. -// example1: -// -// dbConfig := &DatabaseConfiguration{DbReference: &ref, Context: ctx, Client: client} -// dbConfig.GetDatabaseParams() -// dbConfig.GetURI() -// -// example2: -type DatabaseConfiguration struct { - DbReference *string - DbInline *DatabaseParams - Namespace string - Context context.Context - Client *Client -} - -func (d *DatabaseConfiguration) GetNamespace() string { - if d.Namespace == "" { - d.Namespace = d.Client.GetOwnerNamespace() - } - return d.Namespace -} - -func (d *DatabaseConfiguration) GetRefDatabaseName() string { - return *d.DbReference -} - -func (d *DatabaseConfiguration) GetRefDatabase(ctx context.Context) (dbv1alpha1.Database, error) { - databaseCR := &dbv1alpha1.Database{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: d.Client.GetOwnerNamespace(), - Name: d.GetRefDatabaseName(), - }, - } - if err := d.Client.Get(ctx, databaseCR); err != nil { - return dbv1alpha1.Database{}, err - } - return *databaseCR, nil -} - -func (d *DatabaseConfiguration) GetRefDatabaseConnection(name string) (dbv1alpha1.DatabaseConnection, error) { - databaseConnectionCR := &dbv1alpha1.DatabaseConnection{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: d.GetNamespace(), - Name: name, - }, - } - - if err := d.Client.Get(d.Context, databaseConnectionCR); err != nil { - return dbv1alpha1.DatabaseConnection{}, err - } - return *databaseConnectionCR, nil -} - -func (d *DatabaseConfiguration) GetCredential(name string) (*DatabaseCredential, error) { - namespace := d.GetNamespace() - secret := &corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: namespace, - Name: name, - }, - } - - if err := d.Client.Get(d.Context, secret); err != nil { - return nil, err - } - - username, ok := secret.Data[DbUsernameName] - if !ok { - return nil, fmt.Errorf("username not found in secret. Name: %s, Namespace %s", name, namespace) - } - if username == nil { - return nil, fmt.Errorf("username is empty in secret. Name: %s, Namespace %s", name, namespace) - } - - password, ok := secret.Data[DbPasswordName] - if !ok { - return nil, fmt.Errorf("password not found in secret. Name: %s, Namespace %s", name, namespace) - } - if password == nil { - return nil, fmt.Errorf("password is empty in secret. Name: %s, Namespace %s", name, namespace) - } - - return &DatabaseCredential{ - Username: string(username), - Password: string(password), - }, nil -} - -func (d *DatabaseConfiguration) getDatabaseParamsFromResource() (*DatabaseParams, error) { - db, err := d.GetRefDatabase(d.Context) - if err != nil { - return nil, err - } - credential := &DatabaseCredential{} - - if db.Spec.Credential.ExistSecret != "" { - c, err := d.GetCredential(db.Spec.Credential.ExistSecret) - if err != nil { - return nil, err - } - credential = c - } else { - credential.Username = db.Spec.Credential.Username - credential.Password = db.Spec.Credential.Password - } - - dbConnection, err := d.GetRefDatabaseConnection(db.Spec.Reference) - if err != nil { - return nil, err - } - - dbParams := &DatabaseParams{ - Username: credential.Username, - Password: credential.Password, - } - - provider := dbConnection.Spec.Provider - - if provider.Postgres != nil { - dbParams.DbType = Postgres - dbParams.Driver = provider.Postgres.Driver - dbParams.Host = provider.Postgres.Host - dbParams.Port = int32(provider.Mysql.Port) - dbParams.DbName = db.Spec.DatabaseName - return dbParams, nil - } else if provider.Mysql != nil { - dbParams.DbType = Mysql - dbParams.Driver = provider.Mysql.Driver - dbParams.Host = provider.Mysql.Host - dbParams.Port = int32(provider.Mysql.Port) - dbParams.DbName = db.Spec.DatabaseName - return dbParams, nil - } else { - return &DatabaseParams{ - DbType: Derby, - Driver: "", - Username: "", - Password: "", - Host: "", - Port: 0, - DbName: "", - }, nil - } -} - -func (d *DatabaseConfiguration) getDatabaseParamsFromInline() (*DatabaseParams, error) { - return d.DbInline, nil -} - -func (d *DatabaseConfiguration) GetDatabaseParams() (*DatabaseParams, error) { - if d.DbReference != nil { - return d.getDatabaseParamsFromResource() - } - if d.DbInline != nil { - return d.getDatabaseParamsFromInline() - } - return nil, fmt.Errorf("invalid database configuration, dbReference and dbInline cannot be empty at the same time") -} - -// GetJDBCUrl returns the JDBC URL for the database. -// Supported: -// - Postgres -// - Mysql -// - Derby -// - `derby:dbName;create=true`, the dbName is a file path. -func (d *DatabaseConfiguration) GetJDBCUrl() (string, error) { - if d.DbReference != nil { - if refData, err := d.getDatabaseParamsFromResource(); err != nil { - return "", err - } else { - return toJDBCUrl(*refData) - } - } - if d.DbInline != nil { - return toJDBCUrl(*d.DbInline) - } - return "", fmt.Errorf("invalid database configuration, dbReference and dbInline cannot be empty at the same time") -} - -func toJDBCUrl(params DatabaseParams) (string, error) { - var jdbcPrefix string - switch params.DbType { - case Mysql: - jdbcPrefix = "jdbc:mysql" - - return fmt.Sprintf("%s://%s:%d/%s", - jdbcPrefix, - params.Host, - params.Port, - params.DbName, - ), nil - - case Postgres: - jdbcPrefix = "jdbc:postgresql" - return fmt.Sprintf("%s://%s:%d/%s", - jdbcPrefix, - params.Host, - params.Port, - params.DbName, - ), nil - case Derby: - jdbcPrefix = "jdbc:derby" - return fmt.Sprintf("%s:%s;create=true", - jdbcPrefix, - params.DbName, - ), nil - default: - return "", fmt.Errorf("unknown jdbc prefix for driver %s", params.DbType) - } -}