Skip to content

Commit

Permalink
fix: harness tf state struct and state upload (#202)
Browse files Browse the repository at this point in the history
* fix tf state struct

* simplify resource links fn
  • Loading branch information
floreks authored May 29, 2024
1 parent 7ff5253 commit 5f8d115
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 43 deletions.
73 changes: 33 additions & 40 deletions pkg/harness/tool/terraform/api/v4/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions pkg/harness/tool/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}),
Expand All @@ -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(),
Expand All @@ -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()),
Expand Down

0 comments on commit 5f8d115

Please sign in to comment.