github.com/terraform-linters/tflint@v0.51.2-0.20240520175844-3750771571b6/plugin/stub-generator/sources/customrulesettesting/custom/runner.go (about)

     1  package custom
     2  
     3  import (
     4  	"github.com/terraform-linters/tflint-plugin-sdk/hclext"
     5  	"github.com/terraform-linters/tflint-plugin-sdk/tflint"
     6  )
     7  
     8  type Runner struct {
     9  	tflint.Runner
    10  	CustomConfig *Config
    11  }
    12  
    13  func NewRunner(runner tflint.Runner, config *Config) (*Runner, error) {
    14  	providers, err := runner.GetModuleContent(
    15  		&hclext.BodySchema{
    16  			Blocks: []hclext.BlockSchema{
    17  				{
    18  					Type:       "provider",
    19  					LabelNames: []string{"name"},
    20  					Body: &hclext.BodySchema{
    21  						Attributes: []hclext.AttributeSchema{
    22  							{Name: "zone"},
    23  						},
    24  						Blocks: []hclext.BlockSchema{
    25  							{
    26  								Type: "annotation",
    27  								Body: &hclext.BodySchema{
    28  									Attributes: []hclext.AttributeSchema{
    29  										{Name: "value"},
    30  									},
    31  								},
    32  							},
    33  						},
    34  					},
    35  				},
    36  			},
    37  		},
    38  		&tflint.GetModuleContentOption{ModuleCtx: tflint.RootModuleCtxType},
    39  	)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	for _, provider := range providers.Blocks {
    45  		if provider.Labels[0] != "custom" {
    46  			continue
    47  		}
    48  
    49  		opts := &tflint.EvaluateExprOption{ModuleCtx: tflint.RootModuleCtxType}
    50  
    51  		if attr, exists := provider.Body.Attributes["zone"]; exists {
    52  			err := runner.EvaluateExpr(attr.Expr, func(zone string) error {
    53  				config.Zone = zone
    54  				return nil
    55  			}, opts)
    56  			if err != nil {
    57  				return nil, err
    58  			}
    59  		}
    60  
    61  		for _, annotation := range provider.Body.Blocks {
    62  			if attr, exists := annotation.Body.Attributes["value"]; exists {
    63  				err := runner.EvaluateExpr(attr.Expr, func(val string) error {
    64  					config.Annotation = val
    65  					return nil
    66  				}, opts)
    67  				if err != nil {
    68  					return nil, err
    69  				}
    70  			}
    71  		}
    72  	}
    73  
    74  	return &Runner{
    75  		Runner:       runner,
    76  		CustomConfig: config,
    77  	}, nil
    78  }