-
Notifications
You must be signed in to change notification settings - Fork 59
/
model.go
144 lines (128 loc) · 4.72 KB
/
model.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
//(C) Copyright [2020] Hewlett Packard Enterprise Development LP
//
//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 tmodel ....
package tmodel
import (
"context"
"encoding/json"
"fmt"
"github.com/ODIM-Project/ODIM/lib-utilities/common"
"github.com/ODIM-Project/ODIM/lib-utilities/errors"
l "github.com/ODIM-Project/ODIM/lib-utilities/logs"
)
// Target is for sending the requst to south bound/plugin
type Target struct {
ManagerAddress string `json:"ManagerAddress"`
Password []byte `json:"Password"`
UserName string `json:"UserName"`
PostBody []byte `json:"PostBody"`
DeviceUUID string `json:"DeviceUUID"`
PluginID string `json:"PluginID"`
}
// Plugin is the model for plugin information
type Plugin struct {
IP string
Port string
Username string
Password []byte
ID string
PluginType string
PreferredAuthType string
}
var (
// GetDBConnectionFunc is pointer func for common.GetDBConnection
GetDBConnectionFunc = common.GetDBConnection
)
// GetResource fetches a resource from database using table and key
func GetResource(ctx context.Context, Table, key string, dbtype common.DbType) (string, *errors.Error) {
conn, err := GetDBConnectionFunc(common.InMemory)
if err != nil {
return "", err
}
resourceData, err := conn.Read(Table, key)
if err != nil {
return "", errors.PackError(err.ErrNo(), "error while trying to get resource details: ", err.Error())
}
var resource string
if errs := json.Unmarshal([]byte(resourceData), &resource); errs != nil {
return "", errors.PackError(errors.UndefinedErrorType, errs)
}
l.LogWithFields(ctx).Debugf("resource details : %s", resource)
return resource, nil
}
// GetAllKeysFromTable fetches all keys in a given table
func GetAllKeysFromTable(ctx context.Context, table string, dbtype common.DbType) ([]string, error) {
conn, err := GetDBConnectionFunc(dbtype)
if err != nil {
return nil, err
}
keysArray, err := conn.GetAllDetails(table)
if err != nil {
return nil, fmt.Errorf("error while trying to get all keys from table - %v: %v", table, err.Error())
}
l.LogWithFields(ctx).Debug("all keys from table: ", keysArray)
return keysArray, nil
}
// GetPluginData will fetch plugin details
func GetPluginData(pluginID string) (Plugin, *errors.Error) {
var plugin Plugin
conn, err := GetDBConnectionFunc(common.OnDisk)
if err != nil {
return plugin, err
}
plugindata, err := conn.Read("Plugin", pluginID)
if err != nil {
return plugin, errors.PackError(err.ErrNo(), "error while trying to fetch plugin data: ", err.Error())
}
if err := json.Unmarshal([]byte(plugindata), &plugin); err != nil {
return plugin, errors.PackError(errors.JSONUnmarshalFailed, err)
}
bytepw, errs := common.DecryptWithPrivateKey([]byte(plugin.Password))
if errs != nil {
return Plugin{}, errors.PackError(errors.DecryptionFailed, "error: "+pluginID+" plugin password decryption failed: "+errs.Error())
}
plugin.Password = bytepw
return plugin, nil
}
// GetTarget fetches the System(Target Device Credentials) table details
func GetTarget(ctx context.Context, deviceUUID string) (*Target, *errors.Error) {
var target Target
conn, err := GetDBConnectionFunc(common.OnDisk)
if err != nil {
return nil, err
}
data, err := conn.Read("System", deviceUUID)
if err != nil {
return nil, errors.PackError(err.ErrNo(), "error while trying to get compute details: ", err.Error())
}
if errs := json.Unmarshal([]byte(data), &target); errs != nil {
return nil, errors.PackError(errors.UndefinedErrorType, errs)
}
l.LogWithFields(ctx).Debugf("target post body : %s", string(target.PostBody))
return &target, nil
}
// GenericSave will save any resource data into the database
func GenericSave(ctx context.Context, body []byte, table string, key string) error {
connPool, err := GetDBConnectionFunc(common.InMemory)
if err != nil {
return fmt.Errorf("error while trying to connecting to DB: %v", err.Error())
}
if err = connPool.AddResourceData(table, key, string(body)); err != nil {
if errors.DBKeyAlreadyExist == err.ErrNo() {
return fmt.Errorf("error while trying to create new %v resource: %v", table, err.Error())
}
l.LogWithFields(ctx).Warn("Skipped saving of duplicate data with key " + key)
}
return nil
}