Skip to content

Commit

Permalink
preparations for stage7
Browse files Browse the repository at this point in the history
  • Loading branch information
yuvalpress committed Dec 4, 2023
1 parent e1bb654 commit 51961a8
Show file tree
Hide file tree
Showing 21 changed files with 272 additions and 143 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ diffSets.json
dist
vendor
NOTES.txt
terrap-cli
7 changes: 4 additions & 3 deletions cmd/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ Copyright © 2023 Sirrend
package cmd

import (
"strings"

"github.com/enescakir/emoji"
"github.com/sirrend/terrap-cli/internal/cli_utils"
"github.com/sirrend/terrap-cli/internal/commons"
"github.com/sirrend/terrap-cli/internal/providers_api"
"github.com/sirrend/terrap-cli/internal/receiver"
"github.com/sirrend/terrap-cli/internal/state"
"github.com/sirrend/terrap-cli/internal/utils"
"github.com/sirrend/terrap-cli/internal/workspace"
"github.com/spf13/cobra"
"strings"
)

// getContextCmd shows the providers which are currently in context
Expand Down Expand Up @@ -71,7 +72,7 @@ var getSupportedProvidersCmd = &cobra.Command{
dataPrinted = false
)
table := cli_utils.GetTable([]string{"Provider", "Min Version", "Max Version"}) // initialize new table
providers, _ := providers_api.GetSupportedProviders()
providers, _ := receiver.GetSupportedProviders()

