-
Notifications
You must be signed in to change notification settings - Fork 14
/
resource_variable.go
109 lines (92 loc) · 2.38 KB
/
resource_variable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"fmt"
"github.com/apache/airflow-client-go/airflow"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceVariableCreate(d *schema.ResourceData, m interface{}) error {
pcfg := m.(ProviderConfig)
client := pcfg.ApiClient
key := d.Get("key").(string)
value := d.Get("value").(string)
v := airflow.Variable{
Key: &key,
Value: &value,
}
req := client.VariableApi.PostVariables(pcfg.AuthContext)
_, _, err := req.Variable(v).Execute()
if err != nil {
return err
}
d.SetId(key)
return resourceVariableRead(d, m)
}
func resourceVariableRead(d *schema.ResourceData, m interface{}) error {
pcfg := m.(ProviderConfig)
client := pcfg.ApiClient
key := d.Id()
req := client.VariableApi.GetVariable(pcfg.AuthContext, key)
variable, resp, err := req.Execute()
if resp != nil && resp.StatusCode == 404 {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("failed to get variable `%s` from Airflow: %w", key, err)
}
d.Set("key", variable.Key)
d.Set("value", variable.Value)
return nil
}
func resourceVariableUpdate(d *schema.ResourceData, m interface{}) error {
pcfg := m.(ProviderConfig)
client := pcfg.ApiClient
key := d.Id()
value := d.Get("value").(string)
v := airflow.Variable{
Key: &key,
Value: &value,
}
req := client.VariableApi.PatchVariable(pcfg.AuthContext, key)
_, _, err := req.Variable(v).Execute()
if err != nil {
return err
}
d.SetId(key)
return resourceVariableRead(d, m)
}
func resourceVariableDelete(d *schema.ResourceData, m interface{}) error {
pcfg := m.(ProviderConfig)
client := pcfg.ApiClient
key := d.Id()
req := client.VariableApi.DeleteVariable(pcfg.AuthContext, key)
_, err := req.Execute()
return err
}
func resourceVariableImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
if err := resourceVariableRead(d, m); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}
func resourceVariable() *schema.Resource {
return &schema.Resource{
Create: resourceVariableCreate,
Read: resourceVariableRead,
Update: resourceVariableUpdate,
Delete: resourceVariableDelete,
Importer: &schema.ResourceImporter{
State: resourceVariableImport,
},
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeString,
Required: true,
},
},
}
}