|
| 1 | +// Pipeline Promotion Resource |
| 2 | +// |
| 3 | +// This resource allows promoting releases between apps in a Heroku Pipeline. |
| 4 | +// Currently promotes the latest release from the source app to target apps. |
| 5 | +// |
| 6 | +// DEPENDENCY: The 'release_id' field requires Flow team to add Promotion#release_id |
| 7 | +// API support. Until then, only latest release promotion is supported. |
| 8 | +package heroku |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "fmt" |
| 13 | + "log" |
| 14 | + |
| 15 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 16 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 17 | + heroku "github.com/heroku/heroku-go/v6" |
| 18 | +) |
| 19 | + |
| 20 | +func resourceHerokuPipelinePromotion() *schema.Resource { |
| 21 | + return &schema.Resource{ |
| 22 | + Create: resourceHerokuPipelinePromotionCreate, |
| 23 | + Read: resourceHerokuPipelinePromotionRead, |
| 24 | + Delete: resourceHerokuPipelinePromotionDelete, |
| 25 | + |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "pipeline": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Required: true, |
| 30 | + ForceNew: true, |
| 31 | + ValidateFunc: validation.IsUUID, |
| 32 | + Description: "Pipeline ID for the promotion", |
| 33 | + }, |
| 34 | + |
| 35 | + "source_app_id": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Required: true, |
| 38 | + ForceNew: true, |
| 39 | + ValidateFunc: validation.IsUUID, |
| 40 | + Description: "Source app ID to promote from", |
| 41 | + }, |
| 42 | + |
| 43 | + "release_id": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Optional: true, |
| 46 | + ForceNew: true, |
| 47 | + ValidateFunc: validation.IsUUID, |
| 48 | + Description: "Specific release ID to promote (requires Flow team API update)", |
| 49 | + }, |
| 50 | + |
| 51 | + "targets": { |
| 52 | + Type: schema.TypeSet, |
| 53 | + Required: true, |
| 54 | + ForceNew: true, |
| 55 | + Description: "Set of target app IDs to promote to", |
| 56 | + Elem: &schema.Schema{ |
| 57 | + Type: schema.TypeString, |
| 58 | + ValidateFunc: validation.IsUUID, |
| 59 | + }, |
| 60 | + }, |
| 61 | + |
| 62 | + // Computed fields |
| 63 | + "status": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + Description: "Status of the promotion (pending, completed)", |
| 67 | + }, |
| 68 | + |
| 69 | + "created_at": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Computed: true, |
| 72 | + Description: "When the promotion was created", |
| 73 | + }, |
| 74 | + |
| 75 | + "promoted_release_id": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Computed: true, |
| 78 | + Description: "ID of the release that was actually promoted", |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func resourceHerokuPipelinePromotionCreate(d *schema.ResourceData, meta interface{}) error { |
| 85 | + client := meta.(*Config).Api |
| 86 | + |
| 87 | + log.Printf("[DEBUG] Creating pipeline promotion") |
| 88 | + |
| 89 | + // Check if release_id is specified - this requires Flow team API support |
| 90 | + if releaseID, ok := d.GetOk("release_id"); ok { |
| 91 | + return fmt.Errorf("release_id parameter (%s) is not yet supported - waiting for Flow team to add Promotion#release_id API support", releaseID.(string)) |
| 92 | + } |
| 93 | + |
| 94 | + // Build promotion options using current API |
| 95 | + pipelineID := d.Get("pipeline").(string) |
| 96 | + sourceAppID := d.Get("source_app_id").(string) |
| 97 | + targets := d.Get("targets").(*schema.Set) |
| 98 | + |
| 99 | + opts := heroku.PipelinePromotionCreateOpts{} |
| 100 | + opts.Pipeline.ID = pipelineID |
| 101 | + opts.Source.App = &struct { |
| 102 | + ID *string `json:"id,omitempty" url:"id,omitempty,key"` |
| 103 | + }{ID: &sourceAppID} |
| 104 | + |
| 105 | + // Convert targets set to slice |
| 106 | + for _, target := range targets.List() { |
| 107 | + targetAppID := target.(string) |
| 108 | + targetApp := &struct { |
| 109 | + ID *string `json:"id,omitempty" url:"id,omitempty,key"` |
| 110 | + }{ID: &targetAppID} |
| 111 | + |
| 112 | + opts.Targets = append(opts.Targets, struct { |
| 113 | + App *struct { |
| 114 | + ID *string `json:"id,omitempty" url:"id,omitempty,key"` |
| 115 | + } `json:"app,omitempty" url:"app,omitempty,key"` |
| 116 | + }{App: targetApp}) |
| 117 | + } |
| 118 | + |
| 119 | + log.Printf("[DEBUG] Pipeline promotion create configuration: %#v", opts) |
| 120 | + |
| 121 | + promotion, err := client.PipelinePromotionCreate(context.TODO(), opts) |
| 122 | + if err != nil { |
| 123 | + return fmt.Errorf("error creating pipeline promotion: %s", err) |
| 124 | + } |
| 125 | + |
| 126 | + log.Printf("[INFO] Created pipeline promotion ID: %s", promotion.ID) |
| 127 | + d.SetId(promotion.ID) |
| 128 | + |
| 129 | + return resourceHerokuPipelinePromotionRead(d, meta) |
| 130 | +} |
| 131 | + |
| 132 | +func resourceHerokuPipelinePromotionRead(d *schema.ResourceData, meta interface{}) error { |
| 133 | + client := meta.(*Config).Api |
| 134 | + |
| 135 | + log.Printf("[DEBUG] Reading pipeline promotion: %s", d.Id()) |
| 136 | + |
| 137 | + promotion, err := client.PipelinePromotionInfo(context.TODO(), d.Id()) |
| 138 | + if err != nil { |
| 139 | + return fmt.Errorf("error retrieving pipeline promotion: %s", err) |
| 140 | + } |
| 141 | + |
| 142 | + // Set computed fields |
| 143 | + d.Set("status", promotion.Status) |
| 144 | + d.Set("created_at", promotion.CreatedAt.String()) |
| 145 | + |
| 146 | + // Set the release that was actually promoted |
| 147 | + if promotion.Source.Release.ID != "" { |
| 148 | + d.Set("promoted_release_id", promotion.Source.Release.ID) |
| 149 | + } |
| 150 | + |
| 151 | + // Set configuration from API response |
| 152 | + d.Set("pipeline", promotion.Pipeline.ID) |
| 153 | + d.Set("source_app_id", promotion.Source.App.ID) |
| 154 | + |
| 155 | + log.Printf("[DEBUG] Pipeline promotion read completed for: %s", d.Id()) |
| 156 | + return nil |
| 157 | +} |
| 158 | + |
| 159 | +func resourceHerokuPipelinePromotionDelete(d *schema.ResourceData, meta interface{}) error { |
| 160 | + log.Printf("[INFO] There is no DELETE for pipeline promotion resource so this is a no-op. Promotion will be removed from state.") |
| 161 | + return nil |
| 162 | +} |
0 commit comments