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

Fix: Resource config name #124

Merged
merged 4 commits into from
Jun 29, 2020
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
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## [Unreleased]

### Fixed

- Error with the Resource name always being the alphanumeric instead of the Tag Name
([PR #124](https://github.com/cycloidio/terracognita/issues/124))

## [0.5.0] _2020-06-19_

### Added
Expand All @@ -25,7 +30,7 @@
- Upgraded all the Provider and Terraform versions
([PR #114](https://github.com/cycloidio/terracognita/pull/114))

## Fixed
### Fixed

- Error when importing `aws_iam_user_group_membership` without groups
([Issue #104](https://github.com/cycloidio/terracognita/issues/104))
Expand Down Expand Up @@ -73,7 +78,7 @@
- 'raws' lib to be an internal library instead of a dependency
([Issue #69](https://github.com/cycloidio/terracognita/issues/69))

## Fixed
### Fixed

- '--region' flag working for different subcommands
([PR #63](https://github.com/cycloidio/terracognita/pull/63))
Expand Down
4 changes: 2 additions & 2 deletions hcl/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ func (w *Writer) Has(key string) (bool, error) {
name := strings.Join(keys[1:], "")

if _, ok := w.Config["resource"].(map[string]map[string]interface{})[keys[0]][name]; ok {
return false, errors.Wrapf(errcode.ErrWriterAlreadyExistsKey, "with key %q", key)
return true, nil
}

return true, nil
return false, nil
}

// Sync writes the content of the Config to the
Expand Down
12 changes: 11 additions & 1 deletion hcl/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ func TestHCLWriter_Write(t *testing.T) {
value = map[string]interface{}{
"key": "value",
}
key = "type.name"
)

err := hw.Write("type.name", value)
err := hw.Write(key, value)
require.NoError(t, err)

assert.Equal(t, map[string]interface{}{
Expand All @@ -43,6 +44,15 @@ func TestHCLWriter_Write(t *testing.T) {
},
},
}, hw.Config)
t.Run("Has", func(t *testing.T) {
ok, err := hw.Has(key)
require.NoError(t, err)
assert.True(t, ok)

ok, err = hw.Has("type.new")
require.NoError(t, err)
assert.False(t, ok)
})
})
t.Run("ErrRequiredKey", func(t *testing.T) {
var (
Expand Down
6 changes: 3 additions & 3 deletions provider/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func (r *resource) State(w writer.Writer) error {
// and store it, so net time it'll use that one on any config
if r.configName == "" {
configName := tag.GetNameFromTag(r.provider.TagKey(), r.data, r.id)
if ok, err := w.Has(configName); err != nil {
if ok, err := w.Has(fmt.Sprintf("%s.%s", r.resourceType, configName)); err != nil {
return err
} else if ok {
configName = pwgen.Alpha(5)
Expand Down Expand Up @@ -428,8 +428,8 @@ func (r *resource) HCL(w writer.Writer) error {
// If it does not have any configName we will generate one
// and store it, so net time it'll use that one on any config
if r.configName == "" {
configName := fmt.Sprintf("%s.%s", r.resourceType, tag.GetNameFromTag(r.provider.TagKey(), r.data, r.id))
if ok, err := w.Has(configName); err != nil {
configName := tag.GetNameFromTag(r.provider.TagKey(), r.data, r.id)
if ok, err := w.Has(fmt.Sprintf("%s.%s", r.resourceType, configName)); err != nil {
return err
} else if ok {
configName = pwgen.Alpha(5)
Expand Down
14 changes: 12 additions & 2 deletions state/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestWrite(t *testing.T) {
b = &bytes.Buffer{}
sw = state.NewWriter(b)
tp = "aws_iam_user"
key = "aws.name"
)
defer ctrl.Finish()

Expand All @@ -53,12 +54,21 @@ func TestWrite(t *testing.T) {

prv.EXPECT().String().Return("aws").AnyTimes()

err = sw.Write("aws.name", res)
err = sw.Write(key, res)
require.NoError(t, err)

assert.Equal(t, map[string]provider.Resource{
"aws.name": res,
key: res,
}, sw.Config)
t.Run("Has", func(t *testing.T) {
ok, err := sw.Has(key)
require.NoError(t, err)
assert.True(t, ok)

ok, err = sw.Has("aws.new")
require.NoError(t, err)
assert.False(t, ok)
})
})
t.Run("ErrRequiredKey", func(t *testing.T) {
sw := state.NewWriter(nil)
Expand Down