forked from cookiesowns/edgecontroller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
139 lines (125 loc) · 3.27 KB
/
app.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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2019 Intel Corporation
package cce
import (
"errors"
"fmt"
"net/url"
"strings"
"github.com/open-ness/edgecontroller/uuid"
)
// App is an application.
type App struct {
ID string `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
Version string `json:"version"`
Vendor string `json:"vendor"`
Description string `json:"description"`
Cores int `json:"cores"`
Memory int `json:"memory"` // in MB
Ports []PortProto `json:"ports,omitempty"`
Source string `json:"source"`
EPAFeatures []EPAFeature `json:"epafeatures,omitempty"`
}
// PortProto is a port and protocol combination. It is typically used to represent the ports and protocols that an
// application is listening on and needs exposed.
type PortProto struct {
Port uint32 `json:"port,omitempty"`
Protocol string `json:"protocol,omitempty"`
}
func (pp PortProto) String() string {
return fmt.Sprintf("%d/%s", pp.Port, pp.Protocol)
}
// EPAFeature is a key-value pair used to represent
// Enhanced Platform Awareness feature settings
type EPAFeature struct {
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}
// GetTableName returns the name of the persistence table.
func (*App) GetTableName() string {
return "apps"
}
// GetID gets the ID.
func (app *App) GetID() string {
return app.ID
}
// SetID sets the ID.
func (app *App) SetID(id string) {
app.ID = id
}
// Validate validates the model.
func (app *App) Validate() error { // nolint: gocyclo
if !uuid.IsValid(app.ID) {
return errors.New("id not a valid uuid")
}
if app.Type != "container" && app.Type != "vm" {
return errors.New(`type must be either "container" or "vm"`)
}
if app.Name == "" {
return errors.New("name cannot be empty")
}
if app.Vendor == "" {
return errors.New("vendor cannot be empty")
}
if app.Version == "" {
return errors.New("version cannot be empty")
}
if app.Cores < 1 || app.Cores > MaxCores {
return fmt.Errorf("cores must be in [1..%d]", MaxCores)
}
if app.Memory < 1 || app.Memory > MaxMemory {
return fmt.Errorf("memory must be in [1..%d]", MaxMemory)
}
for _, pp := range app.Ports {
switch pp.Protocol {
case "tcp", "udp", "icmp", "sctp", "all":
case "":
if pp.Port == 0 {
continue // permit empty / no setting
}
default:
return fmt.Errorf("protocol must be tcp, udp, sctp, icmp or all")
}
if pp.Port < 1 || pp.Port > MaxPort {
return fmt.Errorf("port must be in [1..%d]", MaxPort)
}
}
if app.Source == "" {
return errors.New("source cannot be empty")
}
if _, err := url.ParseRequestURI(app.Source); err != nil {
return errors.New("source cannot be parsed as a URI")
}
return nil
}
// FilterFields returns the filterable fields for this model.
func (*App) FilterFields() []string {
return []string{}
}
func (app *App) String() string {
return fmt.Sprintf(strings.TrimSpace(`
App[
ID: %s
Name: %s
Version: %s
Vendor: %s
Description: %s
Cores: %d
Memory: %d
Ports: %s
Source: %s
EPAFeatures: %s
]`),
app.ID,
app.Name,
app.Version,
app.Vendor,
app.Description,
app.Cores,
app.Memory,
app.Ports,
app.Source,
app.EPAFeatures)
}