|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "reflect" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/google/uuid" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 14 | +) |
| 15 | + |
| 16 | +func agentResource() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + Description: "Use this resource to associate an agent.", |
| 19 | + CreateContext: func(c context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics { |
| 20 | + // This should be a real authentication token! |
| 21 | + resourceData.SetId(uuid.NewString()) |
| 22 | + err := resourceData.Set("token", uuid.NewString()) |
| 23 | + if err != nil { |
| 24 | + return diag.FromErr(err) |
| 25 | + } |
| 26 | + return updateInitScript(resourceData, i) |
| 27 | + }, |
| 28 | + ReadWithoutTimeout: func(c context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics { |
| 29 | + err := resourceData.Set("token", uuid.NewString()) |
| 30 | + if err != nil { |
| 31 | + return diag.FromErr(err) |
| 32 | + } |
| 33 | + return updateInitScript(resourceData, i) |
| 34 | + }, |
| 35 | + DeleteContext: func(c context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { |
| 36 | + return nil |
| 37 | + }, |
| 38 | + Schema: map[string]*schema.Schema{ |
| 39 | + "init_script": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + Description: "Run this script on startup of an instance to initialize the agent.", |
| 43 | + }, |
| 44 | + "arch": { |
| 45 | + Type: schema.TypeString, |
| 46 | + ForceNew: true, |
| 47 | + Required: true, |
| 48 | + Description: `The architecture the agent will run on. Must be one of: "amd64", "armv7", "arm64".`, |
| 49 | + ValidateFunc: validation.StringInSlice([]string{"amd64", "armv7", "arm64"}, false), |
| 50 | + }, |
| 51 | + "auth": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Default: "token", |
| 54 | + ForceNew: true, |
| 55 | + Optional: true, |
| 56 | + Description: `The authentication type the agent will use. Must be one of: "token", "google-instance-identity", "aws-instance-identity", "azure-instance-identity".`, |
| 57 | + ValidateFunc: validation.StringInSlice([]string{"token", "google-instance-identity", "aws-instance-identity", "azure-instance-identity"}, false), |
| 58 | + }, |
| 59 | + "dir": { |
| 60 | + Type: schema.TypeString, |
| 61 | + ForceNew: true, |
| 62 | + Optional: true, |
| 63 | + Description: "The starting directory when a user creates a shell session. Defaults to $HOME.", |
| 64 | + }, |
| 65 | + "env": { |
| 66 | + ForceNew: true, |
| 67 | + Description: "A mapping of environment variables to set inside the workspace.", |
| 68 | + Type: schema.TypeMap, |
| 69 | + Optional: true, |
| 70 | + }, |
| 71 | + "os": { |
| 72 | + Type: schema.TypeString, |
| 73 | + ForceNew: true, |
| 74 | + Required: true, |
| 75 | + Description: `The operating system the agent will run on. Must be one of: "linux", "darwin", or "windows".`, |
| 76 | + ValidateFunc: validation.StringInSlice([]string{"linux", "darwin", "windows"}, false), |
| 77 | + }, |
| 78 | + "startup_script": { |
| 79 | + ForceNew: true, |
| 80 | + Description: "A script to run after the agent starts.", |
| 81 | + Type: schema.TypeString, |
| 82 | + Optional: true, |
| 83 | + }, |
| 84 | + "token": { |
| 85 | + ForceNew: true, |
| 86 | + Sensitive: true, |
| 87 | + Description: `Set the environment variable "CODER_AGENT_TOKEN" with this token to authenticate an agent.`, |
| 88 | + Type: schema.TypeString, |
| 89 | + Computed: true, |
| 90 | + }, |
| 91 | + }, |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func agentInstanceResource() *schema.Resource { |
| 96 | + return &schema.Resource{ |
| 97 | + Description: "Use this resource to associate an instance ID with an agent for zero-trust " + |
| 98 | + "authentication. This association is done automatically for \"google_compute_instance\", " + |
| 99 | + "\"aws_instance\", \"azurerm_linux_virtual_machine\", and " + |
| 100 | + "\"azurerm_windows_virtual_machine\" resources.", |
| 101 | + CreateContext: func(c context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics { |
| 102 | + resourceData.SetId(uuid.NewString()) |
| 103 | + return nil |
| 104 | + }, |
| 105 | + ReadContext: func(c context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics { |
| 106 | + return nil |
| 107 | + }, |
| 108 | + DeleteContext: func(c context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { |
| 109 | + return nil |
| 110 | + }, |
| 111 | + Schema: map[string]*schema.Schema{ |
| 112 | + "agent_id": { |
| 113 | + Type: schema.TypeString, |
| 114 | + Description: `The "id" property of a "coder_agent" resource to associate with.`, |
| 115 | + ForceNew: true, |
| 116 | + Required: true, |
| 117 | + }, |
| 118 | + "instance_id": { |
| 119 | + ForceNew: true, |
| 120 | + Required: true, |
| 121 | + Description: `The instance identifier of a provisioned resource.`, |
| 122 | + Type: schema.TypeString, |
| 123 | + }, |
| 124 | + }, |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// updateInitScript fetches parameters from a "coder_agent" to produce the |
| 129 | +// agent script from environment variables. |
| 130 | +func updateInitScript(resourceData *schema.ResourceData, i interface{}) diag.Diagnostics { |
| 131 | + config, valid := i.(config) |
| 132 | + if !valid { |
| 133 | + return diag.Errorf("config was unexpected type %q", reflect.TypeOf(i).String()) |
| 134 | + } |
| 135 | + auth, valid := resourceData.Get("auth").(string) |
| 136 | + if !valid { |
| 137 | + return diag.Errorf("auth was unexpected type %q", reflect.TypeOf(resourceData.Get("auth"))) |
| 138 | + } |
| 139 | + operatingSystem, valid := resourceData.Get("os").(string) |
| 140 | + if !valid { |
| 141 | + return diag.Errorf("os was unexpected type %q", reflect.TypeOf(resourceData.Get("os"))) |
| 142 | + } |
| 143 | + arch, valid := resourceData.Get("arch").(string) |
| 144 | + if !valid { |
| 145 | + return diag.Errorf("arch was unexpected type %q", reflect.TypeOf(resourceData.Get("arch"))) |
| 146 | + } |
| 147 | + accessURL, err := config.URL.Parse("/") |
| 148 | + if err != nil { |
| 149 | + return diag.Errorf("parse access url: %s", err) |
| 150 | + } |
| 151 | + script := os.Getenv(fmt.Sprintf("CODER_AGENT_SCRIPT_%s_%s", operatingSystem, arch)) |
| 152 | + if script != "" { |
| 153 | + script = strings.ReplaceAll(script, "${ACCESS_URL}", accessURL.String()) |
| 154 | + script = strings.ReplaceAll(script, "${AUTH_TYPE}", auth) |
| 155 | + } |
| 156 | + err = resourceData.Set("init_script", script) |
| 157 | + if err != nil { |
| 158 | + return diag.FromErr(err) |
| 159 | + } |
| 160 | + return nil |
| 161 | +} |
0 commit comments