Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

feat: add additional config methods to coder sdk #419

Merged
merged 3 commits into from
Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions coder-sdk/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# coder-sdk

`coder-sdk` is a Go client library for [Coder](https://coder.com).
It is not yet stable and therefore we do not recommend depending on the current state of its public APIs.
`coder-sdk` is a Go client library for [Coder](https://coder.com).
It is not yet stable and therefore we do not recommend depending on the current
state of its public APIs.

## Usage

Expand Down
24 changes: 11 additions & 13 deletions coder-sdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,25 @@ type ClientOptions struct {

// Token is the API Token used to authenticate (optional).
//
// If Token is provided, the DefaultClient will use it to
// authenticate. If it is not provided, the client requires
// another type of credential, such as an Email/Password pair.
// If Token is provided, the DefaultClient will use it to authenticate.
// If it is not provided, the client requires another type of
// credential, such as an Email/Password pair.
Token string

// Email used to authenticate with Coder.
//
// If you supply an Email and Password pair, NewClient will
// exchange these credentials for a Token during initialization.
// This is only applicable for the built-in authentication
// provider. The client will not retain these credentials in
// memory after NewClient returns.
// If you supply an Email and Password pair, NewClient will exchange
// these credentials for a Token during initialization. This is only
// applicable for the built-in authentication provider. The client will
// not retain these credentials in memory after NewClient returns.
Email string

// Password used to authenticate with Coder.
//
// If you supply an Email and Password pair, NewClient will
// exchange these credentials for a Token during initialization.
// This is only applicable for the built-in authentication
// provider. The client will not retain these credentials in
// memory after NewClient returns.
// If you supply an Email and Password pair, NewClient will exchange
// these credentials for a Token during initialization. This is only
// applicable for the built-in authentication provider. The client will
// not retain these credentials in memory after NewClient returns.
Password string
}

Expand Down
93 changes: 81 additions & 12 deletions coder-sdk/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ const (
AuthProviderOIDC AuthProviderType = "oidc"
)

// ConfigAuth describes the authentication configuration for a Coder deployment.
// ConfigAuth describes the authentication configuration for a Coder
// deployment.
type ConfigAuth struct {
ProviderType *AuthProviderType `json:"provider_type"`
OIDC *ConfigOIDC `json:"oidc"`
SAML *ConfigSAML `json:"saml"`
}

// ConfigOIDC describes the OIDC configuration for single-signon support in Coder.
// ConfigOIDC describes the OIDC configuration for single-signon support in
// Coder.
type ConfigOIDC struct {
ClientID *string `json:"client_id"`
ClientSecret *string `json:"client_secret"`
Expand All @@ -38,26 +40,30 @@ type ConfigSAML struct {
PublicKeyCertificate *string `json:"public_key_certificate"`
}

// ConfigOAuthBitbucketServer describes the Bitbucket integration configuration for a Coder deployment.
// ConfigOAuthBitbucketServer describes the Bitbucket integration configuration
// for a Coder deployment.
type ConfigOAuthBitbucketServer struct {
BaseURL string `json:"base_url" diff:"oauth.bitbucket_server.base_url"`
}

// ConfigOAuthGitHub describes the Github integration configuration for a Coder deployment.
// ConfigOAuthGitHub describes the Github integration configuration for a Coder
// deployment.
type ConfigOAuthGitHub struct {
BaseURL string `json:"base_url"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
}

// ConfigOAuthGitLab describes the GitLab integration configuration for a Coder deployment.
// ConfigOAuthGitLab describes the GitLab integration configuration for a Coder
// deployment.
type ConfigOAuthGitLab struct {
BaseURL string `json:"base_url"`
ClientID string `json:"client_id" `
ClientSecret string `json:"client_secret"`
}

// ConfigOAuth describes the aggregate git integration configuration for a Coder deployment.
// ConfigOAuth describes the aggregate git integration configuration for a
// Coder deployment.
type ConfigOAuth struct {
BitbucketServer ConfigOAuthBitbucketServer `json:"bitbucket_server"`
GitHub ConfigOAuthGitHub `json:"github"`
Expand Down Expand Up @@ -140,18 +146,81 @@ func (c *DefaultClient) PutSiteConfigExtensionMarketplace(ctx context.Context, r

// ConfigWorkspaces is the site configuration for workspace attributes.
type ConfigWorkspaces struct {
GPUVendor string `json:"gpu_vendor,omitempty" valid:"in(nvidia|amd)"`
EnableContainerVMs bool `json:"enable_container_vms,omitempty"`
EnableWorkspacesAsCode bool `json:"enable_workspaces_as_code,omitempty"`
EnableP2P bool `json:"enable_p2p,omitempty"`
GPUVendor string `json:"gpu_vendor"`
EnableContainerVMs bool `json:"enable_container_vms"`
EnableWorkspacesAsCode bool `json:"enable_workspaces_as_code"`
}

// SiteConfigWorkspaces fetches the workspace configuration.
func (c *DefaultClient) SiteConfigWorkspaces(ctx context.Context) (*ConfigWorkspaces, error) {
var conf ConfigWorkspaces
// TODO: use the `/api/v0/workspaces/config route once we migrate from using general config
if err := c.requestBody(ctx, http.MethodGet, "/api/private/config", nil, &conf); err != nil {
if err := c.requestBody(ctx, http.MethodGet, "/api/v0/workspaces/config", nil, &conf); err != nil {
return nil, err
}
return &conf, nil
}

// PutSiteConfigWorkspaces sets the workspace configuration.
func (c *DefaultClient) PutSiteConfigWorkspaces(ctx context.Context, req ConfigWorkspaces) error {
return c.requestBody(ctx, http.MethodPut, "/api/v0/workspaces/config", req, nil)
}

type ConfigDormancy struct {
// UserDormancyThresholdDays is not currently updatable.
// UserDormancyThresholdDays int `json:"user_dormancy_threshold_days"`
UserDeletionThresholdDays int `json:"user_deletion_threshold_days"`
}

// SiteConfigDormancy fetches the dormancy configuration.
func (c *DefaultClient) SiteConfigDormancy(ctx context.Context) (*ConfigDormancy, error) {
var conf ConfigDormancy
if err := c.requestBody(ctx, http.MethodGet, "/api/private/dormancy/config", nil, &conf); err != nil {
return nil, err
}
return &conf, nil
}

// PutSiteConfigDormancy sets the dormancy configuration.
func (c *DefaultClient) PutSiteConfigDormancy(ctx context.Context, req ConfigDormancy) error {
return c.requestBody(ctx, http.MethodPut, "/api/private/dormancy/config", req, nil)
}

type ConfigDevURLAccess struct {
Private bool `json:"private"`
Org bool `json:"org"`
Authed bool `json:"authed"`
Public bool `json:"public"`
}

// SiteConfigDevURLAccess fetches the DevURL access configuration.
func (c *DefaultClient) SiteConfigDevURLAccess(ctx context.Context) (*ConfigDevURLAccess, error) {
var conf ConfigDevURLAccess
if err := c.requestBody(ctx, http.MethodGet, "/api/private/devurls/config", nil, &conf); err != nil {
return nil, err
}
return &conf, nil
}

// PutSiteConfigDevURLAccess sets the DevURL access configuration.
func (c *DefaultClient) PutSiteConfigDevURLAccess(ctx context.Context, req ConfigDevURLAccess) error {
return c.requestBody(ctx, http.MethodPut, "/api/private/devurls/config", req, nil)
}

// ConfigSSHSettings is the site configuration for SSH.
type ConfigSSHSettings struct {
KeygenAlgorithm string `json:"keygen_algorithm"`
}

// SiteConfigSSHSettings fetches the SSH configuration.
func (c *DefaultClient) SiteConfigSSHSettings(ctx context.Context) (*ConfigSSHSettings, error) {
var conf ConfigSSHSettings
if err := c.requestBody(ctx, http.MethodGet, "/api/private/ssh/config", nil, &conf); err != nil {
return nil, err
}
return &conf, nil
}

// PutSiteConfigSSHSettings sets the SSH configuration.
func (c *DefaultClient) PutSiteConfigSSHSettings(ctx context.Context, req ConfigSSHSettings) error {
return c.requestBody(ctx, http.MethodPut, "/api/private/ssh/config", req, nil)
}
4 changes: 2 additions & 2 deletions internal/cmd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func Test_users(t *testing.T) {

func assertAdmin(t *testing.T, users []coder.User) {
for _, u := range users {
if u.Username == "admin" {
if u.Username == "kyle" {
return
}
}
slogtest.Fatal(t, "did not find admin user", slog.F("users", users))
slogtest.Fatal(t, "did not find kyle user", slog.F("users", users))
Comment on lines +27 to +31
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did Kyle rename admin to kyle on master?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API no longer returns the admin user so I just picked another.

}