Skip to content

Commit

Permalink
initial terraform command work
Browse files Browse the repository at this point in the history
  • Loading branch information
displague committed Aug 8, 2020
1 parent 94aba8b commit 34f7c44
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
52 changes: 21 additions & 31 deletions cmd/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@
package cmd

import (
"fmt"

"github.com/hashicorp/terraform/internal/initwd"
"github.com/hashicorp/terraform/registry"
"github.com/hashicorp/terraform/registry/regsrc"

"github.com/packethost/packngo"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -58,29 +55,15 @@ packet terraform create --name [project_name]
`,
Run: func(cmd *cobra.Command, args []string) {
req := packngo.ProjectCreateRequest{
Name: name,
}

if organizationID != "" {
req.OrganizationID = organizationID
}

if paymentMethodID != "" {
req.PaymentMethodID = paymentMethodID
}
if terraformName != "" {
dir := "/Users/marques/terraform/" + terraformName
reg := registry.NewClient(nil, nil)

p, _, err := PacknGo.Projects.Create(&req)
if err != nil {
fmt.Println("Client error:", err)
return
diag := initwd.DirFromModule(dir, terraformName, "packet/"+terraformName+"/packet/1.0.0", reg, nil)
if diag != nil {
panic(diag)
}
}

data := make([][]string, 1)

data[0] = []string{p.ID, p.Name, p.Created}
header := []string{"ID", "Name", "Created"}
output(p, header, &data)
},
}

Expand All @@ -98,24 +81,31 @@ packet terraform get
Retrieve a specific project:
packet terraform get -n [project_name]
`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
// TODO(displague) hashicorp registry client does not include list names by namespaces function
if terraformName != "" {
reg := registry.NewClient(nil, nil)
modQuery := &regsrc.Module{RawNamespace: "displague", RawProvider: "packet"}
modQuery := &regsrc.Module{RawNamespace: "displague", RawName: terraformName, RawProvider: "packet"}

resp, err := reg.ModuleVersions(modQuery)
if err != nil {
fmt.Println("Client error:", err)
return
return err
}

data := make([][]string, len(resp.Modules))
for n, mod := range resp.Modules {
for _, v := range mod.Versions {
data[n] = []string{mod.Source, v.Version}
mod, err := regsrc.ParseModuleSource(mod.Source)
if err != nil {
return err
}

data[n] = []string{mod.RawName, v.Version}
}
}
header := []string{"ID", "Name", "Created"}
header := []string{"Source", "Version"}
output(resp, header, &data)
}
return nil
},
}
Loading

0 comments on commit 34f7c44

Please sign in to comment.