-
Notifications
You must be signed in to change notification settings - Fork 90
/
resource_argocd_application.go
158 lines (147 loc) · 4.33 KB
/
resource_argocd_application.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package argocd
import (
"context"
"fmt"
applicationClient "github.com/argoproj/argo-cd/pkg/apiclient/application"
application "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"strings"
"time"
)
func resourceArgoCDApplication() *schema.Resource {
return &schema.Resource{
Create: resourceArgoCDApplicationCreate,
Read: resourceArgoCDApplicationRead,
Update: resourceArgoCDApplicationUpdate,
Delete: resourceArgoCDApplicationDelete,
// TODO: add importer acceptance tests
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"metadata": metadataSchema("applications.argoproj.io"),
"spec": applicationSpecSchema(),
},
}
}
func resourceArgoCDApplicationCreate(d *schema.ResourceData, meta interface{}) error {
objectMeta, spec, err := expandApplication(d)
if err != nil {
return err
}
server := meta.(ServerInterface)
c := *server.ApplicationClient
app, err := c.Get(context.Background(), &applicationClient.ApplicationQuery{
Name: &objectMeta.Name,
})
if err != nil {
switch strings.Contains(err.Error(), "NotFound") {
case true:
default:
return err
}
}
if app != nil {
switch app.DeletionTimestamp {
case nil:
default:
// Pre-existing app is still in Kubernetes soft deletion queue
time.Sleep(time.Duration(*app.DeletionGracePeriodSeconds))
}
}
featureApplicationLevelSyncOptionsSupported, err := server.isFeatureSupported(featureApplicationLevelSyncOptions)
if err != nil {
return err
}
if !featureApplicationLevelSyncOptionsSupported &&
spec.SyncPolicy != nil &&
spec.SyncPolicy.SyncOptions != nil {
return fmt.Errorf(
"application-level sync_options is only supported from ArgoCD %s onwards",
featureVersionConstraintsMap[featureApplicationLevelSyncOptions].String())
}
app, err = c.Create(context.Background(), &applicationClient.ApplicationCreateRequest{
Application: application.Application{
ObjectMeta: objectMeta,
Spec: spec,
},
})
if err != nil {
return err
}
if app == nil {
return fmt.Errorf("something went wrong during application creation")
}
d.SetId(app.Name)
return resourceArgoCDApplicationRead(d, meta)
}
func resourceArgoCDApplicationRead(d *schema.ResourceData, meta interface{}) error {
server := meta.(ServerInterface)
c := *server.ApplicationClient
appName := d.Id()
app, err := c.Get(context.Background(), &applicationClient.ApplicationQuery{
Name: &appName,
},
)
if err != nil {
switch strings.Contains(err.Error(), "NotFound") {
case true:
d.SetId("")
return nil
default:
return err
}
}
err = flattenApplication(app, d)
return err
}
func resourceArgoCDApplicationUpdate(d *schema.ResourceData, meta interface{}) error {
appName := d.Id()
if ok := d.HasChanges("metadata", "spec"); ok {
objectMeta, spec, err := expandApplication(d)
if err != nil {
return err
}
server := meta.(ServerInterface)
c := *server.ApplicationClient
appRequest := &applicationClient.ApplicationUpdateRequest{
Application: &application.Application{
ObjectMeta: objectMeta,
Spec: spec,
}}
featureApplicationLevelSyncOptionsSupported, err := server.isFeatureSupported(featureApplicationLevelSyncOptions)
if err != nil {
return err
}
if !featureApplicationLevelSyncOptionsSupported &&
spec.SyncPolicy != nil &&
spec.SyncPolicy.SyncOptions != nil {
return fmt.Errorf(
"application-level sync_options is only supported from ArgoCD %s onwards",
featureVersionConstraintsMap[featureApplicationLevelSyncOptions].String())
}
app, err := c.Get(context.Background(), &applicationClient.ApplicationQuery{
Name: &appName,
})
if app != nil {
// Kubernetes API requires providing the up-to-date correct ResourceVersion for updates
appRequest.Application.ResourceVersion = app.ResourceVersion
}
_, err = c.Update(context.Background(), appRequest)
if err != nil {
return err
}
}
return resourceArgoCDApplicationRead(d, meta)
}
func resourceArgoCDApplicationDelete(d *schema.ResourceData, meta interface{}) error {
server := meta.(ServerInterface)
c := *server.ApplicationClient
appName := d.Id()
_, err := c.Delete(context.Background(), &applicationClient.ApplicationDeleteRequest{Name: &appName})
if err != nil {
return err
}
d.SetId("")
return nil
}