github.com/wata727/tflint@v0.12.2-0.20191013070026-96dd0d36f385/rules/awsrules/api/aws_route_invalid_egress_only_gateway.go (about)

     1  // This file generated by `tools/api-rule-gen/main.go`. DO NOT EDIT
     2  
     3  package api
     4  
     5  import (
     6  	"fmt"
     7  	"log"
     8  
     9  	hcl "github.com/hashicorp/hcl/v2"
    10  	"github.com/wata727/tflint/tflint"
    11  )
    12  
    13  // AwsRouteInvalidEgressOnlyGatewayRule checks whether attribute value actually exists
    14  type AwsRouteInvalidEgressOnlyGatewayRule struct {
    15  	resourceType  string
    16  	attributeName string
    17  	data          map[string]bool
    18  	dataPrepared  bool
    19  }
    20  
    21  // NewAwsRouteInvalidEgressOnlyGatewayRule returns new rule with default attributes
    22  func NewAwsRouteInvalidEgressOnlyGatewayRule() *AwsRouteInvalidEgressOnlyGatewayRule {
    23  	return &AwsRouteInvalidEgressOnlyGatewayRule{
    24  		resourceType:  "aws_route",
    25  		attributeName: "egress_only_gateway_id",
    26  		data:          map[string]bool{},
    27  		dataPrepared:  false,
    28  	}
    29  }
    30  
    31  // Name returns the rule name
    32  func (r *AwsRouteInvalidEgressOnlyGatewayRule) Name() string {
    33  	return "aws_route_invalid_egress_only_gateway"
    34  }
    35  
    36  // Enabled returns whether the rule is enabled by default
    37  func (r *AwsRouteInvalidEgressOnlyGatewayRule) Enabled() bool {
    38  	return true
    39  }
    40  
    41  // Severity returns the rule severity
    42  func (r *AwsRouteInvalidEgressOnlyGatewayRule) Severity() string {
    43  	return tflint.ERROR
    44  }
    45  
    46  // Link returns the rule reference link
    47  func (r *AwsRouteInvalidEgressOnlyGatewayRule) Link() string {
    48  	return ""
    49  }
    50  
    51  // Check checks whether the attributes are included in the list retrieved by DescribeEgressOnlyInternetGateways
    52  func (r *AwsRouteInvalidEgressOnlyGatewayRule) Check(runner *tflint.Runner) error {
    53  	log.Printf("[TRACE] Check `%s` rule for `%s` runner", r.Name(), runner.TFConfigPath())
    54  
    55  	return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error {
    56  		if !r.dataPrepared {
    57  			log.Print("[DEBUG] invoking DescribeEgressOnlyInternetGateways")
    58  			var err error
    59  			r.data, err = runner.AwsClient.DescribeEgressOnlyInternetGateways()
    60  			if err != nil {
    61  				err := &tflint.Error{
    62  					Code:    tflint.ExternalAPIError,
    63  					Level:   tflint.ErrorLevel,
    64  					Message: "An error occurred while invoking DescribeEgressOnlyInternetGateways",
    65  					Cause:   err,
    66  				}
    67  				log.Printf("[ERROR] %s", err)
    68  				return err
    69  			}
    70  			r.dataPrepared = true
    71  		}
    72  
    73  		var val string
    74  		err := runner.EvaluateExpr(attribute.Expr, &val)
    75  
    76  		return runner.EnsureNoError(err, func() error {
    77  			if !r.data[val] {
    78  				runner.EmitIssue(
    79  					r,
    80  					fmt.Sprintf(`"%s" is invalid egress only internet gateway ID.`, val),
    81  					attribute.Expr.Range(),
    82  				)
    83  			}
    84  			return nil
    85  		})
    86  	})
    87  }