@@ -220,6 +220,9 @@ type Resource struct {
220220 // corresponding OiCS walkthroughs.
221221 Examples []* resource.Examples
222222
223+ // Samples for generating tests and documentation
224+ Samples []* resource.Sample
225+
223226 // If true, generates product operation handling logic.
224227 AutogenAsync bool `yaml:"autogen_async,omitempty"`
225228
@@ -531,6 +534,10 @@ func (r *Resource) Validate() {
531534 example .Validate (r .Name )
532535 }
533536
537+ for _ , sample := range r .Samples {
538+ sample .Validate (r .Name )
539+ }
540+
534541 if r .Async != nil {
535542 r .Async .Validate ()
536543 }
@@ -1301,9 +1308,10 @@ func ImportIdFormats(importFormat, identity []string, baseUrl string) []string {
13011308 return uniq
13021309}
13031310
1311+ // IgnoreReadPropertiesLegacy is the legacy version of IgnoreReadProperties for Examples
13041312// IgnoreReadProperties returns a sorted slice of property names (snake_case) that should be ignored when reading.
13051313// This is useful for downstream code that needs to iterate over these properties.
1306- func (r Resource ) IgnoreReadProperties (e * resource.Examples ) []string {
1314+ func (r Resource ) IgnoreReadPropertiesLegacy (e * resource.Examples ) []string {
13071315 var props []string
13081316 for _ , tp := range r .AllUserProperties () {
13091317 if tp .UrlParamOnly || tp .IsA ("ResourceRef" ) {
@@ -1318,10 +1326,38 @@ func (r Resource) IgnoreReadProperties(e *resource.Examples) []string {
13181326 return props
13191327}
13201328
1329+ // IgnoreReadPropertiesToStringLegacy is the legacy version of IgnoreReadPropertiesToString for Examples
1330+ // IgnoreReadPropertiesToString returns the ignore read properties as a Go-syntax string slice.
1331+ // This is a wrapper around IgnoreReadProperties for backwards compatibility.
1332+ func (r Resource ) IgnoreReadPropertiesToStringLegacy (e * resource.Examples ) string {
1333+ props := r .IgnoreReadPropertiesLegacy (e )
1334+ if len (props ) > 0 {
1335+ return fmt .Sprintf ("[]string{%s}" , strings .Join (quoteStrings (props ), ", " ))
1336+ }
1337+ return ""
1338+ }
1339+
1340+ // IgnoreReadProperties returns a sorted slice of property names (snake_case) that should be ignored when reading.
1341+ // This is useful for downstream code that needs to iterate over these properties.
1342+ func (r Resource ) IgnoreReadProperties (s * resource.Step ) []string {
1343+ var props []string
1344+ for _ , tp := range r .AllUserProperties () {
1345+ if tp .UrlParamOnly || tp .IsA ("ResourceRef" ) {
1346+ props = append (props , google .Underscore (tp .Name ))
1347+ }
1348+ }
1349+ props = append (props , s .IgnoreReadExtra ... )
1350+ props = append (props , r .IgnoreReadLabelsFields (r .PropertiesWithExcluded ())... )
1351+ props = append (props , ignoreReadFields (r .AllUserProperties ())... )
1352+
1353+ slices .Sort (props )
1354+ return props
1355+ }
1356+
13211357// IgnoreReadPropertiesToString returns the ignore read properties as a Go-syntax string slice.
13221358// This is a wrapper around IgnoreReadProperties for backwards compatibility.
1323- func (r Resource ) IgnoreReadPropertiesToString (e * resource.Examples ) string {
1324- props := r .IgnoreReadProperties (e )
1359+ func (r Resource ) IgnoreReadPropertiesToString (s * resource.Step ) string {
1360+ props := r .IgnoreReadProperties (s )
13251361 if len (props ) > 0 {
13261362 return fmt .Sprintf ("[]string{%s}" , strings .Join (quoteStrings (props ), ", " ))
13271363 }
@@ -1933,6 +1969,40 @@ func (r Resource) TestExamples() []*resource.Examples {
19331969 })
19341970}
19351971
1972+ func (r Resource ) TestSamples () []* resource.Sample {
1973+ return google .Reject (google .Reject (r .Samples , func (s * resource.Sample ) bool {
1974+ return s .ExcludeTest
1975+ }), func (s * resource.Sample ) bool {
1976+ return s .MinVersion != "" && slices .Index (product .ORDER , r .TargetVersionName ) < slices .Index (product .ORDER , s .MinVersion )
1977+ })
1978+ }
1979+
1980+ func (r Resource ) TestSampleSetUp () {
1981+ res := make (map [string ]string )
1982+ for _ , sample := range r .Samples {
1983+ sample .TargetVersionName = r .TargetVersionName
1984+ if sample .MinVersion != "" && slices .Index (product .ORDER , r .TargetVersionName ) < slices .Index (product .ORDER , sample .MinVersion ) {
1985+ continue
1986+ }
1987+ for _ , step := range sample .Steps {
1988+ if step .MinVersion != "" && slices .Index (product .ORDER , r .TargetVersionName ) < slices .Index (product .ORDER , step .MinVersion ) {
1989+ continue
1990+ }
1991+ step .PrimaryResourceId = sample .PrimaryResourceId
1992+ packageName := filepath .Base (filepath .Dir (r .SourceYamlFile ))
1993+ if step .ConfigPath == "" {
1994+ step .ConfigPath = fmt .Sprintf ("templates/terraform/samples/services/%s/%s.tf.tmpl" , packageName , step .Name )
1995+ }
1996+ step .SetHCLText ()
1997+ configName := step .Name
1998+ if _ , ok := res [step .Name ]; ! ok {
1999+ res [configName ] = sample .Name
2000+ sample .NewConfigFuncs = append (sample .NewConfigFuncs , step )
2001+ }
2002+ }
2003+ }
2004+ }
2005+
19362006func (r Resource ) VersionedProvider (exampleVersion string ) bool {
19372007 var vp string
19382008 if exampleVersion != "" {
0 commit comments