Skip to content

Commit d214ba0

Browse files
authored
Add description override codegenerator option (#4456)
Use this option to override the Subnet IPConfigurations field to explain that it is removed if it crosses 2000 entries.
1 parent b98c83f commit d214ba0

11 files changed

+109
-3
lines changed

v2/api/network/v1api20201101/virtual_networks_subnet_types_gen.go

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v2/api/network/v1api20240301/virtual_networks_subnet_types_gen.go

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v2/azure-arm.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,9 @@ status:
895895
# Set to 'false' to disable our heuristics if a property is incorrectly
896896
# identified as an ARM reference.
897897
#
898+
# $description: <string>
899+
# Overrides the property description with the provided value.
900+
#
898901
# $importConfigMapMode: <optional|required>
899902
# Specifies that the property can be imported from a config map.
900903
# Optional: The property may be specified as string or imported from a config map.
@@ -3104,6 +3107,9 @@ objectModelConfiguration:
31043107
VirtualNetworks_Subnet: # TODO: There must always be a parent and child in the same API version. See https://github.com/Azure/azure-service-operator/issues/3002
31053108
$exportAs: VirtualNetworksSubnet
31063109
$supportedFrom: v2.0.0-alpha.1
3110+
VirtualNetworksSubnet_STATUS:
3111+
IpConfigurations:
3112+
$description: "An array of references to the network interface IP configurations using subnet. This field is not included if there are more than 2000 entries."
31073113
VirtualNetworks_VirtualNetworkPeering:
31083114
$exportAs: VirtualNetworksVirtualNetworkPeering
31093115
$supportedFrom: v2.0.0-alpha.1
@@ -3409,6 +3415,9 @@ objectModelConfiguration:
34093415
VirtualNetworks_Subnet: # TODO: There must always be a parent and child in the same API version. See https://github.com/Azure/azure-service-operator/issues/3002
34103416
$exportAs: VirtualNetworksSubnet
34113417
$supportedFrom: v2.11.0
3418+
VirtualNetworksSubnet_STATUS:
3419+
IpConfigurations:
3420+
$description: "An array of references to the network interface IP configurations using subnet. This field is not included if there are more than 2000 entries."
34123421
VirtualNetworks_VirtualNetworkPeering:
34133422
$exportAs: VirtualNetworksVirtualNetworkPeering
34143423
$supportedFrom: v2.11.0

v2/cmd/asoctl/go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ require (
103103
github.com/prometheus/common v0.55.0 // indirect
104104
github.com/prometheus/procfs v0.15.1 // indirect
105105
github.com/rivo/uniseg v0.4.7 // indirect
106-
github.com/rotisserie/eris v0.5.4 // indirect
107106
github.com/samber/lo v1.47.0 // indirect
108107
github.com/spf13/pflag v1.0.5 // indirect
109108
github.com/stoewer/go-strcase v1.3.0 // indirect

v2/tools/generator/internal/codegen/code_generator.go

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ func createAllPipelineStages(
199199
pipeline.StripUnreferencedTypeDefinitions(),
200200

201201
pipeline.RenameProperties(configuration.ObjectModelConfiguration),
202+
pipeline.OverrideDescriptions(configuration), // After flatten and rename to make easier to use
202203
pipeline.AddStatusConditions(idFactory).UsedFor(pipeline.ARMTarget),
203204

204205
pipeline.AddOperatorSpec(configuration, idFactory).UsedFor(pipeline.ARMTarget),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation.
3+
* Licensed under the MIT license.
4+
*/
5+
6+
package pipeline
7+
8+
import (
9+
"context"
10+
11+
"github.com/pkg/errors"
12+
13+
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/astmodel"
14+
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/config"
15+
)
16+
17+
// OverrideDescriptionsStageId is the unique identifier for this pipeline stage
18+
const OverrideDescriptionsStageId = "overrideDescriptions"
19+
20+
// OverrideDescriptions overrides the specified property descriptions
21+
func OverrideDescriptions(configuration *config.Configuration) *Stage {
22+
stage := NewStage(
23+
OverrideDescriptionsStageId,
24+
"Applies the configured description overrides",
25+
func(ctx context.Context, state *State) (*State, error) {
26+
visitor := createDescriptionOverrideVisitor(configuration)
27+
result := make(astmodel.TypeDefinitionSet)
28+
for _, def := range state.Definitions() {
29+
newDef, err := visitor.VisitDefinition(def, def.Name())
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
result.Add(newDef)
35+
}
36+
37+
if err := configuration.ObjectModelConfiguration.Description.VerifyConsumed(); err != nil {
38+
return nil, errors.Wrap(err, "verifying description augmentation")
39+
}
40+
41+
return state.WithDefinitions(result), nil
42+
})
43+
44+
stage.RequiresPrerequisiteStages(FlattenPropertiesStageId, RenamePropertiesStageID)
45+
return stage
46+
}
47+
48+
func createDescriptionOverrideVisitor(
49+
configuration *config.Configuration,
50+
) astmodel.TypeVisitor[astmodel.InternalTypeName] {
51+
visitor := astmodel.TypeVisitorBuilder[astmodel.InternalTypeName]{
52+
VisitObjectType: func(
53+
this *astmodel.TypeVisitor[astmodel.InternalTypeName],
54+
it *astmodel.ObjectType,
55+
ctx astmodel.InternalTypeName,
56+
) (astmodel.Type, error) {
57+
result := it
58+
name := ctx
59+
for _, prop := range it.Properties().AsSlice() {
60+
// If the property has an overridden description, use it
61+
description, ok := configuration.ObjectModelConfiguration.Description.Lookup(name, prop.PropertyName())
62+
if !ok {
63+
continue
64+
}
65+
prop = prop.WithDescription(description)
66+
result = result.WithProperty(prop)
67+
}
68+
69+
return result, nil
70+
},
71+
}
72+
73+
return visitor.Build()
74+
}

v2/tools/generator/internal/codegen/testdata/TestGolden_NewARMCodeGeneratorFromConfigCreatesRightPipeline.golden

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ applyKubernetesResourceInterface azure Add the KubernetesR
4747
flattenProperties Apply flattening to properties marked for flattening
4848
stripUnreferenced Strip unreferenced types
4949
renameProperties Rename properties
50+
overrideDescriptions Applies the configured description overrides
5051
addStatusConditions azure Add the property 'Conditions' to all status types and implements genruntime.Conditioner on all resources
5152
addOperatorSpec azure Adds the property 'OperatorSpec' to all Spec types that require it
5253
addKubernetesExporter azure Adds the KubernetesExporter interface to resources that need it

v2/tools/generator/internal/codegen/testdata/TestGolden_NewCrossplaneCodeGeneratorFromConfigCreatesRightPipeline.golden

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ addSerializationTypeTag Adds a property tag to p
3737
flattenProperties Apply flattening to properties marked for flattening
3838
stripUnreferenced Strip unreferenced types
3939
renameProperties Rename properties
40+
overrideDescriptions Applies the configured description overrides
4041
addCrossplaneOwnerProperties crossplane Add the 3-tuple of (xName, xNameRef, xNameSelector) for each owning resource
4142
addCrossplaneForProviderProperty crossplane Add a 'ForProvider' property on every spec
4243
addCrossplaneAtProviderProperty crossplane Add an 'AtProvider' property on every status

v2/tools/generator/internal/codegen/testdata/TestGolden_NewTestCodeGeneratorCreatesRightPipeline.golden

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ applyKubernetesResourceInterface azure Add the KubernetesResource
3939
flattenProperties Apply flattening to properties marked for flattening
4040
stripUnused Strip unused types for test
4141
renameProperties Rename properties
42+
overrideDescriptions Applies the configured description overrides
4243
addStatusConditions azure Add the property 'Conditions' to all status types and implements genruntime.Conditioner on all resources
4344
addOperatorSpec azure Adds the property 'OperatorSpec' to all Spec types that require it
4445
addKubernetesExporter azure Adds the KubernetesExporter interface to resources that need it

v2/tools/generator/internal/config/object_model_configuration.go

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type ObjectModelConfiguration struct {
5050

5151
// Property access fields here (alphabetical, please)
5252
ARMReference propertyAccess[bool]
53+
Description propertyAccess[string]
5354
ImportConfigMapMode propertyAccess[ImportConfigMapMode]
5455
IsSecret propertyAccess[bool]
5556
PropertyNameInNextVersion propertyAccess[string]
@@ -110,6 +111,8 @@ func NewObjectModelConfiguration() *ObjectModelConfiguration {
110111
// Initialize property access fields here (alphabetical, please)
111112
result.ARMReference = makePropertyAccess[bool](
112113
result, func(c *PropertyConfiguration) *configurable[bool] { return &c.ARMReference })
114+
result.Description = makePropertyAccess[string](
115+
result, func(c *PropertyConfiguration) *configurable[string] { return &c.Description })
113116
result.ImportConfigMapMode = makePropertyAccess[ImportConfigMapMode](
114117
result, func(c *PropertyConfiguration) *configurable[ImportConfigMapMode] { return &c.ImportConfigMapMode })
115118
result.IsSecret = makePropertyAccess[bool](

v2/tools/generator/internal/config/property_configuration.go

+15
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type PropertyConfiguration struct {
2424
name string
2525
// Configurable properties here (alphabetical, please)
2626
ARMReference configurable[bool] // Specify whether this property is an ARM reference
27+
Description configurable[string] // Specify a description override for this property
2728
ImportConfigMapMode configurable[ImportConfigMapMode] // The config map mode
2829
IsSecret configurable[bool] // Specify whether this property is a secret
2930
NameInNextVersion configurable[string] // Name this property has in the next version
@@ -42,6 +43,7 @@ const (
4243
// Tags used in yaml files to specify configurable properties. Alphabetical please.
4344
const (
4445
armReferenceTag = "$armReference" // Bool specifying whether a property is an ARM reference
46+
descriptionTag = "$description" // String overriding the properties default description
4547
exportAsConfigMapPropertyNameTag = "$exportAsConfigMapPropertyName" // String specifying the name of the property set to export this property as a config map.
4648
importConfigMapModeTag = "$importConfigMapMode" // string specifying the ImportConfigMapMode mode
4749
isSecretTag = "$isSecret" // Bool specifying whether a property contains a secret
@@ -56,6 +58,7 @@ func NewPropertyConfiguration(name string) *PropertyConfiguration {
5658
name: name,
5759
// Initialize configurable properties here (alphabetical, please)
5860
ARMReference: makeConfigurable[bool](armReferenceTag, scope),
61+
Description: makeConfigurable[string](descriptionTag, scope),
5962
ImportConfigMapMode: makeConfigurable[ImportConfigMapMode](importConfigMapModeTag, scope),
6063
IsSecret: makeConfigurable[bool](isSecretTag, scope),
6164
NameInNextVersion: makeConfigurable[string](nameInNextVersionTag, scope),
@@ -165,6 +168,18 @@ func (pc *PropertyConfiguration) UnmarshalYAML(value *yaml.Node) error {
165168
continue
166169
}
167170

171+
// description: string
172+
if strings.EqualFold(lastId, descriptionTag) && c.Kind == yaml.ScalarNode {
173+
var description string
174+
err := c.Decode(&description)
175+
if err != nil {
176+
return eris.Wrapf(err, "decoding %s", descriptionTag)
177+
}
178+
179+
pc.Description.Set(description)
180+
continue
181+
}
182+
168183
// No handler for this value, return an error
169184
return eris.Errorf(
170185
"property configuration, unexpected yaml value %s: %s (line %d col %d)", lastId, c.Value, c.Line, c.Column)

0 commit comments

Comments
 (0)