Browse Source

Adding targets section for configuration (#30)

* Adding targets section for configuration

Will add a --target <resource> for each item in a comma separated list to the plan and apply commands

Updated the docs to show the new behavior. If left undefined there is no regression in functionality.

* Combine the --target append steps

No reason to waste lines

* Updating docs for YAML list of strings

* Documentation for single and multiple targets
pull/31/head
edwinavalos 8 years ago
committed by Jacob McCann
parent
commit
4197abea8d
  1. 43
      DOCS.md
  2. 9
      main.go
  3. 17
      plugin.go

43
DOCS.md

@ -124,6 +124,49 @@ pipeline:
root_dir: some/path/here root_dir: some/path/here
``` ```
## Targets
You may want to only target a specific list of resources within your terraform code. To achieve this you can specify the `targets` parameter. If left undefined all resources will be planned/applied against as the default behavior.
Single target:
```yaml
pipeline:
terraform:
image: jmccann/drone-terraform:0.5
plan: false
targets: aws_security_group.generic_sg
remote:
backend: S3
config:
bucket: my-terraform-config-bucket
key: tf-states/my-project
region: us-east-1
vars:
app_name: my-project
app_version: 1.0.0
```
Multiple targets:
```yaml
pipeline:
terraform:
image: jmccann/drone-terraform:0.5
plan: false
targets:
- aws_security_group.generic_sg
- aws_security_group.app_sg
remote:
backend: S3
config:
bucket: my-terraform-config-bucket
key: tf-states/my-project
region: us-east-1
vars:
app_name: my-project
app_version: 1.0.0
```
## Parallelism ## Parallelism
You may want to limit the number of concurrent operations as Terraform walks its graph. You may want to limit the number of concurrent operations as Terraform walks its graph.
If you want to change Terraform's default parallelism (currently equal to 10) then set the `parallelism` parameter. If you want to change Terraform's default parallelism (currently equal to 10) then set the `parallelism` parameter.

9
main.go

@ -4,8 +4,8 @@ import (
"encoding/json" "encoding/json"
"os" "os"
"github.com/joho/godotenv"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/joho/godotenv"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -73,6 +73,12 @@ func main() {
Name: "env-file", Name: "env-file",
Usage: "source env file", Usage: "source env file",
}, },
cli.StringSliceFlag{
Name: "targets",
Usage: "targets to run apply or plan on",
EnvVar: "PLUGIN_TARGETS",
},
} }
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
@ -116,6 +122,7 @@ func run(c *cli.Context) error {
RoleARN: c.String("role_arn_to_assume"), RoleARN: c.String("role_arn_to_assume"),
RootDir: c.String("root_dir"), RootDir: c.String("root_dir"),
Parallelism: c.Int("parallelism"), Parallelism: c.Int("parallelism"),
Targets: c.StringSlice("targets"),
}, },
} }

17
plugin.go

@ -2,11 +2,11 @@ package main
import ( import (
"fmt" "fmt"
"github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds" "github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts" "github.com/aws/aws-sdk-go/service/sts"
"github.com/Sirupsen/logrus"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
@ -25,6 +25,7 @@ type (
RoleARN string RoleARN string
RootDir string RootDir string
Parallelism int Parallelism int
Targets []string
} }
Remote struct { Remote struct {
@ -52,9 +53,9 @@ func (p Plugin) Exec() error {
commands = append(commands, remoteConfigCommand(remote)) commands = append(commands, remoteConfigCommand(remote))
} }
commands = append(commands, getModules()) commands = append(commands, getModules())
commands = append(commands, planCommand(p.Config.Vars, p.Config.Secrets, p.Config.Parallelism)) commands = append(commands, planCommand(p.Config.Vars, p.Config.Secrets, p.Config.Parallelism, p.Config.Targets))
if !p.Config.Plan { if !p.Config.Plan {
commands = append(commands, applyCommand(p.Config.Parallelism)) commands = append(commands, applyCommand(p.Config.Parallelism, p.Config.Targets))
} }
commands = append(commands, deleteCache()) commands = append(commands, deleteCache())
@ -123,11 +124,14 @@ func getModules() *exec.Cmd {
) )
} }
func planCommand(variables map[string]string, secrets map[string]string, parallelism int) *exec.Cmd { func planCommand(variables map[string]string, secrets map[string]string, parallelism int, targets []string) *exec.Cmd {
args := []string{ args := []string{
"plan", "plan",
"-out=plan.tfout", "-out=plan.tfout",
} }
for _, v := range targets {
args = append(args, "--target", fmt.Sprintf("%s", v))
}
for k, v := range variables { for k, v := range variables {
args = append(args, "-var") args = append(args, "-var")
args = append(args, fmt.Sprintf("%s=%s", k, v)) args = append(args, fmt.Sprintf("%s=%s", k, v))
@ -145,10 +149,13 @@ func planCommand(variables map[string]string, secrets map[string]string, paralle
) )
} }
func applyCommand(parallelism int) *exec.Cmd { func applyCommand(parallelism int, targets []string) *exec.Cmd {
args := []string{ args := []string{
"apply", "apply",
} }
for _, v := range targets {
args = append(args, "--target", fmt.Sprintf("%s", v))
}
if parallelism > 0 { if parallelism > 0 {
args = append(args, fmt.Sprintf("-parallelism=%d", parallelism)) args = append(args, fmt.Sprintf("-parallelism=%d", parallelism))
} }

Loading…
Cancel
Save