|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/google/uuid" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 10 | +) |
| 11 | + |
| 12 | +func scriptOrderResource() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + SchemaVersion: 1, |
| 15 | + |
| 16 | + Description: "Use this resource to declare ordering constraints between `coder_script` resources on an agent. Multiple `coder_script_order` resources are additive; each `rule` block adds edges to the startup dependency graph. Reference scripts by their id, e.g. `coder_script.install.id`.", |
| 17 | + CreateContext: func(_ context.Context, rd *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 18 | + rd.SetId(uuid.NewString()) |
| 19 | + // Validate that every rule declares at least one script to run. |
| 20 | + rules, _ := rd.Get("rule").([]interface{}) |
| 21 | + for i, raw := range rules { |
| 22 | + m, _ := raw.(map[string]interface{}) |
| 23 | + run, _ := m["run"].([]interface{}) |
| 24 | + if len(run) == 0 { |
| 25 | + return diag.Errorf("rule[%d]: \"run\" must contain at least one coder_script id", i) |
| 26 | + } |
| 27 | + } |
| 28 | + return nil |
| 29 | + }, |
| 30 | + ReadContext: schema.NoopContext, |
| 31 | + DeleteContext: schema.NoopContext, |
| 32 | + Schema: map[string]*schema.Schema{ |
| 33 | + "rule": { |
| 34 | + Type: schema.TypeList, |
| 35 | + Description: "Each `rule` block declares that the scripts in `run` must wait for the scripts in `after` to reach `state`.", |
| 36 | + ForceNew: true, |
| 37 | + Optional: true, |
| 38 | + Elem: &schema.Resource{ |
| 39 | + Schema: map[string]*schema.Schema{ |
| 40 | + "run": { |
| 41 | + Type: schema.TypeList, |
| 42 | + Description: "The ids of the `coder_script` resources this rule applies to (e.g. `coder_script.install.id`).", |
| 43 | + Required: true, |
| 44 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 45 | + }, |
| 46 | + "after": { |
| 47 | + Type: schema.TypeList, |
| 48 | + Description: "The ids of the `coder_script` resources that must reach `state` before the `run` scripts start.", |
| 49 | + Optional: true, |
| 50 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 51 | + }, |
| 52 | + "state": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Description: "The lifecycle state the `after` scripts must reach before the `run` scripts may start. One of `completes` or `starts`.", |
| 55 | + Optional: true, |
| 56 | + Default: "completes", |
| 57 | + ValidateFunc: validation.StringInSlice([]string{"completes", "starts"}, false), |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
0 commit comments