-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathgeneric.go
104 lines (93 loc) · 2.52 KB
/
generic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright 2021 kloeckner.i GmbH
*
* 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 dbinstance
import (
"errors"
"strconv"
kcidb "github.com/kloeckner-i/db-operator/pkg/utils/database"
"github.com/sirupsen/logrus"
)
// Generic represents database instance which can be connected by address and port
type Generic struct {
Host string
Port uint16
Engine string
User string
Password string
PublicIP string
SSLEnabled bool
SkipCAVerify bool
}
func makeInterface(in *Generic) (kcidb.Database, error) {
switch in.Engine {
case "postgres":
db := kcidb.Postgres{
Host: in.Host,
Port: in.Port,
User: in.User,
Password: in.Password,
Database: "postgres",
SSLEnabled: in.SSLEnabled,
SkipCAVerify: in.SkipCAVerify,
}
return db, nil
case "mysql":
db := kcidb.Mysql{
Host: in.Host,
Port: in.Port,
User: in.User,
Password: in.Password,
Database: "mysql",
SSLEnabled: in.SSLEnabled,
SkipCAVerify: in.SkipCAVerify,
}
return db, nil
default:
return nil, errors.New("not supported engine type")
}
}
func (ins *Generic) state() (string, error) {
logrus.Debug("generic db instance not support a state check")
return "NOT_SUPPORTED", nil
}
func (ins *Generic) exist() error {
db, err := makeInterface(ins)
if err != nil {
logrus.Errorf("can not check if instance exists because of %s", err)
return err
}
err = db.CheckStatus()
if err != nil {
logrus.Error(err)
return err
}
return nil // instance exist
}
func (ins *Generic) create() error {
return errors.New("creating generic db instance is not yet implimented")
}
func (ins *Generic) update() error {
logrus.Debug("updating generic db instance is not yet implimented")
return nil
}
func (ins *Generic) getInfoMap() (map[string]string, error) {
data := map[string]string{
"DB_CONN": ins.Host,
"DB_PORT": strconv.FormatInt(int64(ins.Port), 10),
"DB_PUBLIC_IP": ins.PublicIP,
}
return data, nil
}