-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource.go
101 lines (83 loc) · 3.43 KB
/
resource.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package cfschema
import (
"fmt"
)
type Resource struct {
AdditionalIdentifiers []PropertyJsonPointers `json:"additionalIdentifiers,omitempty"`
AdditionalProperties *bool `json:"additionalProperties,omitempty"`
AllOf []*PropertySubschema `json:"allOf,omitempty"`
AnyOf []*PropertySubschema `json:"anyOf,omitempty"`
ConditionalCreateOnlyProperties PropertyJsonPointers `json:"conditionalCreateOnlyProperties,omitempty"`
CreateOnlyProperties PropertyJsonPointers `json:"createOnlyProperties,omitempty"`
Definitions map[string]*Property `json:"definitions,omitempty"`
DeprecatedProperties PropertyJsonPointers `json:"deprecatedProperties,omitempty"`
Description *string `json:"description,omitempty"`
Handlers map[string]*Handler `json:"handlers,omitempty"`
NonPublicDefinitions PropertyJsonPointers `json:"nonPublicDefinitions,omitempty"`
NonPublicProperties PropertyJsonPointers `json:"nonPublicProperties,omitempty"`
OneOf []*PropertySubschema `json:"oneOf,omitempty"`
PrimaryIdentifier PropertyJsonPointers `json:"primaryIdentifier,omitempty"`
Properties map[string]*Property `json:"properties,omitempty"`
PropertyTransform PropertyTransform `json:"propertyTransform,omitempty"`
ReadOnlyProperties PropertyJsonPointers `json:"readOnlyProperties,omitempty"`
ReplacementStrategy *string `json:"replacementStrategy,omitempty"`
Required []string `json:"required,omitempty"`
ResourceLink *ResourceLink `json:"resourceLink,omitempty"`
SourceURL *string `json:"sourceUrl,omitempty"`
Taggable *bool `json:"taggable,omitempty"`
Tagging *Tagging `json:"tagging,omitempty"`
TypeName *string `json:"typeName,omitempty"`
WriteOnlyProperties PropertyJsonPointers `json:"writeOnlyProperties,omitempty"`
}
func (r *Resource) IsCreateOnlyPropertyPath(path string) bool {
if r == nil {
return false
}
for _, createOnlyProperty := range r.CreateOnlyProperties {
if createOnlyProperty.EqualsStringPath(path) {
return true
}
}
return false
}
func (r *Resource) IsRequired(name string) bool {
if r == nil {
return false
}
for _, req := range r.Required {
if req == name {
return true
}
}
return false
}
// ResolveReference resolves a Reference (JSON Pointer) into a Property.
func (r *Resource) ResolveReference(ref Reference) (*Property, error) {
if r == nil {
return nil, nil
}
typ, err := ref.Type()
if err != nil {
return nil, err
}
var properties map[string]*Property
switch typ {
case ReferenceTypeDefinitions:
properties = r.Definitions
case ReferenceTypeProperties:
properties = r.Properties
default:
return nil, fmt.Errorf("unexpected Reference type: %s", typ)
}
field, err := ref.Field()
if err != nil {
return nil, err
}
property, ok := properties[field]
if !ok || property == nil {
return nil, fmt.Errorf("%s/%s not found", typ, field)
}
return property, nil
}