Skip to content

Commit

Permalink
feat: include deprecation message in docs (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtojek authored Jun 26, 2024
1 parent b73066b commit dd5f55c
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 13 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ fmt:

gen:
go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs@latest
go run ./scripts/docsgen/...

build: terraform-provider-coder

Expand Down
14 changes: 7 additions & 7 deletions docs/data-sources/workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ resource "kubernetes_pod" "dev" {
- `access_url` (String) The access URL of the Coder deployment provisioning this workspace.
- `id` (String) UUID of the workspace.
- `name` (String) Name of the workspace.
- `owner` (String, Deprecated) Username of the workspace owner.
- `owner_email` (String, Deprecated) Email address of the workspace owner.
- `owner_groups` (List of String, Deprecated) List of groups the workspace owner belongs to.
- `owner_id` (String, Deprecated) UUID of the workspace owner.
- `owner_name` (String, Deprecated) Name of the workspace owner.
- `owner_oidc_access_token` (String, Deprecated) A valid OpenID Connect access token of the workspace owner. This is only available if the workspace owner authenticated with OpenID Connect. If a valid token cannot be obtained, this value will be an empty string.
- `owner_session_token` (String, Deprecated) Session token for authenticating with a Coder deployment. It is regenerated everytime a workspace is started.
- `owner` (String, Deprecated: Use `coder_workspace_owner.name` instead.) Username of the workspace owner.
- `owner_email` (String, Deprecated: Use `coder_workspace_owner.email` instead.) Email address of the workspace owner.
- `owner_groups` (List of String, Deprecated: Use `coder_workspace_owner.groups` instead.) List of groups the workspace owner belongs to.
- `owner_id` (String, Deprecated: Use `coder_workspace_owner.id` instead.) UUID of the workspace owner.
- `owner_name` (String, Deprecated: Use `coder_workspace_owner.full_name` instead.) Name of the workspace owner.
- `owner_oidc_access_token` (String, Deprecated: Use `coder_workspace_owner.oidc_access_token` instead.) A valid OpenID Connect access token of the workspace owner. This is only available if the workspace owner authenticated with OpenID Connect. If a valid token cannot be obtained, this value will be an empty string.
- `owner_session_token` (String, Deprecated: Use `coder_workspace_owner.session_token` instead.) Session token for authenticating with a Coder deployment. It is regenerated everytime a workspace is started.
- `start_count` (Number) A computed count based on "transition" state. If "start", count will equal 1.
- `template_id` (String) ID of the workspace's template.
- `template_name` (String) Name of the workspace's template.
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ resource "google_compute_instance" "dev" {

### Optional

- `feature_use_managed_variables` (Boolean, Deprecated) Feature: use managed Terraform variables. The feature flag is not used anymore as Terraform variables are now exclusively utilized for template-wide variables.
- `feature_use_managed_variables` (Boolean, Deprecated: Terraform variables are now exclusively utilized for template-wide variables after the removal of support for legacy parameters.) Feature: use managed Terraform variables. The feature flag is not used anymore as Terraform variables are now exclusively utilized for template-wide variables.
- `url` (String) The URL to access Coder.
6 changes: 3 additions & 3 deletions docs/resources/agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ resource "kubernetes_pod" "dev" {
- `dir` (String) The starting directory when a user creates a shell session. Defaults to $HOME.
- `display_apps` (Block Set, Max: 1) The list of built-in apps to display in the agent bar. (see [below for nested schema](#nestedblock--display_apps))
- `env` (Map of String) A mapping of environment variables to set inside the workspace.
- `login_before_ready` (Boolean, Deprecated) This option defines whether or not the user can (by default) login to the workspace before it is ready. Ready means that e.g. the startup_script is done and has exited. When enabled, users may see an incomplete workspace when logging in.
- `login_before_ready` (Boolean, Deprecated: Configure startup_script_behavior instead. This attribute will be removed in a future version of the provider.) This option defines whether or not the user can (by default) login to the workspace before it is ready. Ready means that e.g. the startup_script is done and has exited. When enabled, users may see an incomplete workspace when logging in.
- `metadata` (Block List) Each "metadata" block defines a single item consisting of a key/value pair. This feature is in alpha and may break in future releases. (see [below for nested schema](#nestedblock--metadata))
- `motd_file` (String) The path to a file within the workspace containing a message to display to users when they login via SSH. A typical value would be /etc/motd.
- `order` (Number) The order determines the position of agents in the UI presentation. The lowest order is shown first and agents with equal order are sorted by name (ascending order).
- `shutdown_script` (String) A script to run before the agent is stopped. The script should exit when it is done to signal that the workspace can be stopped. This option is an alias for defining a "coder_script" resource with "run_on_stop" set to true.
- `shutdown_script_timeout` (Number, Deprecated) Time in seconds until the agent lifecycle status is marked as timed out during shutdown, this happens when the shutdown script has not completed (exited) in the given time.
- `shutdown_script_timeout` (Number, Deprecated: This feature is deprecated and has no effect. This attribute will be removed in a future version of the provider.) Time in seconds until the agent lifecycle status is marked as timed out during shutdown, this happens when the shutdown script has not completed (exited) in the given time.
- `startup_script` (String) A script to run after the agent starts. The script should exit when it is done to signal that the agent is ready. This option is an alias for defining a "coder_script" resource with "run_on_start" set to true.
- `startup_script_behavior` (String) This option sets the behavior of the "startup_script". When set to "blocking", the startup_script must exit before the workspace is ready. When set to "non-blocking", the startup_script may run in the background and the workspace will be ready immediately. Default is "non-blocking", although "blocking" is recommended. This option is an alias for defining a "coder_script" resource with "start_blocks_login" set to true (blocking).
- `startup_script_timeout` (Number, Deprecated) Time in seconds until the agent lifecycle status is marked as timed out during start, this happens when the startup script has not completed (exited) in the given time.
- `startup_script_timeout` (Number, Deprecated: This feature is deprecated and has no effect. This attribute will be removed in a future version of the provider.) Time in seconds until the agent lifecycle status is marked as timed out during start, this happens when the startup script has not completed (exited) in the given time.
- `troubleshooting_url` (String) A URL to a document with instructions for troubleshooting problems with the agent.

### Read-Only
Expand Down
4 changes: 2 additions & 2 deletions docs/resources/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ resource "coder_app" "vim" {
- `external` (Boolean) Specifies whether "url" is opened on the client machine instead of proxied through the workspace.
- `healthcheck` (Block Set, Max: 1) HTTP health checking to determine the application readiness. (see [below for nested schema](#nestedblock--healthcheck))
- `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/<path>"`.
- `name` (String, Deprecated) A display name to identify the app.
- `name` (String, Deprecated: `name` on apps is deprecated, use `display_name` instead) A display name to identify the app.
- `order` (Number) The order determines the position of app in the UI presentation. The lowest order is shown first and apps with equal order are sorted by name (ascending order).
- `relative_path` (Boolean, Deprecated) Specifies whether the URL will be accessed via a relative path or wildcard. Use if wildcard routing is unavailable. Defaults to true.
- `relative_path` (Boolean, Deprecated: `relative_path` on apps is deprecated, use `subdomain` instead.) Specifies whether the URL will be accessed via a relative path or wildcard. Use if wildcard routing is unavailable. Defaults to true.
- `share` (String) Determines the "level" which the application is shared at. Valid levels are "owner" (default), "authenticated" and "public". Level "owner" disables sharing on the app, so only the workspace owner can access it. Level "authenticated" shares the app with all authenticated users. Level "public" shares it with any user, including unauthenticated users. Permitted application sharing levels can be configured site-wide via a flag on `coder server` (Enterprise only).
- `subdomain` (Boolean) Determines whether the app will be accessed via it's own subdomain or whether it will be accessed via a path on Coder. If wildcards have not been setup by the administrator then apps with "subdomain" set to true will not be accessible. Defaults to false.
- `url` (String) An external url if "external=true" or a URL to be proxied to from inside the workspace. This should be of the form "http://localhost:PORT[/SUBPATH]". Either "command" or "url" may be specified, but not both.
Expand Down
89 changes: 89 additions & 0 deletions scripts/docsgen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

import (
"bytes"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/coder/terraform-provider-coder/provider"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"golang.org/x/xerrors"
)

// This script patches Markdown docs generated by `terraform-plugin-docs` to expose the original deprecation message.

const docsDir = "docs" // FIXME expose as flag?

var reDeprecatedProperty = regexp.MustCompile("`([^`]+)` \\(([^,\\)]+), Deprecated\\) ([^\n]+)")

func main() {
p := provider.New()
err := exposeDeprecationMessage(p)
if err != nil {
log.Fatal(err)
}
}

func exposeDeprecationMessage(p *schema.Provider) error {
// Patch data-sources
for dataSourceName, dataSource := range p.DataSourcesMap {
docFile := filepath.Join(docsDir, "data-sources", strings.Replace(dataSourceName, "coder_", "", 1)+".md")

err := adjustDocFile(docFile, dataSource.Schema)
if err != nil {
return xerrors.Errorf("unable to adjust data-source doc file (data-source: %s): %w", dataSourceName, err)
}
}

// Patch resources
for resourceName, resource := range p.ResourcesMap {
docFile := filepath.Join(docsDir, "resources", strings.Replace(resourceName, "coder_", "", 1)+".md")

err := adjustDocFile(docFile, resource.Schema)
if err != nil {
return xerrors.Errorf("unable to adjust resource doc file (resource: %s): %w", resourceName, err)
}
}

// Patch index
docFile := filepath.Join(docsDir, "index.md")
err := adjustDocFile(docFile, p.Schema)
if err != nil {
return xerrors.Errorf("unable to adjust index doc file: %w", err)
}
return nil
}

func adjustDocFile(docPath string, schemas map[string]*schema.Schema) error {
doc, err := os.ReadFile(docPath)
if err != nil {
return xerrors.Errorf("can't read the source doc file: %w", err)
}

result := writeDeprecationMessage(doc, schemas)

err = os.WriteFile(docPath, result, 0644)
if err != nil {
return xerrors.Errorf("can't write modified doc file: %w", err)
}
return nil
}

func writeDeprecationMessage(doc []byte, schemas map[string]*schema.Schema) []byte {
return reDeprecatedProperty.ReplaceAllFunc(doc, func(m []byte) []byte {
matches := reDeprecatedProperty.FindSubmatch(m)
propertyName := matches[1]
description := matches[3]

sch := schemas[string(propertyName)]
if string(description) != sch.Description {
log.Printf("warn: same property name `%s` but description does not match, most likely a different property", propertyName)
return m
}
return bytes.Replace(m, []byte("Deprecated"), []byte(fmt.Sprintf("Deprecated: %s", sch.Deprecated)), 1)
})
}

0 comments on commit dd5f55c

Please sign in to comment.