forked from DrFaust92/terraform-provider-airflow
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresource_variable.go
More file actions
103 lines (85 loc) · 2.42 KB
/
Copy pathresource_variable.go
File metadata and controls
103 lines (85 loc) · 2.42 KB
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
package main
import (
"fmt"
"github.com/apache/airflow-client-go/airflow"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceVariable() *schema.Resource {
return &schema.Resource{
Create: resourceVariableCreate,
Read: resourceVariableRead,
Update: resourceVariableUpdate,
Delete: resourceVariableDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"value": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceVariableCreate(d *schema.ResourceData, m interface{}) error {
pcfg := m.(ProviderConfig)
client := pcfg.ApiClient
key := d.Get("key").(string)
val := d.Get("value").(string)
varApi := client.VariableApi
_, _, err := varApi.PostVariables(pcfg.AuthContext).Variable(airflow.Variable{
Key: &key,
Value: &val,
}).Execute()
if err != nil {
return fmt.Errorf("failed to create variable `%s` from Airflow: %w", key, err)
}
d.SetId(key)
return resourceVariableRead(d, m)
}
func resourceVariableRead(d *schema.ResourceData, m interface{}) error {
pcfg := m.(ProviderConfig)
client := pcfg.ApiClient
variable, resp, err := client.VariableApi.GetVariable(pcfg.AuthContext, d.Id()).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", d.Id(), 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
val := d.Get("value").(string)
key := d.Id()
_, _, err := client.VariableApi.PatchVariable(pcfg.AuthContext, key).Variable(airflow.Variable{
Key: &key,
Value: &val,
}).Execute()
if err != nil {
return fmt.Errorf("failed to update variable `%s` from Airflow: %w", key, err)
}
return resourceVariableRead(d, m)
}
func resourceVariableDelete(d *schema.ResourceData, m interface{}) error {
pcfg := m.(ProviderConfig)
client := pcfg.ApiClient
resp, err := client.VariableApi.DeleteVariable(pcfg.AuthContext, d.Id()).Execute()
if err != nil {
return fmt.Errorf("failed to delete variable `%s` from Airflow: %w", d.Id(), err)
}
if resp != nil && resp.StatusCode == 404 {
return nil
}
return nil
}