Skip to content

Commit

Permalink
support custom variable output path provider config
Browse files Browse the repository at this point in the history
  • Loading branch information
Qingping Hou committed May 5, 2020
1 parent 3256feb commit d94db5e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
22 changes: 13 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ import (
type Variables map[string]string

type AirflowClient struct {
lock sync.Mutex
VariablesOutputPath string
lock sync.Mutex
}

func getOutputFilePath() string {
func (self *AirflowClient) getOutputFilePath() string {
if self.VariablesOutputPath != "" {
return self.VariablesOutputPath
}
path, err := os.Getwd()
if err != nil {
panic("Failed to get current working directory")
}
return filepath.Join(path, "airflow_variables.json")
}

func loadVariables(vars *Variables) {
output_path := getOutputFilePath()
func (self *AirflowClient) loadVariables(vars *Variables) {
output_path := self.getOutputFilePath()
_, err := os.Stat(output_path)
if os.IsNotExist(err) {
return
Expand All @@ -44,7 +48,7 @@ func (self *AirflowClient) ReadVariable(key string) string {
self.lock.Lock()
defer self.lock.Unlock()

loadVariables(&vars)
self.loadVariables(&vars)
return vars[key]
}

Expand All @@ -53,15 +57,15 @@ func (self *AirflowClient) DeleteVariable(key string) error {
self.lock.Lock()
defer self.lock.Unlock()

loadVariables(&vars)
self.loadVariables(&vars)
delete(vars, key)

b, err := json.Marshal(vars)
if err != nil {
return err
}

output_path := getOutputFilePath()
output_path := self.getOutputFilePath()
err = ioutil.WriteFile(output_path, b, 0644)
if err != nil {
return err
Expand All @@ -75,15 +79,15 @@ func (self *AirflowClient) SetVariable(key string, value string) error {
self.lock.Lock()
defer self.lock.Unlock()

loadVariables(&vars)
self.loadVariables(&vars)
vars[key] = value

b, err := json.Marshal(vars)
if err != nil {
return err
}

output_path := getOutputFilePath()
output_path := self.getOutputFilePath()
err = ioutil.WriteFile(output_path, b, 0644)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions examples/variable/demo.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
provider "airflow" {
variables_output_path = "./variables.json"
}

resource "airflow_variable" "foo" {
Expand Down
12 changes: 10 additions & 2 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ import (

func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"variables_output_path": {
Type: schema.TypeString,
Optional: true,
},
},
ResourcesMap: map[string]*schema.Resource{
"airflow_variable": resourceVariable(),
},
ConfigureFunc: func(*schema.ResourceData) (interface{}, error) {
return &AirflowClient{}, nil
ConfigureFunc: func(p *schema.ResourceData) (interface{}, error) {
return &AirflowClient{
VariablesOutputPath: p.Get("variables_output_path").(string),
}, nil
},
}
}

0 comments on commit d94db5e

Please sign in to comment.