Skip to content

Commit af30720

Browse files
lantolicarriecwk
andauthored
chore: Create plugin command with test infra (#12)
* initial command * pass parameters * overwriteOutput flag * add unit tests * add simple transformation functionality * transformation tests * add comment * extract consts * adjust doc * refactor PreRun so argument required checks can happen first * add scaffolding for convert command adv_cluster v1 to v2 * adjust doc * update usage doc * typo * Update README.md Co-authored-by: carriecwk <[email protected]> --------- Co-authored-by: carriecwk <[email protected]>
1 parent e0e3eba commit af30720

File tree

16 files changed

+358
-23
lines changed

16 files changed

+358
-23
lines changed

.github/workflows/code-health.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ jobs:
1818
go-version-file: 'go.mod'
1919
- name: Build
2020
run: make build
21+
- name: Unit Test
22+
run: make test
2123
lint:
2224
runs-on: ubuntu-latest
2325
permissions: {}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
bin
22
bin-plugin
3+
.vscode

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ tools: ## Install the dev tools (dependencies)
2323
clean: ## Clean binary folders
2424
rm -rf ./bin ./bin-plugin
2525

26+
.PHONY: test
27+
test: ## Run unit tests
28+
go test ./... -timeout=30s -parallel=4 -race
29+
30+
.PHONY: test-update
31+
test-update: ## Run unit tests and update the golden files
32+
go test ./... -timeout=30s -parallel=4 -race -update
33+
2634
.PHONY: local
2735
local: clean build ## Allow to run the plugin locally
2836
@echo "==> Configuring plugin locally"

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ atlas plugin install github.com/mongodb-labs/atlas-cli-plugin-terraform
1717

1818
## Usage
1919

20+
### Convert cluster to advanced_cluster v2
21+
If you want to convert a Terraform configuration from `mongodbatlas_cluster` to `mongodbatlas_advanced_cluster` schema v2, use the following command:
22+
```bash
23+
atlas terraform clusterToAdvancedCluster --file in.tf --output out.tf
24+
```
25+
26+
you can also use shorter aliases, e.g.:
27+
```bash
28+
atlas tf clu2adv -f in.tf -o out.tf
29+
```
30+
31+
If you want to overwrite the output file if it exists, or even use the same output file as the input file, use the `--overwriteOutput true` or the `-w` flag.
32+
2033

2134
## Contributing
2235

cmd/plugin/main.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import (
44
"fmt"
55
"os"
66

7-
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/cli/hello"
7+
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/cli/adv2"
8+
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/cli/clu2adv"
89
"github.com/spf13/cobra"
910
)
1011

@@ -14,10 +15,7 @@ func main() {
1415
Short: "Utilities for Terraform's MongoDB Atlas Provider",
1516
Aliases: []string{"tf"},
1617
}
17-
18-
terraformCmd.AddCommand(
19-
hello.Builder(),
20-
)
18+
terraformCmd.AddCommand(clu2adv.Builder(), adv2.Builder())
2119

2220
completionOption := &cobra.CompletionOptions{
2321
DisableDefaultCmd: true,

go.mod

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,28 @@ module github.com/mongodb-labs/atlas-cli-plugin-terraform
22

33
go 1.23.4
44

5-
require github.com/spf13/cobra v1.8.1
5+
require (
6+
github.com/hashicorp/hcl/v2 v2.23.0
7+
github.com/sebdah/goldie/v2 v2.5.5
8+
github.com/spf13/afero v1.12.0
9+
github.com/spf13/cobra v1.8.1
10+
github.com/stretchr/testify v1.4.0
11+
)
612

713
require (
14+
github.com/agext/levenshtein v1.2.1 // indirect
15+
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
16+
github.com/davecgh/go-spew v1.1.1 // indirect
17+
github.com/google/go-cmp v0.6.0 // indirect
818
github.com/inconshreveable/mousetrap v1.1.0 // indirect
19+
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
20+
github.com/pmezard/go-difflib v1.0.0 // indirect
21+
github.com/sergi/go-diff v1.3.1 // indirect
922
github.com/spf13/pflag v1.0.5 // indirect
23+
github.com/zclconf/go-cty v1.16.2 // indirect
24+
golang.org/x/mod v0.22.0 // indirect
25+
golang.org/x/sync v0.10.0 // indirect
26+
golang.org/x/text v0.21.0 // indirect
27+
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
28+
gopkg.in/yaml.v2 v2.4.0 // indirect
1029
)

go.sum

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,61 @@
1+
github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
2+
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
3+
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=
4+
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
15
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
6+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
8+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9+
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
10+
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
11+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
12+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
13+
github.com/hashicorp/hcl/v2 v2.23.0 h1:Fphj1/gCylPxHutVSEOf2fBOh1VE4AuLV7+kbJf3qos=
14+
github.com/hashicorp/hcl/v2 v2.23.0/go.mod h1:62ZYHrXgPoX8xBnzl8QzbWq4dyDsDtfCRgIq1rbJEvA=
215
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
316
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
17+
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
18+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
19+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
20+
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
21+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
22+
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
23+
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
24+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
25+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
26+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
427
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
28+
github.com/sebdah/goldie/v2 v2.5.5 h1:rx1mwF95RxZ3/83sdS4Yp7t2C5TCokvWP4TBRbAyEWY=
29+
github.com/sebdah/goldie/v2 v2.5.5/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI=
30+
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
31+
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
32+
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
33+
github.com/spf13/afero v1.12.0 h1:UcOPyRBYczmFn6yvphxkn9ZEOY65cpwGKb5mL36mrqs=
34+
github.com/spf13/afero v1.12.0/go.mod h1:ZTlWwG4/ahT8W7T0WQ5uYmjI9duaLQGy3Q2OAl4sk/4=
535
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
636
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
737
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
838
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
39+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
40+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
41+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
42+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
43+
github.com/zclconf/go-cty v1.16.2 h1:LAJSwc3v81IRBZyUVQDUdZ7hs3SYs9jv0eZJDWHD/70=
44+
github.com/zclconf/go-cty v1.16.2/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE=
45+
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940 h1:4r45xpDWB6ZMSMNJFMOjqrGHynW3DIBuR2H9j0ug+Mo=
46+
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940/go.mod h1:CmBdvvj3nqzfzJ6nTCIwDTPZ56aVGvDrmztiO5g3qrM=
47+
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
48+
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
49+
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
50+
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
51+
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
52+
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
53+
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
54+
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
955
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
56+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
57+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
58+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
59+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
60+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
1061
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/cli/adv2/adv2.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package adv2
2+
3+
import (
4+
"errors"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
func Builder() *cobra.Command {
10+
cmd := &cobra.Command{
11+
Use: "advancedClusterV1ToV2",
12+
Short: "Convert advanced_cluster v1 to v2",
13+
Long: "Convert a Terraform configuration from mongodbatlas_advanced_cluster schema v1 to v2",
14+
Aliases: []string{"adv2"},
15+
RunE: func(_ *cobra.Command, _ []string) error {
16+
return errors.New("TODO: not implemented yet, will be implemented in the future")
17+
},
18+
}
19+
return cmd
20+
}

internal/cli/clu2adv/clu2adv.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package clu2adv
2+
3+
import (
4+
"github.com/spf13/afero"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
func Builder() *cobra.Command {
9+
o := &opts{fs: afero.NewOsFs()}
10+
cmd := &cobra.Command{
11+
Use: "clusterToAdvancedCluster",
12+
Short: "Convert cluster to advanced_cluster v2",
13+
Long: "Convert a Terraform configuration from mongodbatlas_cluster to mongodbatlas_advanced_cluster schema v2",
14+
Aliases: []string{"clu2adv"},
15+
RunE: func(_ *cobra.Command, _ []string) error {
16+
if err := o.PreRun(); err != nil {
17+
return err
18+
}
19+
return o.Run()
20+
},
21+
}
22+
cmd.Flags().StringVarP(&o.file, "file", "f", "", "input file")
23+
_ = cmd.MarkFlagRequired("file")
24+
cmd.Flags().StringVarP(&o.output, "output", "o", "", "output file")
25+
_ = cmd.MarkFlagRequired("output")
26+
cmd.Flags().BoolVarP(&o.overwriteOutput, "overwriteOutput", "w", false, "overwrite output file if exists")
27+
return cmd
28+
}

internal/cli/clu2adv/opts.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package clu2adv
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/file"
7+
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/hcl"
8+
"github.com/spf13/afero"
9+
)
10+
11+
type opts struct {
12+
fs afero.Fs
13+
file string
14+
output string
15+
overwriteOutput bool
16+
}
17+
18+
func (o *opts) PreRun() error {
19+
if err := file.MustExist(o.fs, o.file); err != nil {
20+
return err
21+
}
22+
if !o.overwriteOutput {
23+
return file.MustNotExist(o.fs, o.output)
24+
}
25+
return nil
26+
}
27+
28+
func (o *opts) Run() error {
29+
inConfig, err := afero.ReadFile(o.fs, o.file)
30+
if err != nil {
31+
return fmt.Errorf("failed to read file %s: %w", o.file, err)
32+
}
33+
outConfig, err := hcl.ClusterToAdvancedCluster(inConfig)
34+
if err != nil {
35+
return err
36+
}
37+
if err := afero.WriteFile(o.fs, o.output, outConfig, 0o600); err != nil {
38+
return fmt.Errorf("failed to write file %s: %w", o.output, err)
39+
}
40+
return nil
41+
}

0 commit comments

Comments
 (0)