remove useless file. prevent getter in attribute from copiying slice and

map. remove useless exported function from resource
main
Martin Guibert 2021-06-02 12:01:55 +02:00
parent b5c48dbbfc
commit 3a544133e2
8 changed files with 57 additions and 95 deletions

34
pkg/helpers/interface.go Normal file
View File

@ -0,0 +1,34 @@
package helpers
import "strings"
func Join(elems []interface{}, sep string) string {
firstElemt, ok := elems[0].(string)
if !ok {
panic("cannot join a slice that contains something else than strings")
}
switch len(elems) {
case 0:
return ""
case 1:
return firstElemt
}
n := len(sep) * (len(elems) - 1)
for i := 0; i < len(elems); i++ {
n += len(elems[i].(string))
}
var b strings.Builder
b.Grow(n)
b.WriteString(firstElemt)
for _, s := range elems[1:] {
b.WriteString(sep)
elem, ok := s.(string)
if !ok {
panic("cannot join a slice that contains something else than strings")
}
b.WriteString(elem)
}
return b.String()
}

View File

@ -1,20 +0,0 @@
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/hashicorp/aws" {
version = "3.42.0"
hashes = [
"h1:quV6hK7ewiHWBznGWCb/gJ6JAPm6UtouBUrhAjv6oRY=",
"zh:126c856a6eedddd8571f161a826a407ba5655a37a6241393560a96b8c4beca1a",
"zh:1a4868e6ac734b5fc2e79a4a889d176286b66664aad709435aa6acee5871d5b0",
"zh:40fed7637ab8ddeb93bef06aded35d970f0628025b97459ae805463e8aa0a58a",
"zh:68def3c0a5a1aac1db6372c51daef858b707f03052626d3427ac24cba6f2014d",
"zh:6db7ec9c8d1803a0b6f40a664aa892e0f8894562de83061fa7ac1bc51ff5e7e5",
"zh:7058abaad595930b3f97dc04e45c112b2dbf37d098372a849081f7081da2fb52",
"zh:8c25adb15a19da301c478aa1f4a4d8647cabdf8e5dae8331d4490f80ea718c26",
"zh:8e129b847401e39fcbc54817726dab877f36b7f00ff5ed76f7b43470abe99ff9",
"zh:d268bb267a2d6b39df7ddee8efa7c1ef7a15cf335dfa5f2e64c9dae9b623a1b8",
"zh:d6eeb3614a0ab50f8e9ab5666ae5754ea668ce327310e5b21b7f04a18d7611a8",
"zh:f5d3c58055dff6e38562b75d3edc908cb2f1e45c6914f6b00f4773359ce49324",
]
}

View File

@ -1,21 +0,0 @@
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/hashicorp/aws" {
version = "3.42.0"
hashes = [
"h1:C6/yDp6BhuDFx0qdkBuJj/OWUJpAoraHTJaU6ac38Rw=",
"h1:quV6hK7ewiHWBznGWCb/gJ6JAPm6UtouBUrhAjv6oRY=",
"zh:126c856a6eedddd8571f161a826a407ba5655a37a6241393560a96b8c4beca1a",
"zh:1a4868e6ac734b5fc2e79a4a889d176286b66664aad709435aa6acee5871d5b0",
"zh:40fed7637ab8ddeb93bef06aded35d970f0628025b97459ae805463e8aa0a58a",
"zh:68def3c0a5a1aac1db6372c51daef858b707f03052626d3427ac24cba6f2014d",
"zh:6db7ec9c8d1803a0b6f40a664aa892e0f8894562de83061fa7ac1bc51ff5e7e5",
"zh:7058abaad595930b3f97dc04e45c112b2dbf37d098372a849081f7081da2fb52",
"zh:8c25adb15a19da301c478aa1f4a4d8647cabdf8e5dae8331d4490f80ea718c26",
"zh:8e129b847401e39fcbc54817726dab877f36b7f00ff5ed76f7b43470abe99ff9",
"zh:d268bb267a2d6b39df7ddee8efa7c1ef7a15cf335dfa5f2e64c9dae9b623a1b8",
"zh:d6eeb3614a0ab50f8e9ab5666ae5754ea668ce327310e5b21b7f04a18d7611a8",
"zh:f5d3c58055dff6e38562b75d3edc908cb2f1e45c6914f6b00f4773359ce49324",
]
}

View File

