-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.go
171 lines (155 loc) · 4.46 KB
/
template.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package template
import (
"fmt"
"net"
"strconv"
"strings"
"github.com/Tinyblargon/DemoOnDemand/dod/global"
"github.com/Tinyblargon/DemoOnDemand/dod/helper/database"
"github.com/Tinyblargon/DemoOnDemand/dod/helper/file"
"github.com/Tinyblargon/DemoOnDemand/dod/helper/taskstatus"
"github.com/Tinyblargon/DemoOnDemand/dod/helper/template"
"github.com/Tinyblargon/DemoOnDemand/dod/helper/util"
"github.com/Tinyblargon/DemoOnDemand/dod/helper/vsphere/folder"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/object"
"gopkg.in/yaml.v2"
)
type PortForward struct {
SourcePort uint `json:"sourceport" yaml:"sourceport"`
DestinationPort uint `json:"destinationport,omitempty" yaml:"destinationport,omitempty"`
DestinationIP string `json:"destinationip" yaml:"destinationip"`
Protocol string `json:"protocol,omitempty" yaml:"protocol,omitempty"`
}
type Config struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Description string `json:"description,omitempty" yaml:"description"`
Path string `json:"path,omitempty" yaml:"path,omitempty"`
PortForwards []*PortForward `json:"portforwards" yaml:"portforwards"`
Networks *[]template.Network `json:"networks" yaml:"networks"`
}
func Get(templateName string) (templateConfig *Config, err error) {
contents, err := file.Read(global.ConfigFolder + "/" + templateName)
if err != nil {
return
}
err = yaml.Unmarshal(contents, &templateConfig)
return
}
func Destroy(client *govmomi.Client, dc *object.Datacenter, TempalateName string, status *taskstatus.Status) (err error) {
inUse, err := database.CheckTemplateInUse(global.DB, TempalateName)
if err != nil {
return
}
if inUse {
return fmt.Errorf("unable to remove template, template is in use")
}
err = folder.Delete(client, dc, global.TemplateFodler+"/"+TempalateName, status)
if err != nil {
return
}
return file.Delete(global.ConfigFolder + "/" + TempalateName)
}
func List() (files []string, err error) {
return file.ReadDir(global.ConfigFolder)
}
// Imports a new demo from the speciefid folder
func (c *Config) Import(client *govmomi.Client, dc *object.Datacenter, pool string, status *taskstatus.Status) (err error) {
filePath := global.ConfigFolder + "/" + c.Name
err = folder.Clone(client, dc, nil, c.Path, global.TemplateFodler+"/"+c.Name, pool, true, status)
if err != nil {
return
}
return c.WriteToFile(filePath)
}
func (c *Config) Defaults() {
for _, e := range c.PortForwards {
if e.DestinationPort == 0 {
e.DestinationPort = e.SourcePort
}
if e.Protocol == "" {
e.Protocol = "TCP"
}
}
}
func (c *Config) Validate(nameAndPathEmpty bool) (err error) {
if !nameAndPathEmpty {
if c.Name == "" {
return fmt.Errorf("name may not be empty")
}
if c.Path == "" {
return fmt.Errorf("path may not be empty")
}
}
err = c.ValidatePortforwards()
if err != nil {
return
}
for _, e := range c.PortForwards {
err = e.ValidateIP()
if err != nil {
return
}
}
for _, e := range *(c.Networks) {
err = e.ValidateRouterCIDR()
if err != nil {
return
}
}
return err
}
func (c *Config) WriteToFile(filePath string) error {
c.Name = ""
c.Path = ""
data, _ := yaml.Marshal(c)
return file.Write(filePath, data)
}
func (c *Config) ValidatePortforwards() (err error) {
list := make([]string, 0)
for _, e := range c.PortForwards {
err = ValidateSourcePort(e.SourcePort)
if err != nil {
return
}
err = ValidateDestinationPort(e.DestinationPort)
if err != nil {
return
}
err = ValidateProtocol(e.Protocol)
if err != nil {
return
}
item := e.Protocol + strconv.Itoa(int(e.SourcePort))
if !util.IsStringUnique(&list, item) {
return fmt.Errorf("duplicate sourceport.")
}
list = append(list, item)
}
return
}
func ValidateSourcePort(port uint) error {
if port == 0 && port > 65353 {
return fmt.Errorf("%d is not an valid sourceport.", port)
}
return nil
}
func ValidateDestinationPort(port uint) error {
if port > 65353 {
return fmt.Errorf("%d is not an valid destinationport.", port)
}
return nil
}
func ValidateProtocol(protocol string) error {
if !strings.EqualFold(protocol, "TCP") && !strings.EqualFold(protocol, "UDP") {
return fmt.Errorf("%s is not an valid protocol.", protocol)
}
return nil
}
func (p *PortForward) ValidateIP() error {
trial := net.ParseIP(p.DestinationIP)
if trial.To4() == nil && trial.To16() == nil {
return fmt.Errorf("%v is not an valid IP address.", trial)
}
return nil
}