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

tf localfiles support in brokerpak #231

Closed
Closed
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
8 changes: 8 additions & 0 deletions pkg/brokerpak/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,18 @@ func (m *Manifest) packDefinitions(tmp, base string) error {
return fmt.Errorf("couldn't load provision template %s: %v", defn.ProvisionSettings.TemplateRef, err)
}

if err := defn.ProvisionSettings.LoadLocalFile(base); err != nil {
return fmt.Errorf("couldn't load provision local files: %v", err)
}

if err := defn.BindSettings.LoadTemplate(base); err != nil {
return fmt.Errorf("couldn't load bind template %s: %v", defn.BindSettings.TemplateRef, err)
}

if err := defn.BindSettings.LoadLocalFile(base); err != nil {
return fmt.Errorf("couldn't load bind local files: %v", err)
}

clearRefs(&defn.ProvisionSettings)
clearRefs(&defn.BindSettings)

Expand Down
51 changes: 51 additions & 0 deletions pkg/providers/tf/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"io/ioutil"
"path"
"path/filepath"

"code.cloudfoundry.org/lager"
"github.com/cloudfoundry-incubator/cloud-service-broker/pkg/broker"
Expand Down Expand Up @@ -109,6 +110,8 @@ type TfServiceDefinitionV1Action struct {
Outputs []broker.BrokerVariable `yaml:"outputs"`
Templates map[string]string `yaml:"templates"`
TemplateRefs map[string]string `yaml:"template_refs"`
LocalFiles map[string]string `yaml:"localfiles"`
LocalFileRefs []string `yaml:"localfile_refs"`
ImportVariables []ImportVariable `yaml:"import_inputs"`
ImportParameterMappings []ImportParameterMapping `yaml:"import_parameter_mappings"`
ImportParametersToDelete []string `yaml:"import_parameters_to_delete"`
Expand Down Expand Up @@ -166,6 +169,39 @@ func (action *TfServiceDefinitionV1Action) LoadTemplate(srcDir string) error {
return nil
}

func loadLocalFile(filePath string) (string, error) {
if filePath == "" {
return "", nil
}
buff, err := ioutil.ReadFile(filePath)

if err != nil {
return "", err
}
return string(buff), nil
}

// LoadLocalFile loads local file refs if provided
func (action *TfServiceDefinitionV1Action) LoadLocalFile(srcDir string) error {
var err error

if action.LocalFiles == nil {
action.LocalFiles = make(map[string]string)
}

for _, ref := range action.LocalFileRefs {
if ref != "" {
name := filepath.Base(ref)
action.LocalFiles[name], err = loadLocalFile(path.Join(srcDir, ref))
if err != nil {
return err
}
}
}

return nil
}

// Validate implements validation.Validatable.
func (action *TfServiceDefinitionV1Action) Validate() (errs *validation.FieldError) {
for i, v := range action.PlanInputs {
Expand Down Expand Up @@ -321,13 +357,28 @@ func (tfb *TfServiceDefinitionV1) loadTemplates() error {
return err
}

// Load additionnals local files for terraform file
func (tfb *TfServiceDefinitionV1) loadLocalFiles() error {
err := tfb.BindSettings.LoadLocalFile(".")

if err == nil {
err = tfb.ProvisionSettings.LoadLocalFile(".")
}

return err
}

// ToService converts the flat TfServiceDefinitionV1 into a broker.ServiceDefinition
// that the registry can use.
func (tfb *TfServiceDefinitionV1) ToService(executor wrapper.TerraformExecutor) (*broker.ServiceDefinition, error) {
if err := tfb.loadTemplates(); err != nil {
return nil, err
}

if err := tfb.loadLocalFiles(); err != nil {
return nil, err
}

if err := tfb.Validate(); err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/providers/tf/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (provider *terraformProvider) importCreate(ctx context.Context, vars *varco
return "", err
}

workspace, err := wrapper.NewWorkspace(varsMap, "", action.Templates, parameterMappings, action.ImportParametersToDelete, addParams)
workspace, err := wrapper.NewWorkspace(varsMap, "", action.Templates, action.LocalFiles, parameterMappings, action.ImportParametersToDelete, addParams)
if err != nil {
return tfId, err
}
Expand All @@ -174,7 +174,7 @@ func (provider *terraformProvider) create(ctx context.Context, vars *varcontext.
return "", err
}

workspace, err := wrapper.NewWorkspace(vars.ToMap(), action.Template, action.Templates, []wrapper.ParameterMapping{}, []string{}, []wrapper.ParameterMapping{})
workspace, err := wrapper.NewWorkspace(vars.ToMap(), action.Template, action.Templates, action.LocalFiles, []wrapper.ParameterMapping{}, []string{}, []wrapper.ParameterMapping{})
if err != nil {
return tfId, err
}
Expand Down
27 changes: 26 additions & 1 deletion pkg/providers/tf/wrapper/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type TerraformExecutor func(context.Context, *exec.Cmd) (ExecutionOutput, error)
func NewWorkspace(templateVars map[string]interface{},
terraformTemplate string,
terraformTemplates map[string]string,
terraformLocalFiles map[string]string,
importParameterMappings []ParameterMapping,
parametersToRemove []string,
parametersToAdd []ParameterMapping) (*TerraformWorkspace, error) {
Expand All @@ -70,7 +71,8 @@ func NewWorkspace(templateVars map[string]interface{},
}

workspace := TerraformWorkspace{
Modules: []ModuleDefinition{tfModule},
LocalFiles: terraformLocalFiles,
Modules: []ModuleDefinition{tfModule},
Instances: []ModuleInstance{
{
ModuleName: tfModule.Name,
Expand Down Expand Up @@ -116,6 +118,9 @@ type TerraformWorkspace struct {
Instances []ModuleInstance `json:"instances"`
State []byte `json:"tfstate"`

// terraform local files
LocalFiles map[string]string `json:"localfiles"`

// Executor is a function that gets invoked to shell out to Terraform.
// If left nil, the default executor is used.
Executor TerraformExecutor `json:"-"`
Expand All @@ -133,6 +138,7 @@ func (workspace *TerraformWorkspace) String() string {
b.WriteString("# Terraform Workspace\n")
fmt.Fprintf(&b, "modules: %d\n", len(workspace.Modules))
fmt.Fprintf(&b, "instances: %d\n", len(workspace.Instances))
fmt.Fprintf(&b, "localfiles: %d\n", len(workspace.LocalFiles))
fmt.Fprintln(&b)

for _, instance := range workspace.Instances {
Expand Down Expand Up @@ -184,6 +190,16 @@ func (workspace *TerraformWorkspace) initializedFsFlat() error {
if err == nil {
err = ioutil.WriteFile(path.Join(workspace.dir, "terraform.tfvars.json"), variables, 0755)
}

// init local files ?
if len(workspace.LocalFiles) > 0 {
for name, lf := range workspace.LocalFiles {
if err := ioutil.WriteFile(path.Join(workspace.dir, name), []byte(lf), 0755); err != nil {
return err
}
}
}

return err
}

Expand All @@ -210,6 +226,15 @@ func (workspace *TerraformWorkspace) initializeFsModules() error {
}
}

// init local files
if len(workspace.LocalFiles) > 0 {
for name, lf := range workspace.LocalFiles {
if err := ioutil.WriteFile(path.Join(parent, name), []byte(lf), 0755); err != nil {
return err
}
}
}

var err error
if outputs[module.Name], err = module.Outputs(); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions pkg/providers/tf/wrapper/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestTerraformWorkspace_Invariants(t *testing.T) {
t.Run(tn, func(t *testing.T) {
// construct workspace
const definitionTfContents = "variable azure_tenant_id { type = string }"
ws, err := NewWorkspace(map[string]interface{}{}, definitionTfContents, map[string]string{}, []ParameterMapping{}, []string{}, []ParameterMapping{})
ws, err := NewWorkspace(map[string]interface{}{}, definitionTfContents, map[string]string{}, map[string]string{}, []ParameterMapping{}, []string{}, []ParameterMapping{})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestTerraformWorkspace_InvariantsFlat(t *testing.T) {
t.Run(tn, func(t *testing.T) {
// construct workspace
const variablesTfContents = "variable azure_tenant_id { type = string }"
ws, err := NewWorkspace(map[string]interface{}{}, ``, map[string]string{"variables": variablesTfContents}, []ParameterMapping{}, []string{}, []ParameterMapping{})
ws, err := NewWorkspace(map[string]interface{}{}, ``, map[string]string{"variables": variablesTfContents}, map[string]string{}, []ParameterMapping{}, []string{}, []ParameterMapping{})
if err != nil {
t.Fatal(err)
}
Expand Down