func()

in tflint-ruleset-blueprint/rules/terraform_doc_sample_restricted_blocks.go [48:93]


func (r *TerraformDocSamplesRestrictedBlocks) Check(runner tflint.Runner) error {
	path, err := runner.GetModulePath()
	if err != nil {
		return err
	}
	if !path.IsRoot() {
		// Each sample must be a root module.
		return nil
	}

	// Extract restricted blocks if any from config.
	restrictedBlocksSchema := make([]hclext.BlockSchema, 0, len(restrictedBlocks))
	for _, rb := range restrictedBlocks {
		rs := hclext.BlockSchema{
			Type:       rb,
			LabelNames: []string{"name"},
			Body:       &hclext.BodySchema{},
		}
		restrictedBlocksSchema = append(restrictedBlocksSchema, rs)
	}
	body, err := runner.GetModuleContent(&hclext.BodySchema{
		Blocks: restrictedBlocksSchema,
	}, &tflint.GetModuleContentOption{ExpandMode: tflint.ExpandModeNone})
	if err != nil {
		return err
	}

	// Emit issues if extracted blocks are found.
	blocks := body.Blocks.ByType()
	for _, rBlockType := range restrictedBlocks {
		rBlocks, ok := blocks[rBlockType]
		if ok {
			for _, rBlock := range rBlocks {
				err := runner.EmitIssue(
					r,
					fmt.Sprintf("doc sample restricted block type %s", rBlockType),
					rBlock.DefRange,
				)
				if err != nil {
					return err
				}
			}
		}
	}
	return nil
}