Skip to content

Commit

Permalink
v2: stop using factored type blocks
Browse files Browse the repository at this point in the history
Updates #cleanup

Signed-off-by: Percy Wegmann <[email protected]>
  • Loading branch information
oxtoacart committed Sep 20, 2024
1 parent d22e281 commit bdb25c2
Show file tree
Hide file tree
Showing 11 changed files with 310 additions and 334 deletions.
84 changes: 41 additions & 43 deletions v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,49 @@ import (
"github.com/tailscale/hujson"
)

type (
// Client is used to perform actions against the Tailscale API.
Client struct {
// BaseURL is the base URL for accessing the Tailscale API server. Defaults to https://api.tailscale.com.
BaseURL *url.URL
// UserAgent configures the User-Agent HTTP header for requests. Defaults to "tailscale-client-go".
UserAgent string
// APIKey allows specifying an APIKey to use for authentication.
// To use OAuth Client credentials, construct an [http.Client] using [OAuthConfig] and specify that below.
APIKey string
// Tailnet allows specifying a specific Tailnet by name, to which this Client will connect by default.
Tailnet string

// HTTP is the [http.Client] to use for requests to the API server.
// If not specified, a new [http.Client] with a Timeout of 1 minute will be used.
HTTP *http.Client

initOnce sync.Once

// Specific resources
contacts *ContactsResource
devicePosture *DevicePostureResource
devices *DevicesResource
dns *DNSResource
keys *KeysResource
logging *LoggingResource
policyFile *PolicyFileResource
tailnetSettings *TailnetSettingsResource
users *UsersResource
webhooks *WebhooksResource
}
// Client is used to perform actions against the Tailscale API.
type Client struct {
// BaseURL is the base URL for accessing the Tailscale API server. Defaults to https://api.tailscale.com.
BaseURL *url.URL
// UserAgent configures the User-Agent HTTP header for requests. Defaults to "tailscale-client-go".
UserAgent string
// APIKey allows specifying an APIKey to use for authentication.
// To use OAuth Client credentials, construct an [http.Client] using [OAuthConfig] and specify that below.
APIKey string
// Tailnet allows specifying a specific Tailnet by name, to which this Client will connect by default.
Tailnet string

// HTTP is the [http.Client] to use for requests to the API server.
// If not specified, a new [http.Client] with a Timeout of 1 minute will be used.
HTTP *http.Client

initOnce sync.Once

// Specific resources
contacts *ContactsResource
devicePosture *DevicePostureResource
devices *DevicesResource
dns *DNSResource
keys *KeysResource
logging *LoggingResource
policyFile *PolicyFileResource
tailnetSettings *TailnetSettingsResource
users *UsersResource
webhooks *WebhooksResource
}

// APIError type describes an error as returned by the Tailscale API.
APIError struct {
Message string `json:"message"`
Data []APIErrorData `json:"data"`
status int
}
// APIError type describes an error as returned by the Tailscale API.
type APIError struct {
Message string `json:"message"`
Data []APIErrorData `json:"data"`
status int
}

// APIErrorData type describes elements of the data field within errors returned by the Tailscale API.
APIErrorData struct {
User string `json:"user"`
Errors []string `json:"errors"`
}
)
// APIErrorData type describes elements of the data field within errors returned by the Tailscale API.
type APIErrorData struct {
User string `json:"user"`
Errors []string `json:"errors"`
}

const defaultContentType = "application/json"
const defaultHttpClientTimeout = time.Minute
Expand Down
42 changes: 20 additions & 22 deletions v2/contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,29 @@ const (
ContactSecurity ContactType = "security"
)

type (
// ContactType defines the type of contact.
ContactType string
// ContactType defines the type of contact.
type ContactType string

// Contacts type defines the object returned when retrieving contacts.
Contacts struct {
Account Contact `json:"account"`
Support Contact `json:"support"`
Security Contact `json:"security"`
}
// Contacts type defines the object returned when retrieving contacts.
type Contacts struct {
Account Contact `json:"account"`
Support Contact `json:"support"`
Security Contact `json:"security"`
}

// Contact type defines the structure of an individual contact for the tailnet.
Contact struct {
Email string `json:"email"`
// FallbackEmail is the email used when Email has not been verified.
FallbackEmail string `json:"fallbackEmail,omitempty"`
// NeedsVerification is true if Email needs to be verified.
NeedsVerification bool `json:"needsVerification"`
}
// Contact type defines the structure of an individual contact for the tailnet.
type Contact struct {
Email string `json:"email"`
// FallbackEmail is the email used when Email has not been verified.
FallbackEmail string `json:"fallbackEmail,omitempty"`
// NeedsVerification is true if Email needs to be verified.
NeedsVerification bool `json:"needsVerification"`
}

// UpdateContactRequest type defines the structure of a request to update a Contact.
UpdateContactRequest struct {
Email *string `json:"email,omitempty"`
}
)
// UpdateContactRequest type defines the structure of a request to update a Contact.
type UpdateContactRequest struct {
Email *string `json:"email,omitempty"`
}

