feat: Support config files with viper (#4558)

This commit is contained in:
Garrett Delfosse
2022-10-21 15:26:39 -04:00
committed by GitHub
parent 2c47cda3d1
commit c8e299c8f1
35 changed files with 920 additions and 1089 deletions

View File

@ -0,0 +1,97 @@
package codersdk
import (
"context"
"encoding/json"
"net/http"
"time"
"golang.org/x/xerrors"
)
// DeploymentConfig is the central configuration for the coder server.
// Secret values should specify `json:"-"` to prevent them from being returned by the API.
type DeploymentConfig struct {
AccessURL DeploymentConfigField[string] `json:"access_url"`
WildcardAccessURL DeploymentConfigField[string] `json:"wildcard_access_url"`
Address DeploymentConfigField[string] `json:"address"`
AutobuildPollInterval DeploymentConfigField[time.Duration] `json:"autobuild_poll_interval"`
DERPServerEnable DeploymentConfigField[bool] `json:"derp_server_enabled"`
DERPServerRegionID DeploymentConfigField[int] `json:"derp_server_region_id"`
DERPServerRegionCode DeploymentConfigField[string] `json:"derp_server_region_code"`
DERPServerRegionName DeploymentConfigField[string] `json:"derp_server_region_name"`
DERPServerSTUNAddresses DeploymentConfigField[[]string] `json:"derp_server_stun_address"`
DERPServerRelayAddress DeploymentConfigField[string] `json:"derp_server_relay_address"`
DERPConfigURL DeploymentConfigField[string] `json:"derp_config_url"`
DERPConfigPath DeploymentConfigField[string] `json:"derp_config_path"`
PrometheusEnable DeploymentConfigField[bool] `json:"prometheus_enabled"`
PrometheusAddress DeploymentConfigField[string] `json:"prometheus_address"`
PprofEnable DeploymentConfigField[bool] `json:"pprof_enabled"`
PprofAddress DeploymentConfigField[string] `json:"pprof_address"`
CacheDirectory DeploymentConfigField[string] `json:"cache_directory"`
InMemoryDatabase DeploymentConfigField[bool] `json:"in_memory_database"`
ProvisionerDaemons DeploymentConfigField[int] `json:"provisioner_daemon_count"`
PostgresURL DeploymentConfigField[string] `json:"-"`
OAuth2GithubClientID DeploymentConfigField[string] `json:"oauth2_github_client_id"`
OAuth2GithubClientSecret DeploymentConfigField[string] `json:"-"`
OAuth2GithubAllowedOrganizations DeploymentConfigField[[]string] `json:"oauth2_github_allowed_organizations"`
OAuth2GithubAllowedTeams DeploymentConfigField[[]string] `json:"oauth2_github_allowed_teams"`
OAuth2GithubAllowSignups DeploymentConfigField[bool] `json:"oauth2_github_allow_signups"`
OAuth2GithubEnterpriseBaseURL DeploymentConfigField[string] `json:"oauth2_github_enterprise_base_url"`
OIDCAllowSignups DeploymentConfigField[bool] `json:"oidc_allow_signups"`
OIDCClientID DeploymentConfigField[string] `json:"oidc_client_id"`
OIDCClientSecret DeploymentConfigField[string] `json:"-"`
OIDCEmailDomain DeploymentConfigField[string] `json:"oidc_email_domain"`
OIDCIssuerURL DeploymentConfigField[string] `json:"oidc_issuer_url"`
OIDCScopes DeploymentConfigField[[]string] `json:"oidc_scopes"`
TelemetryEnable DeploymentConfigField[bool] `json:"telemetry_enable"`
TelemetryTrace DeploymentConfigField[bool] `json:"telemetry_trace_enable"`
TelemetryURL DeploymentConfigField[string] `json:"telemetry_url"`
TLSEnable DeploymentConfigField[bool] `json:"tls_enable"`
TLSCertFiles DeploymentConfigField[[]string] `json:"tls_cert_files"`
TLSClientCAFile DeploymentConfigField[string] `json:"tls_client_ca_file"`
TLSClientAuth DeploymentConfigField[string] `json:"tls_client_auth"`
TLSKeyFiles DeploymentConfigField[[]string] `json:"tls_key_files"`
TLSMinVersion DeploymentConfigField[string] `json:"tls_min_version"`
TraceEnable DeploymentConfigField[bool] `json:"trace_enable"`
SecureAuthCookie DeploymentConfigField[bool] `json:"secure_auth_cookie"`
SSHKeygenAlgorithm DeploymentConfigField[string] `json:"ssh_keygen_algorithm"`
AutoImportTemplates DeploymentConfigField[[]string] `json:"auto_import_templates"`
MetricsCacheRefreshInterval DeploymentConfigField[time.Duration] `json:"metrics_cache_refresh_interval"`
AgentStatRefreshInterval DeploymentConfigField[time.Duration] `json:"agent_stat_refresh_interval"`
AuditLogging DeploymentConfigField[bool] `json:"audit_logging"`
BrowserOnly DeploymentConfigField[bool] `json:"browser_only"`
SCIMAPIKey DeploymentConfigField[string] `json:"-"`
UserWorkspaceQuota DeploymentConfigField[int] `json:"user_workspace_quota"`
}
type Flaggable interface {
string | bool | int | time.Duration | []string
}
type DeploymentConfigField[T Flaggable] struct {
Key string `json:"key"`
Name string `json:"name"`
Usage string `json:"usage"`
Flag string `json:"flag"`
Shorthand string `json:"shorthand"`
Enterprise bool `json:"enterprise"`
Hidden bool `json:"hidden"`
Value T `json:"value"`
}
// DeploymentConfig returns the deployment config for the coder server.
func (c *Client) DeploymentConfig(ctx context.Context) (DeploymentConfig, error) {
res, err := c.Request(ctx, http.MethodGet, "/api/v2/config/deployment", nil)
if err != nil {
return DeploymentConfig{}, xerrors.Errorf("execute request: %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return DeploymentConfig{}, readBodyAsError(res)
}
var df DeploymentConfig
return df, json.NewDecoder(res.Body).Decode(&df)
}

View File

@ -1,142 +0,0 @@
package codersdk
import (
"context"
"encoding/json"
"net/http"
"time"
"golang.org/x/xerrors"
)
type DeploymentFlags struct {
AccessURL *StringFlag `json:"access_url" typescript:",notnull"`
WildcardAccessURL *StringFlag `json:"wildcard_access_url" typescript:",notnull"`
Address *StringFlag `json:"address" typescript:",notnull"`
AutobuildPollInterval *DurationFlag `json:"autobuild_poll_interval" typescript:",notnull"`
DerpServerEnable *BoolFlag `json:"derp_server_enabled" typescript:",notnull"`
DerpServerRegionID *IntFlag `json:"derp_server_region_id" typescript:",notnull"`
DerpServerRegionCode *StringFlag `json:"derp_server_region_code" typescript:",notnull"`
DerpServerRegionName *StringFlag `json:"derp_server_region_name" typescript:",notnull"`
DerpServerSTUNAddresses *StringArrayFlag `json:"derp_server_stun_address" typescript:",notnull"`
DerpServerRelayAddress *StringFlag `json:"derp_server_relay_address" typescript:",notnull"`
DerpConfigURL *StringFlag `json:"derp_config_url" typescript:",notnull"`
DerpConfigPath *StringFlag `json:"derp_config_path" typescript:",notnull"`
PromEnabled *BoolFlag `json:"prom_enabled" typescript:",notnull"`
PromAddress *StringFlag `json:"prom_address" typescript:",notnull"`
PprofEnabled *BoolFlag `json:"pprof_enabled" typescript:",notnull"`
PprofAddress *StringFlag `json:"pprof_address" typescript:",notnull"`
CacheDir *StringFlag `json:"cache_dir" typescript:",notnull"`
InMemoryDatabase *BoolFlag `json:"in_memory_database" typescript:",notnull"`
ProvisionerDaemonCount *IntFlag `json:"provisioner_daemon_count" typescript:",notnull"`
PostgresURL *StringFlag `json:"postgres_url" typescript:",notnull"`
OAuth2GithubClientID *StringFlag `json:"oauth2_github_client_id" typescript:",notnull"`
OAuth2GithubClientSecret *StringFlag `json:"oauth2_github_client_secret" typescript:",notnull"`
OAuth2GithubAllowedOrganizations *StringArrayFlag `json:"oauth2_github_allowed_organizations" typescript:",notnull"`
OAuth2GithubAllowedTeams *StringArrayFlag `json:"oauth2_github_allowed_teams" typescript:",notnull"`
OAuth2GithubAllowSignups *BoolFlag `json:"oauth2_github_allow_signups" typescript:",notnull"`
OAuth2GithubEnterpriseBaseURL *StringFlag `json:"oauth2_github_enterprise_base_url" typescript:",notnull"`
OIDCAllowSignups *BoolFlag `json:"oidc_allow_signups" typescript:",notnull"`
OIDCClientID *StringFlag `json:"oidc_client_id" typescript:",notnull"`
OIDCClientSecret *StringFlag `json:"oidc_client_secret" typescript:",notnull"`
OIDCEmailDomain *StringFlag `json:"oidc_email_domain" typescript:",notnull"`
OIDCIssuerURL *StringFlag `json:"oidc_issuer_url" typescript:",notnull"`
OIDCScopes *StringArrayFlag `json:"oidc_scopes" typescript:",notnull"`
TelemetryEnable *BoolFlag `json:"telemetry_enable" typescript:",notnull"`
TelemetryTraceEnable *BoolFlag `json:"telemetry_trace_enable" typescript:",notnull"`
TelemetryURL *StringFlag `json:"telemetry_url" typescript:",notnull"`
TLSEnable *BoolFlag `json:"tls_enable" typescript:",notnull"`
TLSCertFiles *StringArrayFlag `json:"tls_cert_files" typescript:",notnull"`
TLSClientCAFile *StringFlag `json:"tls_client_ca_file" typescript:",notnull"`
TLSClientAuth *StringFlag `json:"tls_client_auth" typescript:",notnull"`
TLSKeyFiles *StringArrayFlag `json:"tls_key_files" typescript:",notnull"`
TLSMinVersion *StringFlag `json:"tls_min_version" typescript:",notnull"`
TraceEnable *BoolFlag `json:"trace_enable" typescript:",notnull"`
SecureAuthCookie *BoolFlag `json:"secure_auth_cookie" typescript:",notnull"`
SSHKeygenAlgorithm *StringFlag `json:"ssh_keygen_algorithm" typescript:",notnull"`
AutoImportTemplates *StringArrayFlag `json:"auto_import_templates" typescript:",notnull"`
MetricsCacheRefreshInterval *DurationFlag `json:"metrics_cache_refresh_interval" typescript:",notnull"`
AgentStatRefreshInterval *DurationFlag `json:"agent_stat_refresh_interval" typescript:",notnull"`
Verbose *BoolFlag `json:"verbose" typescript:",notnull"`
AuditLogging *BoolFlag `json:"audit_logging" typescript:",notnull"`
BrowserOnly *BoolFlag `json:"browser_only" typescript:",notnull"`
SCIMAuthHeader *StringFlag `json:"scim_auth_header" typescript:",notnull"`
UserWorkspaceQuota *IntFlag `json:"user_workspace_quota" typescript:",notnull"`
}
type StringFlag struct {
Name string `json:"name"`
Flag string `json:"flag"`
EnvVar string `json:"env_var"`
Shorthand string `json:"shorthand"`
Description string `json:"description"`
Enterprise bool `json:"enterprise"`
Secret bool `json:"secret"`
Hidden bool `json:"hidden"`
Default string `json:"default"`
Value string `json:"value"`
}
type BoolFlag struct {
Name string `json:"name"`
Flag string `json:"flag"`
EnvVar string `json:"env_var"`
Shorthand string `json:"shorthand"`
Description string `json:"description"`
Enterprise bool `json:"enterprise"`
Hidden bool `json:"hidden"`
Default bool `json:"default"`
Value bool `json:"value"`
}
type IntFlag struct {
Name string `json:"name"`
Flag string `json:"flag"`
EnvVar string `json:"env_var"`
Shorthand string `json:"shorthand"`
Description string `json:"description"`
Enterprise bool `json:"enterprise"`
Hidden bool `json:"hidden"`
Default int `json:"default"`
Value int `json:"value"`
}
type DurationFlag struct {
Name string `json:"name"`
Flag string `json:"flag"`
EnvVar string `json:"env_var"`
Shorthand string `json:"shorthand"`
Description string `json:"description"`
Enterprise bool `json:"enterprise"`
Hidden bool `json:"hidden"`
Default time.Duration `json:"default"`
Value time.Duration `json:"value"`
}
type StringArrayFlag struct {
Name string `json:"name"`
Flag string `json:"flag"`
EnvVar string `json:"env_var"`
Shorthand string `json:"shorthand"`
Description string `json:"description"`
Enterprise bool `json:"enterprise"`
Hidden bool `json:"hidden"`
Default []string `json:"default"`
Value []string `json:"value"`
}
// DeploymentFlags returns the deployment level flags for the coder server.
func (c *Client) DeploymentFlags(ctx context.Context) (DeploymentFlags, error) {
res, err := c.Request(ctx, http.MethodGet, "/api/v2/flags/deployment", nil)
if err != nil {
return DeploymentFlags{}, xerrors.Errorf("execute request: %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return DeploymentFlags{}, readBodyAsError(res)
}
var df DeploymentFlags
return df, json.NewDecoder(res.Body).Decode(&df)
}