// go over providers retrieved from API
for _, provider := range providers {
Expand Down
20 changes: 11 additions & 9 deletions cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ package cmd

import (
"fmt"
"os"
"strings"

"github.com/enescakir/emoji"
"github.com/fatih/color"
"github.com/sirrend/terrap-cli/internal/annotate"
"github.com/sirrend/terrap-cli/internal/cli_utils"
"github.com/sirrend/terrap-cli/internal/commons"
"github.com/sirrend/terrap-cli/internal/files_handler"
"github.com/sirrend/terrap-cli/internal/rules_api"
"github.com/sirrend/terrap-cli/internal/parser"
"github.com/sirrend/terrap-cli/internal/receiver"
"github.com/sirrend/terrap-cli/internal/scanning"
"github.com/sirrend/terrap-cli/internal/state"
"github.com/sirrend/terrap-cli/internal/utils"
"github.com/sirrend/terrap-cli/internal/workspace"
"github.com/spf13/cobra"
"os"
"strings"
)

var (
Expand All @@ -30,8 +32,8 @@ var (

// appliedRules is used to keep track of the rules applied in context
type appliedRules struct {
ruleSet rules_api.RuleSet
rules []rules_api.Rule
ruleSet parser.RuleSet
rules []parser.Rule
}

// scanCmd represents the scan command
Expand All @@ -42,7 +44,7 @@ var scanCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
var workspace workspace.Workspace
var asText []appliedRules
asJson := map[string][]rules_api.Rule{}
asJson := map[string][]parser.Rule{}

providersSet := cmd.Flag("fixed-providers").Changed
if utils.IsInitialized(".") || providersSet {
Expand All @@ -65,7 +67,7 @@ var scanCmd = &cobra.Command{
if len(workspace.Providers) > 0 {

for provider, version := range workspace.Providers {
rulebook, err := rules_api.GetRules(provider, version.String())
rulebook, err := receiver.GetRules(provider, version.String())
// validate rulebook downloaded
if err != nil {
if strings.Contains(err.Error(), utils.StripProviderPrefix(provider)) {
Expand Down Expand Up @@ -105,7 +107,7 @@ var scanCmd = &cobra.Command{
// combine ruleSets with applied rules
} else {
if ruleset.Rules != nil {
var rules []rules_api.Rule
var rules []parser.Rule
for _, rule := range ruleset.Rules {
if apply, err := rule.DoesRuleApplyInContext(file, resource.Name, resource.Type); err == nil && apply {
rules = append(rules, rule)
Expand All @@ -132,7 +134,7 @@ var scanCmd = &cobra.Command{
utils.PrettyPrintStruct(map[string]interface{}{file: asJson})
}

asJson = map[string][]rules_api.Rule{} // reset for next provider
asJson = map[string][]parser.Rule{} // reset for next provider

} else {
if len(asText) > 0 {
Expand Down
9 changes: 5 additions & 4 deletions cmd/whats_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ package cmd

import (
"fmt"
"os"
"strings"

"github.com/enescakir/emoji"
"github.com/fatih/color"
"github.com/sirrend/terrap-cli/internal/cli_utils"
"github.com/sirrend/terrap-cli/internal/commons"
"github.com/sirrend/terrap-cli/internal/files_handler"
"github.com/sirrend/terrap-cli/internal/rules_api"
"github.com/sirrend/terrap-cli/internal/receiver"
"github.com/sirrend/terrap-cli/internal/state"
"github.com/sirrend/terrap-cli/internal/utils"
"github.com/sirrend/terrap-cli/internal/workspace"
"github.com/spf13/cobra"
"os"
"strings"
)

// whatsNewCmd represents the whatsNew command
Expand All @@ -40,7 +41,7 @@ var whatsNewCmd = &cobra.Command{
}

for provider, version := range workspace.Providers { // go over every provider in user's folder
rulebook, err := rules_api.GetRules(provider, version.String())
rulebook, err := receiver.GetRules(provider, version.String())
// validate rulebook downloaded
if err != nil {
if strings.Contains(err.Error(), utils.StripProviderPrefix(provider)) {
Expand Down
7 changes: 4 additions & 3 deletions internal/annotate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package annotate
import (
"bufio"
"fmt"
"github.com/sirrend/terrap-cli/internal/files_handler"
"github.com/sirrend/terrap-cli/internal/rules_api"
"os"

"github.com/sirrend/terrap-cli/internal/files_handler"
"github.com/sirrend/terrap-cli/internal/parser"
)

func AddLineInPosition(resource files_handler.Resource, newLine string, pos int) {
Expand Down Expand Up @@ -51,7 +52,7 @@ func AddLineInPosition(resource files_handler.Resource, newLine string, pos int)
writer.Flush()
}

func AddAnnotationByRuleSet(resource files_handler.Resource, ruleSet rules_api.RuleSet) {
func AddAnnotationByRuleSet(resource files_handler.Resource, ruleSet parser.RuleSet) {
for _, rule := range ruleSet.Rules {
pos := FindAttributeInResourceDeclaration(resource, rule.Path)

Expand Down
31 changes: 31 additions & 0 deletions internal/files_handler/block_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package files_handler

import (
"github.com/hashicorp/hcl/v2/hclwrite"
)

// Block represents a configuration block, which can contain attributes or nested blocks
type Block struct {
Type string
Attributes []*Attribute
Blocks []*Block
}

// Init initializes the Block object with the given hclwrite.Block and block type
func (b *Block) Init(block *hclwrite.Block, blockType string) {
b.Type = blockType
b.Attributes = []*Attribute{}
b.Blocks = []*Block{}

for name, attr := range block.Body().Attributes() {
a := &Attribute{}
a.Init(attr, name)
b.Attributes = append(b.Attributes, a)
}

for _, nestedBlock := range block.Body().Blocks() {
nested := &Block{}
nested.Init(nestedBlock, nestedBlock.Type())
b.Blocks = append(b.Blocks, nested)
}
}
45 changes: 29 additions & 16 deletions internal/files_handler/resource.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package files_handler

import (
"fmt"

"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/hashicorp/terraform-config-inspect/tfconfig"
"github.com/sirrend/terrap-cli/internal/rules_api"
"github.com/sirrend/terrap-cli/internal/parser"
"github.com/sirrend/terrap-cli/internal/utils"
"github.com/spf13/cast"
)
Expand All @@ -17,6 +19,7 @@ type Resource struct {
Provider tfconfig.ProviderRef
Pos tfconfig.SourcePos
Attributes []Attribute
BlockTypes []Block
}

// Init
Expand All @@ -33,8 +36,10 @@ func (r *Resource) Init(block *hclwrite.Block, metadata *tfconfig.Resource) {
r.Alias = block.Labels()[1]
r.Pos = metadata.Pos
r.Provider = metadata.Provider
r.FullNameSequence = r.Type + "." + r.Name + "." + r.Alias
r.FullNameSequence = fmt.Sprintf("%s.%s.%s", r.Type, r.Name, r.Alias)

r.analyzeAttributes(block) // fill the attributes slice
r.analyzeBlockTypes(block) // fill the blocks slice
}

// GetAttributesKeys
Expand Down Expand Up @@ -68,6 +73,22 @@ func (r *Resource) analyzeAttributes(block *hclwrite.Block) {
}
}

// analyzeBlockTypes
/*
@brief:
analyzeAttributes initializes all the attributes found under the given block
@params:
block - *hclwrite.Block - the block to inspect
*/
func (r *Resource) analyzeBlockTypes(block *hclwrite.Block) {
blockHolder := Block{}

for _, block := range block.Body().Blocks() {
blockHolder.Init(block, block.Type())
r.BlockTypes = append(r.BlockTypes, blockHolder)
}
}

// IsDataSource
/*
@brief:
Expand All @@ -76,11 +97,7 @@ func (r *Resource) analyzeAttributes(block *hclwrite.Block) {
bool
*/
func (r Resource) IsDataSource() bool {
if r.Type == "data" {
return true
}

return false
return r.Type == "data"
}

// IsResource
Expand All @@ -91,11 +108,7 @@ func (r Resource) IsDataSource() bool {
bool
*/
func (r Resource) IsResource() bool {
if r.Type == "resource" {
return true
}

return false
return r.Type == "resource"
}

// GetRuleset
Expand All @@ -109,16 +122,16 @@ func (r Resource) IsResource() bool {
*gabs.Container - the ruleset to execute
error - if exists
*/
func (r Resource) GetRuleset(rulebook rules_api.Rulebook, appearances map[string][]string) (rulesetObj rules_api.RuleSet, err error) {
var rules []rules_api.Rule
func (r Resource) GetRuleset(rulebook parser.Rulebook, appearances map[string][]string) (rulesetObj parser.RuleSet, err error) {
var rules []parser.Rule

if ruleset, err := rulebook.GetRuleSetByResource(r.Name, r.Type); err == nil {
if ruleset != nil {
if components, err := ruleset.Children(); err == nil {
for _, component := range components {
if rulesSlice, err := component.Children(); err == nil {
for _, rule := range rulesSlice {
rules = append(rules, rules_api.Rule{
rules = append(rules, parser.Rule{
Path: utils.MustUnquote(rule.Path("HumanReadablePath").String()),
Operation: utils.MustUnquote(rule.Path("Operation").String()),
ComponentName: utils.MustUnquote(rule.Path("AttributeKey").String()),
Expand All @@ -132,7 +145,7 @@ func (r Resource) GetRuleset(rulebook rules_api.Rulebook, appearances map[string
}
}

rulesetObj = rules_api.RuleSet{
rulesetObj = parser.RuleSet{
ResourceName: r.Name,
Appearances: appearances[r.Name],
Rules: rules,
Expand Down
21 changes: 11 additions & 10 deletions internal/files_handler/utils.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package files_handler

import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/hashicorp/terraform-config-inspect/tfconfig"
"github.com/sirrend/terrap-cli/internal/utils"
"os"
"path"
"path/filepath"
"strings"
)

// convertFileToHCLObject
Expand Down Expand Up @@ -80,7 +82,10 @@ func analyzeResources(resources map[string]*tfconfig.Resource) ([]Resource, erro

for _, block := range blocks {
if block.Type() == "data" || block.Type() == "resource" {
if strings.Contains(resourceData.Type+"."+resourceData.Name, block.Labels()[0]+"."+block.Labels()[1]) {

if strings.Contains(
fmt.Sprintf("%s.%s", resourceData.Type, resourceData.Name),
fmt.Sprintf("%s.%s", block.Labels()[0], block.Labels()[1])) {
r.Init(block, resourceData)
break // stop searching after allocating the right block
}
Expand Down Expand Up @@ -134,9 +139,5 @@ func getLocalModuleResources(dir string, module tfconfig.ModuleCall) ([]Resource
bool - true if tf, otherwise false
*/
func isTerraformFile(path string) bool {
if filepath.Ext(path) == ".tf" {
return true
}

return false
return filepath.Ext(path) == ".tf"
}
5 changes: 5 additions & 0 deletions internal/parser/component_rulebook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package parser

type ComponentRulebook struct {
Versions map[string]Version `json:"versions"`
}
Loading

0 comments on commit 51961a8

Please sign in to comment.