-
Notifications
You must be signed in to change notification settings - Fork 187
/
inject_original_version_property.go
59 lines (46 loc) · 2.03 KB
/
inject_original_version_property.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
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/
package pipeline
import (
"context"
"github.com/pkg/errors"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/astmodel"
)
// InjectOriginalVersionPropertyStageID is the unique identifier for this pipeline stage
const InjectOriginalVersionPropertyStageID = "injectOriginalVersionProperty"
// InjectOriginalVersionProperty injects the property OriginalVersion into each Storage Spec type
// This property gets populated by reading from the OriginalVersion() function previously injected into the API Spec
// types, allowing us to recover the original version used to create each custom resource, and giving the operator the
// information needed to interact with ARM using the correct API version.
func InjectOriginalVersionProperty() *Stage {
stage := NewLegacyStage(
InjectOriginalVersionPropertyStageID,
"Inject the property OriginalVersion into each Storage Spec type",
func(ctx context.Context, definitions astmodel.TypeDefinitionSet) (astmodel.TypeDefinitionSet, error) {
injector := astmodel.NewPropertyInjector()
result := definitions.Copy()
doesNotHaveOriginalVersionFunction := func(definition astmodel.TypeDefinition) bool {
ot, ok := astmodel.AsObjectType(definition.Type())
if !ok {
// Not an object
return false
}
// Skip objects that have OriginalVersion() functions
return !ot.HasFunctionWithName("OriginalVersion")
}
storageSpecs := astmodel.FindSpecDefinitions(definitions).Where(doesNotHaveOriginalVersionFunction)
for name, def := range storageSpecs {
prop := astmodel.NewPropertyDefinition("OriginalVersion", "originalVersion", astmodel.StringType)
prop.WithDescription("returns the original API version used to create the resource")
defWithProp, err := injector.Inject(def, prop)
if err != nil {
return nil, errors.Wrapf(err, "injecting OriginalVersion into %s", name)
}
result[defWithProp.Name()] = defWithProp
}
return result, nil
})
return stage
}