@ -15,9 +15,9 @@ func initAwsInstanceMetaData(resourceSchemaRepository resource.SchemaRepositoryI
resourceSchemaRepository.SetHumanReadableAttributesFunc(AwsInstanceResourceType, func(res *resource.AbstractResource) map[string]string { resourceSchemaRepository.SetHumanReadableAttributesFunc(AwsInstanceResourceType, func(res *resource.AbstractResource) map[string]string {
val := res.Attrs val := res.Attrs
attrs := make(map[string]string) attrs := make(map[string]string)
if tags := val.GetStringMap("tags"); tags != nil { if tags := val.GetMap("tags"); tags != nil {
if name, ok := tags["name"]; ok { if name, ok := tags["name"]; ok {
attrs["Name"] = name attrs["Name"] = name.(string)
} }
} }
return attrs return attrs

View File

@ -12,9 +12,9 @@ func initAwsRoute53HealthCheckMetaData(resourceSchemaRepository resource.SchemaR
resourceSchemaRepository.SetHumanReadableAttributesFunc(AwsRoute53HealthCheckResourceType, func(res *resource.AbstractResource) map[string]string { resourceSchemaRepository.SetHumanReadableAttributesFunc(AwsRoute53HealthCheckResourceType, func(res *resource.AbstractResource) map[string]string {
val := res.Attrs val := res.Attrs
attrs := make(map[string]string) attrs := make(map[string]string)
if tags := val.GetStringMap("tags"); tags != nil { if tags := val.GetMap("tags"); tags != nil {
if name, ok := tags["name"]; ok { if name, ok := tags["name"]; ok {
attrs["Name"] = name attrs["Name"] = name.(string)
} }
} }
port := val.GetInt("port") port := val.GetInt("port")

View File

@ -3,8 +3,8 @@ package aws
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"strings"
"github.com/cloudskiff/driftctl/pkg/helpers"
"github.com/cloudskiff/driftctl/pkg/resource" "github.com/cloudskiff/driftctl/pkg/resource"
"github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/hashcode"
) )
@ -111,14 +111,14 @@ func initAwsSecurityGroupRuleMetaData(resourceSchemaRepository resource.SchemaRe
case "ingress": case "ingress":
sourceOrDestination = "Source" sourceOrDestination = "Source"
} }
if ipv4 := val.GetStringSlice("cidr_blocks"); len(ipv4) > 0 { if ipv4 := val.GetSlice("cidr_blocks"); len(ipv4) > 0 {
attrs[sourceOrDestination] = strings.Join(ipv4, ", ") attrs[sourceOrDestination] = helpers.Join(ipv4, ", ")
} }
if ipv6 := val.GetStringSlice("ipv6_cidr_blocks"); len(ipv6) > 0 { if ipv6 := val.GetSlice("ipv6_cidr_blocks"); len(ipv6) > 0 {
attrs[sourceOrDestination] = strings.Join(ipv6, ", ") attrs[sourceOrDestination] = helpers.Join(ipv6, ", ")
} }
if prefixList := val.GetStringSlice("prefix_list_ids"); len(prefixList) > 0 { if prefixList := val.GetSlice("prefix_list_ids"); len(prefixList) > 0 {
attrs[sourceOrDestination] = strings.Join(prefixList, ", ") attrs[sourceOrDestination] = helpers.Join(prefixList, ", ")
} }
if sourceSgID := val.GetString("source_security_group_id"); sourceSgID != nil && *sourceSgID != "" { if sourceSgID := val.GetString("source_security_group_id"); sourceSgID != nil && *sourceSgID != "" {
attrs[sourceOrDestination] = *sourceSgID attrs[sourceOrDestination] = *sourceSgID

View File

@ -1,7 +1,10 @@
package resource package resource
import ( import (
"encoding/json"
"github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json"
) )
type Deserializer struct { type Deserializer struct {
@ -16,8 +19,15 @@ func (s *Deserializer) Deserialize(ty string, rawList []cty.Value) ([]Resource,
resources := make([]Resource, 0) resources := make([]Resource, 0)
for _, rawResource := range rawList { for _, rawResource := range rawList {
rawResource := rawResource rawResource := rawResource
attrs := ToResourceAttributes(&rawResource) var attrs Attributes
res := s.factory.CreateAbstractResource(ty, rawResource.GetAttr("id").AsString(), *attrs)
bytes, _ := ctyjson.Marshal(rawResource, rawResource.Type())
err := json.Unmarshal(bytes, &attrs)
if err != nil {
return nil, err
}
res := s.factory.CreateAbstractResource(ty, rawResource.GetAttr("id").AsString(), attrs)
resources = append(resources, res) resources = append(resources, res)
} }
return resources, nil return resources, nil

View File

@ -8,8 +8,6 @@ import (
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json"
) )
type Resource interface { type Resource interface {
@ -113,21 +111,6 @@ func Sort(res []Resource) []Resource {
return res return res
} }
func ToResourceAttributes(val *cty.Value) *Attributes {
if val == nil {
return nil
}
bytes, _ := ctyjson.Marshal(*val, val.Type())
var attrs Attributes
err := json.Unmarshal(bytes, &attrs)
if err != nil {
panic(err)
}
return &attrs
}
type Attributes map[string]interface{} type Attributes map[string]interface{}
func (a *Attributes) Copy() *Attributes { func (a *Attributes) Copy() *Attributes {
@ -162,18 +145,6 @@ func (a *Attributes) GetString(path string) *string {
return &v return &v
} }
func (a *Attributes) GetStringSlice(path string) []string {
val := a.GetSlice(path)
if val == nil {
return nil
}
slice := make([]string, 0, len(val))
for _, v := range val {
slice = append(slice, v.(string))
}
return slice
}
func (a *Attributes) GetBool(path string) *bool { func (a *Attributes) GetBool(path string) *bool {
val, exist := (*a)[path] val, exist := (*a)[path]
if !exist { if !exist {
@ -209,18 +180,6 @@ func (a *Attributes) GetMap(path string) map[string]interface{} {
return val.(map[string]interface{}) return val.(map[string]interface{})
} }
func (a *Attributes) GetStringMap(path string) map[string]string {
val := a.GetMap(path)
if val == nil {
return nil
}
stringMap := make(map[string]string)
for k, v := range val {
stringMap[k] = v.(string)
}
return stringMap
}
func (a *Attributes) SafeDelete(path []string) { func (a *Attributes) SafeDelete(path []string) {
for i, key := range path { for i, key := range path {
if i == len(path)-1 { if i == len(path)-1 {