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

[WIP] add a Terraform module interface #73

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
120 changes: 120 additions & 0 deletions cmd/terraform.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright © 2020 Packet, an Equinix Company <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package cmd

import (
"errors"

// TODO(displague) use of internal package not allowed
// "github.com/hashicorp/terraform/internal/initwd"
"github.com/hashicorp/terraform/registry"
"github.com/hashicorp/terraform/registry/regsrc"
"github.com/spf13/cobra"
)

// projectCmd represents the project command
var terraformCmd = &cobra.Command{
Use: "terraform",
// Aliases: []string{"registry"},
Short: "Terraform operations",
Long: `Terraform operations: create, delete, update and get`,
}

func init() {
createTerraformCmd.Flags().StringVarP(&name, "name", "n", "", "Name of the project")
retrieveTerraformCmd.Flags().StringVarP(&terraformName, "name", "n", "", "Name of the terraform module")

_ = createTerraformCmd.MarkFlagRequired("name")
terraformCmd.AddCommand(createTerraformCmd, retrieveTerraformCmd) // deleteTerraformCmd, updateTerraformCmd

rootCmd.AddCommand(terraformCmd)
}

// projectCreateCmd represents the projectCreate command
var createTerraformCmd = &cobra.Command{
Use: "create",
Short: "Creates a terraform module project",
Long: `Example:

packet terraform create --name [project_name]

`,
RunE: func(cmd *cobra.Command, args []string) error {
if terraformName != "" {
/*
dir := "/Users/marques/terraform/" + terraformName
reg := registry.NewClient(nil, nil)

diag := initwd.DirFromModule(dir, terraformName, "packet/"+terraformName+"/packet/1.0.0", reg, nil)
if diag != nil {
return diag
}
*/
return errors.New("create not supported")
}
return errors.New("need a name")
},
}

var terraformName string

// retrieveTerraformCmd represents the retrieveTerraform command
var retrieveTerraformCmd = &cobra.Command{
Use: "get",
Short: "Retrieves all available terraform modules or a single terraform modules",
Long: `Example:

Retrieve all projects:
packet terraform get

Retrieve a specific project:
packet terraform get -n [project_name]
`,
RunE: func(cmd *cobra.Command, args []string) error {
if terraformName != "" {
reg := registry.NewClient(nil, nil)
modQuery := &regsrc.Module{RawNamespace: "displague", RawName: terraformName, RawProvider: "packet"}

resp, err := reg.ModuleVersions(modQuery)
if err != nil {
return errors.New("could not get module versions")
// TODO(displague) wrap err
}

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

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

// TODO(displague) hashicorp registry client does not include list names by namespaces function
return errors.New("list not supported")
},
}
1 change: 1 addition & 0 deletions docs/packet.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Command line interface for Packet Host
* [packet plan](packet_plan.md) - Plan operations
* [packet project](packet_project.md) - Project operations
* [packet ssh-key](packet_ssh-key.md) - SSH key operations
* [packet terraform](packet_terraform.md) - Terraform operations
* [packet user](packet_user.md) - User operations
* [packet virtual-network](packet_virtual-network.md) - Virtual network operations
* [packet volume](packet_volume.md) - Volume operations
Expand Down
26 changes: 26 additions & 0 deletions docs/packet_terraform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## packet terraform

Terraform operations

### Synopsis

Terraform operations: create, delete, update and get

### Options

```
-h, --help help for terraform
```

### Options inherited from parent commands

```
--config string Path to JSON or YAML configuration file
```

### SEE ALSO

* [packet](packet.md) - Command line interface for Packet Host
* [packet terraform create](packet_terraform_create.md) - Creates a terraform module project
* [packet terraform get](packet_terraform_get.md) - Retrieves all available terraform modules or a single terraform modules

33 changes: 33 additions & 0 deletions docs/packet_terraform_create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## packet terraform create

Creates a terraform module project

### Synopsis

Example:

packet terraform create --name [project_name]



```
packet terraform create [flags]
```

### Options

```
-h, --help help for create
-n, --name string Name of the project
```

### Options inherited from parent commands

```
--config string Path to JSON or YAML configuration file
```

### SEE ALSO

* [packet terraform](packet_terraform.md) - Terraform operations

36 changes: 36 additions & 0 deletions docs/packet_terraform_get.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## packet terraform get

Retrieves all available terraform modules or a single terraform modules

### Synopsis

Example:

Retrieve all projects:
packet terraform get

Retrieve a specific project:
packet terraform get -n [project_name]


```
packet terraform get [flags]
```

### Options

```
-h, --help help for get
-n, --name string Name of the terraform module
```

### Options inherited from parent commands

```
--config string Path to JSON or YAML configuration file
```

### SEE ALSO

* [packet terraform](packet_terraform.md) - Terraform operations

9 changes: 2 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@ module github.com/packethost/packet-cli
go 1.13

require (
github.com/chzyer/logex v1.1.10 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 // indirect
github.com/ghodss/yaml v1.0.0
github.com/hashicorp/terraform v0.12.29
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a // indirect
github.com/lunixbochs/vtclean v0.0.0-20170504063817-d14193dfc626 // indirect
github.com/manifoldco/promptui v0.3.0
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.3 // indirect
github.com/mattn/go-runewidth v0.0.2 // indirect
github.com/olekukonko/tablewriter v0.0.0-20180506121414-d4647c9c7a84
github.com/packethost/packngo v0.2.0
github.com/pkg/errors v0.8.0
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.4.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
Expand Down
Loading