-
Notifications
You must be signed in to change notification settings - Fork 23
/
artifacts.go
119 lines (107 loc) · 3.42 KB
/
artifacts.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
// Copyright 2018 Bull S.A.S. Atos Technologies - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France.
//
// 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 tosca
import (
"github.com/pkg/errors"
"github.com/ystia/yorc/v3/log"
)
// ArtifactDefMap is a map of ArtifactDefinition
type ArtifactDefMap map[string]ArtifactDefinition
// UnmarshalYAML unmarshals a yaml into an ArtifactDefMap
func (adm *ArtifactDefMap) UnmarshalYAML(unmarshal func(interface{}) error) error {
log.Debugf("Resolving in artifacts in standard TOSCA format")
// Either a map or a seq
*adm = make(ArtifactDefMap)
var m map[string]ArtifactDefinition
if err := unmarshal(&m); err == nil {
for k, v := range m {
(*adm)[k] = v
}
return nil
}
log.Debugf("Resolving in artifacts in Alien format 1.2")
//var l []map[string]interface{}
var l []ArtifactDefinition
if err := unmarshal(&l); err == nil {
log.Debugf("list: %v", l)
for _, a := range l {
(*adm)[a.name] = a
}
return nil
}
log.Debugf("Resolving in artifacts in Alien format 1.3")
var lmap []ArtifactDefMap
if err := unmarshal(&lmap); err != nil {
return err
}
for _, m := range lmap {
for k, v := range m {
(*adm)[k] = v
}
}
return nil
}
// An ArtifactDefinition is the representation of a TOSCA Artifact Definition
//
// See http://docs.oasis-open.org/tosca/TOSCA-Simple-Profile-YAML/v1.2/TOSCA-Simple-Profile-YAML-v1.2.html#DEFN_ENTITY_ARTIFACT_DEF for more details
type ArtifactDefinition struct {
Type string `yaml:"type,omitempty"`
File string `yaml:"file,omitempty"`
Description string `yaml:"description,omitempty"`
Repository string `yaml:"repository,omitempty"`
DeployPath string `yaml:"deploy_path,omitempty"`
// Extra types used in list (A4C) mode
name string
}
// UnmarshalYAML unmarshals a yaml into an ArtifactDefinition
func (a *ArtifactDefinition) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err == nil {
a.File = s
return nil
}
var str struct {
Type string `yaml:"type"`
File string `yaml:"file"`
Description string `yaml:"description,omitempty"`
Repository string `yaml:"repository,omitempty"`
DeployPath string `yaml:"deploy_path,omitempty"`
// Extra types
MimeType string `yaml:"mime_type,omitempty"`
XXX map[string]interface{} `yaml:",inline"`
}
if err := unmarshal(&str); err != nil {
return err
}
log.Debugf("Unmarshalled complex ArtifactDefinition %+v", str)
a.Type = str.Type
a.File = str.File
a.Description = str.Description
a.Repository = str.Repository
a.DeployPath = str.DeployPath
if str.File == "" && len(str.XXX) == 1 {
for k, v := range str.XXX {
a.name = k
var ok bool
a.File, ok = v.(string)
if !ok {
return errors.New("Missing mandatory attribute \"file\" for artifact")
}
}
}
if a.File == "" {
return errors.New("Missing mandatory attribute \"file\" for artifact")
}
return nil
}