forked from IBM/ubiquity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datamodel.go
156 lines (132 loc) · 4.55 KB
/
datamodel.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Copyright 2017 IBM Corp.
*
* 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 scbe
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
"github.com/midoblgsm/ubiquity/model"
"github.com/midoblgsm/ubiquity/resources"
"github.com/midoblgsm/ubiquity/utils/logs"
)
//go:generate counterfeiter -o ../../fakes/fake_ScbeDataModel.go . ScbeDataModel
type ScbeDataModel interface {
CreateVolumeTable() error
DeleteVolume(name string) error
InsertVolume(volumeName string, wwn string, attachTo string, fstype string) error
GetVolume(name string) (ScbeVolume, bool, error)
ListVolumes() ([]ScbeVolume, error)
UpdateVolumeAttachTo(volumeName string, scbeVolume ScbeVolume, host2attach string) error
}
type scbeDataModel struct {
logger logs.Logger
database *gorm.DB
backend string
}
type ScbeVolume struct {
ID uint
Volume resources.Volume
VolumeID uint
WWN string
AttachTo string
FSType string
}
func NewScbeDataModel(db *gorm.DB, backend string) ScbeDataModel {
return &scbeDataModel{logger: logs.GetLogger(), database: db, backend: backend}
}
// CreateVolumeTable create the SCBE backend table
func (d *scbeDataModel) CreateVolumeTable() error {
defer d.logger.Trace(logs.DEBUG)()
if err := d.database.AutoMigrate(&ScbeVolume{}).Error; err != nil {
return d.logger.ErrorRet(err, "failed")
}
return nil
}
// DeleteVolume if vol exist in DB then delete it (both in the generic table and the specific one)
func (d *scbeDataModel) DeleteVolume(name string) error {
defer d.logger.Trace(logs.DEBUG)()
volume, exists, err := d.GetVolume(name)
if err != nil {
return err
}
if exists == false {
return d.logger.ErrorRet(&volumeNotFoundError{name}, "failed")
}
if err := d.database.Delete(&volume).Error; err != nil {
return d.logger.ErrorRet(err, "database.Delete failed")
}
if err := model.DeleteVolume(d.database, &volume.Volume).Error; err != nil {
return d.logger.ErrorRet(err, "model.DeleteVolume failed")
}
return nil
}
// InsertVolume volume name and its details given in opts
func (d *scbeDataModel) InsertVolume(volumeName string, wwn string, attachTo string, fstype string) error {
defer d.logger.Trace(logs.DEBUG)()
volume := ScbeVolume{
Volume: resources.Volume{Name: volumeName,
Backend: fmt.Sprintf("%s", d.backend)},
WWN: wwn,
AttachTo: attachTo,
FSType: fstype,
}
if err := d.database.Create(&volume).Error; err != nil {
return d.logger.ErrorRet(err, "database.Create failed")
}
return nil
}
// GetVolume return ScbeVolume if exist in DB, else return false and err
func (d *scbeDataModel) GetVolume(name string) (ScbeVolume, bool, error) {
defer d.logger.Trace(logs.DEBUG)()
volume, err := model.GetVolume(d.database, name, d.backend)
if err != nil {
if err.Error() == "record not found" {
return ScbeVolume{}, false, nil
}
return ScbeVolume{}, false, d.logger.ErrorRet(err, "model.GetVolume failed")
}
var scbeVolume ScbeVolume
if err := d.database.Where("volume_id = ?", volume.ID).Preload("Volume").First(&scbeVolume).Error; err != nil {
if err.Error() == "record not found" {
return ScbeVolume{}, false, nil
}
return ScbeVolume{}, false, d.logger.ErrorRet(err, "failed")
}
return scbeVolume, true, nil
}
func (d *scbeDataModel) ListVolumes() ([]ScbeVolume, error) {
defer d.logger.Trace(logs.DEBUG)()
var volumesInDb []ScbeVolume
if err := d.database.Preload("Volume").Find(&volumesInDb).Error; err != nil {
return nil, d.logger.ErrorRet(err, "failed")
}
// hack: to be replaced by proper DB filtering (joins)
var volumes []ScbeVolume
for _, volume := range volumesInDb {
if volume.Volume.Backend == d.backend {
volumes = append(volumes, volume)
}
}
return volumes, nil
}
func (d *scbeDataModel) UpdateVolumeAttachTo(volumeName string, scbeVolume ScbeVolume, host2attach string) error {
defer d.logger.Trace(logs.DEBUG)()
err := d.database.Table("scbe_volumes").Where("volume_id = ?", scbeVolume.ID).Update("attach_to", host2attach).Error
if err != nil {
return d.logger.ErrorRet(err, "failed", logs.Args{{"volumeName", volumeName}})
}
return nil
}