generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 78
/
component.go
107 lines (85 loc) · 3.16 KB
/
component.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
package v1beta1
import (
"fmt"
"path/filepath"
"github.com/layer5io/meshkit/database"
"github.com/layer5io/meshkit/models/meshmodel/entity"
"github.com/layer5io/meshkit/utils"
"gorm.io/gorm/clause"
"github.com/google/uuid"
)
type VersionMeta struct {
SchemaVersion string `json:"schemaVersion,omitempty" yaml:"schemaVersion"`
Version string `json:"version,omitempty" yaml:"version"`
}
type TypeMeta struct {
Kind string `json:"kind,omitempty" yaml:"kind"`
Version string `json:"version,omitempty" yaml:"version"`
}
type ComponentFormat string
const (
JSON ComponentFormat = "JSON"
YAML ComponentFormat = "YAML"
CUE ComponentFormat = "CUE"
)
// Contains information as extracted from the core underlying component eg: Pod's apiVersion, kind and schema
type ComponentEntity struct {
TypeMeta
Schema string `json:"schema,omitempty" yaml:"schema"`
}
// swagger:response ComponentDefinition
type ComponentDefinition struct {
ID uuid.UUID `json:"id"`
VersionMeta
DisplayName string `json:"displayName" gorm:"displayName"`
Description string `json:"description" gorm:"description"`
Format ComponentFormat `json:"format" yaml:"format"`
ModelID uuid.UUID `json:"-" gorm:"index:idx_component_definition_dbs_model_id,column:model_id"`
Model Model `json:"model" gorm:"foreignKey:ModelID;references:ID"`
Metadata map[string]interface{} `json:"metadata" yaml:"metadata" gorm:"type:bytes;serializer:json"`
Component ComponentEntity `json:"component,omitempty" yaml:"component" gorm:"type:bytes;serializer:json"`
}
func (c ComponentDefinition) TableName() string {
return "component_definition_dbs"
}
func (c ComponentDefinition) Type() entity.EntityType {
return entity.ComponentDefinition
}
func (c *ComponentDefinition) GenerateID() (uuid.UUID, error) {
return uuid.New(), nil
}
func (c ComponentDefinition) GetID() uuid.UUID {
return c.ID
}
func (c *ComponentDefinition) GetEntityDetail() string {
return fmt.Sprintf("type: %s, definition version: %s, name: %s, model: %s, version: %s", c.Type(), c.Version, c.DisplayName, c.Model.Name, c.Model.Version)
}
func (c *ComponentDefinition) Create(db *database.Handler, hostID uuid.UUID) (uuid.UUID, error) {
c.ID, _ = c.GenerateID()
isAnnotation, _ := c.Metadata["isAnnotation"].(bool)
if c.Component.Schema == "" && !isAnnotation { //For components which has an empty schema and is not an annotation, return error
// return ErrEmptySchema()
return uuid.Nil, nil
}
mid, err := c.Model.Create(db, hostID)
if err != nil {
return uuid.UUID{}, err
}
if !utils.IsSchemaEmpty(c.Component.Schema) {
c.Metadata["hasInvalidSchema"] = true
}
c.ModelID = mid
err = db.Omit(clause.Associations).Create(&c).Error
return c.ID, err
}
func (m *ComponentDefinition) UpdateStatus(db *database.Handler, status entity.EntityStatus) error {
return nil
}
func (c ComponentDefinition) WriteComponentDefinition(componentDirPath string) error {
if c.Component.Kind == "" {
return nil
}
componentPath := filepath.Join(componentDirPath, c.Component.Kind+".json")
err := utils.WriteJSONToFile[ComponentDefinition](componentPath, c)
return err
}