Skip to content

Commit

Permalink
Merge pull request #4 from mhlias/support_TF_target_resource
Browse files Browse the repository at this point in the history
Add support for terraform targeted resources in plan and apply.
  • Loading branch information
mhlias authored May 4, 2017
2 parents 4b5912b + 8014c0e commit 991a16f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This tool wraps terraform execution forcing a specific structure while providing
- Creates an S3 bucket in the current account and enables versioning.
- Configures remote terraform state on the created S3 bucket.
- Provides management of remote git/github terraform modules using the Terrafile concept.
- Provides plan and apply functionality and keeps the local & remote states in sync.
- Provides plan and apply functionality with resources target support and keeps the local & remote states in sync.


### Setup Requirements
Expand Down Expand Up @@ -98,6 +98,7 @@ The tool accepts the following parameters:
-p Terraform Plan
-s Sync remote S3 state
-u Fetch and update modules from remote repo
-t Terraform resources to target only, (-t resourcetype.resource resourcetype2.resource2)
```

Expand Down
15 changes: 15 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ type conf struct {
account string
}

type multiflag []string

func (d *multiflag) String() string {
return fmt.Sprintf("%d", *d)
}

func (d *multiflag) Set(value string) error {
*d = append(*d, value)
return nil
}

var targetsTF multiflag

func main() {

use_mfa := true
Expand All @@ -40,6 +53,7 @@ func main() {
modulesPtr := flag.Bool("u", false, "Fetch and update modules from remote repo")
outputsPtr := flag.Bool("o", false, "Display Terraform outputs")
configPtr := flag.Bool("c", false, "Force reconfiguration of Tholos")
flag.Var(&targetsTF, "t", "Terraform resources to target only, (-t resourcetype.resource resourcetype2.resource2)")

flag.Parse()

Expand Down Expand Up @@ -105,6 +119,7 @@ func main() {
State_filename: fmt.Sprintf("%s-%s-%s.tfstate", project_config.Project, project_config.account, project_config.environment),
Versioning: true,
Encrypt_s3_state: project_config.Encrypt_s3_state,
TargetsTF: targetsTF,
}

modules := &tf_helper.Modules{}
Expand Down
7 changes: 7 additions & 0 deletions tf_helper/apply.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tf_helper

import (
"fmt"
"log"
)

Expand All @@ -10,6 +11,12 @@ func (c *Config) Apply() {

exec_args := []string{"apply", "plans/plan.tfplan"}

if len(c.TargetsTF) > 0 {
for _, t := range c.TargetsTF {
exec_args = append(exec_args, fmt.Sprintf("-target=%s", t))
}
}

log.Println("[INFO] Applying Terraform plan.")

if !ExecCmd(cmd_name, exec_args) {
Expand Down
7 changes: 7 additions & 0 deletions tf_helper/plan.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tf_helper

import (
"fmt"
"log"

"github.com/mhlias/tholos/tholos"
Expand Down Expand Up @@ -42,6 +43,12 @@ func (c *Config) Plan(tholos_conf *tholos.Tholos_config) {

exec_args = []string{"plan", "-module-depth=3", "-refresh=true", "-out=plans/plan.tfplan", "-var-file=params/env.tfvars"}

if len(c.TargetsTF) > 0 {
for _, t := range c.TargetsTF {
exec_args = append(exec_args, fmt.Sprintf("-target=%s", t))
}
}

log.Println("[INFO] Running Terraform plan.")

if !ExecCmd(cmd_name, exec_args) {
Expand Down
1 change: 1 addition & 0 deletions tf_helper/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Config struct {
State_filename string
Encrypt_s3_state bool
Versioning bool
TargetsTF []string
}

func (c *Config) Create_bucket(client interface{}) bool {
Expand Down

0 comments on commit 991a16f

Please sign in to comment.