Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ExpandMode to GetModuleContentOption #208

Merged
merged 1 commit into from
Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions plugin/fromproto/fromproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ func GetModuleContentOption(opts *proto.GetModuleContent_Option) tflint.GetModul
return tflint.GetModuleContentOption{
ModuleCtx: ModuleCtxType(opts.ModuleCtx),
IncludeNotCreated: opts.IncludeNotCreated,
ExpandMode: ExpandMode(opts.ExpandMode),
Hint: GetModuleContentHint(opts.Hint),
}
}
Expand All @@ -254,6 +255,20 @@ func ModuleCtxType(ty proto.ModuleCtxType) tflint.ModuleCtxType {
}
}

// ExpandMode converts proto.GetModuleContent_ExpandMode to tflint.ExpandMode
func ExpandMode(mode proto.GetModuleContent_ExpandMode) tflint.ExpandMode {
switch mode {
case proto.GetModuleContent_EXPAND_MODE_UNSPECIFIED:
return tflint.ExpandModeExpand
case proto.GetModuleContent_EXPAND_MODE_EXPAND:
return tflint.ExpandModeExpand
case proto.GetModuleContent_EXPAND_MODE_NONE:
return tflint.ExpandModeNone
default:
panic(fmt.Sprintf("invalid ExpandMode: %s", mode))
}
}

// GetModuleContentHint converts proto.GetModuleContent_Hint to tflint.GetModuleContentHint
func GetModuleContentHint(hint *proto.GetModuleContent_Hint) tflint.GetModuleContentHint {
if hint == nil {
Expand Down
6 changes: 6 additions & 0 deletions plugin/plugin2host/plugin2host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ volume_size = 10`)
return &hclext.BodySchema{}, &tflint.GetModuleContentOption{
ModuleCtx: tflint.RootModuleCtxType,
IncludeNotCreated: true,
ExpandMode: tflint.ExpandModeNone,
Hint: tflint.GetModuleContentHint{ResourceType: "aws_instance"},
}
},
Expand All @@ -615,6 +616,11 @@ volume_size = 10`)
&hcl.Diagnostic{Severity: hcl.DiagError, Summary: "unexpected includeNotCreatedResources options"},
}
}
if opts.ExpandMode != tflint.ExpandModeNone {
return &hclext.BodyContent{}, hcl.Diagnostics{
&hcl.Diagnostic{Severity: hcl.DiagError, Summary: "unexpected expand mode options"},
}
}
if opts.Hint.ResourceType != "aws_instance" {
return &hclext.BodyContent{}, hcl.Diagnostics{
&hcl.Diagnostic{Severity: hcl.DiagError, Summary: "unexpected hint options"},
Expand Down
907 changes: 489 additions & 418 deletions plugin/proto/tflint.pb.go

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion plugin/proto/tflint.proto
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,16 @@ message GetModuleContent {
message Hint {
string resource_type = 1;
}
enum ExpandMode {
EXPAND_MODE_UNSPECIFIED = 0;
EXPAND_MODE_NONE = 1;
EXPAND_MODE_EXPAND = 2;
}
message Option {
ModuleCtxType module_ctx = 1;
Hint hint = 2;
bool include_not_created = 3;
bool include_not_created = 3 [deprecated = true];
ExpandMode expand_mode = 4;
}

message Request {
Expand Down
15 changes: 14 additions & 1 deletion plugin/toproto/toproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ func GetModuleContentOption(opts *tflint.GetModuleContentOption) *proto.GetModul

return &proto.GetModuleContent_Option{
ModuleCtx: ModuleCtxType(opts.ModuleCtx),
IncludeNotCreated: opts.IncludeNotCreated,
IncludeNotCreated: opts.IncludeNotCreated || opts.ExpandMode == tflint.ExpandModeNone,
ExpandMode: ExpandMode(opts.ExpandMode),
Hint: GetModuleContentHint(opts.Hint),
}
}
Expand All @@ -203,6 +204,18 @@ func ModuleCtxType(ty tflint.ModuleCtxType) proto.ModuleCtxType {
}
}

// ExpandMode converts tflint.ExpandMode to proto.GetModuleContent_ExpandMode
func ExpandMode(mode tflint.ExpandMode) proto.GetModuleContent_ExpandMode {
switch mode {
case tflint.ExpandModeExpand:
return proto.GetModuleContent_EXPAND_MODE_EXPAND
case tflint.ExpandModeNone:
return proto.GetModuleContent_EXPAND_MODE_NONE
default:
panic(fmt.Sprintf("invalid ExpandMode: %s", mode))
}
}

// GetModuleContentHint converts tflint.GetModuleContentHint to proto.GetModuleContentHint
func GetModuleContentHint(hint tflint.GetModuleContentHint) *proto.GetModuleContent_Hint {
return &proto.GetModuleContent_Hint{
Expand Down
24 changes: 24 additions & 0 deletions tflint/expandmode_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 17 additions & 2 deletions tflint/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,29 @@ const (
RootModuleCtxType
)

// ExpandMode represents whether the block retrieved by GetModuleContent is expanded by the meta-arguments.
//
//go:generate stringer -type=ExpandMode
type ExpandMode int32

const (
// ExpandModeExpand is the mode for expanding blocks based on the meta-arguments. The default is this behavior.
ExpandModeExpand ExpandMode = iota
// ExpandModeNone is the mode that does not expand blocks.
ExpandModeNone
)

// GetModuleContentOption is an option that controls the behavior when getting a module content.
type GetModuleContentOption struct {
// Specify the module to be acquired.
ModuleCtx ModuleCtxType
// Whether it includes resources that are not created, for example because count is 0 or unknown.
IncludeNotCreated bool
// Whether resources and modules are expanded by the count/for_each meta-arguments.
ExpandMode ExpandMode
// Hint is info for optimizing a query. This is an advanced option and it is not intended to be used directly from plugins.
Hint GetModuleContentHint

// Deprecated: Use ExpandMode instead.
IncludeNotCreated bool
}

// GetModuleContentHint is info for optimizing a query. This is an advanced option and it is not intended to be used directly from plugins.
Expand Down