/
plugin_config_schema.go
88 lines (78 loc) · 2.98 KB
/
plugin_config_schema.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
package openapi
import (
"fmt"
"gopkg.in/yaml.v2"
)
// ServiceConfigurations contains the map with all service configurations
type ServiceConfigurations map[string]ServiceConfiguration
// PluginConfigSchema defines the interface/expected behaviour for PluginConfigSchema implementations.
type PluginConfigSchema interface {
// Validate performs a check to confirm that the schema content is correct
Validate() error
// GetServiceConfig returns the service configuration for a given provider name
GetServiceConfig(providerName string) (ServiceConfiguration, error)
// GetAllServiceConfigurations returns all the service configuration
GetAllServiceConfigurations() (ServiceConfigurations, error)
// GetVersion returns the plugin configuration version
GetVersion() (string, error)
// Marshal serializes the value provided into a YAML document
Marshal() ([]byte, error)
}
// PluginConfigSchemaV1 defines PluginConfigSchema version 1
// Configuration example:
// version: '1'
// services:
// monitor:
// swagger-url: http://monitor-api.com/swagger.json
// plugin_version: 0.14.0
// insecure_skip_verify: true
// cdn:
// swagger-url: https://cdn-api.com/swagger.json
// vm:
// swagger-url: http://vm-api.com/swagger.json
type PluginConfigSchemaV1 struct {
Version string `yaml:"version"`
Services map[string]*ServiceConfigV1 `yaml:"services"`
}
// NewPluginConfigSchemaV1 creates a new PluginConfigSchemaV1 that implements PluginConfigSchema interface
func NewPluginConfigSchemaV1(services map[string]*ServiceConfigV1) *PluginConfigSchemaV1 {
return &PluginConfigSchemaV1{
Version: "1",
Services: services,
}
}
// Validate makes sure that schema data is correct
func (p *PluginConfigSchemaV1) Validate() error {
if p.Version != "1" {
return fmt.Errorf("provider configuration version not matching current implementation, please use version '1' of provider configuration specification")
}
return nil
}
// GetServiceConfig returns the configuration for the given provider name
func (p *PluginConfigSchemaV1) GetServiceConfig(providerName string) (ServiceConfiguration, error) {
if providerName == "" {
return nil, fmt.Errorf("providerName not specified")
}
serviceConfig, exists := p.Services[providerName]
if !exists {
return nil, fmt.Errorf("'%s' not found in provider's services configuration", providerName)
}
return serviceConfig, nil
}
// GetVersion returns the plugin configuration version
func (p *PluginConfigSchemaV1) GetVersion() (string, error) {
return p.Version, nil
}
// GetAllServiceConfigurations returns all the service configuration
func (p *PluginConfigSchemaV1) GetAllServiceConfigurations() (ServiceConfigurations, error) {
serviceConfigurations := ServiceConfigurations{}
for k, v := range p.Services {
serviceConfigurations[k] = v
}
return serviceConfigurations, nil
}
// Marshal serializes the value provided into a YAML document
func (p *PluginConfigSchemaV1) Marshal() ([]byte, error) {
out, err := yaml.Marshal(p)
return out, err
}