diff --git a/docs/resources/script.md b/docs/resources/script.md new file mode 100644 index 00000000..51213aac --- /dev/null +++ b/docs/resources/script.md @@ -0,0 +1,36 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "coder_script Resource - terraform-provider-coder" +subcategory: "" +description: |- + Use this resource to run a script from an agent. +--- + +# coder_script (Resource) + +Use this resource to run a script from an agent. + + + + +## Schema + +### Required + +- `agent_id` (String) The "id" property of a "coder_agent" resource to associate with. +- `display_name` (String) The display name of the script to display logs in the dashboard. +- `script` (String) The script to run. + +### Optional + +- `cron` (String) The cron schedule to run the script on. This is a cron expression. +- `icon` (String) A URL to an icon that will display in the dashboard. View built-in icons here: https://github.com/coder/coder/tree/main/site/static/icon. Use a built-in icon with `data.coder_workspace.me.access_url + "/icon/"`. +- `log_path` (String) The path of a file to write the logs to. If relative, it will be appended to tmp. +- `run_on_start` (Boolean) This option defines whether or not the script should run when the agent starts. +- `run_on_stop` (Boolean) This option defines whether or not the script should run when the agent stops. +- `start_blocks_login` (Boolean) This option defines whether or not the user can (by default) login to the workspace before this script completes running on start. When enabled, users may see an incomplete workspace when logging in. +- `timeout` (Number) Time in seconds until the agent lifecycle status is marked as timed out, this happens when the script has not completed (exited) in the given time. + +### Read-Only + +- `id` (String) The ID of this resource. diff --git a/provider/provider.go b/provider/provider.go index 32058b5b..9ea6685b 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -78,6 +78,7 @@ func New() *schema.Provider { "coder_agent_instance": agentInstanceResource(), "coder_app": appResource(), "coder_metadata": metadataResource(), + "coder_script": scriptResource(), }, } } diff --git a/provider/script.go b/provider/script.go new file mode 100644 index 00000000..fd02d4cc --- /dev/null +++ b/provider/script.go @@ -0,0 +1,91 @@ +package provider + +import ( + "context" + + "github.com/google/uuid" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" +) + +func scriptResource() *schema.Resource { + return &schema.Resource{ + Description: "Use this resource to run a script from an agent.", + CreateContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { + rd.SetId(uuid.NewString()) + return nil + }, + ReadContext: schema.NoopContext, + DeleteContext: schema.NoopContext, + Schema: map[string]*schema.Schema{ + "agent_id": { + Type: schema.TypeString, + Description: `The "id" property of a "coder_agent" resource to associate with.`, + ForceNew: true, + Required: true, + }, + "display_name": { + Type: schema.TypeString, + Description: "The display name of the script to display logs in the dashboard.", + ForceNew: true, + Required: true, + }, + "log_path": { + Type: schema.TypeString, + Description: "The path of a file to write the logs to. If relative, it will be appended to tmp.", + ForceNew: true, + Optional: true, + }, + "icon": { + Type: schema.TypeString, + ForceNew: true, + Optional: true, + Description: "A URL to an icon that will display in the dashboard. View built-in " + + "icons here: https://github.com/coder/coder/tree/main/site/static/icon. Use a " + + "built-in icon with `data.coder_workspace.me.access_url + \"/icon/\"`.", + }, + "script": { + ForceNew: true, + Type: schema.TypeString, + Required: true, + Description: "The script to run.", + }, + "cron": { + ForceNew: true, + Type: schema.TypeString, + Optional: true, + Description: "The cron schedule to run the script on. This is a cron expression.", + }, + "start_blocks_login": { + Type: schema.TypeBool, + Default: false, + ForceNew: true, + Optional: true, + Description: "This option defines whether or not the user can (by default) login to the workspace before this script completes running on start. When enabled, users may see an incomplete workspace when logging in.", + }, + "run_on_start": { + Type: schema.TypeBool, + Default: false, + ForceNew: true, + Optional: true, + Description: "This option defines whether or not the script should run when the agent starts.", + }, + "run_on_stop": { + Type: schema.TypeBool, + Default: false, + ForceNew: true, + Optional: true, + Description: "This option defines whether or not the script should run when the agent stops.", + }, + "timeout": { + Type: schema.TypeInt, + Default: 0, + ForceNew: true, + Optional: true, + Description: "Time in seconds until the agent lifecycle status is marked as timed out, this happens when the script has not completed (exited) in the given time.", + ValidateFunc: validation.IntAtLeast(1), + }, + }, + } +}