|
| 1 | +package digitalocean |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/digitalocean/godo" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 11 | +) |
| 12 | + |
| 13 | +func resourceDigitalOceanApp() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Create: resourceDigitalOceanAppCreate, |
| 16 | + Read: resourceDigitalOceanAppRead, |
| 17 | + Update: resourceDigitalOceanAppUpdate, |
| 18 | + Delete: resourceDigitalOceanAppDelete, |
| 19 | + Importer: &schema.ResourceImporter{ |
| 20 | + State: schema.ImportStatePassthrough, |
| 21 | + }, |
| 22 | + |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "spec": { |
| 25 | + Type: schema.TypeList, |
| 26 | + Optional: true, |
| 27 | + MaxItems: 1, |
| 28 | + Description: "A DigitalOcean App Platform Spec", |
| 29 | + Elem: &schema.Resource{ |
| 30 | + Schema: appSpecSchema(), |
| 31 | + }, |
| 32 | + }, |
| 33 | + |
| 34 | + // Computed attributes |
| 35 | + "default_ingress": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Computed: true, |
| 38 | + Description: "The default URL to access the App", |
| 39 | + }, |
| 40 | + |
| 41 | + "live_url": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + |
| 46 | + // TODO: The full Deployment should be a data source, not a resource |
| 47 | + // specify the app id for the active deployment, include a deployment |
| 48 | + // id for a specific one |
| 49 | + "active_deployment_id": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Computed: true, |
| 52 | + Description: "The ID the App's currently active deployment", |
| 53 | + }, |
| 54 | + |
| 55 | + "updated_at": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Computed: true, |
| 58 | + Description: "The date and time of when the App was last updated", |
| 59 | + }, |
| 60 | + "created_at": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Computed: true, |
| 63 | + Description: "The date and time of when the App was created", |
| 64 | + }, |
| 65 | + }, |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func resourceDigitalOceanAppCreate(d *schema.ResourceData, meta interface{}) error { |
| 70 | + client := meta.(*CombinedConfig).godoClient() |
| 71 | + appCreateRequest := &godo.AppCreateRequest{} |
| 72 | + appCreateRequest.Spec = expandAppSpec(d.Get("spec").([]interface{})) |
| 73 | + |
| 74 | + log.Printf("[DEBUG] App create request: %#v", appCreateRequest) |
| 75 | + app, _, err := client.Apps.Create(context.Background(), appCreateRequest) |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("Error creating App: %s", err) |
| 78 | + } |
| 79 | + |
| 80 | + d.SetId(app.ID) |
| 81 | + log.Printf("[DEBUG] Waiting for app (%s) deployment to become active", app.ID) |
| 82 | + |
| 83 | + err = waitForAppDeployment(client, app.ID) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + log.Printf("[INFO] App created, ID: %s", d.Id()) |
| 89 | + |
| 90 | + return resourceDigitalOceanAppRead(d, meta) |
| 91 | +} |
| 92 | + |
| 93 | +func resourceDigitalOceanAppRead(d *schema.ResourceData, meta interface{}) error { |
| 94 | + client := meta.(*CombinedConfig).godoClient() |
| 95 | + |
| 96 | + app, resp, err := client.Apps.Get(context.Background(), d.Id()) |
| 97 | + if err != nil { |
| 98 | + if resp != nil && resp.StatusCode == 404 { |
| 99 | + log.Printf("[DEBUG] App (%s) was not found - removing from state", d.Id()) |
| 100 | + d.SetId("") |
| 101 | + return nil |
| 102 | + } |
| 103 | + return fmt.Errorf("Error reading App: %s", err) |
| 104 | + } |
| 105 | + |
| 106 | + d.SetId(app.ID) |
| 107 | + d.Set("default_ingress", app.DefaultIngress) |
| 108 | + d.Set("live_url", app.LiveURL) |
| 109 | + d.Set("active_deployment_id", app.ActiveDeployment.ID) |
| 110 | + d.Set("updated_at", app.UpdatedAt.UTC().String()) |
| 111 | + d.Set("created_at", app.CreatedAt.UTC().String()) |
| 112 | + |
| 113 | + if err := d.Set("spec", flattenAppSpec(app.Spec)); err != nil { |
| 114 | + return fmt.Errorf("[DEBUG] Error setting app spec: %#v", err) |
| 115 | + } |
| 116 | + |
| 117 | + return err |
| 118 | +} |
| 119 | + |
| 120 | +func resourceDigitalOceanAppUpdate(d *schema.ResourceData, meta interface{}) error { |
| 121 | + client := meta.(*CombinedConfig).godoClient() |
| 122 | + |
| 123 | + if d.HasChange("spec") { |
| 124 | + appUpdateRequest := &godo.AppUpdateRequest{} |
| 125 | + appUpdateRequest.Spec = expandAppSpec(d.Get("spec").([]interface{})) |
| 126 | + |
| 127 | + app, _, err := client.Apps.Update(context.Background(), d.Id(), appUpdateRequest) |
| 128 | + if err != nil { |
| 129 | + return fmt.Errorf("Error updating app (%s): %s", d.Id(), err) |
| 130 | + } |
| 131 | + |
| 132 | + log.Printf("[DEBUG] Waiting for app (%s) deployment to become active", app.ID) |
| 133 | + err = waitForAppDeployment(client, app.ID) |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + |
| 138 | + log.Printf("[INFO] Updated app (%s)", app.ID) |
| 139 | + } |
| 140 | + |
| 141 | + return resourceDigitalOceanAppRead(d, meta) |
| 142 | +} |
| 143 | + |
| 144 | +func resourceDigitalOceanAppDelete(d *schema.ResourceData, meta interface{}) error { |
| 145 | + client := meta.(*CombinedConfig).godoClient() |
| 146 | + |
| 147 | + log.Printf("[INFO] Deleting App: %s", d.Id()) |
| 148 | + _, err := client.Apps.Delete(context.Background(), d.Id()) |
| 149 | + if err != nil { |
| 150 | + return fmt.Errorf("Error deletingApp: %s", err) |
| 151 | + } |
| 152 | + |
| 153 | + d.SetId("") |
| 154 | + return nil |
| 155 | +} |
| 156 | + |
| 157 | +func waitForAppDeployment(client *godo.Client, id string) error { |
| 158 | + tickerInterval := 10 //10s |
| 159 | + timeout := 1800 //1800s, 30min |
| 160 | + n := 0 |
| 161 | + |
| 162 | + var deploymentID string |
| 163 | + ticker := time.NewTicker(time.Duration(tickerInterval) * time.Second) |
| 164 | + for range ticker.C { |
| 165 | + if n*tickerInterval > timeout { |
| 166 | + ticker.Stop() |
| 167 | + break |
| 168 | + } |
| 169 | + |
| 170 | + if deploymentID == "" { |
| 171 | + app, _, err := client.Apps.Get(context.Background(), id) |
| 172 | + if err != nil { |
| 173 | + return fmt.Errorf("Error trying to read app deployment state: %s", err) |
| 174 | + } |
| 175 | + |
| 176 | + if app.InProgressDeployment != nil { |
| 177 | + deploymentID = app.InProgressDeployment.ID |
| 178 | + } |
| 179 | + |
| 180 | + } else { |
| 181 | + deployment, _, err := client.Apps.GetDeployment(context.Background(), id, deploymentID) |
| 182 | + if err != nil { |
| 183 | + ticker.Stop() |
| 184 | + return fmt.Errorf("Error trying to read app deployment state: %s", err) |
| 185 | + } |
| 186 | + |
| 187 | + allSuccessful := deployment.Progress.SuccessSteps == deployment.Progress.TotalSteps |
| 188 | + if allSuccessful { |
| 189 | + ticker.Stop() |
| 190 | + return nil |
| 191 | + } |
| 192 | + |
| 193 | + if deployment.Progress.ErrorSteps > 0 { |
| 194 | + ticker.Stop() |
| 195 | + return fmt.Errorf("error deploying app (%s) (deployment ID: %s):\n%s", id, deployment.ID, godo.Stringify(deployment.Progress)) |
| 196 | + } |
| 197 | + |
| 198 | + log.Printf("[DEBUG] Waiting for app (%s) deployment (%s) to become active. Phase: %s (%d/%d)", |
| 199 | + id, deployment.ID, deployment.Phase, deployment.Progress.SuccessSteps, deployment.Progress.TotalSteps) |
| 200 | + } |
| 201 | + |
| 202 | + n++ |
| 203 | + } |
| 204 | + |
| 205 | + return fmt.Errorf("timeout waiting to app (%s) deployment", id) |
| 206 | +} |
0 commit comments