From 5f8d115227e3fb266b2db4f04c720faedb49ffae Mon Sep 17 00:00:00 2001 From: Sebastian Florek Date: Wed, 29 May 2024 10:29:59 -0500 Subject: [PATCH] fix: harness tf state struct and state upload (#202) * fix tf state struct * simplify resource links fn --- pkg/harness/tool/terraform/api/v4/state.go | 73 ++++++++++------------ pkg/harness/tool/terraform/terraform.go | 6 +- 2 files changed, 36 insertions(+), 43 deletions(-) diff --git a/pkg/harness/tool/terraform/api/v4/state.go b/pkg/harness/tool/terraform/api/v4/state.go index 0f167903..ce71cde5 100644 --- a/pkg/harness/tool/terraform/api/v4/state.go +++ b/pkg/harness/tool/terraform/api/v4/state.go @@ -4,16 +4,32 @@ import ( "encoding/json" "maps" - "github.com/pluralsh/polly/algorithms" "k8s.io/klog/v2" ) // State represents a terraform state file structure. // Targets state file schema version 4. type State struct { - Version string `json:"terraform_version"` - Outputs Outputs `json:"outputs"` - Resources Resources `json:"resources"` + Version string `json:"terraform_version"` + Values Values `json:"values"` +} + +type Values struct { + Outputs Outputs `json:"outputs"` + RootModule RootModule `json:"root_module"` +} + +type RootModule struct { + Resources Resources `json:"resources"` + ChildModules ChildModules `json:"child_modules"` +} + +type ChildModules []ChildModule + +type ChildModule struct { + Address string `json:"address"` + Resources Resources `json:"resources"` + ChildModules ChildModules `json:"child_modules"` } type Outputs map[string]Output @@ -41,50 +57,27 @@ func (in *Output) ValueString() string { type Resources []Resource type Resource struct { - Mode string `json:"mode"` - Type string `json:"type"` - Name string `json:"name"` - Provider string `json:"provider"` - Instances []ResourceInstance `json:"instances"` + Address string `json:"address"` + Mode string `json:"mode"` + Type string `json:"type"` + Name string `json:"name"` + Provider string `json:"provider_name"` + SchemaVersion int `json:"schema_version"` + Values map[string]interface{} `json:"values"` + SensitiveValues map[string]interface{} `json:"sensitive_values"` + DependsOn []string `json:"depends_on"` } func (in Resource) Configuration() string { configurationMap := make(map[string]interface{}) - attributesList := algorithms.Map( - in.Instances, - func(i ResourceInstance) map[string]interface{} { - return i.Attributes - }, - ) - - for _, attributes := range attributesList { - maps.Copy(configurationMap, attributes) - } + + maps.Copy(configurationMap, in.Values) + maps.Copy(configurationMap, in.SensitiveValues) configuration, _ := json.Marshal(configurationMap) return string(configuration) } func (in Resource) Links() []string { - links := make([]string, 0) - - for _, instance := range in.Instances { - links = append(links, instance.Dependencies...) - } - - return links -} - -type ResourceMode string - -const ( - ResourceModeManaged ResourceMode = "managed" - ResourceModeData ResourceMode = "data" -) - -type ResourceInstances []ResourceInstance - -type ResourceInstance struct { - Attributes map[string]interface{} `json:"attributes"` - Dependencies []string `json:"dependencies"` + return in.DependsOn } diff --git a/pkg/harness/tool/terraform/terraform.go b/pkg/harness/tool/terraform/terraform.go index 07ab9aa2..2f6d373a 100644 --- a/pkg/harness/tool/terraform/terraform.go +++ b/pkg/harness/tool/terraform/terraform.go @@ -26,7 +26,7 @@ func (in *Terraform) State() (*console.StackStateAttributes, error) { return &console.StackStateAttributes{ State: algorithms.Map( - state.Resources, + state.Values.RootModule.Resources, func(r v4.Resource) *console.StackStateResourceAttributes { return in.resource(r) }), @@ -53,7 +53,7 @@ func (in *Terraform) Output() ([]*console.StackOutputAttributes, error) { return nil, err } - for k, v := range state.Outputs { + for k, v := range state.Values.Outputs { result = append(result, &console.StackOutputAttributes{ Name: k, Value: v.ValueString(), @@ -80,7 +80,7 @@ func (in *Terraform) Modifier(stage console.StepStage) v1.Modifier { func (in *Terraform) resource(r v4.Resource) *console.StackStateResourceAttributes { return &console.StackStateResourceAttributes{ - Identifier: r.Name, + Identifier: r.Address, Resource: r.Type, Name: r.Name, Configuration: lo.ToPtr(r.Configuration()),