// Get retieves the [Contacts] for the tailnet.
func (cr *ContactsResource) Get(ctx context.Context) (*Contacts, error) {
Expand Down
56 changes: 27 additions & 29 deletions v2/device_posture.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,35 @@ const (
PostureIntegrationProviderSentinelOne PostureIntegrationProvider = "sentinelone"
)

type (
// PostureIntegrationProvider identifies a supported posture integration data provider.
PostureIntegrationProvider string

// PostureIntegration is a configured posture integration.
PostureIntegration struct {
ID string `json:"id,omitempty"`
Provider PostureIntegrationProvider `json:"provider,omitempty"`
CloudID string `json:"cloudId,omitempty"`
ClientID string `json:"clientId,omitempty"`
TenantID string `json:"tenantId,omitempty"`
}
// PostureIntegrationProvider identifies a supported posture integration data provider.
type PostureIntegrationProvider string

// PostureIntegration is a configured posture integration.
type PostureIntegration struct {
ID string `json:"id,omitempty"`
Provider PostureIntegrationProvider `json:"provider,omitempty"`
CloudID string `json:"cloudId,omitempty"`
ClientID string `json:"clientId,omitempty"`
TenantID string `json:"tenantId,omitempty"`
}

// CreatePostureIntegrationRequest is a request to create a posture integration.
CreatePostureIntegrationRequest struct {
Provider PostureIntegrationProvider `json:"provider,omitempty"`
CloudID string `json:"cloudId,omitempty"`
ClientID string `json:"clientId,omitempty"`
TenantID string `json:"tenantId,omitempty"`
ClientSecret string `json:"clientSecret,omitempty"`
}
// CreatePostureIntegrationRequest is a request to create a posture integration.
type CreatePostureIntegrationRequest struct {
Provider PostureIntegrationProvider `json:"provider,omitempty"`
CloudID string `json:"cloudId,omitempty"`
ClientID string `json:"clientId,omitempty"`
TenantID string `json:"tenantId,omitempty"`
ClientSecret string `json:"clientSecret,omitempty"`
}

// UpdatePostureIntegrationRequest is a request to update a posture integration.
UpdatePostureIntegrationRequest struct {
CloudID string `json:"cloudId,omitempty"`
ClientID string `json:"clientId,omitempty"`
TenantID string `json:"tenantId,omitempty"`
// ClientSecret may be omitted to preserve the existing value
ClientSecret *string `json:"clientSecret,omitempty"`
}
)
// UpdatePostureIntegrationRequest is a request to update a posture integration.
type UpdatePostureIntegrationRequest struct {
CloudID string `json:"cloudId,omitempty"`
ClientID string `json:"clientId,omitempty"`
TenantID string `json:"tenantId,omitempty"`
// ClientSecret may be omitted to preserve the existing value
ClientSecret *string `json:"clientSecret,omitempty"`
}

// List lists every configured [PostureIntegration].
func (pr *DevicePostureResource) ListIntegrations(ctx context.Context) ([]PostureIntegration, error) {
Expand Down
22 changes: 9 additions & 13 deletions v2/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ type DevicesResource struct {
*Client
}

type (
DeviceRoutes struct {
Advertised []string `json:"advertisedRoutes"`
Enabled []string `json:"enabledRoutes"`
}
)
type DeviceRoutes struct {
Advertised []string `json:"advertisedRoutes"`
Enabled []string `json:"enabledRoutes"`
}

// Time wraps a time and allows for unmarshalling timestamps that represent an empty time as an empty string (e.g "")
// this is used by the tailscale API when it returns devices that have no created date, such as its hello service.
Expand Down Expand Up @@ -127,13 +125,11 @@ func (dr *DevicesResource) SetTags(ctx context.Context, deviceID string, tags []
return dr.do(req, nil)
}

type (
// DeviceKey type represents the properties of the key of an individual device within
// the tailnet.
DeviceKey struct {
KeyExpiryDisabled bool `json:"keyExpiryDisabled"` // Whether or not this device's key will ever expire.
}
)
// DeviceKey type represents the properties of the key of an individual device within
// the tailnet.
type DeviceKey struct {
KeyExpiryDisabled bool `json:"keyExpiryDisabled"` // Whether or not this device's key will ever expire.
}

// SetKey updates the properties of a device's key.
func (dr *DevicesResource) SetKey(ctx context.Context, deviceID string, key DeviceKey) error {
Expand Down
16 changes: 7 additions & 9 deletions v2/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ type DNSResource struct {
*Client
}

type (
// SplitDNSRequest is a map from domain names to a list of nameservers.
SplitDNSRequest map[string][]string
// SplitDNSRequest is a map from domain names to a list of nameservers.
type SplitDNSRequest map[string][]string

// SplitDNSResponse is a map from domain names to a list of nameservers.
SplitDNSResponse SplitDNSRequest
// SplitDNSResponse is a map from domain names to a list of nameservers.
type SplitDNSResponse SplitDNSRequest

DNSPreferences struct {
MagicDNS bool `json:"magicDNS"`
}
)
type DNSPreferences struct {
MagicDNS bool `json:"magicDNS"`
}

// SetSearchPaths replaces the list of search paths with the list supplied by the user and returns an error otherwise.
func (dr *DNSResource) SetSearchPaths(ctx context.Context, searchPaths []string) error {
Expand Down
60 changes: 29 additions & 31 deletions v2/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,37 @@ type KeysResource struct {
*Client
}

type (
// KeyCapabilities describes the capabilities of an authentication key.
KeyCapabilities struct {
Devices struct {
Create struct {
Reusable bool `json:"reusable"`
Ephemeral bool `json:"ephemeral"`
Tags []string `json:"tags"`
Preauthorized bool `json:"preauthorized"`
} `json:"create"`
} `json:"devices"`
}
// KeyCapabilities describes the capabilities of an authentication key.
type KeyCapabilities struct {
Devices struct {
Create struct {
Reusable bool `json:"reusable"`
Ephemeral bool `json:"ephemeral"`
Tags []string `json:"tags"`
Preauthorized bool `json:"preauthorized"`
} `json:"create"`
} `json:"devices"`
}

// CreateKeyRequest describes the definition of an authentication key to create.
CreateKeyRequest struct {
Capabilities KeyCapabilities `json:"capabilities"`
ExpirySeconds int64 `json:"expirySeconds"`
Description string `json:"description"`
}
// CreateKeyRequest describes the definition of an authentication key to create.
type CreateKeyRequest struct {
Capabilities KeyCapabilities `json:"capabilities"`
ExpirySeconds int64 `json:"expirySeconds"`
Description string `json:"description"`
}

// Key describes an authentication key within the tailnet.
Key struct {
ID string `json:"id"`
Key string `json:"key"`
Description string `json:"description"`
Created time.Time `json:"created"`
Expires time.Time `json:"expires"`
Revoked time.Time `json:"revoked"`
Invalid bool `json:"invalid"`
Capabilities KeyCapabilities `json:"capabilities"`
UserID string `json:"userId"`
}
)
// Key describes an authentication key within the tailnet.
type Key struct {
ID string `json:"id"`
Key string `json:"key"`
Description string `json:"description"`
Created time.Time `json:"created"`
Expires time.Time `json:"expires"`
Revoked time.Time `json:"revoked"`
Invalid bool `json:"invalid"`
Capabilities KeyCapabilities `json:"capabilities"`
UserID string `json:"userId"`
}

// Create creates a new authentication key. Returns the generated [Key] if successful.
func (kr *KeysResource) Create(ctx context.Context, ckr CreateKeyRequest) (*Key, error) {
Expand Down
38 changes: 18 additions & 20 deletions v2/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,27 @@ const (
LogTypeNetwork LogType = "network"
)

type (
// LogstreamConfiguration type defines a log stream entity in tailscale.
LogstreamConfiguration struct {
LogType LogType `json:"logType,omitempty"`
DestinationType LogstreamEndpointType `json:"destinationType,omitempty"`
URL string `json:"url,omitempty"`
User string `json:"user,omitempty"`
}
// LogstreamConfiguration type defines a log stream entity in tailscale.
type LogstreamConfiguration struct {
LogType LogType `json:"logType,omitempty"`
DestinationType LogstreamEndpointType `json:"destinationType,omitempty"`
URL string `json:"url,omitempty"`
User string `json:"user,omitempty"`
}

// SetLogstreamConfigurationRequest type defines a request for setting a LogstreamConfiguration.
SetLogstreamConfigurationRequest struct {
DestinationType LogstreamEndpointType `json:"destinationType,omitempty"`
URL string `json:"url,omitempty"`
User string `json:"user,omitempty"`
Token string `json:"token,omitempty"`
}
// SetLogstreamConfigurationRequest type defines a request for setting a LogstreamConfiguration.
type SetLogstreamConfigurationRequest struct {
DestinationType LogstreamEndpointType `json:"destinationType,omitempty"`
URL string `json:"url,omitempty"`
User string `json:"user,omitempty"`
Token string `json:"token,omitempty"`
}

// LogstreamEndpointType describes the type of the endpoint.
LogstreamEndpointType string
// LogstreamEndpointType describes the type of the endpoint.
type LogstreamEndpointType string

// LogType describes the type of logging.
LogType string
)
// LogType describes the type of logging.
type LogType string

// LogstreamConfiguration retrieves the tailnet's [LogstreamConfiguration] for the given [LogType].
func (lr *LoggingResource) LogstreamConfiguration(ctx context.Context, logType LogType) (*LogstreamConfiguration, error) {
Expand Down
Loading

0 comments on commit bdb25c2

Please sign in to comment.