|
| 1 | +package argocd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + applicationClient "github.com/argoproj/argo-cd/pkg/apiclient/application" |
| 7 | + application "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +func resourceArgoCDApplication() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Create: resourceArgoCDApplicationCreate, |
| 16 | + Read: resourceArgoCDApplicationRead, |
| 17 | + Update: resourceArgoCDApplicationUpdate, |
| 18 | + Delete: resourceArgoCDApplicationDelete, |
| 19 | + // TODO: add importer acceptance tests |
| 20 | + Importer: &schema.ResourceImporter{ |
| 21 | + State: schema.ImportStatePassthrough, |
| 22 | + }, |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "metadata": metadataSchema("applications.argoproj.io"), |
| 25 | + "spec": applicationSpecSchema(), |
| 26 | + }, |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func resourceArgoCDApplicationCreate(d *schema.ResourceData, meta interface{}) error { |
| 31 | + objectMeta, spec, err := expandApplication(d) |
| 32 | + if err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + server := meta.(ServerInterface) |
| 36 | + c := server.ApplicationClient |
| 37 | + app, err := c.Get(context.Background(), &applicationClient.ApplicationQuery{ |
| 38 | + Name: &objectMeta.Name, |
| 39 | + }) |
| 40 | + if err != nil { |
| 41 | + switch strings.Contains(err.Error(), "NotFound") { |
| 42 | + case true: |
| 43 | + default: |
| 44 | + return err |
| 45 | + } |
| 46 | + } |
| 47 | + if app != nil { |
| 48 | + switch app.DeletionTimestamp { |
| 49 | + case nil: |
| 50 | + default: |
| 51 | + // Pre-existing app is still in Kubernetes soft deletion queue |
| 52 | + time.Sleep(time.Duration(*app.DeletionGracePeriodSeconds)) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + featureApplicationLevelSyncOptionsSupported, err := server.isFeatureSupported(featureApplicationLevelSyncOptions) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + if !featureApplicationLevelSyncOptionsSupported && |
| 61 | + spec.SyncPolicy != nil && |
| 62 | + spec.SyncPolicy.SyncOptions != nil { |
| 63 | + return fmt.Errorf( |
| 64 | + "application-level sync_options is only supported from ArgoCD %s onwards", |
| 65 | + featureVersionConstraintsMap[featureApplicationLevelSyncOptions].String()) |
| 66 | + } |
| 67 | + |
| 68 | + app, err = c.Create(context.Background(), &applicationClient.ApplicationCreateRequest{ |
| 69 | + Application: application.Application{ |
| 70 | + ObjectMeta: objectMeta, |
| 71 | + Spec: spec, |
| 72 | + }, |
| 73 | + }) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + if app == nil { |
| 78 | + return fmt.Errorf("something went wrong during application creation") |
| 79 | + } |
| 80 | + d.SetId(app.Name) |
| 81 | + return resourceArgoCDApplicationRead(d, meta) |
| 82 | +} |
| 83 | + |
| 84 | +func resourceArgoCDApplicationRead(d *schema.ResourceData, meta interface{}) error { |
| 85 | + server := meta.(ServerInterface) |
| 86 | + c := server.ApplicationClient |
| 87 | + appName := d.Id() |
| 88 | + app, err := c.Get(context.Background(), &applicationClient.ApplicationQuery{ |
| 89 | + Name: &appName, |
| 90 | + }, |
| 91 | + ) |
| 92 | + if err != nil { |
| 93 | + switch strings.Contains(err.Error(), "NotFound") { |
| 94 | + case true: |
| 95 | + d.SetId("") |
| 96 | + return nil |
| 97 | + default: |
| 98 | + return err |
| 99 | + } |
| 100 | + } |
| 101 | + err = flattenApplication(app, d) |
| 102 | + return err |
| 103 | +} |
| 104 | + |
| 105 | +func resourceArgoCDApplicationUpdate(d *schema.ResourceData, meta interface{}) error { |
| 106 | + appName := d.Id() |
| 107 | + |
| 108 | + if ok := d.HasChanges("metadata", "spec"); ok { |
| 109 | + objectMeta, spec, err := expandApplication(d) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + server := meta.(ServerInterface) |
| 114 | + c := server.ApplicationClient |
| 115 | + appRequest := &applicationClient.ApplicationUpdateRequest{ |
| 116 | + Application: &application.Application{ |
| 117 | + ObjectMeta: objectMeta, |
| 118 | + Spec: spec, |
| 119 | + }} |
| 120 | + |
| 121 | + featureApplicationLevelSyncOptionsSupported, err := server.isFeatureSupported(featureApplicationLevelSyncOptions) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + if !featureApplicationLevelSyncOptionsSupported && |
| 126 | + spec.SyncPolicy != nil && |
| 127 | + spec.SyncPolicy.SyncOptions != nil { |
| 128 | + return fmt.Errorf( |
| 129 | + "application-level sync_options is only supported from ArgoCD %s onwards", |
| 130 | + featureVersionConstraintsMap[featureApplicationLevelSyncOptions].String()) |
| 131 | + } |
| 132 | + |
| 133 | + app, err := c.Get(context.Background(), &applicationClient.ApplicationQuery{ |
| 134 | + Name: &appName, |
| 135 | + }) |
| 136 | + if app != nil { |
| 137 | + // Kubernetes API requires providing the up-to-date correct ResourceVersion for updates |
| 138 | + appRequest.Application.ResourceVersion = app.ResourceVersion |
| 139 | + } |
| 140 | + _, err = c.Update(context.Background(), appRequest) |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + } |
| 145 | + return resourceArgoCDApplicationRead(d, meta) |
| 146 | +} |
| 147 | + |
| 148 | +func resourceArgoCDApplicationDelete(d *schema.ResourceData, meta interface{}) error { |
| 149 | + server := meta.(ServerInterface) |
| 150 | + c := server.ApplicationClient |
| 151 | + appName := d.Id() |
| 152 | + _, err := c.Delete(context.Background(), &applicationClient.ApplicationDeleteRequest{Name: &appName}) |
| 153 | + if err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + d.SetId("") |
| 157 | + return nil |
| 158 | +} |
0 commit comments