-
Notifications
You must be signed in to change notification settings - Fork 187
/
inject_original_version_function.go
51 lines (40 loc) · 1.72 KB
/
inject_original_version_function.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
/*
* 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"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/functions"
)
// InjectOriginalVersionFunctionStageID is the unique identifier for this pipeline stage
const InjectOriginalVersionFunctionStageID = "injectOriginalVersionFunction"
// InjectOriginalVersionFunction injects the function OriginalVersion() into each Spec type
// This function allows us to recover the original version used to create each custom resource, giving the operator the
// information needed to interact with ARM using the correct API version.
// We run this stage before we create any storage types, ensuring only API versions get the function.
func InjectOriginalVersionFunction(idFactory astmodel.IdentifierFactory) *Stage {
stage := NewLegacyStage(
InjectOriginalVersionFunctionStageID,
"Inject the function OriginalVersion() into each Spec type",
func(ctx context.Context, definitions astmodel.TypeDefinitionSet) (astmodel.TypeDefinitionSet, error) {
injector := astmodel.NewFunctionInjector()
result := definitions.Copy()
specs := astmodel.FindSpecDefinitions(definitions)
for name, def := range specs {
fn := functions.NewOriginalVersionFunction(idFactory)
defWithFn, err := injector.Inject(def, fn)
if err != nil {
return nil, errors.Wrapf(err, "injecting OriginalVersion() into %s", name)
}
result[defWithFn.Name()] = defWithFn
}
return result, nil
})
stage.RequiresPostrequisiteStages(
CreateStorageTypesStageID,
InjectOriginalVersionPropertyStageID)
return stage
}