@@ -326,7 +326,10 @@ func (t *Terraform) GenerateOperation(outputFolder string) {
326326
327327// Generate the IAM policy for this object. This is used to query and test
328328// IAM policies separately from the resource itself
329- func (t * Terraform ) GenerateIamPolicy (object api.Resource , templateData TemplateData , outputFolder string , generateCode , generateDocs bool ) {
329+ func (t * Terraform ) GenerateIamPolicyLegacy (object api.Resource , templateData TemplateData , outputFolder string , generateCode , generateDocs bool ) {
330+ if object .IamPolicy .ExampleConfigBody == "" {
331+ object .IamPolicy .ExampleConfigBody = "templates/terraform/iam/iam_attributes_legacy.go.tmpl"
332+ }
330333 if generateCode && object .IamPolicy != nil && (object .IamPolicy .MinVersion == "" || slices .Index (product .ORDER , object .IamPolicy .MinVersion ) <= slices .Index (product .ORDER , t .TargetVersionName )) {
331334 productName := t .Product .ApiName
332335 targetFolder := path .Join (outputFolder , t .FolderName (), "services" , productName )
@@ -341,6 +344,42 @@ func (t *Terraform) GenerateIamPolicy(object api.Resource, templateData Template
341344 return e .ExcludeTest
342345 })
343346 if len (examples ) != 0 {
347+ targetFilePath := path .Join (targetFolder , fmt .Sprintf ("iam_%s_generated_test.go" , t .ResourceGoFilename (object )))
348+ templateData .GenerateIamPolicyTestFileLegacy (targetFilePath , object )
349+ }
350+ }
351+ if generateDocs {
352+ t .GenerateIamDocumentation (object , templateData , outputFolder , generateCode , generateDocs )
353+ }
354+ }
355+
356+ func (t * Terraform ) GenerateIamPolicy (object api.Resource , templateData TemplateData , outputFolder string , generateCode , generateDocs bool ) {
357+ if object .Samples != nil && object .Examples != nil {
358+ log .Fatalf ("Both Samples and Examples block exist in %v" , object .Name )
359+ }
360+ if object .Examples != nil {
361+ t .GenerateIamPolicyLegacy (object , templateData , outputFolder , generateCode , generateDocs )
362+ return
363+ }
364+
365+ if object .IamPolicy .SampleConfigBody == "" {
366+ object .IamPolicy .SampleConfigBody = "templates/terraform/iam/iam_attributes.go.tmpl"
367+ }
368+
369+ if generateCode && object .IamPolicy != nil && (object .IamPolicy .MinVersion == "" || slices .Index (product .ORDER , object .IamPolicy .MinVersion ) <= slices .Index (product .ORDER , t .TargetVersionName )) {
370+ productName := t .Product .ApiName
371+ targetFolder := path .Join (outputFolder , t .FolderName (), "services" , productName )
372+ if err := os .MkdirAll (targetFolder , os .ModePerm ); err != nil {
373+ log .Println (fmt .Errorf ("error creating parent directory %v: %v" , targetFolder , err ))
374+ }
375+ targetFilePath := path .Join (targetFolder , fmt .Sprintf ("iam_%s.go" , t .ResourceGoFilename (object )))
376+ templateData .GenerateIamPolicyFile (targetFilePath , object )
377+
378+ // Only generate test if testable example configs exist.
379+ samples := google .Reject (object .Samples , func (s * resource.Sample ) bool {
380+ return s .ExcludeTest
381+ })
382+ if len (samples ) != 0 {
344383 targetFilePath := path .Join (targetFolder , fmt .Sprintf ("iam_%s_generated_test.go" , t .ResourceGoFilename (object )))
345384 templateData .GenerateIamPolicyTestFile (targetFilePath , object )
346385 }
0 commit comments