ditch stuff not in greenhost api

This commit is contained in:
Kiara Grouwstra 2022-08-14 22:48:26 +02:00
parent 63c3cb3677
commit ca83da38ea
48 changed files with 0 additions and 20075 deletions

View File

@ -1,81 +0,0 @@
package godo
import (
"context"
"fmt"
"net/http"
)
const oneClickBasePath = "v2/1-clicks"
// OneClickService is an interface for interacting with 1-clicks with the
// DigitalOcean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/1-Click-Applications
type OneClickService interface {
List(context.Context, string) ([]*OneClick, *Response, error)
InstallKubernetes(context.Context, *InstallKubernetesAppsRequest) (*InstallKubernetesAppsResponse, *Response, error)
}
var _ OneClickService = &OneClickServiceOp{}
// OneClickServiceOp interfaces with 1-click endpoints in the DigitalOcean API.
type OneClickServiceOp struct {
client *Client
}
// OneClick is the structure of a 1-click
type OneClick struct {
Slug string `json:"slug"`
Type string `json:"type"`
}
// OneClicksRoot is the root of the json payload that contains a list of 1-clicks
type OneClicksRoot struct {
List []*OneClick `json:"1_clicks"`
}
// InstallKubernetesAppsRequest represents a request required to install 1-click kubernetes apps
type InstallKubernetesAppsRequest struct {
Slugs []string `json:"addon_slugs"`
ClusterUUID string `json:"cluster_uuid"`
}
// InstallKubernetesAppsResponse is the response of a kubernetes 1-click install request
type InstallKubernetesAppsResponse struct {
Message string `json:"message"`
}
// List returns a list of the available 1-click applications.
func (ocs *OneClickServiceOp) List(ctx context.Context, oneClickType string) ([]*OneClick, *Response, error) {
path := fmt.Sprintf(`%s?type=%s`, oneClickBasePath, oneClickType)
req, err := ocs.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(OneClicksRoot)
resp, err := ocs.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.List, resp, nil
}
// InstallKubernetes installs an addon on a kubernetes cluster
func (ocs *OneClickServiceOp) InstallKubernetes(ctx context.Context, install *InstallKubernetesAppsRequest) (*InstallKubernetesAppsResponse, *Response, error) {
path := fmt.Sprintf(oneClickBasePath + "/kubernetes")
req, err := ocs.client.NewRequest(ctx, http.MethodPost, path, install)
if err != nil {
return nil, nil, err
}
responseMessage := new(InstallKubernetesAppsResponse)
resp, err := ocs.client.Do(ctx, req, responseMessage)
if err != nil {
return nil, resp, err
}
return responseMessage, resp, err
}

View File

@ -1,80 +0,0 @@
package godo
import (
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var testOneClick = &OneClick{
Slug: "test-slug",
Type: "droplet",
}
var testOneClickJSON = `
{
"slug":"test-slug",
"type":"droplet"
}
`
var testMessage = &InstallKubernetesAppsResponse{
Message: "test message",
}
var testMessageJSON = `
{
"message" : "test message"
}
`
var kubernetesPayload = &InstallKubernetesAppsRequest{
ClusterUUID: "123",
Slugs: []string{"slug1", "slug2"},
}
func TestOneClick_List(t *testing.T) {
setup()
defer teardown()
svc := client.OneClick
path := "/v2/1-clicks"
want := []*OneClick{
testOneClick,
}
jsonBlob := `
{
"1_clicks": [
` + testOneClickJSON + `
]
}
`
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jsonBlob)
})
got, _, err := svc.List(ctx, "")
require.NoError(t, err)
assert.Equal(t, want, got)
}
func TestOneClick_InstallKubernetes(t *testing.T) {
setup()
defer teardown()
svc := client.OneClick
path := "/v2/1-clicks/kubernetes"
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
fmt.Fprint(w, testMessageJSON)
})
got, _, err := svc.InstallKubernetes(ctx, kubernetesPayload)
require.NoError(t, err)
assert.Equal(t, testMessage, got)
}

View File

@ -1,68 +0,0 @@
package godo
import (
"context"
"net/http"
)
// AccountService is an interface for interfacing with the Account
// endpoints of the DigitalOcean API
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Account
type AccountService interface {
Get(context.Context) (*Account, *Response, error)
}
// AccountServiceOp handles communication with the Account related methods of
// the DigitalOcean API.
type AccountServiceOp struct {
client *Client
}
var _ AccountService = &AccountServiceOp{}
// Account represents a DigitalOcean Account
type Account struct {
DropletLimit int `json:"droplet_limit,omitempty"`
FloatingIPLimit int `json:"floating_ip_limit,omitempty"`
ReservedIPLimit int `json:"reserved_ip_limit,omitempty"`
VolumeLimit int `json:"volume_limit,omitempty"`
Email string `json:"email,omitempty"`
UUID string `json:"uuid,omitempty"`
EmailVerified bool `json:"email_verified,omitempty"`
Status string `json:"status,omitempty"`
StatusMessage string `json:"status_message,omitempty"`
Team *TeamInfo `json:"team,omitempty"`
}
// TeamInfo contains information about the currently team context.
type TeamInfo struct {
Name string `json:"name,omitempty"`
UUID string `json:"uuid,omitempty"`
}
type accountRoot struct {
Account *Account `json:"account"`
}
func (r Account) String() string {
return Stringify(r)
}
// Get DigitalOcean account info
func (s *AccountServiceOp) Get(ctx context.Context) (*Account, *Response, error) {
path := "v2/account"
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(accountRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Account, resp, err
}

View File

@ -1,135 +0,0 @@
package godo
import (
"fmt"
"net/http"
"reflect"
"testing"
)
func TestAccountGet(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/account", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
response := `
{ "account": {
"droplet_limit": 25,
"floating_ip_limit": 25,
"reserved_ip_limit": 25,
"volume_limit": 22,
"email": "sammy@digitalocean.com",
"uuid": "b6fr89dbf6d9156cace5f3c78dc9851d957381ef",
"email_verified": true
}
}`
fmt.Fprint(w, response)
})
acct, _, err := client.Account.Get(ctx)
if err != nil {
t.Errorf("Account.Get returned error: %v", err)
}
expected := &Account{DropletLimit: 25, FloatingIPLimit: 25, ReservedIPLimit: 25, Email: "sammy@digitalocean.com",
UUID: "b6fr89dbf6d9156cace5f3c78dc9851d957381ef", EmailVerified: true, VolumeLimit: 22}
if !reflect.DeepEqual(acct, expected) {
t.Errorf("Account.Get returned %+v, expected %+v", acct, expected)
}
}
func TestAccountString(t *testing.T) {
acct := &Account{
DropletLimit: 25,
FloatingIPLimit: 25,
ReservedIPLimit: 25,
VolumeLimit: 22,
Email: "sammy@digitalocean.com",
UUID: "b6fr89dbf6d9156cace5f3c78dc9851d957381ef",
EmailVerified: true,
Status: "active",
StatusMessage: "message",
}
stringified := acct.String()
expected := `godo.Account{DropletLimit:25, FloatingIPLimit:25, ReservedIPLimit:25, VolumeLimit:22, Email:"sammy@digitalocean.com", UUID:"b6fr89dbf6d9156cace5f3c78dc9851d957381ef", EmailVerified:true, Status:"active", StatusMessage:"message"}`
if expected != stringified {
t.Errorf("\n got %+v\nexpected %+v", stringified, expected)
}
}
func TestAccountGetWithTeam(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/account", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
response := `
{ "account": {
"droplet_limit": 25,
"floating_ip_limit": 25,
"volume_limit": 22,
"email": "sammy@digitalocean.com",
"uuid": "b6fr89dbf6d9156cace5f3c78dc9851d957381ef",
"email_verified": true,
"team": {
"name": "My Team",
"uuid": "b6fr89dbf6d9156cace5f3c78dc9851d957381ef"
}
}
}`
fmt.Fprint(w, response)
})
acct, _, err := client.Account.Get(ctx)
if err != nil {
t.Errorf("Account.Get returned error: %v", err)
}
expected := &Account{
DropletLimit: 25,
FloatingIPLimit: 25,
Email: "sammy@digitalocean.com",
UUID: "b6fr89dbf6d9156cace5f3c78dc9851d957381ef",
EmailVerified: true,
VolumeLimit: 22,
Team: &TeamInfo{
Name: "My Team",
UUID: "b6fr89dbf6d9156cace5f3c78dc9851d957381ef",
},
}
if !reflect.DeepEqual(acct, expected) {
t.Errorf("Account.Get returned %+v, expected %+v", acct, expected)
}
}
func TestAccountStringWithTeam(t *testing.T) {
acct := &Account{
DropletLimit: 25,
FloatingIPLimit: 25,
ReservedIPLimit: 25,
VolumeLimit: 22,
Email: "sammy@digitalocean.com",
UUID: "b6fr89dbf6d9156cace5f3c78dc9851d957381ef",
EmailVerified: true,
Status: "active",
StatusMessage: "message",
Team: &TeamInfo{
Name: "My Team",
UUID: "b6fr89dbf6d9156cace5f3c78dc9851d957381ef",
},
}
stringified := acct.String()
expected := `godo.Account{DropletLimit:25, FloatingIPLimit:25, ReservedIPLimit:25, VolumeLimit:22, Email:"sammy@digitalocean.com", UUID:"b6fr89dbf6d9156cace5f3c78dc9851d957381ef", EmailVerified:true, Status:"active", StatusMessage:"message", Team:godo.TeamInfo{Name:"My Team", UUID:"b6fr89dbf6d9156cace5f3c78dc9851d957381ef"}}`
if expected != stringified {
t.Errorf("\n got %+v\nexpected %+v", stringified, expected)
}
}

View File

@ -1,955 +0,0 @@
// Code generated by golang.org/x/tools/cmd/bundle. DO NOT EDIT.
// $ bundle -pkg godo -prefix ./dev/dist/godo
package godo
import (
"time"
)
// AppAlert Represents an alert configured for an app or component.
type AppAlert struct {
// The ID of the alert. This will be auto-generated by App Platform once the spec is submitted.
ID string `json:"id,omitempty"`
// Name of the component this alert applies to.
ComponentName string `json:"component_name,omitempty"`
Spec *AppAlertSpec `json:"spec,omitempty"`
// Email destinations for the alert when triggered.
Emails []string `json:"emails,omitempty"`
// Slack webhook destinations for the alert when triggered.
SlackWebhooks []*AppAlertSlackWebhook `json:"slack_webhooks,omitempty"`
Phase AppAlertPhase `json:"phase,omitempty"`
Progress *AppAlertProgress `json:"progress,omitempty"`
}
// AppAlertPhase the model 'AppAlertPhase'
type AppAlertPhase string
// List of AppAlertPhase
const (
AppAlertPhase_Unknown AppAlertPhase = "UNKNOWN"
AppAlertPhase_Pending AppAlertPhase = "PENDING"
AppAlertPhase_Configuring AppAlertPhase = "CONFIGURING"
AppAlertPhase_Active AppAlertPhase = "ACTIVE"
AppAlertPhase_Error AppAlertPhase = "ERROR"
)
// AppAlertProgress struct for AppAlertProgress
type AppAlertProgress struct {
Steps []*AppAlertProgressStep `json:"steps,omitempty"`
}
// AppAlertProgressStep struct for AppAlertProgressStep
type AppAlertProgressStep struct {
Name string `json:"name,omitempty"`
Status AppAlertProgressStepStatus `json:"status,omitempty"`
Steps []*AppAlertProgressStep `json:"steps,omitempty"`
StartedAt time.Time `json:"started_at,omitempty"`
EndedAt time.Time `json:"ended_at,omitempty"`
Reason *AppAlertProgressStepReason `json:"reason,omitempty"`
}
// AppAlertProgressStepReason struct for AppAlertProgressStepReason
type AppAlertProgressStepReason struct {
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
// AppAlertProgressStepStatus the model 'AppAlertProgressStepStatus'
type AppAlertProgressStepStatus string
// List of AppAlertProgressStepStatus
const (
AppAlertProgressStepStatus_Unknown AppAlertProgressStepStatus = "UNKNOWN"
AppAlertProgressStepStatus_Pending AppAlertProgressStepStatus = "PENDING"
AppAlertProgressStepStatus_Running AppAlertProgressStepStatus = "RUNNING"
AppAlertProgressStepStatus_Error AppAlertProgressStepStatus = "ERROR"
AppAlertProgressStepStatus_Success AppAlertProgressStepStatus = "SUCCESS"
)
// AppAlertSlackWebhook Configuration of a Slack alerting destination.
type AppAlertSlackWebhook struct {
// URL for the Slack webhook. The value will be encrypted on the app spec after it is submitted.
URL string `json:"url,omitempty"`
// Name of the Slack channel.
Channel string `json:"channel,omitempty"`
}
// App An application's configuration and status.
type App struct {
ID string `json:"id,omitempty"`
OwnerUUID string `json:"owner_uuid,omitempty"`
Spec *AppSpec `json:"spec"`
LastDeploymentActiveAt time.Time `json:"last_deployment_active_at,omitempty"`
DefaultIngress string `json:"default_ingress,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
ActiveDeployment *Deployment `json:"active_deployment,omitempty"`
InProgressDeployment *Deployment `json:"in_progress_deployment,omitempty"`
LastDeploymentCreatedAt time.Time `json:"last_deployment_created_at,omitempty"`
LiveURL string `json:"live_url,omitempty"`
Region *AppRegion `json:"region,omitempty"`
TierSlug string `json:"tier_slug,omitempty"`
LiveURLBase string `json:"live_url_base,omitempty"`
LiveDomain string `json:"live_domain,omitempty"`
Domains []*AppDomain `json:"domains,omitempty"`
PinnedDeployment *Deployment `json:"pinned_deployment,omitempty"`
}
// AppAlertSpec Configuration of an alert for the app or a individual component.
type AppAlertSpec struct {
Rule AppAlertSpecRule `json:"rule,omitempty"`
// Determines whether or not the alert is disabled.
Disabled bool `json:"disabled,omitempty"`
Operator AppAlertSpecOperator `json:"operator,omitempty"`
// The meaning is dependent upon the rule. It is used in conjunction with the operator and window to determine when an alert should trigger.
Value float32 `json:"value,omitempty"`
Window AppAlertSpecWindow `json:"window,omitempty"`
}
// AppAlertSpecOperator the model 'AppAlertSpecOperator'
type AppAlertSpecOperator string
// List of AppAlertSpecOperator
const (
AppAlertSpecOperator_UnspecifiedOperator AppAlertSpecOperator = "UNSPECIFIED_OPERATOR"
AppAlertSpecOperator_GreaterThan AppAlertSpecOperator = "GREATER_THAN"
AppAlertSpecOperator_LessThan AppAlertSpecOperator = "LESS_THAN"
)
// AppAlertSpecRule - CPU_UTILIZATION: Represents CPU for a given container instance. Only applicable at the component level. - MEM_UTILIZATION: Represents RAM for a given container instance. Only applicable at the component level. - RESTART_COUNT: Represents restart count for a given container instance. Only applicable at the component level. - DEPLOYMENT_FAILED: Represents whether a deployment has failed. Only applicable at the app level. - DEPLOYMENT_LIVE: Represents whether a deployment has succeeded. Only applicable at the app level. - DOMAIN_FAILED: Represents whether a domain configuration has failed. Only applicable at the app level. - DOMAIN_LIVE: Represents whether a domain configuration has succeeded. Only applicable at the app level. - FUNCTIONS_ACTIVATION_COUNT: Represents an activation count for a given functions instance. Only applicable to functions components. - FUNCTIONS_AVERAGE_DURATION_MS: Represents the average duration for function runtimes. Only applicable to functions components. - FUNCTIONS_ERROR_RATE_PER_MINUTE: Represents an error rate per minute for a given functions instance. Only applicable to functions components. - FUNCTIONS_AVERAGE_WAIT_TIME_MS: Represents the average wait time for functions. Only applicable to functions components. - FUNCTIONS_ERROR_COUNT: Represents an error count for a given functions instance. Only applicable to functions components. - FUNCTIONS_GB_RATE_PER_SECOND: Represents the rate of memory consumption (GB x seconds) for functions. Only applicable to functions components.
type AppAlertSpecRule string
// List of AppAlertSpecRule
const (
AppAlertSpecRule_UnspecifiedRule AppAlertSpecRule = "UNSPECIFIED_RULE"
AppAlertSpecRule_CPUUtilization AppAlertSpecRule = "CPU_UTILIZATION"
AppAlertSpecRule_MemUtilization AppAlertSpecRule = "MEM_UTILIZATION"
AppAlertSpecRule_RestartCount AppAlertSpecRule = "RESTART_COUNT"
AppAlertSpecRule_DeploymentFailed AppAlertSpecRule = "DEPLOYMENT_FAILED"
AppAlertSpecRule_DeploymentLive AppAlertSpecRule = "DEPLOYMENT_LIVE"
AppAlertSpecRule_DomainFailed AppAlertSpecRule = "DOMAIN_FAILED"
AppAlertSpecRule_DomainLive AppAlertSpecRule = "DOMAIN_LIVE"
AppAlertSpecRule_FunctionsActivationCount AppAlertSpecRule = "FUNCTIONS_ACTIVATION_COUNT"
AppAlertSpecRule_FunctionsAverageDurationMS AppAlertSpecRule = "FUNCTIONS_AVERAGE_DURATION_MS"
AppAlertSpecRule_FunctionsErrorRatePerMinute AppAlertSpecRule = "FUNCTIONS_ERROR_RATE_PER_MINUTE"
AppAlertSpecRule_FunctionsAverageWaitTimeMs AppAlertSpecRule = "FUNCTIONS_AVERAGE_WAIT_TIME_MS"
AppAlertSpecRule_FunctionsErrorCount AppAlertSpecRule = "FUNCTIONS_ERROR_COUNT"
AppAlertSpecRule_FunctionsGBRatePerSecond AppAlertSpecRule = "FUNCTIONS_GB_RATE_PER_SECOND"
)
// AppAlertSpecWindow the model 'AppAlertSpecWindow'
type AppAlertSpecWindow string
// List of AppAlertSpecWindow
const (
AppAlertSpecWindow_UnspecifiedWindow AppAlertSpecWindow = "UNSPECIFIED_WINDOW"
AppAlertSpecWindow_FiveMinutes AppAlertSpecWindow = "FIVE_MINUTES"
AppAlertSpecWindow_TenMinutes AppAlertSpecWindow = "TEN_MINUTES"
AppAlertSpecWindow_ThirtyMinutes AppAlertSpecWindow = "THIRTY_MINUTES"
AppAlertSpecWindow_OneHour AppAlertSpecWindow = "ONE_HOUR"
)
// AppDatabaseSpec struct for AppDatabaseSpec
type AppDatabaseSpec struct {
// The name. Must be unique across all components within the same app.
Name string `json:"name"`
Engine AppDatabaseSpecEngine `json:"engine,omitempty"`
Version string `json:"version,omitempty"`
// Deprecated.
Size string `json:"size,omitempty"`
// Deprecated.
NumNodes int64 `json:"num_nodes,omitempty"`
// Whether this is a production or dev database.
Production bool `json:"production,omitempty"`
// The name of the underlying DigitalOcean DBaaS cluster. This is required for production databases. For dev databases, if cluster_name is not set, a new cluster will be provisioned.
ClusterName string `json:"cluster_name,omitempty"`
// The name of the MySQL or PostgreSQL database to configure.
DBName string `json:"db_name,omitempty"`
// The name of the MySQL or PostgreSQL user to configure.
DBUser string `json:"db_user,omitempty"`
}
// AppDatabaseSpecEngine the model 'AppDatabaseSpecEngine'
type AppDatabaseSpecEngine string
// List of AppDatabaseSpecEngine
const (
AppDatabaseSpecEngine_Unset AppDatabaseSpecEngine = "UNSET"
AppDatabaseSpecEngine_MySQL AppDatabaseSpecEngine = "MYSQL"
AppDatabaseSpecEngine_PG AppDatabaseSpecEngine = "PG"
AppDatabaseSpecEngine_Redis AppDatabaseSpecEngine = "REDIS"
AppDatabaseSpecEngine_MongoDB AppDatabaseSpecEngine = "MONGODB"
)
// AppDomainSpec struct for AppDomainSpec
type AppDomainSpec struct {
Domain string `json:"domain"`
Type AppDomainSpecType `json:"type,omitempty"`
Wildcard bool `json:"wildcard,omitempty"`
// Optional. If the domain uses DigitalOcean DNS and you would like App Platform to automatically manage it for you, set this to the name of the domain on your account. For example, If the domain you are adding is `app.domain.com`, the zone could be `domain.com`.
Zone string `json:"zone,omitempty"`
Certificate string `json:"certificate,omitempty"`
// Optional. The minimum version of TLS a client application can use to access resources for the domain. Must be one of the following values wrapped within quotations: `\"1.2\"` or `\"1.3\"`.
MinimumTLSVersion string `json:"minimum_tls_version,omitempty"`
}
// AppDomainSpecType the model 'AppDomainSpecType'
type AppDomainSpecType string
// List of AppDomainSpecType
const (
AppDomainSpecType_Unspecified AppDomainSpecType = "UNSPECIFIED"
AppDomainSpecType_Default AppDomainSpecType = "DEFAULT"
AppDomainSpecType_Primary AppDomainSpecType = "PRIMARY"
AppDomainSpecType_Alias AppDomainSpecType = "ALIAS"
)
// AppFunctionsSpec struct for AppFunctionsSpec
type AppFunctionsSpec struct {
// The name. Must be unique across all components within the same app.
Name string `json:"name"`
Git *GitSourceSpec `json:"git,omitempty"`
GitHub *GitHubSourceSpec `json:"github,omitempty"`
GitLab *GitLabSourceSpec `json:"gitlab,omitempty"`
// An optional path to the working directory to use for the build. Must be relative to the root of the repo.
SourceDir string `json:"source_dir,omitempty"`
// A list of environment variables made available to the component.
Envs []*AppVariableDefinition `json:"envs,omitempty"`
// A list of HTTP routes that should be routed to this component.
Routes []*AppRouteSpec `json:"routes,omitempty"`
// A list of configured alerts the user has enabled.
Alerts []*AppAlertSpec `json:"alerts,omitempty"`
// A list of configured log forwarding destinations.
LogDestinations []*AppLogDestinationSpec `json:"log_destinations,omitempty"`
CORS *AppCORSPolicy `json:"cors,omitempty"`
}
// AppIngressSpec struct for AppIngressSpec
type AppIngressSpec struct {
LoadBalancer AppIngressSpecLoadBalancer `json:"load_balancer,omitempty"`
LoadBalancerSize int64 `json:"load_balancer_size,omitempty"`
}
// AppIngressSpecLoadBalancer the model 'AppIngressSpecLoadBalancer'
type AppIngressSpecLoadBalancer string
// List of AppIngressSpecLoadBalancer
const (
AppIngressSpecLoadBalancer_Unknown AppIngressSpecLoadBalancer = "UNKNOWN"
AppIngressSpecLoadBalancer_DigitalOcean AppIngressSpecLoadBalancer = "DIGITALOCEAN"
)
// AppJobSpec struct for AppJobSpec
type AppJobSpec struct {
// The name. Must be unique across all components within the same app.
Name string `json:"name"`
Git *GitSourceSpec `json:"git,omitempty"`
GitHub *GitHubSourceSpec `json:"github,omitempty"`
Image *ImageSourceSpec `json:"image,omitempty"`
GitLab *GitLabSourceSpec `json:"gitlab,omitempty"`
// The path to the Dockerfile relative to the root of the repo. If set, it will be used to build this component. Otherwise, App Platform will attempt to build it using buildpacks.
DockerfilePath string `json:"dockerfile_path,omitempty"`
// An optional build command to run while building this component from source.
BuildCommand string `json:"build_command,omitempty"`
// An optional run command to override the component's default.
RunCommand string `json:"run_command,omitempty"`
// An optional path to the working directory to use for the build. For Dockerfile builds, this will be used as the build context. Must be relative to the root of the repo.
SourceDir string `json:"source_dir,omitempty"`
// An environment slug describing the type of this app. For a full list, please refer to [the product documentation](https://www.digitalocean.com/docs/app-platform/).
EnvironmentSlug string `json:"environment_slug,omitempty"`
// A list of environment variables made available to the component.
Envs []*AppVariableDefinition `json:"envs,omitempty"`
// The instance size to use for this component.
InstanceSizeSlug string `json:"instance_size_slug,omitempty"`
InstanceCount int64 `json:"instance_count,omitempty"`
Kind AppJobSpecKind `json:"kind,omitempty"`
// A list of configured alerts which apply to the component.
Alerts []*AppAlertSpec `json:"alerts,omitempty"`
// A list of configured log forwarding destinations.
LogDestinations []*AppLogDestinationSpec `json:"log_destinations,omitempty"`
}
// AppJobSpecKind - UNSPECIFIED: Default job type, will auto-complete to POST_DEPLOY kind. - PRE_DEPLOY: Indicates a job that runs before an app deployment. - POST_DEPLOY: Indicates a job that runs after an app deployment. - FAILED_DEPLOY: Indicates a job that runs after a component fails to deploy.
type AppJobSpecKind string
// List of AppJobSpecKind
const (
AppJobSpecKind_Unspecified AppJobSpecKind = "UNSPECIFIED"
AppJobSpecKind_PreDeploy AppJobSpecKind = "PRE_DEPLOY"
AppJobSpecKind_PostDeploy AppJobSpecKind = "POST_DEPLOY"
AppJobSpecKind_FailedDeploy AppJobSpecKind = "FAILED_DEPLOY"
)
// AppLogDestinationSpec struct for AppLogDestinationSpec
type AppLogDestinationSpec struct {
Name string `json:"name"`
Papertrail *AppLogDestinationSpecPapertrail `json:"papertrail,omitempty"`
Datadog *AppLogDestinationSpecDataDog `json:"datadog,omitempty"`
Logtail *AppLogDestinationSpecLogtail `json:"logtail,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
TLSInsecure bool `json:"tls_insecure,omitempty"`
Headers []*AppLogDestinationSpecHeader `json:"headers,omitempty"`
}
// AppLogDestinationSpecDataDog DataDog configuration.
type AppLogDestinationSpecDataDog struct {
// Datadog HTTP log intake endpoint.
Endpoint string `json:"endpoint,omitempty"`
// Datadog API key.
ApiKey string `json:"api_key"`
}
// AppLogDestinationSpecHeader struct for AppLogDestinationSpecHeader
type AppLogDestinationSpecHeader struct {
// The name
Key string `json:"key"`
// The header value.
Value string `json:"value,omitempty"`
}
// AppLogDestinationSpecLogtail Logtail configuration.
type AppLogDestinationSpecLogtail struct {
// Logtail token.
Token string `json:"token"`
}
// AppLogDestinationSpecPapertrail Papertrail configuration.
type AppLogDestinationSpecPapertrail struct {
// Papertrail syslog endpoint.
Endpoint string `json:"endpoint"`
}
// AppRouteSpec struct for AppRouteSpec
type AppRouteSpec struct {
// An HTTP path prefix. Paths must start with / and must be unique across all components within an app.
Path string `json:"path,omitempty"`
// An optional flag to preserve the path that is forwarded to the backend service. By default, the HTTP request path will be trimmed from the left when forwarded to the component. For example, a component with `path=/api` will have requests to `/api/list` trimmed to `/list`. If this value is `true`, the path will remain `/api/list`. Note: this is not applicable for Functions Components.
PreservePathPrefix bool `json:"preserve_path_prefix,omitempty"`
}
// AppServiceSpec struct for AppServiceSpec
type AppServiceSpec struct {
// The name. Must be unique across all components within the same app.
Name string `json:"name"`
Git *GitSourceSpec `json:"git,omitempty"`
GitHub *GitHubSourceSpec `json:"github,omitempty"`
Image *ImageSourceSpec `json:"image,omitempty"`
GitLab *GitLabSourceSpec `json:"gitlab,omitempty"`
// The path to the Dockerfile relative to the root of the repo. If set, it will be used to build this component. Otherwise, App Platform will attempt to build it using buildpacks.
DockerfilePath string `json:"dockerfile_path,omitempty"`
// An optional build command to run while building this component from source.
BuildCommand string `json:"build_command,omitempty"`
// An optional run command to override the component's default.
RunCommand string `json:"run_command,omitempty"`
// An optional path to the working directory to use for the build. For Dockerfile builds, this will be used as the build context. Must be relative to the root of the repo.
SourceDir string `json:"source_dir,omitempty"`
// An environment slug describing the type of this app. For a full list, please refer to [the product documentation](https://www.digitalocean.com/docs/app-platform/).
EnvironmentSlug string `json:"environment_slug,omitempty"`
// A list of environment variables made available to the component.
Envs []*AppVariableDefinition `json:"envs,omitempty"`
InstanceSizeSlug string `json:"instance_size_slug,omitempty"`
InstanceCount int64 `json:"instance_count,omitempty"`
// The internal port on which this service's run command will listen. Default: 8080 If there is not an environment variable with the name `PORT`, one will be automatically added with its value set to the value of this field.
HTTPPort int64 `json:"http_port,omitempty"`
// A list of HTTP routes that should be routed to this component.
Routes []*AppRouteSpec `json:"routes,omitempty"`
HealthCheck *AppServiceSpecHealthCheck `json:"health_check,omitempty"`
CORS *AppCORSPolicy `json:"cors,omitempty"`
// The ports on which this service will listen for internal traffic.
InternalPorts []int64 `json:"internal_ports,omitempty"`
// A list of configured alerts which apply to the component.
Alerts []*AppAlertSpec `json:"alerts,omitempty"`
// A list of configured log forwarding destinations.
LogDestinations []*AppLogDestinationSpec `json:"log_destinations,omitempty"`
}
// AppServiceSpecHealthCheck struct for AppServiceSpecHealthCheck
type AppServiceSpecHealthCheck struct {
// Deprecated. Use http_path instead.
Path string `json:"path,omitempty"`
// The number of seconds to wait before beginning health checks.
InitialDelaySeconds int32 `json:"initial_delay_seconds,omitempty"`
// The number of seconds to wait between health checks.
PeriodSeconds int32 `json:"period_seconds,omitempty"`
// The number of seconds after which the check times out.
TimeoutSeconds int32 `json:"timeout_seconds,omitempty"`
// The number of successful health checks before considered healthy.
SuccessThreshold int32 `json:"success_threshold,omitempty"`
// The number of failed health checks before considered unhealthy.
FailureThreshold int32 `json:"failure_threshold,omitempty"`
// The route path used for the HTTP health check ping. If not set, the HTTP health check will be disabled and a TCP health check used instead.
HTTPPath string `json:"http_path,omitempty"`
// The port on which the health check will be performed. If not set, the health check will be performed on the component's http_port.
Port int64 `json:"port,omitempty"`
}
// AppSpec The desired configuration of an application.
type AppSpec struct {
// The name of the app. Must be unique across all apps in the same account.
Name string `json:"name"`
// Workloads which expose publicly-accessible HTTP services.
Services []*AppServiceSpec `json:"services,omitempty"`
// Content which can be rendered to static web assets.
StaticSites []*AppStaticSiteSpec `json:"static_sites,omitempty"`
// Workloads which do not expose publicly-accessible HTTP services.
Workers []*AppWorkerSpec `json:"workers,omitempty"`
// Pre and post deployment workloads which do not expose publicly-accessible HTTP routes.
Jobs []*AppJobSpec `json:"jobs,omitempty"`
// Workloads which expose publicly-accessible HTTP services via Functions Components.
Functions []*AppFunctionsSpec `json:"functions,omitempty"`
// Database instances which can provide persistence to workloads within the application.
Databases []*AppDatabaseSpec `json:"databases,omitempty"`
// A set of hostnames where the application will be available.
Domains []*AppDomainSpec `json:"domains,omitempty"`
Region string `json:"region,omitempty"`
// A list of environment variables made available to all components in the app.
Envs []*AppVariableDefinition `json:"envs,omitempty"`
// A list of alerts which apply to the app.
Alerts []*AppAlertSpec `json:"alerts,omitempty"`
Ingress *AppIngressSpec `json:"ingress,omitempty"`
Features []string `json:"features,omitempty"`
}
// AppStaticSiteSpec struct for AppStaticSiteSpec
type AppStaticSiteSpec struct {
// The name. Must be unique across all components within the same app.
Name string `json:"name"`
Git *GitSourceSpec `json:"git,omitempty"`
GitHub *GitHubSourceSpec `json:"github,omitempty"`
GitLab *GitLabSourceSpec `json:"gitlab,omitempty"`
// The path to the Dockerfile relative to the root of the repo. If set, it will be used to build this component. Otherwise, App Platform will attempt to build it using buildpacks.
DockerfilePath string `json:"dockerfile_path,omitempty"`
// An optional build command to run while building this component from source.
BuildCommand string `json:"build_command,omitempty"`
// An optional path to the working directory to use for the build. For Dockerfile builds, this will be used as the build context. Must be relative to the root of the repo.
SourceDir string `json:"source_dir,omitempty"`
// An environment slug describing the type of this app. For a full list, please refer to [the product documentation](https://www.digitalocean.com/docs/app-platform/).
EnvironmentSlug string `json:"environment_slug,omitempty"`
// An optional path to where the built assets will be located, relative to the build context. If not set, App Platform will automatically scan for these directory names: `_static`, `dist`, `public`, `build`.
OutputDir string `json:"output_dir,omitempty"`
IndexDocument string `json:"index_document,omitempty"`
// The name of the error document to use when serving this static site. Default: 404.html. If no such file exists within the built assets, App Platform will supply one.
ErrorDocument string `json:"error_document,omitempty"`
// A list of environment variables made available to the component.
Envs []*AppVariableDefinition `json:"envs,omitempty"`
// A list of HTTP routes that should be routed to this component.
Routes []*AppRouteSpec `json:"routes,omitempty"`
CORS *AppCORSPolicy `json:"cors,omitempty"`
// The name of the document to use as the fallback for any requests to documents that are not found when serving this static site. Only 1 of `catchall_document` or `error_document` can be set.
CatchallDocument string `json:"catchall_document,omitempty"`
}
// AppVariableDefinition struct for AppVariableDefinition
type AppVariableDefinition struct {
// The name
Key string `json:"key"`
// The value. If the type is `SECRET`, the value will be encrypted on first submission. On following submissions, the encrypted value should be used.
Value string `json:"value,omitempty"`
Scope AppVariableScope `json:"scope,omitempty"`
Type AppVariableType `json:"type,omitempty"`
}
// AppWorkerSpec struct for AppWorkerSpec
type AppWorkerSpec struct {
// The name. Must be unique across all components within the same app.
Name string `json:"name"`
Git *GitSourceSpec `json:"git,omitempty"`
GitHub *GitHubSourceSpec `json:"github,omitempty"`
Image *ImageSourceSpec `json:"image,omitempty"`
GitLab *GitLabSourceSpec `json:"gitlab,omitempty"`
// The path to the Dockerfile relative to the root of the repo. If set, it will be used to build this component. Otherwise, App Platform will attempt to build it using buildpacks.
DockerfilePath string `json:"dockerfile_path,omitempty"`
// An optional build command to run while building this component from source.
BuildCommand string `json:"build_command,omitempty"`
// An optional run command to override the component's default.
RunCommand string `json:"run_command,omitempty"`
// An optional path to the working directory to use for the build. For Dockerfile builds, this will be used as the build context. Must be relative to the root of the repo.
SourceDir string `json:"source_dir,omitempty"`
// An environment slug describing the type of this app. For a full list, please refer to [the product documentation](https://www.digitalocean.com/docs/app-platform/).
EnvironmentSlug string `json:"environment_slug,omitempty"`
// A list of environment variables made available to the component.
Envs []*AppVariableDefinition `json:"envs,omitempty"`
// The instance size to use for this component.
InstanceSizeSlug string `json:"instance_size_slug,omitempty"`
InstanceCount int64 `json:"instance_count,omitempty"`
// A list of configured alerts which apply to the component.
Alerts []*AppAlertSpec `json:"alerts,omitempty"`
// A list of configured log forwarding destinations.
LogDestinations []*AppLogDestinationSpec `json:"log_destinations,omitempty"`
}
// DeploymentCauseDetailsDigitalOceanUser struct for DeploymentCauseDetailsDigitalOceanUser
type DeploymentCauseDetailsDigitalOceanUser struct {
UUID string `json:"uuid,omitempty"`
Email string `json:"email,omitempty"`
FullName string `json:"full_name,omitempty"`
}
// DeploymentCauseDetailsDigitalOceanUserAction struct for DeploymentCauseDetailsDigitalOceanUserAction
type DeploymentCauseDetailsDigitalOceanUserAction struct {
User *DeploymentCauseDetailsDigitalOceanUser `json:"user,omitempty"`
Name DeploymentCauseDetailsDigitalOceanUserActionName `json:"name,omitempty"`
}
// DeploymentCauseDetailsGitPush struct for DeploymentCauseDetailsGitPush
type DeploymentCauseDetailsGitPush struct {
GitHub *GitHubSourceSpec `json:"github,omitempty"`
GitLab *GitLabSourceSpec `json:"gitlab,omitempty"`
Username string `json:"username,omitempty"`
CommitAuthor string `json:"commit_author,omitempty"`
CommitSHA string `json:"commit_sha,omitempty"`
CommitMessage string `json:"commit_message,omitempty"`
}
// AppCORSPolicy struct for AppCORSPolicy
type AppCORSPolicy struct {
// The set of allowed CORS origins. This configures the Access-Control-Allow-Origin header.
AllowOrigins []*AppStringMatch `json:"allow_origins,omitempty"`
// The set of allowed HTTP methods. This configures the Access-Control-Allow-Methods header.
AllowMethods []string `json:"allow_methods,omitempty"`
// The set of allowed HTTP request headers. This configures the Access-Control-Allow-Headers header.
AllowHeaders []string `json:"allow_headers,omitempty"`
// The set of HTTP response headers that browsers are allowed to access. This configures the Access-Control-Expose-Headers header.
ExposeHeaders []string `json:"expose_headers,omitempty"`
// An optional duration specifying how long browsers can cache the results of a preflight request. This configures the Access-Control-Max-Age header. Example: `5h30m`.
MaxAge string `json:"max_age,omitempty"`
// Whether browsers should expose the response to the client-side JavaScript code when the request's credentials mode is `include`. This configures the Access-Control-Allow-Credentials header.
AllowCredentials bool `json:"allow_credentials,omitempty"`
}
// AppCreateRequest struct for AppCreateRequest
type AppCreateRequest struct {
Spec *AppSpec `json:"spec"`
}
// DeployTemplate struct for DeployTemplate
type DeployTemplate struct {
Spec *AppSpec `json:"spec,omitempty"`
}
// Deployment struct for Deployment
type Deployment struct {
ID string `json:"id,omitempty"`
Spec *AppSpec `json:"spec,omitempty"`
Services []*DeploymentService `json:"services,omitempty"`
StaticSites []*DeploymentStaticSite `json:"static_sites,omitempty"`
Workers []*DeploymentWorker `json:"workers,omitempty"`
Jobs []*DeploymentJob `json:"jobs,omitempty"`
Functions []*DeploymentFunctions `json:"functions,omitempty"`
PhaseLastUpdatedAt time.Time `json:"phase_last_updated_at,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
Cause string `json:"cause,omitempty"`
ClonedFrom string `json:"cloned_from,omitempty"`
Progress *DeploymentProgress `json:"progress,omitempty"`
Phase DeploymentPhase `json:"phase,omitempty"`
TierSlug string `json:"tier_slug,omitempty"`
PreviousDeploymentID string `json:"previous_deployment_id,omitempty"`
CauseDetails *DeploymentCauseDetails `json:"cause_details,omitempty"`
LoadBalancerID string `json:"load_balancer_id,omitempty"`
}
// DeploymentCauseDetails struct for DeploymentCauseDetails
type DeploymentCauseDetails struct {
DigitalOceanUserAction *DeploymentCauseDetailsDigitalOceanUserAction `json:"digitalocean_user_action,omitempty"`
GitPush *DeploymentCauseDetailsGitPush `json:"git_push,omitempty"`
Internal bool `json:"internal,omitempty"`
Type DeploymentCauseDetailsType `json:"type,omitempty"`
}
// DeploymentCauseDetailsType - MANUAL: A deployment that was manually created - DEPLOY_ON_PUSH: A deployment that was automatically created by a Deploy on Push hook - MAINTENANCE: A deployment created for App Platform maintenance - MANUAL_ROLLBACK: A rollback deployment that was manually created - AUTO_ROLLBACK: An automatic rollback deployment created as a result of a previous deployment failing - UPDATE_DATABASE_TRUSTED_SOURCES: A deployment that was created due to an update in database trusted sources.
type DeploymentCauseDetailsType string
// List of DeploymentCauseDetailsType
const (
DeploymentCauseDetailsType_Unknown DeploymentCauseDetailsType = "UNKNOWN"
DeploymentCauseDetailsType_Manual DeploymentCauseDetailsType = "MANUAL"
DeploymentCauseDetailsType_DeployOnPush DeploymentCauseDetailsType = "DEPLOY_ON_PUSH"
DeploymentCauseDetailsType_Maintenance DeploymentCauseDetailsType = "MAINTENANCE"
DeploymentCauseDetailsType_ManualRollback DeploymentCauseDetailsType = "MANUAL_ROLLBACK"
DeploymentCauseDetailsType_AutoRollback DeploymentCauseDetailsType = "AUTO_ROLLBACK"
DeploymentCauseDetailsType_UpdateDatabaseTrustedSources DeploymentCauseDetailsType = "UPDATE_DATABASE_TRUSTED_SOURCES"
)
// DeploymentFunctions struct for DeploymentFunctions
type DeploymentFunctions struct {
Name string `json:"name,omitempty"`
// The commit hash of the repository that was used to build this functions component.
SourceCommitHash string `json:"source_commit_hash,omitempty"`
// The namespace where the functions are deployed.
Namespace string `json:"namespace,omitempty"`
}
// DeploymentJob struct for DeploymentJob
type DeploymentJob struct {
Name string `json:"name,omitempty"`
SourceCommitHash string `json:"source_commit_hash,omitempty"`
}
// DeploymentPhase the model 'DeploymentPhase'
type DeploymentPhase string
// List of DeploymentPhase
const (
DeploymentPhase_Unknown DeploymentPhase = "UNKNOWN"
DeploymentPhase_PendingBuild DeploymentPhase = "PENDING_BUILD"
DeploymentPhase_Building DeploymentPhase = "BUILDING"
DeploymentPhase_PendingDeploy DeploymentPhase = "PENDING_DEPLOY"
DeploymentPhase_Deploying DeploymentPhase = "DEPLOYING"
DeploymentPhase_Active DeploymentPhase = "ACTIVE"
DeploymentPhase_Superseded DeploymentPhase = "SUPERSEDED"
DeploymentPhase_Error DeploymentPhase = "ERROR"
DeploymentPhase_Canceled DeploymentPhase = "CANCELED"
)
// DeploymentProgress struct for DeploymentProgress
type DeploymentProgress struct {
PendingSteps int32 `json:"pending_steps,omitempty"`
RunningSteps int32 `json:"running_steps,omitempty"`
SuccessSteps int32 `json:"success_steps,omitempty"`
ErrorSteps int32 `json:"error_steps,omitempty"`
TotalSteps int32 `json:"total_steps,omitempty"`
Steps []*DeploymentProgressStep `json:"steps,omitempty"`
SummarySteps []*DeploymentProgressStep `json:"summary_steps,omitempty"`
}
// DeploymentProgressStep struct for DeploymentProgressStep
type DeploymentProgressStep struct {
Name string `json:"name,omitempty"`
Status DeploymentProgressStepStatus `json:"status,omitempty"`
Steps []*DeploymentProgressStep `json:"steps,omitempty"`
StartedAt time.Time `json:"started_at,omitempty"`
EndedAt time.Time `json:"ended_at,omitempty"`
Reason *DeploymentProgressStepReason `json:"reason,omitempty"`
ComponentName string `json:"component_name,omitempty"`
// The base of a human-readable description of the step intended to be combined with the component name for presentation. For example: `message_base` = \"Building service\" `component_name` = \"api\"
MessageBase string `json:"message_base,omitempty"`
}
// DeploymentProgressStepReason struct for DeploymentProgressStepReason
type DeploymentProgressStepReason struct {
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
// DeploymentProgressStepStatus the model 'DeploymentProgressStepStatus'
type DeploymentProgressStepStatus string
// List of DeploymentProgressStepStatus
const (
DeploymentProgressStepStatus_Unknown DeploymentProgressStepStatus = "UNKNOWN"
DeploymentProgressStepStatus_Pending DeploymentProgressStepStatus = "PENDING"
DeploymentProgressStepStatus_Running DeploymentProgressStepStatus = "RUNNING"
DeploymentProgressStepStatus_Error DeploymentProgressStepStatus = "ERROR"
DeploymentProgressStepStatus_Success DeploymentProgressStepStatus = "SUCCESS"
)
// DeploymentService struct for DeploymentService
type DeploymentService struct {
Name string `json:"name,omitempty"`
SourceCommitHash string `json:"source_commit_hash,omitempty"`
}
// DeploymentStaticSite struct for DeploymentStaticSite
type DeploymentStaticSite struct {
Name string `json:"name,omitempty"`
SourceCommitHash string `json:"source_commit_hash,omitempty"`
}
// DeploymentWorker struct for DeploymentWorker
type DeploymentWorker struct {
Name string `json:"name,omitempty"`
SourceCommitHash string `json:"source_commit_hash,omitempty"`
}
// DetectRequest struct for DetectRequest
type DetectRequest struct {
Git *GitSourceSpec `json:"git,omitempty"`
GitHub *GitHubSourceSpec `json:"github,omitempty"`
GitLab *GitLabSourceSpec `json:"gitlab,omitempty"`
// An optional commit hash to use instead of the branch specified in the source spec.
CommitSHA string `json:"commit_sha,omitempty"`
// An optional path to the working directory for the detection process.
SourceDir string `json:"source_dir,omitempty"`
}
// DetectResponse struct for DetectResponse
type DetectResponse struct {
Components []*DetectResponseComponent `json:"components,omitempty"`
Template *DeployTemplate `json:"template,omitempty"`
TemplateFound bool `json:"template_found,omitempty"`
TemplateValid bool `json:"template_valid,omitempty"`
TemplateError string `json:"template_error,omitempty"`
}
// DetectResponseComponent struct for DetectResponseComponent
type DetectResponseComponent struct {
Strategy DetectResponseType `json:"strategy,omitempty"`
Types []string `json:"types,omitempty"`
// A list of Dockerfiles that were found for this component. The recommendation is to use the first Dockerfile.
Dockerfiles []string `json:"dockerfiles,omitempty"`
BuildCommand string `json:"build_command,omitempty"`
RunCommand string `json:"run_command,omitempty"`
EnvironmentSlug string `json:"environment_slug,omitempty"`
// A list of HTTP ports that this component may listen on. The recommendation is to use the last port in the list.
HTTPPorts []int64 `json:"http_ports,omitempty"`
EnvVars []*AppVariableDefinition `json:"env_vars,omitempty"`
ServerlessPackages []*DetectResponseServerlessPackage `json:"serverless_packages,omitempty"`
SourceDir string `json:"source_dir,omitempty"`
}
// DetectResponseServerlessFunction struct for DetectResponseServerlessFunction
type DetectResponseServerlessFunction struct {
Name string `json:"name,omitempty"`
Package string `json:"package,omitempty"`
Runtime string `json:"runtime,omitempty"`
Limits *DetectResponseServerlessFunctionLimits `json:"limits,omitempty"`
}
// DetectResponseServerlessFunctionLimits struct for DetectResponseServerlessFunctionLimits
type DetectResponseServerlessFunctionLimits struct {
Timeout string `json:"timeout,omitempty"`
Memory string `json:"memory,omitempty"`
Logs string `json:"logs,omitempty"`
}
// DetectResponseServerlessPackage struct for DetectResponseServerlessPackage
type DetectResponseServerlessPackage struct {
Name string `json:"name,omitempty"`
Functions []*DetectResponseServerlessFunction `json:"functions,omitempty"`
}
// DetectResponseType the model 'DetectResponseType'
type DetectResponseType string
// List of DetectResponseType
const (
DetectResponseType_Unspecified DetectResponseType = "UNSPECIFIED"
DetectResponseType_Dockerfile DetectResponseType = "DOCKERFILE"
DetectResponseType_Buildpack DetectResponseType = "BUILDPACK"
DetectResponseType_HTML DetectResponseType = "HTML"
DetectResponseType_Serverless DetectResponseType = "SERVERLESS"
)
// DeploymentCauseDetailsDigitalOceanUserActionName the model 'CauseDetailsDigitalOceanUserActionName'
type DeploymentCauseDetailsDigitalOceanUserActionName string
// List of DeploymentCauseDetailsDigitalOceanUserActionName
const (
DeploymentCauseDetailsDigitalOceanUserActionName_Unknown DeploymentCauseDetailsDigitalOceanUserActionName = "UNKNOWN"
DeploymentCauseDetailsDigitalOceanUserActionName_CreateDeployment DeploymentCauseDetailsDigitalOceanUserActionName = "CREATE_DEPLOYMENT"
DeploymentCauseDetailsDigitalOceanUserActionName_UpdateSpec DeploymentCauseDetailsDigitalOceanUserActionName = "UPDATE_SPEC"
DeploymentCauseDetailsDigitalOceanUserActionName_ResetDatabasePassword DeploymentCauseDetailsDigitalOceanUserActionName = "RESET_DATABASE_PASSWORD"
DeploymentCauseDetailsDigitalOceanUserActionName_RollbackApp DeploymentCauseDetailsDigitalOceanUserActionName = "ROLLBACK_APP"
DeploymentCauseDetailsDigitalOceanUserActionName_RevertAppRollback DeploymentCauseDetailsDigitalOceanUserActionName = "REVERT_APP_ROLLBACK"
)
// AppDomain struct for AppDomain
type AppDomain struct {
ID string `json:"id,omitempty"`
Spec *AppDomainSpec `json:"spec,omitempty"`
Phase AppDomainPhase `json:"phase,omitempty"`
Progress *AppDomainProgress `json:"progress,omitempty"`
Validation *AppDomainValidation `json:"validation,omitempty"`
}
// AppDomainPhase the model 'AppDomainPhase'
type AppDomainPhase string
// List of AppDomainPhase
const (
AppJobSpecKindPHASE_Unknown AppDomainPhase = "UNKNOWN"
AppJobSpecKindPHASE_Pending AppDomainPhase = "PENDING"
AppJobSpecKindPHASE_Configuring AppDomainPhase = "CONFIGURING"
AppJobSpecKindPHASE_Active AppDomainPhase = "ACTIVE"
AppJobSpecKindPHASE_Error AppDomainPhase = "ERROR"
)
// AppDomainProgress struct for AppDomainProgress
type AppDomainProgress struct {
Steps []*AppDomainProgressStep `json:"steps,omitempty"`
}
// AppDomainProgressStep struct for AppDomainProgressStep
type AppDomainProgressStep struct {
Name string `json:"name,omitempty"`
Status AppDomainProgressStepStatus `json:"status,omitempty"`
Steps []*AppDomainProgressStep `json:"steps,omitempty"`
StartedAt time.Time `json:"started_at,omitempty"`
EndedAt time.Time `json:"ended_at,omitempty"`
Reason *AppDomainProgressStepReason `json:"reason,omitempty"`
}
// AppDomainProgressStepReason struct for AppDomainProgressStepReason
type AppDomainProgressStepReason struct {
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
// AppDomainProgressStepStatus the model 'AppDomainProgressStepStatus'
type AppDomainProgressStepStatus string
// List of AppDomainProgressStepStatus
const (
AppJobSpecKindProgressStepStatus_Unknown AppDomainProgressStepStatus = "UNKNOWN"
AppJobSpecKindProgressStepStatus_Pending AppDomainProgressStepStatus = "PENDING"
AppJobSpecKindProgressStepStatus_Running AppDomainProgressStepStatus = "RUNNING"
AppJobSpecKindProgressStepStatus_Error AppDomainProgressStepStatus = "ERROR"
AppJobSpecKindProgressStepStatus_Success AppDomainProgressStepStatus = "SUCCESS"
)
// AppDomainValidation struct for AppDomainValidation
type AppDomainValidation struct {
TXTName string `json:"txt_name,omitempty"`
TXTValue string `json:"txt_value,omitempty"`
}
// GitHubSourceSpec struct for GitHubSourceSpec
type GitHubSourceSpec struct {
Repo string `json:"repo,omitempty"`
Branch string `json:"branch,omitempty"`
DeployOnPush bool `json:"deploy_on_push,omitempty"`
}
// GitLabSourceSpec struct for GitLabSourceSpec
type GitLabSourceSpec struct {
Repo string `json:"repo,omitempty"`
Branch string `json:"branch,omitempty"`
DeployOnPush bool `json:"deploy_on_push,omitempty"`
}
// GitSourceSpec struct for GitSourceSpec
type GitSourceSpec struct {
RepoCloneURL string `json:"repo_clone_url,omitempty"`
Branch string `json:"branch,omitempty"`
}
// ImageSourceSpec struct for ImageSourceSpec
type ImageSourceSpec struct {
RegistryType ImageSourceSpecRegistryType `json:"registry_type,omitempty"`
// The registry name. Must be left empty for the `DOCR` registry type. Required for the `DOCKER_HUB` registry type.
Registry string `json:"registry,omitempty"`
// The repository name.
Repository string `json:"repository,omitempty"`
// The repository tag. Defaults to `latest` if not provided.
Tag string `json:"tag,omitempty"`
}
// ImageSourceSpecRegistryType - DOCR: The DigitalOcean container registry type. - DOCKER_HUB: The DockerHub container registry type.
type ImageSourceSpecRegistryType string
// List of ImageSourceSpecRegistryType
const (
ImageSourceSpecRegistryType_Unspecified ImageSourceSpecRegistryType = "UNSPECIFIED"
ImageSourceSpecRegistryType_DOCR ImageSourceSpecRegistryType = "DOCR"
ImageSourceSpecRegistryType_DockerHub ImageSourceSpecRegistryType = "DOCKER_HUB"
)
// AppInstanceSize struct for AppInstanceSize
type AppInstanceSize struct {
Name string `json:"name,omitempty"`
Slug string `json:"slug,omitempty"`
CPUType AppInstanceSizeCPUType `json:"cpu_type,omitempty"`
CPUs string `json:"cpus,omitempty"`
MemoryBytes string `json:"memory_bytes,omitempty"`
USDPerMonth string `json:"usd_per_month,omitempty"`
USDPerSecond string `json:"usd_per_second,omitempty"`
TierSlug string `json:"tier_slug,omitempty"`
TierUpgradeTo string `json:"tier_upgrade_to,omitempty"`
TierDowngradeTo string `json:"tier_downgrade_to,omitempty"`
}
// AppInstanceSizeCPUType the model 'AppInstanceSizeCPUType'
type AppInstanceSizeCPUType string
// List of AppInstanceSizeCPUType
const (
AppInstanceSizeCPUType_Unspecified AppInstanceSizeCPUType = "UNSPECIFIED"
AppInstanceSizeCPUType_Shared AppInstanceSizeCPUType = "SHARED"
AppInstanceSizeCPUType_Dedicated AppInstanceSizeCPUType = "DEDICATED"
)
// AppProposeRequest struct for AppProposeRequest
type AppProposeRequest struct {
Spec *AppSpec `json:"spec"`
// An optional ID of an existing app. If set, the spec will be treated as a proposed update to the specified app. The existing app is not modified using this method.
AppID string `json:"app_id,omitempty"`
}
// AppProposeResponse struct for AppProposeResponse
type AppProposeResponse struct {
// Deprecated. Please use AppIsStarter instead.
AppIsStatic bool `json:"app_is_static,omitempty"`
// Indicates whether the app name is available.
AppNameAvailable bool `json:"app_name_available,omitempty"`
// If the app name is unavailable, this will be set to a suggested available name.
AppNameSuggestion string `json:"app_name_suggestion,omitempty"`
// Deprecated. Please use ExistingStarterApps instead.
ExistingStaticApps string `json:"existing_static_apps,omitempty"`
// Deprecated. Please use MaxFreeStarterApps instead.
MaxFreeStaticApps string `json:"max_free_static_apps,omitempty"`
Spec *AppSpec `json:"spec,omitempty"`
// The monthly cost of the proposed app in USD.
AppCost float32 `json:"app_cost,omitempty"`
// The monthly cost of the proposed app in USD using the next pricing plan tier. For example, if you propose an app that uses the Basic tier, the `AppTierUpgradeCost` field displays the monthly cost of the app if it were to use the Professional tier. If the proposed app already uses the most expensive tier, the field is empty.
AppTierUpgradeCost float32 `json:"app_tier_upgrade_cost,omitempty"`
// The monthly cost of the proposed app in USD using the previous pricing plan tier. For example, if you propose an app that uses the Professional tier, the `AppTierDowngradeCost` field displays the monthly cost of the app if it were to use the Basic tier. If the proposed app already uses the lest expensive tier, the field is empty.
AppTierDowngradeCost float32 `json:"app_tier_downgrade_cost,omitempty"`
// The number of existing starter tier apps the account has.
ExistingStarterApps string `json:"existing_starter_apps,omitempty"`
// The maximum number of free starter apps the account can have. Any additional starter apps will be charged for. These include apps with only static sites, functions, and databases.
MaxFreeStarterApps string `json:"max_free_starter_apps,omitempty"`
// Indicates whether the app is a starter tier app.
AppIsStarter bool `json:"app_is_starter,omitempty"`
}
// AppRegion struct for AppRegion
type AppRegion struct {
Slug string `json:"slug,omitempty"`
Label string `json:"label,omitempty"`
Flag string `json:"flag,omitempty"`
Continent string `json:"continent,omitempty"`
Disabled bool `json:"disabled,omitempty"`
DataCenters []string `json:"data_centers,omitempty"`
Reason string `json:"reason,omitempty"`
// Whether or not the region is presented as the default.
Default bool `json:"default,omitempty"`
}
// AppStringMatch struct for AppStringMatch
type AppStringMatch struct {
// Exact string match. Only 1 of `exact`, `prefix`, or `regex` must be set.
Exact string `json:"exact,omitempty"`
// Prefix-based match. Only 1 of `exact`, `prefix`, or `regex` must be set.
Prefix string `json:"prefix,omitempty"`
Regex string `json:"regex,omitempty"`
}
// AppTier struct for AppTier
type AppTier struct {
Name string `json:"name,omitempty"`
Slug string `json:"slug,omitempty"`
EgressBandwidthBytes string `json:"egress_bandwidth_bytes,omitempty"`
BuildSeconds string `json:"build_seconds,omitempty"`
}
// AppVariableScope the model 'AppVariableScope'
type AppVariableScope string
// List of AppVariableScope
const (
AppVariableScope_Unset AppVariableScope = "UNSET"
AppVariableScope_RunTime AppVariableScope = "RUN_TIME"
AppVariableScope_BuildTime AppVariableScope = "BUILD_TIME"
AppVariableScope_RunAndBuildTime AppVariableScope = "RUN_AND_BUILD_TIME"
)
// AppVariableType the model 'AppVariableType'
type AppVariableType string
// List of AppVariableType
const (
AppVariableType_General AppVariableType = "GENERAL"
AppVariableType_Secret AppVariableType = "SECRET"
)

439
apps.go
View File

@ -1,439 +0,0 @@
package godo
import (
"context"
"fmt"
"net/http"
)
const (
appsBasePath = "/v2/apps"
)
// AppLogType is the type of app logs.
type AppLogType string
const (
// AppLogTypeBuild represents build logs.
AppLogTypeBuild AppLogType = "BUILD"
// AppLogTypeDeploy represents deploy logs.
AppLogTypeDeploy AppLogType = "DEPLOY"
// AppLogTypeRun represents run logs.
AppLogTypeRun AppLogType = "RUN"
)
// AppsService is an interface for interfacing with the App Platform endpoints
// of the DigitalOcean API.
type AppsService interface {
Create(ctx context.Context, create *AppCreateRequest) (*App, *Response, error)
Get(ctx context.Context, appID string) (*App, *Response, error)
List(ctx context.Context, opts *ListOptions) ([]*App, *Response, error)
Update(ctx context.Context, appID string, update *AppUpdateRequest) (*App, *Response, error)
Delete(ctx context.Context, appID string) (*Response, error)
Propose(ctx context.Context, propose *AppProposeRequest) (*AppProposeResponse, *Response, error)
GetDeployment(ctx context.Context, appID, deploymentID string) (*Deployment, *Response, error)
ListDeployments(ctx context.Context, appID string, opts *ListOptions) ([]*Deployment, *Response, error)
CreateDeployment(ctx context.Context, appID string, create ...*DeploymentCreateRequest) (*Deployment, *Response, error)
GetLogs(ctx context.Context, appID, deploymentID, component string, logType AppLogType, follow bool, tailLines int) (*AppLogs, *Response, error)
ListRegions(ctx context.Context) ([]*AppRegion, *Response, error)
ListTiers(ctx context.Context) ([]*AppTier, *Response, error)
GetTier(ctx context.Context, slug string) (*AppTier, *Response, error)
ListInstanceSizes(ctx context.Context) ([]*AppInstanceSize, *Response, error)
GetInstanceSize(ctx context.Context, slug string) (*AppInstanceSize, *Response, error)
ListAlerts(ctx context.Context, appID string) ([]*AppAlert, *Response, error)
UpdateAlertDestinations(ctx context.Context, appID, alertID string, update *AlertDestinationUpdateRequest) (*AppAlert, *Response, error)
Detect(ctx context.Context, detect *DetectRequest) (*DetectResponse, *Response, error)
}
// AppLogs represent app logs.
type AppLogs struct {
LiveURL string `json:"live_url"`
HistoricURLs []string `json:"historic_urls"`
}
// AppUpdateRequest represents a request to update an app.
type AppUpdateRequest struct {
Spec *AppSpec `json:"spec"`
}
// DeploymentCreateRequest represents a request to create a deployment.
type DeploymentCreateRequest struct {
ForceBuild bool `json:"force_build"`
}
// AlertDestinationUpdateRequest represents a request to update alert destinations.
type AlertDestinationUpdateRequest struct {
Emails []string `json:"emails"`
SlackWebhooks []*AppAlertSlackWebhook `json:"slack_webhooks"`
}
type appRoot struct {
App *App `json:"app"`
}
type appsRoot struct {
Apps []*App `json:"apps"`
Links []*LinkAction `json:"links"`
Meta *Meta `json:"meta"`
}
type deploymentRoot struct {
Deployment *Deployment `json:"deployment"`
}
type deploymentsRoot struct {
Deployments []*Deployment `json:"deployments"`
Links []*LinkAction `json:"links"`
Meta *Meta `json:"meta"`
}
type appTierRoot struct {
Tier *AppTier `json:"tier"`
}
type appTiersRoot struct {
Tiers []*AppTier `json:"tiers"`
}
type instanceSizeRoot struct {
InstanceSize *AppInstanceSize `json:"instance_size"`
}
type instanceSizesRoot struct {
InstanceSizes []*AppInstanceSize `json:"instance_sizes"`
}
type appRegionsRoot struct {
Regions []*AppRegion `json:"regions"`
}
type appAlertsRoot struct {
Alerts []*AppAlert `json:"alerts"`
}
type appAlertRoot struct {
Alert *AppAlert `json:"alert"`
}
// AppsServiceOp handles communication with Apps methods of the DigitalOcean API.
type AppsServiceOp struct {
client *Client
}
// Create an app.
func (s *AppsServiceOp) Create(ctx context.Context, create *AppCreateRequest) (*App, *Response, error) {
path := appsBasePath
req, err := s.client.NewRequest(ctx, http.MethodPost, path, create)
if err != nil {
return nil, nil, err
}
root := new(appRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.App, resp, nil
}
// Get an app.
func (s *AppsServiceOp) Get(ctx context.Context, appID string) (*App, *Response, error) {
path := fmt.Sprintf("%s/%s", appsBasePath, appID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(appRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.App, resp, nil
}
// List apps.
func (s *AppsServiceOp) List(ctx context.Context, opts *ListOptions) ([]*App, *Response, error) {
path := appsBasePath
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(appsRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Apps, resp, nil
}
// Update an app.
func (s *AppsServiceOp) Update(ctx context.Context, appID string, update *AppUpdateRequest) (*App, *Response, error) {
path := fmt.Sprintf("%s/%s", appsBasePath, appID)
req, err := s.client.NewRequest(ctx, http.MethodPut, path, update)
if err != nil {
return nil, nil, err
}
root := new(appRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.App, resp, nil
}
// Delete an app.
func (s *AppsServiceOp) Delete(ctx context.Context, appID string) (*Response, error) {
path := fmt.Sprintf("%s/%s", appsBasePath, appID)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
// Propose an app.
func (s *AppsServiceOp) Propose(ctx context.Context, propose *AppProposeRequest) (*AppProposeResponse, *Response, error) {
path := fmt.Sprintf("%s/propose", appsBasePath)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, propose)
if err != nil {
return nil, nil, err
}
res := &AppProposeResponse{}
resp, err := s.client.Do(ctx, req, res)
if err != nil {
return nil, resp, err
}
return res, resp, nil
}
// GetDeployment gets an app deployment.
func (s *AppsServiceOp) GetDeployment(ctx context.Context, appID, deploymentID string) (*Deployment, *Response, error) {
path := fmt.Sprintf("%s/%s/deployments/%s", appsBasePath, appID, deploymentID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(deploymentRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Deployment, resp, nil
}
// ListDeployments lists an app deployments.
func (s *AppsServiceOp) ListDeployments(ctx context.Context, appID string, opts *ListOptions) ([]*Deployment, *Response, error) {
path := fmt.Sprintf("%s/%s/deployments", appsBasePath, appID)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(deploymentsRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Deployments, resp, nil
}
// CreateDeployment creates an app deployment.
func (s *AppsServiceOp) CreateDeployment(ctx context.Context, appID string, create ...*DeploymentCreateRequest) (*Deployment, *Response, error) {
path := fmt.Sprintf("%s/%s/deployments", appsBasePath, appID)
var createReq *DeploymentCreateRequest
for _, c := range create {
createReq = c
}
req, err := s.client.NewRequest(ctx, http.MethodPost, path, createReq)
if err != nil {
return nil, nil, err
}
root := new(deploymentRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Deployment, resp, nil
}
// GetLogs retrieves app logs.
func (s *AppsServiceOp) GetLogs(ctx context.Context, appID, deploymentID, component string, logType AppLogType, follow bool, tailLines int) (*AppLogs, *Response, error) {
url := fmt.Sprintf("%s/%s/deployments/%s/logs?type=%s&follow=%t&tail_lines=%d", appsBasePath, appID, deploymentID, logType, follow, tailLines)
if component != "" {
url = fmt.Sprintf("%s&component_name=%s", url, component)
}
req, err := s.client.NewRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, nil, err
}
logs := new(AppLogs)
resp, err := s.client.Do(ctx, req, logs)
if err != nil {
return nil, resp, err
}
return logs, resp, nil
}
// ListRegions lists all regions supported by App Platform.
func (s *AppsServiceOp) ListRegions(ctx context.Context) ([]*AppRegion, *Response, error) {
path := fmt.Sprintf("%s/regions", appsBasePath)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(appRegionsRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Regions, resp, nil
}
// ListTiers lists available app tiers.
func (s *AppsServiceOp) ListTiers(ctx context.Context) ([]*AppTier, *Response, error) {
path := fmt.Sprintf("%s/tiers", appsBasePath)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(appTiersRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Tiers, resp, nil
}
// GetTier retrieves information about a specific app tier.
func (s *AppsServiceOp) GetTier(ctx context.Context, slug string) (*AppTier, *Response, error) {
path := fmt.Sprintf("%s/tiers/%s", appsBasePath, slug)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(appTierRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Tier, resp, nil
}
// ListInstanceSizes lists available instance sizes for service, worker, and job components.
func (s *AppsServiceOp) ListInstanceSizes(ctx context.Context) ([]*AppInstanceSize, *Response, error) {
path := fmt.Sprintf("%s/tiers/instance_sizes", appsBasePath)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(instanceSizesRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.InstanceSizes, resp, nil
}
// GetInstanceSize retrieves information about a specific instance size for service, worker, and job components.
func (s *AppsServiceOp) GetInstanceSize(ctx context.Context, slug string) (*AppInstanceSize, *Response, error) {
path := fmt.Sprintf("%s/tiers/instance_sizes/%s", appsBasePath, slug)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(instanceSizeRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.InstanceSize, resp, nil
}
// ListAlerts retrieves a list of alerts on an app
func (s *AppsServiceOp) ListAlerts(ctx context.Context, appID string) ([]*AppAlert, *Response, error) {
path := fmt.Sprintf("%s/%s/alerts", appsBasePath, appID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(appAlertsRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Alerts, resp, nil
}
// UpdateAlertDestinations updates the alert destinations of an app's alert
func (s *AppsServiceOp) UpdateAlertDestinations(ctx context.Context, appID, alertID string, update *AlertDestinationUpdateRequest) (*AppAlert, *Response, error) {
path := fmt.Sprintf("%s/%s/alerts/%s/destinations", appsBasePath, appID, alertID)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, update)
if err != nil {
return nil, nil, err
}
root := new(appAlertRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Alert, resp, nil
}
// Detect an app.
func (s *AppsServiceOp) Detect(ctx context.Context, detect *DetectRequest) (*DetectResponse, *Response, error) {
path := fmt.Sprintf("%s/detect", appsBasePath)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, detect)
if err != nil {
return nil, nil, err
}
res := &DetectResponse{}
resp, err := s.client.Do(ctx, req, res)
if err != nil {
return nil, resp, err
}
return res, resp, nil
}

View File

@ -1,626 +0,0 @@
package godo
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
testAppSpec = &AppSpec{
Name: "app-name",
Region: testAppRegion.Slug,
Services: []*AppServiceSpec{{
Name: "service-name",
Routes: []*AppRouteSpec{{
Path: "/",
}},
RunCommand: "run-command",
BuildCommand: "build-command",
DockerfilePath: "Dockerfile",
GitHub: &GitHubSourceSpec{
Repo: "owner/service",
Branch: "branch",
},
InstanceSizeSlug: "professional-xs",
InstanceCount: 1,
}},
Workers: []*AppWorkerSpec{{
Name: "worker-name",
RunCommand: "run-command",
BuildCommand: "build-command",
DockerfilePath: "Dockerfile",
GitHub: &GitHubSourceSpec{
Repo: "owner/worker",
Branch: "branch",
},
InstanceSizeSlug: "professional-xs",
InstanceCount: 1,
}},
StaticSites: []*AppStaticSiteSpec{{
Name: "static-name",
BuildCommand: "build-command",
Git: &GitSourceSpec{
RepoCloneURL: "git@githost.com/owner/static.git",
Branch: "branch",
},
OutputDir: "out",
}},
Jobs: []*AppJobSpec{{
Name: "job-name",
RunCommand: "run-command",
BuildCommand: "build-command",
DockerfilePath: "Dockerfile",
GitHub: &GitHubSourceSpec{
Repo: "owner/job",
Branch: "branch",
},
InstanceSizeSlug: "professional-xs",
InstanceCount: 1,
}},
Databases: []*AppDatabaseSpec{{
Name: "db",
Engine: AppDatabaseSpecEngine_MySQL,
Version: "8",
Size: "size",
NumNodes: 1,
Production: true,
ClusterName: "cluster-name",
DBName: "app",
DBUser: "appuser",
}},
Functions: []*AppFunctionsSpec{{
Name: "functions-name",
GitHub: &GitHubSourceSpec{
Repo: "git@githost.com/owner/functions.git",
Branch: "branch",
},
}},
Domains: []*AppDomainSpec{
{
Domain: "example.com",
Type: AppDomainSpecType_Primary,
},
},
}
testAppRegion = AppRegion{
Slug: "ams",
Label: "Amsterdam",
Flag: "netherlands",
Continent: "Europe",
DataCenters: []string{"ams3"},
Default: true,
}
testDeployment = Deployment{
ID: "08f10d33-94c3-4492-b9a3-1603e9ab7fe4",
Spec: testAppSpec,
Services: []*DeploymentService{{
Name: "service-name",
SourceCommitHash: "service-hash",
}},
Workers: []*DeploymentWorker{{
Name: "worker-name",
SourceCommitHash: "worker-hash",
}},
StaticSites: []*DeploymentStaticSite{{
Name: "static-name",
SourceCommitHash: "static-hash",
}},
Jobs: []*DeploymentJob{{
Name: "job-name",
SourceCommitHash: "job-hash",
}},
Functions: []*DeploymentFunctions{{
Name: "functions-name",
SourceCommitHash: "functions-hash",
}},
CreatedAt: time.Unix(1595959200, 0).UTC(),
UpdatedAt: time.Unix(1595959200, 0).UTC(),
PhaseLastUpdatedAt: time.Unix(1595959200, 0).UTC(),
Phase: DeploymentPhase_Active,
Progress: &DeploymentProgress{
SuccessSteps: 1,
TotalSteps: 1,
Steps: []*DeploymentProgressStep{{
Name: "step",
Status: DeploymentProgressStepStatus_Success,
StartedAt: time.Unix(1595959200, 0).UTC(),
EndedAt: time.Unix(1595959200, 0).UTC(),
Steps: []*DeploymentProgressStep{{
Name: "sub",
Status: DeploymentProgressStepStatus_Success,
StartedAt: time.Unix(1595959200, 0).UTC(),
EndedAt: time.Unix(1595959200, 0).UTC(),
}},
}},
},
}
testApp = App{
ID: "1c70f8f3-106e-428b-ae6d-bfc693c77536",
Spec: testAppSpec,
DefaultIngress: "example.com",
LiveURL: "https://example.com",
LiveURLBase: "https://example.com",
LiveDomain: "example.com",
ActiveDeployment: &testDeployment,
InProgressDeployment: &testDeployment,
LastDeploymentCreatedAt: time.Unix(1595959200, 0).UTC(),
LastDeploymentActiveAt: time.Unix(1595959200, 0).UTC(),
CreatedAt: time.Unix(1595959200, 0).UTC(),
UpdatedAt: time.Unix(1595959200, 0).UTC(),
Region: &testAppRegion,
TierSlug: testAppTier.Slug,
}
testAppTier = AppTier{
Name: "Test",
Slug: "test",
EgressBandwidthBytes: "10240",
BuildSeconds: "3000",
}
testInstanceSize = AppInstanceSize{
Name: "Basic XXS",
Slug: "basic-xxs",
CPUType: AppInstanceSizeCPUType_Dedicated,
CPUs: "1",
MemoryBytes: "536870912",
USDPerMonth: "5",
USDPerSecond: "0.0000018896447",
TierSlug: "basic",
TierUpgradeTo: "professional-xs",
TierDowngradeTo: "basic-xxxs",
}
testAlerts = []*AppAlert{
{
ID: "c586fc0d-e8e2-4c50-9bf6-6c0a6b2ed2a7",
Spec: &AppAlertSpec{
Rule: AppAlertSpecRule_DeploymentFailed,
},
Emails: []string{"test@example.com", "test2@example.com"},
SlackWebhooks: []*AppAlertSlackWebhook{
{
URL: "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX",
Channel: "channel name",
},
},
},
}
testAlert = AppAlert{
ID: "c586fc0d-e8e2-4c50-9bf6-6c0a6b2ed2a7",
Spec: &AppAlertSpec{
Rule: AppAlertSpecRule_DeploymentFailed,
},
Emails: []string{"test@example.com", "test2@example.com"},
SlackWebhooks: []*AppAlertSlackWebhook{
{
URL: "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX",
Channel: "channel name",
},
},
}
)
func TestApps_CreateApp(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc("/v2/apps", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
var req AppCreateRequest
err := json.NewDecoder(r.Body).Decode(&req)
require.NoError(t, err)
assert.Equal(t, testAppSpec, req.Spec)
json.NewEncoder(w).Encode(&appRoot{App: &testApp})
})
app, _, err := client.Apps.Create(ctx, &AppCreateRequest{Spec: testAppSpec})
require.NoError(t, err)
assert.Equal(t, &testApp, app)
}
func TestApps_GetApp(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s", testApp.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
json.NewEncoder(w).Encode(&appRoot{App: &testApp})
})
app, _, err := client.Apps.Get(ctx, testApp.ID)
require.NoError(t, err)
assert.Equal(t, &testApp, app)
}
func TestApps_ListApp(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc("/v2/apps", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
json.NewEncoder(w).Encode(&appsRoot{Apps: []*App{&testApp}, Meta: &Meta{Total: 1}, Links: []*LinkAction{}})
})
apps, resp, err := client.Apps.List(ctx, nil)
require.NoError(t, err)
assert.Equal(t, []*App{&testApp}, apps)
assert.Equal(t, 1, resp.Meta.Total)
}
func TestApps_UpdateApp(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
updatedSpec := *testAppSpec
updatedSpec.Name = "new-name"
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s", testApp.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPut)
var req AppUpdateRequest
err := json.NewDecoder(r.Body).Decode(&req)
require.NoError(t, err)
assert.Equal(t, &updatedSpec, req.Spec)
json.NewEncoder(w).Encode(&appRoot{App: &testApp})
})
app, _, err := client.Apps.Update(ctx, testApp.ID, &AppUpdateRequest{Spec: &updatedSpec})
require.NoError(t, err)
assert.Equal(t, &testApp, app)
}
func TestApps_DeleteApp(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s", testApp.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := client.Apps.Delete(ctx, testApp.ID)
require.NoError(t, err)
}
func TestApps_ProposeApp(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
spec := &AppSpec{
Name: "sample-golang",
Services: []*AppServiceSpec{{
Name: "web",
EnvironmentSlug: "go",
RunCommand: "bin/sample-golang",
GitHub: &GitHubSourceSpec{
Repo: "digitalocean/sample-golang",
Branch: "branch",
},
}},
}
mux.HandleFunc("/v2/apps/propose", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
var req AppProposeRequest
err := json.NewDecoder(r.Body).Decode(&req)
require.NoError(t, err)
assert.Equal(t, spec, req.Spec)
assert.Equal(t, testApp.ID, req.AppID)
json.NewEncoder(w).Encode(&AppProposeResponse{
Spec: &AppSpec{
Name: "sample-golang",
Services: []*AppServiceSpec{{
Name: "web",
EnvironmentSlug: "go",
RunCommand: "bin/sample-golang",
GitHub: &GitHubSourceSpec{
Repo: "digitalocean/sample-golang",
Branch: "branch",
},
InstanceCount: 1,
Routes: []*AppRouteSpec{{
Path: "/",
}},
}},
},
AppNameAvailable: true,
})
})
res, _, err := client.Apps.Propose(ctx, &AppProposeRequest{
Spec: spec,
AppID: testApp.ID,
})
require.NoError(t, err)
assert.Equal(t, int64(1), res.Spec.Services[0].InstanceCount)
assert.Equal(t, "/", res.Spec.Services[0].Routes[0].Path)
assert.True(t, res.AppNameAvailable)
}
func TestApps_CreateDeployment(t *testing.T) {
for _, forceBuild := range []bool{true, false} {
t.Run(fmt.Sprintf("ForceBuild_%t", forceBuild), func(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/deployments", testApp.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
var req DeploymentCreateRequest
err := json.NewDecoder(r.Body).Decode(&req)
require.NoError(t, err)
assert.Equal(t, forceBuild, req.ForceBuild)
json.NewEncoder(w).Encode(&deploymentRoot{Deployment: &testDeployment})
})
deployment, _, err := client.Apps.CreateDeployment(ctx, testApp.ID, &DeploymentCreateRequest{
ForceBuild: forceBuild,
})
require.NoError(t, err)
assert.Equal(t, &testDeployment, deployment)
})
}
}
func TestApps_GetDeployment(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/deployments/%s", testApp.ID, testDeployment.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
json.NewEncoder(w).Encode(&deploymentRoot{Deployment: &testDeployment})
})
deployment, _, err := client.Apps.GetDeployment(ctx, testApp.ID, testDeployment.ID)
require.NoError(t, err)
assert.Equal(t, &testDeployment, deployment)
}
func TestApps_ListDeployments(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/deployments", testApp.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
json.NewEncoder(w).Encode(&deploymentsRoot{Deployments: []*Deployment{&testDeployment}, Meta: &Meta{Total: 1}, Links: []*LinkAction{}})
})
deployments, resp, err := client.Apps.ListDeployments(ctx, testApp.ID, nil)
require.NoError(t, err)
assert.Equal(t, []*Deployment{&testDeployment}, deployments)
assert.Equal(t, 1, resp.Meta.Total)
}
func TestApps_GetLogs(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/deployments/%s/logs", testApp.ID, testDeployment.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
assert.Equal(t, "RUN", r.URL.Query().Get("type"))
assert.Equal(t, "true", r.URL.Query().Get("follow"))
assert.Equal(t, "1", r.URL.Query().Get("tail_lines"))
_, hasComponent := r.URL.Query()["component_name"]
assert.False(t, hasComponent)
json.NewEncoder(w).Encode(&AppLogs{LiveURL: "https://live.logs.url"})
})
logs, _, err := client.Apps.GetLogs(ctx, testApp.ID, testDeployment.ID, "", AppLogTypeRun, true, 1)
require.NoError(t, err)
assert.NotEmpty(t, logs.LiveURL)
}
func TestApps_GetLogs_component(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/deployments/%s/logs", testApp.ID, testDeployment.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
assert.Equal(t, "RUN", r.URL.Query().Get("type"))
assert.Equal(t, "true", r.URL.Query().Get("follow"))
assert.Equal(t, "1", r.URL.Query().Get("tail_lines"))
assert.Equal(t, "service-name", r.URL.Query().Get("component_name"))
json.NewEncoder(w).Encode(&AppLogs{LiveURL: "https://live.logs.url"})
})
logs, _, err := client.Apps.GetLogs(ctx, testApp.ID, testDeployment.ID, "service-name", AppLogTypeRun, true, 1)
require.NoError(t, err)
assert.NotEmpty(t, logs.LiveURL)
}
func TestApps_ListRegions(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc("/v2/apps/regions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
json.NewEncoder(w).Encode(&appRegionsRoot{Regions: []*AppRegion{&testAppRegion}})
})
regions, _, err := client.Apps.ListRegions(ctx)
require.NoError(t, err)
assert.Equal(t, []*AppRegion{&testAppRegion}, regions)
}
func TestApps_ListTiers(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc("/v2/apps/tiers", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
json.NewEncoder(w).Encode(&appTiersRoot{Tiers: []*AppTier{&testAppTier}})
})
tiers, _, err := client.Apps.ListTiers(ctx)
require.NoError(t, err)
assert.Equal(t, []*AppTier{&testAppTier}, tiers)
}
func TestApps_GetTier(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc(fmt.Sprintf("/v2/apps/tiers/%s", testAppTier.Slug), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
json.NewEncoder(w).Encode(&appTierRoot{Tier: &testAppTier})
})
tier, _, err := client.Apps.GetTier(ctx, testAppTier.Slug)
require.NoError(t, err)
assert.Equal(t, &testAppTier, tier)
}
func TestApps_ListInstanceSizes(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc("/v2/apps/tiers/instance_sizes", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
json.NewEncoder(w).Encode(&instanceSizesRoot{InstanceSizes: []*AppInstanceSize{&testInstanceSize}})
})
instanceSizes, _, err := client.Apps.ListInstanceSizes(ctx)
require.NoError(t, err)
assert.Equal(t, []*AppInstanceSize{&testInstanceSize}, instanceSizes)
}
func TestApps_GetInstanceSize(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc(fmt.Sprintf("/v2/apps/tiers/instance_sizes/%s", testInstanceSize.Slug), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
json.NewEncoder(w).Encode(&instanceSizeRoot{InstanceSize: &testInstanceSize})
})
instancesize, _, err := client.Apps.GetInstanceSize(ctx, testInstanceSize.Slug)
require.NoError(t, err)
assert.Equal(t, &testInstanceSize, instancesize)
}
func TestApps_ListAppAlerts(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/alerts", testApp.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
json.NewEncoder(w).Encode(&appAlertsRoot{Alerts: testAlerts})
})
appAlerts, _, err := client.Apps.ListAlerts(ctx, testApp.ID)
require.NoError(t, err)
assert.Equal(t, testAlerts, appAlerts)
}
func TestApps_UpdateAppAlertDestinations(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/alerts/%s/destinations", testApp.ID, testAlert.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
json.NewEncoder(w).Encode(&appAlertRoot{Alert: &testAlert})
})
appAlert, _, err := client.Apps.UpdateAlertDestinations(ctx, testApp.ID, testAlert.ID, &AlertDestinationUpdateRequest{Emails: testAlert.Emails, SlackWebhooks: testAlert.SlackWebhooks})
require.NoError(t, err)
assert.Equal(t, &testAlert, appAlert)
}
func TestApps_Detect(t *testing.T) {
setup()
defer teardown()
ctx := context.Background()
gitSource := &GitSourceSpec{
RepoCloneURL: "https://github.com/digitalocean/sample-nodejs.git",
Branch: "main",
}
component := &DetectResponseComponent{
Strategy: DetectResponseType_Buildpack,
EnvVars: []*AppVariableDefinition{{
Key: "k",
Value: "v",
}},
}
mux.HandleFunc("/v2/apps/detect", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
var req DetectRequest
err := json.NewDecoder(r.Body).Decode(&req)
require.NoError(t, err)
assert.Equal(t, gitSource, req.Git)
json.NewEncoder(w).Encode(&DetectResponse{
Components: []*DetectResponseComponent{component},
})
})
res, _, err := client.Apps.Detect(ctx, &DetectRequest{
Git: gitSource,
})
require.NoError(t, err)
assert.Equal(t, component, res.Components[0])
}

View File

@ -1,52 +0,0 @@
package godo
import (
"context"
"net/http"
"time"
)
// BalanceService is an interface for interfacing with the Balance
// endpoints of the DigitalOcean API
// See: https://docs.digitalocean.com/reference/api/api-reference/#operation/balance_get
type BalanceService interface {
Get(context.Context) (*Balance, *Response, error)
}
// BalanceServiceOp handles communication with the Balance related methods of
// the DigitalOcean API.
type BalanceServiceOp struct {
client *Client
}
var _ BalanceService = &BalanceServiceOp{}
// Balance represents a DigitalOcean Balance
type Balance struct {
MonthToDateBalance string `json:"month_to_date_balance"`
AccountBalance string `json:"account_balance"`
MonthToDateUsage string `json:"month_to_date_usage"`
GeneratedAt time.Time `json:"generated_at"`
}
func (r Balance) String() string {
return Stringify(r)
}
// Get DigitalOcean balance info
func (s *BalanceServiceOp) Get(ctx context.Context) (*Balance, *Response, error) {
path := "v2/customers/my/balance"
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(Balance)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root, resp, err
}

View File

@ -1,44 +0,0 @@
package godo
import (
"fmt"
"net/http"
"reflect"
"testing"
"time"
)
func TestBalanceGet(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/customers/my/balance", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
response := `
{
"month_to_date_balance": "23.44",
"account_balance": "12.23",
"month_to_date_usage": "11.21",
"generated_at": "2018-06-21T08:44:38Z"
}
`
fmt.Fprint(w, response)
})
bal, _, err := client.Balance.Get(ctx)
if err != nil {
t.Errorf("Balance.Get returned error: %v", err)
}
expected := &Balance{
MonthToDateBalance: "23.44",
AccountBalance: "12.23",
MonthToDateUsage: "11.21",
GeneratedAt: time.Date(2018, 6, 21, 8, 44, 38, 0, time.UTC),
}
if !reflect.DeepEqual(bal, expected) {
t.Errorf("Balance.Get returned %+v, expected %+v", bal, expected)
}
}

View File

@ -1,72 +0,0 @@
package godo
import (
"context"
"net/http"
"time"
)
const billingHistoryBasePath = "v2/customers/my/billing_history"
// BillingHistoryService is an interface for interfacing with the BillingHistory
// endpoints of the DigitalOcean API
// See: https://docs.digitalocean.com/reference/api/api-reference/#operation/billingHistory_list
type BillingHistoryService interface {
List(context.Context, *ListOptions) (*BillingHistory, *Response, error)
}
// BillingHistoryServiceOp handles communication with the BillingHistory related methods of
// the DigitalOcean API.
type BillingHistoryServiceOp struct {
client *Client
}
var _ BillingHistoryService = &BillingHistoryServiceOp{}
// BillingHistory represents a DigitalOcean Billing History
type BillingHistory struct {
BillingHistory []BillingHistoryEntry `json:"billing_history"`
Links []*LinkAction `json:"links"`
Meta *Meta `json:"meta"`
}
// BillingHistoryEntry represents an entry in a customer's Billing History
type BillingHistoryEntry struct {
Description string `json:"description"`
Amount string `json:"amount"`
InvoiceID *string `json:"invoice_id"`
InvoiceUUID *string `json:"invoice_uuid"`
Date time.Time `json:"date"`
Type string `json:"type"`
}
func (b BillingHistory) String() string {
return Stringify(b)
}
// List the Billing History for a customer
func (s *BillingHistoryServiceOp) List(ctx context.Context, opt *ListOptions) (*BillingHistory, *Response, error) {
path, err := addOptions(billingHistoryBasePath, opt)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(BillingHistory)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root, resp, err
}

View File

@ -1,71 +0,0 @@
package godo
import (
"fmt"
"net/http"
"reflect"
"testing"
"time"
)
func TestBillingHistory_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/customers/my/billing_history", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{
"billing_history": [
{
"description": "Invoice for May 2018",
"amount": "12.34",
"invoice_id": "123",
"invoice_uuid": "example-uuid",
"date": "2018-06-01T08:44:38Z",
"type": "Invoice"
},
{
"description": "Payment (MC 2018)",
"amount": "-12.34",
"date": "2018-06-02T08:44:38Z",
"type": "Payment"
}
],
"meta": {
"total": 2
}
}`)
})
history, resp, err := client.BillingHistory.List(ctx, nil)
if err != nil {
t.Errorf("BillingHistory.List returned error: %v", err)
}
expectedBillingHistory := []BillingHistoryEntry{
{
Description: "Invoice for May 2018",
Amount: "12.34",
InvoiceID: String("123"),
InvoiceUUID: String("example-uuid"),
Date: time.Date(2018, 6, 1, 8, 44, 38, 0, time.UTC),
Type: "Invoice",
},
{
Description: "Payment (MC 2018)",
Amount: "-12.34",
InvoiceID: nil,
InvoiceUUID: nil,
Date: time.Date(2018, 6, 2, 8, 44, 38, 0, time.UTC),
Type: "Payment",
},
}
entries := history.BillingHistory
if !reflect.DeepEqual(entries, expectedBillingHistory) {
t.Errorf("BillingHistory.List\nBillingHistory: got=%#v\nwant=%#v", entries, expectedBillingHistory)
}
expectedMeta := &Meta{Total: 2}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("BillingHistory.List\nMeta: got=%#v\nwant=%#v", resp.Meta, expectedMeta)
}
}

218
cdn.go
View File

@ -1,218 +0,0 @@
package godo
import (
"context"
"fmt"
"net/http"
"time"
)
const cdnBasePath = "v2/cdn/endpoints"
// CDNService is an interface for managing Spaces CDN with the DigitalOcean API.
type CDNService interface {
List(context.Context, *ListOptions) ([]CDN, *Response, error)
Get(context.Context, string) (*CDN, *Response, error)
Create(context.Context, *CDNCreateRequest) (*CDN, *Response, error)
UpdateTTL(context.Context, string, *CDNUpdateTTLRequest) (*CDN, *Response, error)
UpdateCustomDomain(context.Context, string, *CDNUpdateCustomDomainRequest) (*CDN, *Response, error)
FlushCache(context.Context, string, *CDNFlushCacheRequest) (*Response, error)
Delete(context.Context, string) (*Response, error)
}
// CDNServiceOp handles communication with the CDN related methods of the
// DigitalOcean API.
type CDNServiceOp struct {
client *Client
}
var _ CDNService = &CDNServiceOp{}
// CDN represents a DigitalOcean CDN
type CDN struct {
ID string `json:"id"`
Origin string `json:"origin"`
Endpoint string `json:"endpoint"`
CreatedAt time.Time `json:"created_at"`
TTL uint32 `json:"ttl"`
CertificateID string `json:"certificate_id,omitempty"`
CustomDomain string `json:"custom_domain,omitempty"`
}
// CDNRoot represents a response from the DigitalOcean API
type cdnRoot struct {
Endpoint *CDN `json:"endpoint"`
}
type cdnsRoot struct {
Endpoints []CDN `json:"endpoints"`
Links []*LinkAction `json:"links"`
Meta *Meta `json:"meta"`
}
// CDNCreateRequest represents a request to create a CDN.
type CDNCreateRequest struct {
Origin string `json:"origin"`
TTL uint32 `json:"ttl"`
CustomDomain string `json:"custom_domain,omitempty"`
CertificateID string `json:"certificate_id,omitempty"`
}
// CDNUpdateTTLRequest represents a request to update the ttl of a CDN.
type CDNUpdateTTLRequest struct {
TTL uint32 `json:"ttl"`
}
// CDNUpdateCustomDomainRequest represents a request to update the custom domain of a CDN.
type CDNUpdateCustomDomainRequest struct {
CustomDomain string `json:"custom_domain"`
CertificateID string `json:"certificate_id"`
}
// CDNFlushCacheRequest represents a request to flush cache of a CDN.
type CDNFlushCacheRequest struct {
Files []string `json:"files"`
}
// List all CDN endpoints
func (c CDNServiceOp) List(ctx context.Context, opt *ListOptions) ([]CDN, *Response, error) {
path, err := addOptions(cdnBasePath, opt)
if err != nil {
return nil, nil, err
}
req, err := c.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(cdnsRoot)
resp, err := c.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Endpoints, resp, err
}
// Get individual CDN. It requires a non-empty cdn id.
func (c CDNServiceOp) Get(ctx context.Context, id string) (*CDN, *Response, error) {
if len(id) == 0 {
return nil, nil, NewArgError("id", "cannot be an empty string")
}
path := fmt.Sprintf("%s/%s", cdnBasePath, id)
req, err := c.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(cdnRoot)
resp, err := c.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Endpoint, resp, err
}
// Create a new CDN
func (c CDNServiceOp) Create(ctx context.Context, createRequest *CDNCreateRequest) (*CDN, *Response, error) {
if createRequest == nil {
return nil, nil, NewArgError("createRequest", "cannot be nil")
}
req, err := c.client.NewRequest(ctx, http.MethodPost, cdnBasePath, createRequest)
if err != nil {
return nil, nil, err
}
root := new(cdnRoot)
resp, err := c.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Endpoint, resp, err
}
// UpdateTTL updates the ttl of an individual CDN
func (c CDNServiceOp) UpdateTTL(ctx context.Context, id string, updateRequest *CDNUpdateTTLRequest) (*CDN, *Response, error) {
return c.update(ctx, id, updateRequest)
}
// UpdateCustomDomain sets or removes the custom domain of an individual CDN
func (c CDNServiceOp) UpdateCustomDomain(ctx context.Context, id string, updateRequest *CDNUpdateCustomDomainRequest) (*CDN, *Response, error) {
return c.update(ctx, id, updateRequest)
}
func (c CDNServiceOp) update(ctx context.Context, id string, updateRequest interface{}) (*CDN, *Response, error) {
if updateRequest == nil {
return nil, nil, NewArgError("updateRequest", "cannot be nil")
}
if len(id) == 0 {
return nil, nil, NewArgError("id", "cannot be an empty string")
}
path := fmt.Sprintf("%s/%s", cdnBasePath, id)
req, err := c.client.NewRequest(ctx, http.MethodPut, path, updateRequest)
if err != nil {
return nil, nil, err
}
root := new(cdnRoot)
resp, err := c.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Endpoint, resp, err
}
// FlushCache flushes the cache of an individual CDN. Requires a non-empty slice of file paths and/or wildcards
func (c CDNServiceOp) FlushCache(ctx context.Context, id string, flushCacheRequest *CDNFlushCacheRequest) (*Response, error) {
if flushCacheRequest == nil {
return nil, NewArgError("flushCacheRequest", "cannot be nil")
}
if len(id) == 0 {
return nil, NewArgError("id", "cannot be an empty string")
}
path := fmt.Sprintf("%s/%s/cache", cdnBasePath, id)
req, err := c.client.NewRequest(ctx, http.MethodDelete, path, flushCacheRequest)
if err != nil {
return nil, err
}
resp, err := c.client.Do(ctx, req, nil)
return resp, err
}
// Delete an individual CDN
func (c CDNServiceOp) Delete(ctx context.Context, id string) (*Response, error) {
if len(id) == 0 {
return nil, NewArgError("id", "cannot be an empty string")
}
path := fmt.Sprintf("%s/%s", cdnBasePath, id)
req, err := c.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := c.client.Do(ctx, req, nil)
return resp, err
}

View File

@ -1,425 +0,0 @@
package godo
import (
"fmt"
"net/http"
"reflect"
"testing"
"time"
)
func TestCDN_ListCDN(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/cdn/endpoints", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(
w,
`{
"endpoints": [
{
"id": "892071a0-bb95-49bc-8021-3afd67a210bf",
"origin": "my-space.nyc3.digitaloceanspaces.com",
"endpoint": "my-space.nyc3.cdn.digitaloceanspaces.com",
"ttl": 3600,
"created_at": "2012-10-02T15:00:01.05Z"
},
{
"id": "892071a0-bb95-55bd-8021-3afd67a210bf",
"origin": "my-space1.nyc3.digitaloceanspaces.com",
"endpoint": "my-space1.nyc3.cdn.digitaloceanspaces.com",
"ttl": 3600,
"created_at": "2012-10-03T15:00:01.05Z"
}
],
"meta": {
"total": 2
}
}`,
)
})
cdns, resp, err := client.CDNs.List(ctx, nil)
if err != nil {
t.Errorf("CDNs.List returned error: %v", err)
}
expectedCDNs := []CDN{
{
ID: "892071a0-bb95-49bc-8021-3afd67a210bf",
Origin: "my-space.nyc3.digitaloceanspaces.com",
Endpoint: "my-space.nyc3.cdn.digitaloceanspaces.com",
TTL: 3600,
CreatedAt: time.Date(2012, 10, 02, 15, 00, 01, 50000000, time.UTC),
},
{
ID: "892071a0-bb95-55bd-8021-3afd67a210bf",
Origin: "my-space1.nyc3.digitaloceanspaces.com",
Endpoint: "my-space1.nyc3.cdn.digitaloceanspaces.com",
TTL: 3600,
CreatedAt: time.Date(2012, 10, 03, 15, 00, 01, 50000000, time.UTC),
},
}
if !reflect.DeepEqual(cdns, expectedCDNs) {
t.Errorf("CDNs.List returned CDNs %+v, expected %+v", cdns, expectedCDNs)
}
expectedMeta := &Meta{
Total: 2,
}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("CDNs.List returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}
func TestCDN_ListCDNMultiplePages(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/cdn/endpoints", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(
w,
`{
"endpoints": [
{
"id": "892071a0-bb95-49bc-8021-3afd67a210bf",
"origin": "my-space.nyc3.digitaloceanspaces.com",
"endpoint": "my-space.nyc3.cdn.digitaloceanspaces.com",
"ttl": 3600,
"created_at": "2012-10-02T15:00:01.05Z"
},
{
"id": "892071a0-bb95-55bd-8021-3afd67a210bf",
"origin": "my-space1.nyc3.digitaloceanspaces.com",
"endpoint": "my-space1.nyc3.cdn.digitaloceanspaces.com",
"ttl": 3600,
"created_at": "2012-10-03T15:00:01.05Z"
}
],
"links":[]
}`,
)
})
_, _, err := client.CDNs.List(ctx, nil)
if err != nil {
t.Errorf("CDNs.List multiple page returned error: %v", err)
}
}
func TestCDN_RetrievePageByNumber(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/cdn/endpoints", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(
w,
`{
"endpoints": [
{
"id": "892071a0-bb95-49bc-8021-3afd67a210bf",
"origin": "my-space.nyc3.digitaloceanspaces.com",
"endpoint": "my-space.nyc3.cdn.digitaloceanspaces.com",
"ttl": 3600,
"created_at": "2012-10-02T15:00:01.05Z"
},
{
"id": "892071a0-bb95-55bd-8021-3afd67a210bf",
"origin": "my-space1.nyc3.digitaloceanspaces.com",
"endpoint": "my-space1.nyc3.cdn.digitaloceanspaces.com",
"ttl": 3600,
"created_at": "2012-10-03T15:00:01.05Z"
}
],
"links":[]
}`,
)
})
_, _, err := client.CDNs.List(ctx, &ListOptions{Page: 2})
if err != nil {
t.Errorf("CDNs.List singular page returned error: %v", err)
}
}
func TestCDN_GetCDN(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/cdn/endpoints/12345", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(
w,
`{
"endpoint": {
"id": "12345",
"origin": "my-space.nyc3.digitaloceanspaces.com",
"endpoint": "my-space.nyc3.cdn.digitaloceanspaces.com",
"ttl": 3600,
"created_at": "2012-10-02T15:00:01.05Z"
}
}`,
)
})
cdn, _, err := client.CDNs.Get(ctx, "12345")
if err != nil {
t.Errorf("CDNs.Get returned error: %v", err)
}
expected := &CDN{
ID: "12345",
Origin: "my-space.nyc3.digitaloceanspaces.com",
Endpoint: "my-space.nyc3.cdn.digitaloceanspaces.com",
TTL: 3600,
CreatedAt: time.Date(2012, 10, 02, 15, 00, 01, 50000000, time.UTC),
}
if !reflect.DeepEqual(cdn, expected) {
t.Errorf("CDNs.Get returned %+v, expected %+v", cdn, expected)
}
}
func TestCDN_CreateCDN(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/cdn/endpoints", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
fmt.Fprint(
w,
`{
"endpoint": {
"id": "12345",
"origin": "my-space.nyc3.digitaloceanspaces.com",
"endpoint": "my-space.nyc3.cdn.digitaloceanspaces.com",
"ttl": 3600,
"created_at": "2012-10-02T15:00:01.05Z"
}
}`,
)
})
req := &CDNCreateRequest{Origin: "my-space.nyc3.digitaloceanspaces.com", TTL: 3600}
cdn, _, err := client.CDNs.Create(ctx, req)
if err != nil {
t.Errorf("CDNs.Create returned error: %v", err)
}
expected := &CDN{
ID: "12345",
Origin: "my-space.nyc3.digitaloceanspaces.com",
Endpoint: "my-space.nyc3.cdn.digitaloceanspaces.com",
TTL: 3600,
CreatedAt: time.Date(2012, 10, 02, 15, 00, 01, 50000000, time.UTC),
}
if !reflect.DeepEqual(cdn, expected) {
t.Errorf("CDNs.Create returned %+v, expected %+v", cdn, expected)
}
}
func TestCDN_CreateCustomDomainCDN(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/cdn/endpoints", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
fmt.Fprint(
w,
`{
"endpoint": {
"id": "12345",
"origin": "my-space.nyc3.digitaloceanspaces.com",
"endpoint": "my-space.nyc3.cdn.digitaloceanspaces.com",
"ttl": 3600,
"created_at": "2012-10-02T15:00:01.05Z",
"custom_domain": "assets.myacmecorp.com",
"certificate_id": "a20489cc-d278-48d2-8e10-45d42a312451"
}
}`,
)
})
req := &CDNCreateRequest{
Origin: "my-space.nyc3.digitaloceanspaces.com",
TTL: 3600,
CustomDomain: "assets.myacmecorp.com",
CertificateID: "a20489cc-d278-48d2-8e10-45d42a312451",
}
cdn, _, err := client.CDNs.Create(ctx, req)
if err != nil {
t.Errorf("CDNs.Create returned error: %v", err)
}
expected := &CDN{
ID: "12345",
Origin: "my-space.nyc3.digitaloceanspaces.com",
Endpoint: "my-space.nyc3.cdn.digitaloceanspaces.com",
TTL: 3600,
CustomDomain: "assets.myacmecorp.com",
CertificateID: "a20489cc-d278-48d2-8e10-45d42a312451",
CreatedAt: time.Date(2012, 10, 02, 15, 00, 01, 50000000, time.UTC),
}
if !reflect.DeepEqual(cdn, expected) {
t.Errorf("CDNs.Create returned %+v, expected %+v", cdn, expected)
}
}
func TestCDN_DeleteCDN(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/cdn/endpoints/12345", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := client.CDNs.Delete(ctx, "12345")
if err != nil {
t.Errorf("CDNs.Delete returned error: %v", err)
}
}
func TestCDN_UpdateTTLCDN(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/cdn/endpoints/12345", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPut)
fmt.Fprint(
w,
`{
"endpoint": {
"id": "12345",
"origin": "my-space.nyc3.digitaloceanspaces.com",
"endpoint": "my-space.nyc3.cdn.digitaloceanspaces.com",
"ttl": 60,
"created_at": "2012-10-02T15:00:01.05Z"
}
}`,
)
})
req := &CDNUpdateTTLRequest{TTL: 60}
cdn, _, err := client.CDNs.UpdateTTL(ctx, "12345", req)
if err != nil {
t.Errorf("CDNs.UpdateTTL returned error: %v", err)
}
expected := &CDN{
ID: "12345",
Origin: "my-space.nyc3.digitaloceanspaces.com",
Endpoint: "my-space.nyc3.cdn.digitaloceanspaces.com",
TTL: 60,
CreatedAt: time.Date(2012, 10, 02, 15, 00, 01, 50000000, time.UTC),
}
if !reflect.DeepEqual(cdn, expected) {
t.Errorf("CDNs.UpdateTTL returned %+v, expected %+v", cdn, expected)
}
}
func TestCDN_UpdateAddCustomDomainCDN(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/cdn/endpoints/12345", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPut)
fmt.Fprint(
w,
`{
"endpoint": {
"id": "12345",
"origin": "my-space.nyc3.digitaloceanspaces.com",
"endpoint": "my-space.nyc3.cdn.digitaloceanspaces.com",
"ttl": 60,
"created_at": "2012-10-02T15:00:01.05Z",
"custom_domain": "assets.myacmecorp.com",
"certificate_id": "a20489cc-d278-48d2-8e10-45d42a312451"
}
}`,
)
})
req := &CDNUpdateCustomDomainRequest{
CustomDomain: "assets.myacmecorp.com",
CertificateID: "a20489cc-d278-48d2-8e10-45d42a312451",
}
cdn, _, err := client.CDNs.UpdateCustomDomain(ctx, "12345", req)
if err != nil {
t.Errorf("CDNs.UpdateTTL returned error: %v", err)
}
expected := &CDN{
ID: "12345",
Origin: "my-space.nyc3.digitaloceanspaces.com",
Endpoint: "my-space.nyc3.cdn.digitaloceanspaces.com",
CustomDomain: "assets.myacmecorp.com",
CertificateID: "a20489cc-d278-48d2-8e10-45d42a312451",
TTL: 60,
CreatedAt: time.Date(2012, 10, 02, 15, 00, 01, 50000000, time.UTC),
}
if !reflect.DeepEqual(cdn, expected) {
t.Errorf("CDNs.UpdateTTL returned %+v, expected %+v", cdn, expected)
}
}
func TestCDN_UpdateRemoveCustomDomainCDN(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/cdn/endpoints/12345", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPut)
fmt.Fprint(
w,
`{
"endpoint": {
"id": "12345",
"origin": "my-space.nyc3.digitaloceanspaces.com",
"endpoint": "my-space.nyc3.cdn.digitaloceanspaces.com",
"ttl": 60,
"created_at": "2012-10-02T15:00:01.05Z"
}
}`,
)
})
req := &CDNUpdateCustomDomainRequest{}
cdn, _, err := client.CDNs.UpdateCustomDomain(ctx, "12345", req)
if err != nil {
t.Errorf("CDNs.UpdateTTL returned error: %v", err)
}
expected := &CDN{
ID: "12345",
Origin: "my-space.nyc3.digitaloceanspaces.com",
Endpoint: "my-space.nyc3.cdn.digitaloceanspaces.com",
TTL: 60,
CreatedAt: time.Date(2012, 10, 02, 15, 00, 01, 50000000, time.UTC),
}
if !reflect.DeepEqual(cdn, expected) {
t.Errorf("CDNs.UpdateTTL returned %+v, expected %+v", cdn, expected)
}
}
func TestCDN_FluchCacheCDN(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/cdn/endpoints/12345/cache", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
req := &CDNFlushCacheRequest{Files: []string{"*"}}
_, err := client.CDNs.FlushCache(ctx, "12345", req)
if err != nil {
t.Errorf("CDNs.FlushCache returned error: %v", err)
}
}

View File

@ -1,130 +0,0 @@
package godo
import (
"context"
"net/http"
"path"
)
const certificatesBasePath = "/v2/certificates"
// CertificatesService is an interface for managing certificates with the DigitalOcean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Certificates
type CertificatesService interface {
Get(context.Context, string) (*Certificate, *Response, error)
List(context.Context, *ListOptions) ([]Certificate, *Response, error)
Create(context.Context, *CertificateRequest) (*Certificate, *Response, error)
Delete(context.Context, string) (*Response, error)
}
// Certificate represents a DigitalOcean certificate configuration.
type Certificate struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
DNSNames []string `json:"dns_names,omitempty"`
NotAfter string `json:"not_after,omitempty"`
SHA1Fingerprint string `json:"sha1_fingerprint,omitempty"`
Created string `json:"created_at,omitempty"`
State string `json:"state,omitempty"`
Type string `json:"type,omitempty"`
}
// CertificateRequest represents configuration for a new certificate.
type CertificateRequest struct {
Name string `json:"name,omitempty"`
DNSNames []string `json:"dns_names,omitempty"`
PrivateKey string `json:"private_key,omitempty"`
LeafCertificate string `json:"leaf_certificate,omitempty"`
CertificateChain string `json:"certificate_chain,omitempty"`
Type string `json:"type,omitempty"`
}
type certificateRoot struct {
Certificate *Certificate `json:"certificate"`
}
type certificatesRoot struct {
Certificates []Certificate `json:"certificates"`
Links []*LinkAction `json:"links"`
Meta *Meta `json:"meta"`
}
// CertificatesServiceOp handles communication with certificates methods of the DigitalOcean API.
type CertificatesServiceOp struct {
client *Client
}
var _ CertificatesService = &CertificatesServiceOp{}
// Get an existing certificate by its identifier.
func (c *CertificatesServiceOp) Get(ctx context.Context, cID string) (*Certificate, *Response, error) {
urlStr := path.Join(certificatesBasePath, cID)
req, err := c.client.NewRequest(ctx, http.MethodGet, urlStr, nil)
if err != nil {
return nil, nil, err
}
root := new(certificateRoot)
resp, err := c.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Certificate, resp, nil
}
// List all certificates.
func (c *CertificatesServiceOp) List(ctx context.Context, opt *ListOptions) ([]Certificate, *Response, error) {
urlStr, err := addOptions(certificatesBasePath, opt)
if err != nil {
return nil, nil, err
}
req, err := c.client.NewRequest(ctx, http.MethodGet, urlStr, nil)
if err != nil {
return nil, nil, err
}
root := new(certificatesRoot)
resp, err := c.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Certificates, resp, nil
}
// Create a new certificate with provided configuration.
func (c *CertificatesServiceOp) Create(ctx context.Context, cr *CertificateRequest) (*Certificate, *Response, error) {
req, err := c.client.NewRequest(ctx, http.MethodPost, certificatesBasePath, cr)
if err != nil {
return nil, nil, err
}
root := new(certificateRoot)
resp, err := c.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Certificate, resp, nil
}
// Delete a certificate by its identifier.
func (c *CertificatesServiceOp) Delete(ctx context.Context, cID string) (*Response, error) {
urlStr := path.Join(certificatesBasePath, cID)
req, err := c.client.NewRequest(ctx, http.MethodDelete, urlStr, nil)
if err != nil {
return nil, err
}
return c.client.Do(ctx, req, nil)
}

View File

@ -1,251 +0,0 @@
package godo
import (
"encoding/json"
"fmt"
"net/http"
"path"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var certJSONResponse = `
{
"certificate": {
"id": "892071a0-bb95-49bc-8021-3afd67a210bf",
"name": "web-cert-01",
"dns_names": [
"somedomain.com",
"api.somedomain.com"
],
"not_after": "2017-02-22T00:23:00Z",
"sha1_fingerprint": "dfcc9f57d86bf58e321c2c6c31c7a971be244ac7",
"created_at": "2017-02-08T16:02:37Z",
"state": "verified",
"type": "custom"
}
}
`
var certsJSONResponse = `
{
"certificates": [
{
"id": "892071a0-bb95-49bc-8021-3afd67a210bf",
"name": "web-cert-01",
"dns_names": [
"somedomain.com",
"api.somedomain.com"
],
"not_after": "2017-02-22T00:23:00Z",
"sha1_fingerprint": "dfcc9f57d86bf58e321c2c6c31c7a971be244ac7",
"created_at": "2017-02-08T16:02:37Z",
"state": "verified",
"type": "custom"
},
{
"id": "992071a0-bb95-49bc-8021-3afd67a210bf",
"name": "web-cert-02",
"dns_names":["example.com"],
"not_after": "2017-02-22T00:23:00Z",
"sha1_fingerprint": "cfcc9f57d86bf58e321c2c6c31c7a971be244ac7",
"created_at": "2017-02-08T16:02:37Z",
"state": "pending",
"type": "lets_encrypt"
}
],
"links": [],
"meta": {
"total": 2
}
}
`
func TestCertificates_Get(t *testing.T) {
setup()
defer teardown()
urlStr := "/v2/certificates"
cID := "892071a0-bb95-49bc-8021-3afd67a210bf"
urlStr = path.Join(urlStr, cID)
mux.HandleFunc(urlStr, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, certJSONResponse)
})
certificate, _, err := client.Certificates.Get(ctx, cID)
require.NoError(t, err)
expected := &Certificate{
ID: "892071a0-bb95-49bc-8021-3afd67a210bf",
Name: "web-cert-01",
DNSNames: []string{"somedomain.com", "api.somedomain.com"},
NotAfter: "2017-02-22T00:23:00Z",
SHA1Fingerprint: "dfcc9f57d86bf58e321c2c6c31c7a971be244ac7",
Created: "2017-02-08T16:02:37Z",
State: "verified",
Type: "custom",
}
assert.Equal(t, expected, certificate)
}
func TestCertificates_List(t *testing.T) {
setup()
defer teardown()
urlStr := "/v2/certificates"
mux.HandleFunc(urlStr, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, certsJSONResponse)
})
certificates, resp, err := client.Certificates.List(ctx, nil)
require.NoError(t, err)
expectedCertificates := []Certificate{
{
ID: "892071a0-bb95-49bc-8021-3afd67a210bf",
Name: "web-cert-01",
DNSNames: []string{"somedomain.com", "api.somedomain.com"},
NotAfter: "2017-02-22T00:23:00Z",
SHA1Fingerprint: "dfcc9f57d86bf58e321c2c6c31c7a971be244ac7",
Created: "2017-02-08T16:02:37Z",
State: "verified",
Type: "custom",
},
{
ID: "992071a0-bb95-49bc-8021-3afd67a210bf",
Name: "web-cert-02",
DNSNames: []string{"example.com"},
NotAfter: "2017-02-22T00:23:00Z",
SHA1Fingerprint: "cfcc9f57d86bf58e321c2c6c31c7a971be244ac7",
Created: "2017-02-08T16:02:37Z",
State: "pending",
Type: "lets_encrypt",
},
}
assert.Equal(t, expectedCertificates, certificates)
expectedMeta := &Meta{
Total: 2,
}
assert.Equal(t, expectedMeta, resp.Meta)
}
func TestCertificates_Create(t *testing.T) {
tests := []struct {
desc string
createRequest *CertificateRequest
certJSONResponse string
expectedCertificate *Certificate
}{
{
desc: "creates custom certificate",
createRequest: &CertificateRequest{
Name: "web-cert-01",
PrivateKey: "-----BEGIN PRIVATE KEY-----",
LeafCertificate: "-----BEGIN CERTIFICATE-----",
CertificateChain: "-----BEGIN CERTIFICATE-----",
},
certJSONResponse: `{
"certificate": {
"id": "892071a0-bb95-49bc-8021-3afd67a210bf",
"name": "custom-cert",
"dns_names":[],
"not_after": "2017-02-22T00:23:00Z",
"sha1_fingerprint": "dfcc9f57d86bf58e321c2c6c31c7a971be244ac7",
"created_at": "2017-02-08T16:02:37Z",
"state": "verified",
"type": "custom"
}
}`,
expectedCertificate: &Certificate{
ID: "892071a0-bb95-49bc-8021-3afd67a210bf",
Name: "custom-cert",
DNSNames: []string{},
NotAfter: "2017-02-22T00:23:00Z",
SHA1Fingerprint: "dfcc9f57d86bf58e321c2c6c31c7a971be244ac7",
Created: "2017-02-08T16:02:37Z",
State: "verified",
Type: "custom",
},
},
{
desc: "creates let's encrypt certificate",
createRequest: &CertificateRequest{
Name: "lets-encrypt-cert",
DNSNames: []string{"example.com", "api.example.com"},
Type: "lets_encrypt",
},
certJSONResponse: `{
"certificate": {
"id": "91bce928-a983-4c97-a5ee-78c585bf798d",
"name": "lets-encrypt-cert",
"dns_names":["example.com", "api.example.com"],
"not_after": "2022-01-26T15:50:00Z",
"sha1_fingerprint": "2e3c2ba8016faf80f431700ff2865ef6dba30a81",
"created_at": "2017-08-23T20:42:46Z",
"state": "pending",
"type": "lets_encrypt"
}
}`,
expectedCertificate: &Certificate{
ID: "91bce928-a983-4c97-a5ee-78c585bf798d",
Name: "lets-encrypt-cert",
DNSNames: []string{"example.com", "api.example.com"},
NotAfter: "2022-01-26T15:50:00Z",
SHA1Fingerprint: "2e3c2ba8016faf80f431700ff2865ef6dba30a81",
Created: "2017-08-23T20:42:46Z",
State: "pending",
Type: "lets_encrypt",
},
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
setup()
defer teardown()
urlStr := "/v2/certificates"
mux.HandleFunc(urlStr, func(w http.ResponseWriter, r *http.Request) {
v := new(CertificateRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPost)
assert.Equal(t, test.createRequest, v)
fmt.Fprint(w, test.certJSONResponse)
})
certificate, _, err := client.Certificates.Create(ctx, test.createRequest)
require.NoError(t, err)
assert.Equal(t, test.expectedCertificate, certificate)
})
}
}
func TestCertificates_Delete(t *testing.T) {
setup()
defer teardown()
cID := "892071a0-bb95-49bc-8021-3afd67a210bf"
urlStr := "/v2/certificates"
urlStr = path.Join(urlStr, cID)
mux.HandleFunc(urlStr, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := client.Certificates.Delete(ctx, cID)
assert.NoError(t, err)
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,274 +0,0 @@
package godo
import (
"context"
"net/http"
"path"
"strconv"
)
const firewallsBasePath = "/v2/firewalls"
// FirewallsService is an interface for managing Firewalls with the DigitalOcean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Firewalls
type FirewallsService interface {
Get(context.Context, string) (*Firewall, *Response, error)
Create(context.Context, *FirewallRequest) (*Firewall, *Response, error)
Update(context.Context, string, *FirewallRequest) (*Firewall, *Response, error)
Delete(context.Context, string) (*Response, error)
List(context.Context, *ListOptions) ([]Firewall, *Response, error)
ListByDroplet(context.Context, int, *ListOptions) ([]Firewall, *Response, error)
AddDroplets(context.Context, string, ...int) (*Response, error)
RemoveDroplets(context.Context, string, ...int) (*Response, error)
AddTags(context.Context, string, ...string) (*Response, error)
RemoveTags(context.Context, string, ...string) (*Response, error)
AddRules(context.Context, string, *FirewallRulesRequest) (*Response, error)
RemoveRules(context.Context, string, *FirewallRulesRequest) (*Response, error)
}
// FirewallsServiceOp handles communication with Firewalls methods of the DigitalOcean API.
type FirewallsServiceOp struct {
client *Client
}
// Firewall represents a DigitalOcean Firewall configuration.
type Firewall struct {
ID string `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
InboundRules []InboundRule `json:"inbound_rules"`
OutboundRules []OutboundRule `json:"outbound_rules"`
DropletIDs []int `json:"droplet_ids"`
Tags []string `json:"tags"`
Created string `json:"created_at"`
PendingChanges []PendingChange `json:"pending_changes"`
}
// String creates a human-readable description of a Firewall.
func (fw Firewall) String() string {
return Stringify(fw)
}
// URN returns the firewall name in a valid DO API URN form.
func (fw Firewall) URN() string {
return ToURN("Firewall", fw.ID)
}
// FirewallRequest represents the configuration to be applied to an existing or a new Firewall.
type FirewallRequest struct {
Name string `json:"name"`
InboundRules []InboundRule `json:"inbound_rules"`
OutboundRules []OutboundRule `json:"outbound_rules"`
DropletIDs []int `json:"droplet_ids"`
Tags []string `json:"tags"`
}
// FirewallRulesRequest represents rules configuration to be applied to an existing Firewall.
type FirewallRulesRequest struct {
InboundRules []InboundRule `json:"inbound_rules"`
OutboundRules []OutboundRule `json:"outbound_rules"`
}
// InboundRule represents a DigitalOcean Firewall inbound rule.
type InboundRule struct {
Protocol string `json:"protocol,omitempty"`
PortRange string `json:"ports,omitempty"`
Sources *Sources `json:"sources"`
}
// OutboundRule represents a DigitalOcean Firewall outbound rule.
type OutboundRule struct {
Protocol string `json:"protocol,omitempty"`
PortRange string `json:"ports,omitempty"`
Destinations *Destinations `json:"destinations"`
}
// Sources represents a DigitalOcean Firewall InboundRule sources.
type Sources struct {
Addresses []string `json:"addresses,omitempty"`
Tags []string `json:"tags,omitempty"`
DropletIDs []int `json:"droplet_ids,omitempty"`
LoadBalancerUIDs []string `json:"load_balancer_uids,omitempty"`
KubernetesIDs []string `json:"kubernetes_ids,omitempty"`
}
// PendingChange represents a DigitalOcean Firewall status details.
type PendingChange struct {
DropletID int `json:"droplet_id,omitempty"`
Removing bool `json:"removing,omitempty"`
Status string `json:"status,omitempty"`
}
// Destinations represents a DigitalOcean Firewall OutboundRule destinations.
type Destinations struct {
Addresses []string `json:"addresses,omitempty"`
Tags []string `json:"tags,omitempty"`
DropletIDs []int `json:"droplet_ids,omitempty"`
LoadBalancerUIDs []string `json:"load_balancer_uids,omitempty"`
KubernetesIDs []string `json:"kubernetes_ids,omitempty"`
}
var _ FirewallsService = &FirewallsServiceOp{}
// Get an existing Firewall by its identifier.
func (fw *FirewallsServiceOp) Get(ctx context.Context, fID string) (*Firewall, *Response, error) {
path := path.Join(firewallsBasePath, fID)
req, err := fw.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(firewallRoot)
resp, err := fw.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Firewall, resp, err
}
// Create a new Firewall with a given configuration.
func (fw *FirewallsServiceOp) Create(ctx context.Context, fr *FirewallRequest) (*Firewall, *Response, error) {
req, err := fw.client.NewRequest(ctx, http.MethodPost, firewallsBasePath, fr)
if err != nil {
return nil, nil, err
}
root := new(firewallRoot)
resp, err := fw.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Firewall, resp, err
}
// Update an existing Firewall with new configuration.
func (fw *FirewallsServiceOp) Update(ctx context.Context, fID string, fr *FirewallRequest) (*Firewall, *Response, error) {
path := path.Join(firewallsBasePath, fID)
req, err := fw.client.NewRequest(ctx, "PUT", path, fr)
if err != nil {
return nil, nil, err
}
root := new(firewallRoot)
resp, err := fw.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Firewall, resp, err
}
// Delete a Firewall by its identifier.
func (fw *FirewallsServiceOp) Delete(ctx context.Context, fID string) (*Response, error) {
path := path.Join(firewallsBasePath, fID)
return fw.createAndDoReq(ctx, http.MethodDelete, path, nil)
}
// List Firewalls.
func (fw *FirewallsServiceOp) List(ctx context.Context, opt *ListOptions) ([]Firewall, *Response, error) {
path, err := addOptions(firewallsBasePath, opt)
if err != nil {
return nil, nil, err
}
return fw.listHelper(ctx, path)
}
// ListByDroplet Firewalls.
func (fw *FirewallsServiceOp) ListByDroplet(ctx context.Context, dID int, opt *ListOptions) ([]Firewall, *Response, error) {
basePath := path.Join(dropletBasePath, strconv.Itoa(dID), "firewalls")
path, err := addOptions(basePath, opt)
if err != nil {
return nil, nil, err
}
return fw.listHelper(ctx, path)
}
// AddDroplets to a Firewall.
func (fw *FirewallsServiceOp) AddDroplets(ctx context.Context, fID string, dropletIDs ...int) (*Response, error) {
path := path.Join(firewallsBasePath, fID, "droplets")
return fw.createAndDoReq(ctx, http.MethodPost, path, &dropletsRequest{IDs: dropletIDs})
}
// RemoveDroplets from a Firewall.
func (fw *FirewallsServiceOp) RemoveDroplets(ctx context.Context, fID string, dropletIDs ...int) (*Response, error) {
path := path.Join(firewallsBasePath, fID, "droplets")
return fw.createAndDoReq(ctx, http.MethodDelete, path, &dropletsRequest{IDs: dropletIDs})
}
// AddTags to a Firewall.
func (fw *FirewallsServiceOp) AddTags(ctx context.Context, fID string, tags ...string) (*Response, error) {
path := path.Join(firewallsBasePath, fID, "tags")
return fw.createAndDoReq(ctx, http.MethodPost, path, &tagsRequest{Tags: tags})
}
// RemoveTags from a Firewall.
func (fw *FirewallsServiceOp) RemoveTags(ctx context.Context, fID string, tags ...string) (*Response, error) {
path := path.Join(firewallsBasePath, fID, "tags")
return fw.createAndDoReq(ctx, http.MethodDelete, path, &tagsRequest{Tags: tags})
}
// AddRules to a Firewall.
func (fw *FirewallsServiceOp) AddRules(ctx context.Context, fID string, rr *FirewallRulesRequest) (*Response, error) {
path := path.Join(firewallsBasePath, fID, "rules")
return fw.createAndDoReq(ctx, http.MethodPost, path, rr)
}
// RemoveRules from a Firewall.
func (fw *FirewallsServiceOp) RemoveRules(ctx context.Context, fID string, rr *FirewallRulesRequest) (*Response, error) {
path := path.Join(firewallsBasePath, fID, "rules")
return fw.createAndDoReq(ctx, http.MethodDelete, path, rr)
}
type dropletsRequest struct {
IDs []int `json:"droplet_ids"`
}
type tagsRequest struct {
Tags []string `json:"tags"`
}
type firewallRoot struct {
Firewall *Firewall `json:"firewall"`
}
type firewallsRoot struct {
Firewalls []Firewall `json:"firewalls"`
Links []*LinkAction `json:"links"`
Meta *Meta `json:"meta"`
}
func (fw *FirewallsServiceOp) createAndDoReq(ctx context.Context, method, path string, v interface{}) (*Response, error) {
req, err := fw.client.NewRequest(ctx, method, path, v)
if err != nil {
return nil, err
}
return fw.client.Do(ctx, req, nil)
}
func (fw *FirewallsServiceOp) listHelper(ctx context.Context, path string) ([]Firewall, *Response, error) {
req, err := fw.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(firewallsRoot)
resp, err := fw.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Firewalls, resp, err
}

View File

@ -1,849 +0,0 @@
package godo
import (
"encoding/json"
"fmt"
"net/http"
"path"
"reflect"
"testing"
)
var (
firewallCreateJSONBody = `
{
"name": "f-i-r-e-w-a-l-l",
"inbound_rules": [
{
"protocol": "icmp",
"sources": {
"addresses": ["0.0.0.0/0"],
"tags": ["frontend"],
"droplet_ids": [123, 456],
"load_balancer_uids": ["lb-uid"],
"kubernetes_ids": ["doks-01", "doks-02"]
}
},
{
"protocol": "tcp",
"ports": "8000-9000",
"sources": {
"addresses": ["0.0.0.0/0"]
}
}
],
"outbound_rules": [
{
"protocol": "icmp",
"destinations": {
"tags": ["frontend"]
}
},
{
"protocol": "tcp",
"ports": "8000-9000",
"destinations": {
"addresses": ["::/1"]
}
}
],
"droplet_ids": [123],
"tags": ["frontend"]
}
`
firewallRulesJSONBody = `
{
"inbound_rules": [
{
"protocol": "tcp",
"ports": "22",
"sources": {
"addresses": ["0.0.0.0/0"]
}
}
],
"outbound_rules": [
{
"protocol": "tcp",
"ports": "443",
"destinations": {
"addresses": ["0.0.0.0/0"]
}
}
]
}
`
firewallUpdateJSONBody = `
{
"name": "f-i-r-e-w-a-l-l",
"inbound_rules": [
{
"protocol": "tcp",
"ports": "443",
"sources": {
"addresses": ["10.0.0.0/8"]
}
}
],
"droplet_ids": [123],
"tags": []
}
`
firewallUpdateJSONResponse = `
{
"firewall": {
"id": "fe6b88f2-b42b-4bf7-bbd3-5ae20208f0b0",
"name": "f-i-r-e-w-a-l-l",
"inbound_rules": [
{
"protocol": "tcp",
"ports": "443",
"sources": {
"addresses": ["10.0.0.0/8"]
}
}
],
"outbound_rules": [],
"created_at": "2017-04-06T13:07:27Z",
"droplet_ids": [
123
],
"tags": []
}
}
`
firewallJSONResponse = `
{
"firewall": {
"id": "fe6b88f2-b42b-4bf7-bbd3-5ae20208f0b0",
"name": "f-i-r-e-w-a-l-l",
"status": "waiting",
"inbound_rules": [
{
"protocol": "icmp",
"ports": "0",
"sources": {
"tags": ["frontend"]
}
},
{
"protocol": "tcp",
"ports": "8000-9000",
"sources": {
"addresses": ["0.0.0.0/0"]
}
}
],
"outbound_rules": [
{
"protocol": "icmp",
"ports": "0"
},
{
"protocol": "tcp",
"ports": "8000-9000",
"destinations": {
"addresses": ["::/1"]
}
}
],
"created_at": "2017-04-06T13:07:27Z",
"droplet_ids": [
123
],
"tags": [
"frontend"
],
"pending_changes": [
{
"droplet_id": 123,
"removing": false,
"status": "waiting"
}
]
}
}
`
firewallListJSONResponse = `
{
"firewalls": [
{
"id": "fe6b88f2-b42b-4bf7-bbd3-5ae20208f0b0",
"name": "f-i-r-e-w-a-l-l",
"inbound_rules": [
{
"protocol": "icmp",
"ports": "0",
"sources": {
"tags": ["frontend"]
}
},
{
"protocol": "tcp",
"ports": "8000-9000",
"sources": {
"addresses": ["0.0.0.0/0"]
}
}
],
"outbound_rules": [
{
"protocol": "icmp",
"ports": "0"
},
{
"protocol": "tcp",
"ports": "8000-9000",
"destinations": {
"addresses": ["::/1"]
}
}
],
"created_at": "2017-04-06T13:07:27Z",
"droplet_ids": [
123
],
"tags": [
"frontend"
]
}
],
"links": [],
"meta": {
"total": 1
}
}
`
)
func TestFirewalls_Get(t *testing.T) {
setup()
defer teardown()
urlStr := "/v2/firewalls"
fID := "fe6b88f2-b42b-4bf7-bbd3-5ae20208f0b0"
urlStr = path.Join(urlStr, fID)
mux.HandleFunc(urlStr, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, firewallJSONResponse)
})
actualFirewall, _, err := client.Firewalls.Get(ctx, fID)
if err != nil {
t.Errorf("Firewalls.Get returned error: %v", err)
}
expectedFirewall := &Firewall{
ID: "fe6b88f2-b42b-4bf7-bbd3-5ae20208f0b0",
Name: "f-i-r-e-w-a-l-l",
Status: "waiting",
InboundRules: []InboundRule{
{
Protocol: "icmp",
PortRange: "0",
Sources: &Sources{
Tags: []string{"frontend"},
},
},
{
Protocol: "tcp",
PortRange: "8000-9000",
Sources: &Sources{
Addresses: []string{"0.0.0.0/0"},
},
},
},
OutboundRules: []OutboundRule{
{
Protocol: "icmp",
PortRange: "0",
},
{
Protocol: "tcp",
PortRange: "8000-9000",
Destinations: &Destinations{
Addresses: []string{"::/1"},
},
},
},
Created: "2017-04-06T13:07:27Z",
DropletIDs: []int{123},
Tags: []string{"frontend"},
PendingChanges: []PendingChange{
{
DropletID: 123,
Removing: false,
Status: "waiting",
},
},
}
if !reflect.DeepEqual(actualFirewall, expectedFirewall) {
t.Errorf("Firewalls.Get returned %+v, expected %+v", actualFirewall, expectedFirewall)
}
}
func TestFirewalls_Create(t *testing.T) {
setup()
defer teardown()
expectedFirewallRequest := &FirewallRequest{
Name: "f-i-r-e-w-a-l-l",
InboundRules: []InboundRule{
{
Protocol: "icmp",
Sources: &Sources{
Addresses: []string{"0.0.0.0/0"},
Tags: []string{"frontend"},
DropletIDs: []int{123, 456},
LoadBalancerUIDs: []string{"lb-uid"},
KubernetesIDs: []string{"doks-01", "doks-02"},
},
},
{
Protocol: "tcp",
PortRange: "8000-9000",
Sources: &Sources{
Addresses: []string{"0.0.0.0/0"},
},
},
},
OutboundRules: []OutboundRule{
{
Protocol: "icmp",
Destinations: &Destinations{
Tags: []string{"frontend"},
},
},
{
Protocol: "tcp",
PortRange: "8000-9000",
Destinations: &Destinations{
Addresses: []string{"::/1"},
},
},
},
DropletIDs: []int{123},
Tags: []string{"frontend"},
}
mux.HandleFunc("/v2/firewalls", func(w http.ResponseWriter, r *http.Request) {
v := new(FirewallRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, expectedFirewallRequest) {
t.Errorf("Request body = %+v, expected %+v", v, expectedFirewallRequest)
}
var actualFirewallRequest *FirewallRequest
json.Unmarshal([]byte(firewallCreateJSONBody), &actualFirewallRequest)
if !reflect.DeepEqual(actualFirewallRequest, expectedFirewallRequest) {
t.Errorf("Request body = %+v, expected %+v", actualFirewallRequest, expectedFirewallRequest)
}
fmt.Fprint(w, firewallJSONResponse)
})
actualFirewall, _, err := client.Firewalls.Create(ctx, expectedFirewallRequest)
if err != nil {
t.Errorf("Firewalls.Create returned error: %v", err)
}
expectedFirewall := &Firewall{
ID: "fe6b88f2-b42b-4bf7-bbd3-5ae20208f0b0",
Name: "f-i-r-e-w-a-l-l",
Status: "waiting",
InboundRules: []InboundRule{
{
Protocol: "icmp",
PortRange: "0",
Sources: &Sources{
Tags: []string{"frontend"},
},
},
{
Protocol: "tcp",
PortRange: "8000-9000",
Sources: &Sources{
Addresses: []string{"0.0.0.0/0"},
},
},
},
OutboundRules: []OutboundRule{
{
Protocol: "icmp",
PortRange: "0",
},
{
Protocol: "tcp",
PortRange: "8000-9000",
Destinations: &Destinations{
Addresses: []string{"::/1"},
},
},
},
Created: "2017-04-06T13:07:27Z",
DropletIDs: []int{123},
Tags: []string{"frontend"},
PendingChanges: []PendingChange{
{
DropletID: 123,
Removing: false,
Status: "waiting",
},
},
}
if !reflect.DeepEqual(actualFirewall, expectedFirewall) {
t.Errorf("Firewalls.Create returned %+v, expected %+v", actualFirewall, expectedFirewall)
}
}
func TestFirewalls_Update(t *testing.T) {
setup()
defer teardown()
expectedFirewallRequest := &FirewallRequest{
Name: "f-i-r-e-w-a-l-l",
InboundRules: []InboundRule{
{
Protocol: "tcp",
PortRange: "443",
Sources: &Sources{
Addresses: []string{"10.0.0.0/8"},
},
},
},
DropletIDs: []int{123},
Tags: []string{},
}
urlStr := "/v2/firewalls"
fID := "fe6b88f2-b42b-4bf7-bbd3-5ae20208f0b0"
urlStr = path.Join(urlStr, fID)
mux.HandleFunc(urlStr, func(w http.ResponseWriter, r *http.Request) {
v := new(FirewallRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, "PUT")
if !reflect.DeepEqual(v, expectedFirewallRequest) {
t.Errorf("Request body = %+v, expected %+v", v, expectedFirewallRequest)
}
var actualFirewallRequest *FirewallRequest
json.Unmarshal([]byte(firewallUpdateJSONBody), &actualFirewallRequest)
if !reflect.DeepEqual(actualFirewallRequest, expectedFirewallRequest) {
t.Errorf("Request body = %+v, expected %+v", actualFirewallRequest, expectedFirewallRequest)
}
fmt.Fprint(w, firewallUpdateJSONResponse)
})
actualFirewall, _, err := client.Firewalls.Update(ctx, fID, expectedFirewallRequest)
if err != nil {
t.Errorf("Firewalls.Update returned error: %v", err)
}
expectedFirewall := &Firewall{
ID: "fe6b88f2-b42b-4bf7-bbd3-5ae20208f0b0",
Name: "f-i-r-e-w-a-l-l",
InboundRules: []InboundRule{
{
Protocol: "tcp",
PortRange: "443",
Sources: &Sources{
Addresses: []string{"10.0.0.0/8"},
},
},
},
OutboundRules: []OutboundRule{},
Created: "2017-04-06T13:07:27Z",
DropletIDs: []int{123},
Tags: []string{},
}
if !reflect.DeepEqual(actualFirewall, expectedFirewall) {
t.Errorf("Firewalls.Update returned %+v, expected %+v", actualFirewall, expectedFirewall)
}
}
func TestFirewalls_Delete(t *testing.T) {
setup()
defer teardown()
urlStr := "/v2/firewalls"
fID := "fe6b88f2-b42b-4bf7-bbd3-5ae20208f0b0"
urlStr = path.Join(urlStr, fID)
mux.HandleFunc(urlStr, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := client.Firewalls.Delete(ctx, fID)
if err != nil {
t.Errorf("Firewalls.Delete returned error: %v", err)
}
}
func TestFirewalls_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/firewalls", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, firewallListJSONResponse)
})
actualFirewalls, resp, err := client.Firewalls.List(ctx, nil)
if err != nil {
t.Errorf("Firewalls.List returned error: %v", err)
}
expectedFirewalls := makeExpectedFirewalls()
if !reflect.DeepEqual(actualFirewalls, expectedFirewalls) {
t.Errorf("Firewalls.List returned firewalls %+v, expected %+v", actualFirewalls, expectedFirewalls)
}
expectedMeta := &Meta{Total: 1}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Firewalls.List returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}
func TestFirewalls_ListByDroplet(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/droplets/123/firewalls", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, firewallListJSONResponse)
})
actualFirewalls, resp, err := client.Firewalls.ListByDroplet(ctx, 123, nil)
if err != nil {
t.Errorf("Firewalls.List returned error: %v", err)
}
expectedFirewalls := makeExpectedFirewalls()
if !reflect.DeepEqual(actualFirewalls, expectedFirewalls) {
t.Errorf("Firewalls.List returned firewalls %+v, expected %+v", actualFirewalls, expectedFirewalls)
}
expectedMeta := &Meta{Total: 1}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Firewalls.List returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}
func TestFirewalls_AddDroplets(t *testing.T) {
setup()
defer teardown()
dRequest := &dropletsRequest{
IDs: []int{123},
}
fID := "fe6b88f2-b42b-4bf7-bbd3-5ae20208f0b0"
urlStr := path.Join("/v2/firewalls", fID, "droplets")
mux.HandleFunc(urlStr, func(w http.ResponseWriter, r *http.Request) {
v := new(dropletsRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, dRequest) {
t.Errorf("Request body = %+v, expected %+v", v, dRequest)
}
expectedJSONBody := `{"droplet_ids": [123]}`
var actualDropletsRequest *dropletsRequest
json.Unmarshal([]byte(expectedJSONBody), &actualDropletsRequest)
if !reflect.DeepEqual(actualDropletsRequest, dRequest) {
t.Errorf("Request body = %+v, expected %+v", actualDropletsRequest, dRequest)
}
fmt.Fprint(w, nil)
})
_, err := client.Firewalls.AddDroplets(ctx, fID, dRequest.IDs...)
if err != nil {
t.Errorf("Firewalls.AddDroplets returned error: %v", err)
}
}
func TestFirewalls_RemoveDroplets(t *testing.T) {
setup()
defer teardown()
dRequest := &dropletsRequest{
IDs: []int{123, 345},
}
fID := "fe6b88f2-b42b-4bf7-bbd3-5ae20208f0b0"
urlStr := path.Join("/v2/firewalls", fID, "droplets")
mux.HandleFunc(urlStr, func(w http.ResponseWriter, r *http.Request) {
v := new(dropletsRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodDelete)
if !reflect.DeepEqual(v, dRequest) {
t.Errorf("Request body = %+v, expected %+v", v, dRequest)
}
expectedJSONBody := `{"droplet_ids": [123, 345]}`
var actualDropletsRequest *dropletsRequest
json.Unmarshal([]byte(expectedJSONBody), &actualDropletsRequest)
if !reflect.DeepEqual(actualDropletsRequest, dRequest) {
t.Errorf("Request body = %+v, expected %+v", actualDropletsRequest, dRequest)
}
fmt.Fprint(w, nil)
})
_, err := client.Firewalls.RemoveDroplets(ctx, fID, dRequest.IDs...)
if err != nil {
t.Errorf("Firewalls.RemoveDroplets returned error: %v", err)
}
}
func TestFirewalls_AddTags(t *testing.T) {
setup()
defer teardown()
tRequest := &tagsRequest{
Tags: []string{"frontend"},
}
fID := "fe6b88f2-b42b-4bf7-bbd3-5ae20208f0b0"
urlStr := path.Join("/v2/firewalls", fID, "tags")
mux.HandleFunc(urlStr, func(w http.ResponseWriter, r *http.Request) {
v := new(tagsRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, tRequest) {
t.Errorf("Request body = %+v, expected %+v", v, tRequest)
}
var actualTagsRequest *tagsRequest
json.Unmarshal([]byte(`{"tags": ["frontend"]}`), &actualTagsRequest)
if !reflect.DeepEqual(actualTagsRequest, tRequest) {
t.Errorf("Request body = %+v, expected %+v", actualTagsRequest, tRequest)
}
fmt.Fprint(w, nil)
})
_, err := client.Firewalls.AddTags(ctx, fID, tRequest.Tags...)
if err != nil {
t.Errorf("Firewalls.AddTags returned error: %v", err)
}
}
func TestFirewalls_RemoveTags(t *testing.T) {
setup()
defer teardown()
tRequest := &tagsRequest{
Tags: []string{"frontend", "backend"},
}
fID := "fe6b88f2-b42b-4bf7-bbd3-5ae20208f0b0"
urlStr := path.Join("/v2/firewalls", fID, "tags")
mux.HandleFunc(urlStr, func(w http.ResponseWriter, r *http.Request) {
v := new(tagsRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodDelete)
if !reflect.DeepEqual(v, tRequest) {
t.Errorf("Request body = %+v, expected %+v", v, tRequest)
}
var actualTagsRequest *tagsRequest
json.Unmarshal([]byte(`{"tags": ["frontend", "backend"]}`), &actualTagsRequest)
if !reflect.DeepEqual(actualTagsRequest, tRequest) {
t.Errorf("Request body = %+v, expected %+v", actualTagsRequest, tRequest)
}
fmt.Fprint(w, nil)
})
_, err := client.Firewalls.RemoveTags(ctx, fID, tRequest.Tags...)
if err != nil {
t.Errorf("Firewalls.RemoveTags returned error: %v", err)
}
}
func TestFirewalls_AddRules(t *testing.T) {
setup()
defer teardown()
rr := &FirewallRulesRequest{
InboundRules: []InboundRule{
{
Protocol: "tcp",
PortRange: "22",
Sources: &Sources{
Addresses: []string{"0.0.0.0/0"},
},
},
},
OutboundRules: []OutboundRule{
{
Protocol: "tcp",
PortRange: "443",
Destinations: &Destinations{
Addresses: []string{"0.0.0.0/0"},
},
},
},
}
fID := "fe6b88f2-b42b-4bf7-bbd3-5ae20208f0b0"
urlStr := path.Join("/v2/firewalls", fID, "rules")
mux.HandleFunc(urlStr, func(w http.ResponseWriter, r *http.Request) {
v := new(FirewallRulesRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, rr) {
t.Errorf("Request body = %+v, expected %+v", v, rr)
}
var actualFirewallRulesRequest *FirewallRulesRequest
json.Unmarshal([]byte(firewallRulesJSONBody), &actualFirewallRulesRequest)
if !reflect.DeepEqual(actualFirewallRulesRequest, rr) {
t.Errorf("Request body = %+v, expected %+v", actualFirewallRulesRequest, rr)
}
fmt.Fprint(w, nil)
})
_, err := client.Firewalls.AddRules(ctx, fID, rr)
if err != nil {
t.Errorf("Firewalls.AddRules returned error: %v", err)
}
}
func TestFirewalls_RemoveRules(t *testing.T) {
setup()
defer teardown()
rr := &FirewallRulesRequest{
InboundRules: []InboundRule{
{
Protocol: "tcp",
PortRange: "22",
Sources: &Sources{
Addresses: []string{"0.0.0.0/0"},
},
},
},
OutboundRules: []OutboundRule{
{
Protocol: "tcp",
PortRange: "443",
Destinations: &Destinations{
Addresses: []string{"0.0.0.0/0"},
},
},
},
}
fID := "fe6b88f2-b42b-4bf7-bbd3-5ae20208f0b0"
urlStr := path.Join("/v2/firewalls", fID, "rules")
mux.HandleFunc(urlStr, func(w http.ResponseWriter, r *http.Request) {
v := new(FirewallRulesRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodDelete)
if !reflect.DeepEqual(v, rr) {
t.Errorf("Request body = %+v, expected %+v", v, rr)
}
var actualFirewallRulesRequest *FirewallRulesRequest
json.Unmarshal([]byte(firewallRulesJSONBody), &actualFirewallRulesRequest)
if !reflect.DeepEqual(actualFirewallRulesRequest, rr) {
t.Errorf("Request body = %+v, expected %+v", actualFirewallRulesRequest, rr)
}
fmt.Fprint(w, nil)
})
_, err := client.Firewalls.RemoveRules(ctx, fID, rr)
if err != nil {
t.Errorf("Firewalls.RemoveRules returned error: %v", err)
}
}
func makeExpectedFirewalls() []Firewall {
return []Firewall{
{
ID: "fe6b88f2-b42b-4bf7-bbd3-5ae20208f0b0",
Name: "f-i-r-e-w-a-l-l",
InboundRules: []InboundRule{
{
Protocol: "icmp",
PortRange: "0",
Sources: &Sources{
Tags: []string{"frontend"},
},
},
{
Protocol: "tcp",
PortRange: "8000-9000",
Sources: &Sources{
Addresses: []string{"0.0.0.0/0"},
},
},
},
OutboundRules: []OutboundRule{
{
Protocol: "icmp",
PortRange: "0",
},
{
Protocol: "tcp",
PortRange: "8000-9000",
Destinations: &Destinations{
Addresses: []string{"::/1"},
},
},
},
DropletIDs: []int{123},
Tags: []string{"frontend"},
Created: "2017-04-06T13:07:27Z",
},
}
}

View File

@ -1,144 +0,0 @@
package godo
import (
"context"
"fmt"
"net/http"
)
const floatingBasePath = "v2/floating_ips"
// FloatingIPsService is an interface for interfacing with the floating IPs
// endpoints of the Digital Ocean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Floating-IPs
type FloatingIPsService interface {
List(context.Context, *ListOptions) ([]FloatingIP, *Response, error)
Get(context.Context, string) (*FloatingIP, *Response, error)
Create(context.Context, *FloatingIPCreateRequest) (*FloatingIP, *Response, error)
Delete(context.Context, string) (*Response, error)
}
// FloatingIPsServiceOp handles communication with the floating IPs related methods of the
// DigitalOcean API.
type FloatingIPsServiceOp struct {
client *Client
}
var _ FloatingIPsService = &FloatingIPsServiceOp{}
// FloatingIP represents a Digital Ocean floating IP.
type FloatingIP struct {
Region *Region `json:"region"`
Droplet *Droplet `json:"droplet"`
IP string `json:"ip"`
}
func (f FloatingIP) String() string {
return Stringify(f)
}
// URN returns the floating IP in a valid DO API URN form.
func (f FloatingIP) URN() string {
return ToURN("FloatingIP", f.IP)
}
type floatingIPsRoot struct {
FloatingIPs []FloatingIP `json:"floating_ips"`
Links []*LinkAction `json:"links"`
Meta *Meta `json:"meta"`
}
type floatingIPRoot struct {
FloatingIP *FloatingIP `json:"floating_ip"`
Links []*LinkAction `json:"links,omitempty"`
}
// FloatingIPCreateRequest represents a request to create a floating IP.
// Specify DropletID to assign the floating IP to a Droplet or Region
// to reserve it to the region.
type FloatingIPCreateRequest struct {
Region string `json:"region,omitempty"`
DropletID int `json:"droplet_id,omitempty"`
}
// List all floating IPs.
func (f *FloatingIPsServiceOp) List(ctx context.Context, opt *ListOptions) ([]FloatingIP, *Response, error) {
path := floatingBasePath
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
req, err := f.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(floatingIPsRoot)
resp, err := f.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.FloatingIPs, resp, err
}
// Get an individual floating IP.
func (f *FloatingIPsServiceOp) Get(ctx context.Context, ip string) (*FloatingIP, *Response, error) {
path := fmt.Sprintf("%s/%s", floatingBasePath, ip)
req, err := f.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(floatingIPRoot)
resp, err := f.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.FloatingIP, resp, err
}
// Create a floating IP. If the DropletID field of the request is not empty,
// the floating IP will also be assigned to the droplet.
func (f *FloatingIPsServiceOp) Create(ctx context.Context, createRequest *FloatingIPCreateRequest) (*FloatingIP, *Response, error) {
path := floatingBasePath
req, err := f.client.NewRequest(ctx, http.MethodPost, path, createRequest)
if err != nil {
return nil, nil, err
}
root := new(floatingIPRoot)
resp, err := f.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
return root.FloatingIP, resp, err
}
// Delete a floating IP.
func (f *FloatingIPsServiceOp) Delete(ctx context.Context, ip string) (*Response, error) {
path := fmt.Sprintf("%s/%s", floatingBasePath, ip)
req, err := f.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := f.client.Do(ctx, req, nil)
return resp, err
}

View File

@ -1,109 +0,0 @@
package godo
import (
"context"
"fmt"
"net/http"
)
// FloatingIPActionsService is an interface for interfacing with the
// floating IPs actions endpoints of the Digital Ocean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Floating-IP-Actions
type FloatingIPActionsService interface {
Assign(ctx context.Context, ip string, dropletID int) (*Action, *Response, error)
Unassign(ctx context.Context, ip string) (*Action, *Response, error)
Get(ctx context.Context, ip string, actionID int) (*Action, *Response, error)
List(ctx context.Context, ip string, opt *ListOptions) ([]Action, *Response, error)
}
// FloatingIPActionsServiceOp handles communication with the floating IPs
// action related methods of the DigitalOcean API.
type FloatingIPActionsServiceOp struct {
client *Client
}
// Assign a floating IP to a droplet.
func (s *FloatingIPActionsServiceOp) Assign(ctx context.Context, ip string, dropletID int) (*Action, *Response, error) {
request := &ActionRequest{
"type": "assign",
"droplet_id": dropletID,
}
return s.doAction(ctx, ip, request)
}
// Unassign a floating IP from the droplet it is currently assigned to.
func (s *FloatingIPActionsServiceOp) Unassign(ctx context.Context, ip string) (*Action, *Response, error) {
request := &ActionRequest{"type": "unassign"}
return s.doAction(ctx, ip, request)
}
// Get an action for a particular floating IP by id.
func (s *FloatingIPActionsServiceOp) Get(ctx context.Context, ip string, actionID int) (*Action, *Response, error) {
path := fmt.Sprintf("%s/%d", floatingIPActionPath(ip), actionID)
return s.get(ctx, path)
}
// List the actions for a particular floating IP.
func (s *FloatingIPActionsServiceOp) List(ctx context.Context, ip string, opt *ListOptions) ([]Action, *Response, error) {
path := floatingIPActionPath(ip)
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
return s.list(ctx, path)
}
func (s *FloatingIPActionsServiceOp) doAction(ctx context.Context, ip string, request *ActionRequest) (*Action, *Response, error) {
path := floatingIPActionPath(ip)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, request)
if err != nil {
return nil, nil, err
}
root := new(actionRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Event, resp, err
}
func (s *FloatingIPActionsServiceOp) get(ctx context.Context, path string) (*Action, *Response, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(actionRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Event, resp, err
}
func (s *FloatingIPActionsServiceOp) list(ctx context.Context, path string) ([]Action, *Response, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(actionsRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
return root.Actions, resp, err
}
func floatingIPActionPath(ip string) string {
return fmt.Sprintf("%s/%s/actions", floatingBasePath, ip)
}

View File

@ -1,156 +0,0 @@
package godo
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestFloatingIPsActions_Assign(t *testing.T) {
setup()
defer teardown()
dropletID := 12345
assignRequest := &ActionRequest{
"droplet_id": float64(dropletID), // encoding/json decodes numbers as floats
"type": "assign",
}
mux.HandleFunc("/v2/floating_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, assignRequest) {
t.Errorf("Request body = %#v, expected %#v", v, assignRequest)
}
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})
assign, _, err := client.FloatingIPActions.Assign(ctx, "192.168.0.1", 12345)
if err != nil {
t.Errorf("FloatingIPsActions.Assign returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(assign, expected) {
t.Errorf("FloatingIPsActions.Assign returned %+v, expected %+v", assign, expected)
}
}
func TestFloatingIPsActions_Unassign(t *testing.T) {
setup()
defer teardown()
unassignRequest := &ActionRequest{
"type": "unassign",
}
mux.HandleFunc("/v2/floating_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, unassignRequest) {
t.Errorf("Request body = %+v, expected %+v", v, unassignRequest)
}
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})
action, _, err := client.FloatingIPActions.Unassign(ctx, "192.168.0.1")
if err != nil {
t.Errorf("FloatingIPsActions.Get returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(action, expected) {
t.Errorf("FloatingIPsActions.Get returned %+v, expected %+v", action, expected)
}
}
func TestFloatingIPsActions_Get(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/floating_ips/192.168.0.1/actions/456", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})
action, _, err := client.FloatingIPActions.Get(ctx, "192.168.0.1", 456)
if err != nil {
t.Errorf("FloatingIPsActions.Get returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(action, expected) {
t.Errorf("FloatingIPsActions.Get returned %+v, expected %+v", action, expected)
}
}
func TestFloatingIPsActions_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/floating_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprintf(w, `{"actions":[{"status":"in-progress"}]}`)
})
actions, _, err := client.FloatingIPActions.List(ctx, "192.168.0.1", nil)
if err != nil {
t.Errorf("FloatingIPsActions.List returned error: %v", err)
}
expected := []Action{{Status: "in-progress"}}
if !reflect.DeepEqual(actions, expected) {
t.Errorf("FloatingIPsActions.List returned %+v, expected %+v", actions, expected)
}
}
func TestFloatingIPsActions_ListMultiplePages(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/floating_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"actions":[{"status":"in-progress"}], "links":[]}`)
})
_, _, err := client.FloatingIPActions.List(ctx, "192.168.0.1", nil)
if err != nil {
t.Errorf("FloatingIPsActions.List returned error: %v", err)
}
}
func TestFloatingIPsActions_ListPageByNumber(t *testing.T) {
setup()
defer teardown()
jBlob := `
{
"actions":[{"status":"in-progress"}],
"links":[]
}`
mux.HandleFunc("/v2/floating_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})
opt := &ListOptions{Page: 2}
_, _, err := client.FloatingIPActions.List(ctx, "192.168.0.1", opt)
if err != nil {
t.Errorf("FloatingIPsActions.List returned error: %v", err)
}
}

View File

@ -1,145 +0,0 @@
package godo
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestFloatingIPs_ListFloatingIPs(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/floating_ips", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"floating_ips": [{"region":{"slug":"nyc3"},"droplet":{"id":1},"ip":"192.168.0.1"},{"region":{"slug":"nyc3"},"droplet":{"id":2},"ip":"192.168.0.2"}],"meta":{"total":2}}`)
})
floatingIPs, resp, err := client.FloatingIPs.List(ctx, nil)
if err != nil {
t.Errorf("FloatingIPs.List returned error: %v", err)
}
expectedFloatingIPs := []FloatingIP{
{Region: &Region{Slug: "nyc3"}, Droplet: &Droplet{ID: 1}, IP: "192.168.0.1"},
{Region: &Region{Slug: "nyc3"}, Droplet: &Droplet{ID: 2}, IP: "192.168.0.2"},
}
if !reflect.DeepEqual(floatingIPs, expectedFloatingIPs) {
t.Errorf("FloatingIPs.List returned floating IPs %+v, expected %+v", floatingIPs, expectedFloatingIPs)
}
expectedMeta := &Meta{
Total: 2,
}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("FloatingIPs.List returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}
func TestFloatingIPs_ListFloatingIPsMultiplePages(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/floating_ips", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"floating_ips": [{"region":{"slug":"nyc3"},"droplet":{"id":1},"ip":"192.168.0.1"},{"region":{"slug":"nyc3"},"droplet":{"id":2},"ip":"192.168.0.2"}], "links":[]}`)
})
_, _, err := client.FloatingIPs.List(ctx, nil)
if err != nil {
t.Fatal(err)
}
}
func TestFloatingIPs_RetrievePageByNumber(t *testing.T) {
setup()
defer teardown()
jBlob := `
{
"floating_ips": [{"region":{"slug":"nyc3"},"droplet":{"id":1},"ip":"192.168.0.1"},{"region":{"slug":"nyc3"},"droplet":{"id":2},"ip":"192.168.0.2"}],
"links":[]
}`
mux.HandleFunc("/v2/floating_ips", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})
opt := &ListOptions{Page: 2}
_, _, err := client.FloatingIPs.List(ctx, opt)
if err != nil {
t.Fatal(err)
}
}
func TestFloatingIPs_Get(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/floating_ips/192.168.0.1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"floating_ip":{"region":{"slug":"nyc3"},"droplet":{"id":1},"ip":"192.168.0.1"}}`)
})
floatingIP, _, err := client.FloatingIPs.Get(ctx, "192.168.0.1")
if err != nil {
t.Errorf("domain.Get returned error: %v", err)
}
expected := &FloatingIP{Region: &Region{Slug: "nyc3"}, Droplet: &Droplet{ID: 1}, IP: "192.168.0.1"}
if !reflect.DeepEqual(floatingIP, expected) {
t.Errorf("FloatingIPs.Get returned %+v, expected %+v", floatingIP, expected)
}
}
func TestFloatingIPs_Create(t *testing.T) {
setup()
defer teardown()
createRequest := &FloatingIPCreateRequest{
Region: "nyc3",
DropletID: 1,
}
mux.HandleFunc("/v2/floating_ips", func(w http.ResponseWriter, r *http.Request) {
v := new(FloatingIPCreateRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, createRequest) {
t.Errorf("Request body = %+v, expected %+v", v, createRequest)
}
fmt.Fprint(w, `{"floating_ip":{"region":{"slug":"nyc3"},"droplet":{"id":1},"ip":"192.168.0.1"}}`)
})
floatingIP, _, err := client.FloatingIPs.Create(ctx, createRequest)
if err != nil {
t.Errorf("FloatingIPs.Create returned error: %v", err)
}
expected := &FloatingIP{Region: &Region{Slug: "nyc3"}, Droplet: &Droplet{ID: 1}, IP: "192.168.0.1"}
if !reflect.DeepEqual(floatingIP, expected) {
t.Errorf("FloatingIPs.Create returned %+v, expected %+v", floatingIP, expected)
}
}
func TestFloatingIPs_Destroy(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/floating_ips/192.168.0.1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := client.FloatingIPs.Delete(ctx, "192.168.0.1")
if err != nil {
t.Errorf("FloatingIPs.Delete returned error: %v", err)
}
}

52
godo.go
View File

@ -47,39 +47,13 @@ type Client struct {
ratemtx sync.Mutex
// Services used for communicating with the API
Account AccountService
Actions ActionsService
Apps AppsService
Balance BalanceService
BillingHistory BillingHistoryService
CDNs CDNService
Domains DomainsService
Droplets DropletsService
DropletActions DropletActionsService
Images ImagesService
ImageActions ImageActionsService
Invoices InvoicesService
Keys KeysService
Regions RegionsService
Sizes SizesService
FloatingIPs FloatingIPsService
FloatingIPActions FloatingIPActionsService
ReservedIPs ReservedIPsService
ReservedIPActions ReservedIPActionsService
Snapshots SnapshotsService
Storage StorageService
StorageActions StorageActionsService
Tags TagsService
LoadBalancers LoadBalancersService
Certificates CertificatesService
Firewalls FirewallsService
Projects ProjectsService
Kubernetes KubernetesService
Registry RegistryService
Databases DatabasesService
VPCs VPCsService
OneClick OneClickService
Monitoring MonitoringService
// Optional function called after every successful request made to the DO APIs
onRequestCompleted RequestCompletionCallback
@ -208,39 +182,13 @@ func NewClient(httpClient *http.Client) *Client {
baseURL, _ := url.Parse(defaultBaseURL)
c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent}
c.Account = &AccountServiceOp{client: c}
c.Actions = &ActionsServiceOp{client: c}
c.Apps = &AppsServiceOp{client: c}
c.Balance = &BalanceServiceOp{client: c}
c.BillingHistory = &BillingHistoryServiceOp{client: c}
c.CDNs = &CDNServiceOp{client: c}
c.Certificates = &CertificatesServiceOp{client: c}
c.Domains = &DomainsServiceOp{client: c}
c.Droplets = &DropletsServiceOp{client: c}
c.DropletActions = &DropletActionsServiceOp{client: c}
c.Firewalls = &FirewallsServiceOp{client: c}
c.FloatingIPs = &FloatingIPsServiceOp{client: c}
c.FloatingIPActions = &FloatingIPActionsServiceOp{client: c}
c.ReservedIPs = &ReservedIPsServiceOp{client: c}
c.ReservedIPActions = &ReservedIPActionsServiceOp{client: c}
c.Images = &ImagesServiceOp{client: c}
c.ImageActions = &ImageActionsServiceOp{client: c}
c.Invoices = &InvoicesServiceOp{client: c}
c.Keys = &KeysServiceOp{client: c}
c.LoadBalancers = &LoadBalancersServiceOp{client: c}
c.Projects = &ProjectsServiceOp{client: c}
c.Regions = &RegionsServiceOp{client: c}
c.Sizes = &SizesServiceOp{client: c}
c.Snapshots = &SnapshotsServiceOp{client: c}
c.Storage = &StorageServiceOp{client: c}
c.StorageActions = &StorageActionsServiceOp{client: c}
c.Tags = &TagsServiceOp{client: c}
c.Kubernetes = &KubernetesServiceOp{client: c}
c.Registry = &RegistryServiceOp{client: c}
c.Databases = &DatabasesServiceOp{client: c}
c.VPCs = &VPCsServiceOp{client: c}
c.OneClick = &OneClickServiceOp{client: c}
c.Monitoring = &MonitoringServiceOp{client: c}
c.headers = make(map[string]string)

View File

@ -1,226 +0,0 @@
package godo
import (
"bytes"
"context"
"fmt"
"net/http"
"time"
)
const invoicesBasePath = "v2/customers/my/invoices"
// InvoicesService is an interface for interfacing with the Invoice
// endpoints of the DigitalOcean API
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Billing
type InvoicesService interface {
Get(context.Context, string, *ListOptions) (*Invoice, *Response, error)
GetPDF(context.Context, string) ([]byte, *Response, error)
GetCSV(context.Context, string) ([]byte, *Response, error)
List(context.Context, *ListOptions) (*InvoiceList, *Response, error)
GetSummary(context.Context, string) (*InvoiceSummary, *Response, error)
}
// InvoicesServiceOp handles communication with the Invoice related methods of
// the DigitalOcean API.
type InvoicesServiceOp struct {
client *Client
}
var _ InvoicesService = &InvoicesServiceOp{}
// Invoice represents a DigitalOcean Invoice
type Invoice struct {
InvoiceItems []InvoiceItem `json:"invoice_items"`
Links []*LinkAction `json:"links"`
Meta *Meta `json:"meta"`
}
// InvoiceItem represents a line-item on a DigitalOcean Invoice
type InvoiceItem struct {
Product string `json:"product"`
ResourceID string `json:"resource_id"`
ResourceUUID string `json:"resource_uuid"`
GroupDescription string `json:"group_description"`
Description string `json:"description"`
Amount string `json:"amount"`
Duration string `json:"duration"`
DurationUnit string `json:"duration_unit"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
ProjectName string `json:"project_name"`
Category string `json:"category"`
}
// InvoiceList contains a paginated list of all of a customer's invoices.
// The InvoicePreview is the month-to-date usage generated by DigitalOcean.
type InvoiceList struct {
Invoices []InvoiceListItem `json:"invoices"`
InvoicePreview InvoiceListItem `json:"invoice_preview"`
Links []*LinkAction `json:"links"`
Meta *Meta `json:"meta"`
}
// InvoiceListItem contains a small list of information about a customer's invoice.
// More information can be found in the Invoice or InvoiceSummary
type InvoiceListItem struct {
InvoiceUUID string `json:"invoice_uuid"`
Amount string `json:"amount"`
InvoicePeriod string `json:"invoice_period"`
UpdatedAt time.Time `json:"updated_at"`
}
// InvoiceSummary contains metadata and summarized usage for an invoice generated by DigitalOcean
type InvoiceSummary struct {
InvoiceUUID string `json:"invoice_uuid"`
BillingPeriod string `json:"billing_period"`
Amount string `json:"amount"`
UserName string `json:"user_name"`
UserBillingAddress Address `json:"user_billing_address"`
UserCompany string `json:"user_company"`
UserEmail string `json:"user_email"`
ProductCharges InvoiceSummaryBreakdown `json:"product_charges"`
Overages InvoiceSummaryBreakdown `json:"overages"`
Taxes InvoiceSummaryBreakdown `json:"taxes"`
CreditsAndAdjustments InvoiceSummaryBreakdown `json:"credits_and_adjustments"`
}
// Address represents the billing address of a customer
type Address struct {
AddressLine1 string `json:"address_line1"`
AddressLine2 string `json:"address_line2"`
City string `json:"city"`
Region string `json:"region"`
PostalCode string `json:"postal_code"`
CountryISO2Code string `json:"country_iso2_code"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// InvoiceSummaryBreakdown is a grouped set of InvoiceItems from an invoice
type InvoiceSummaryBreakdown struct {
Name string `json:"name"`
Amount string `json:"amount"`
Items []InvoiceSummaryBreakdownItem `json:"items"`
}
// InvoiceSummaryBreakdownItem further breaks down the InvoiceSummary by product
type InvoiceSummaryBreakdownItem struct {
Name string `json:"name"`
Amount string `json:"amount"`
Count string `json:"count"`
}
func (i Invoice) String() string {
return Stringify(i)
}
// Get detailed invoice items for an Invoice
func (s *InvoicesServiceOp) Get(ctx context.Context, invoiceUUID string, opt *ListOptions) (*Invoice, *Response, error) {
path := fmt.Sprintf("%s/%s", invoicesBasePath, invoiceUUID)
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(Invoice)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root, resp, err
}
// List invoices for a customer
func (s *InvoicesServiceOp) List(ctx context.Context, opt *ListOptions) (*InvoiceList, *Response, error) {
path := invoicesBasePath
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(InvoiceList)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root, resp, err
}
// GetSummary returns a summary of metadata and summarized usage for an Invoice
func (s *InvoicesServiceOp) GetSummary(ctx context.Context, invoiceUUID string) (*InvoiceSummary, *Response, error) {
path := fmt.Sprintf("%s/%s/summary", invoicesBasePath, invoiceUUID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(InvoiceSummary)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root, resp, err
}
// GetPDF returns the pdf for an Invoice
func (s *InvoicesServiceOp) GetPDF(ctx context.Context, invoiceUUID string) ([]byte, *Response, error) {
path := fmt.Sprintf("%s/%s/pdf", invoicesBasePath, invoiceUUID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
var root bytes.Buffer
resp, err := s.client.Do(ctx, req, &root)
if err != nil {
return nil, resp, err
}
return root.Bytes(), resp, err
}
// GetCSV returns the csv for an Invoice
func (s *InvoicesServiceOp) GetCSV(ctx context.Context, invoiceUUID string) ([]byte, *Response, error) {
path := fmt.Sprintf("%s/%s/csv", invoicesBasePath, invoiceUUID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
var root bytes.Buffer
resp, err := s.client.Do(ctx, req, &root)
if err != nil {
return nil, resp, err
}
return root.Bytes(), resp, err
}

View File

@ -1,316 +0,0 @@
package godo
import (
"bytes"
"fmt"
"net/http"
"reflect"
"testing"
"time"
)
func TestInvoices_GetInvoices(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/customers/my/invoices/example-invoice-uuid", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{
"invoice_items": [
{
"product": "Droplets",
"resource_id": "1234",
"resource_uuid": "droplet-1234-uuid",
"group_description": "",
"description": "My Example Droplet",
"amount": "12.34",
"duration": "672",
"duration_unit": "Hours",
"start_time": "2018-06-20T08:44:38Z",
"end_time": "2018-06-21T08:44:38Z",
"project_name": "My project",
"category": "iaas"
},
{
"product": "Load Balancers",
"resource_id": "2345",
"resource_uuid": "load-balancer-2345-uuid",
"group_description": "",
"description": "My Example Load Balancer",
"amount": "23.45",
"duration": "744",
"duration_unit": "Hours",
"start_time": "2018-06-20T08:44:38Z",
"end_time": "2018-06-21T08:44:38Z",
"project_name": "My Second Project",
"category": "paas"
}
],
"meta": {
"total": 2
}
}`)
})
invoice, resp, err := client.Invoices.Get(ctx, "example-invoice-uuid", nil)
if err != nil {
t.Errorf("Invoices.Get returned error: %v", err)
}
expectedInvoiceItems := []InvoiceItem{
{
Product: "Droplets",
ResourceID: "1234",
ResourceUUID: "droplet-1234-uuid",
GroupDescription: "",
Description: "My Example Droplet",
Amount: "12.34",
Duration: "672",
DurationUnit: "Hours",
StartTime: time.Date(2018, 6, 20, 8, 44, 38, 0, time.UTC),
EndTime: time.Date(2018, 6, 21, 8, 44, 38, 0, time.UTC),
ProjectName: "My project",
Category: "iaas",
},
{
Product: "Load Balancers",
ResourceID: "2345",
ResourceUUID: "load-balancer-2345-uuid",
GroupDescription: "",
Description: "My Example Load Balancer",
Amount: "23.45",
Duration: "744",
DurationUnit: "Hours",
StartTime: time.Date(2018, 6, 20, 8, 44, 38, 0, time.UTC),
EndTime: time.Date(2018, 6, 21, 8, 44, 38, 0, time.UTC),
ProjectName: "My Second Project",
Category: "paas",
},
}
actualItems := invoice.InvoiceItems
if !reflect.DeepEqual(actualItems, expectedInvoiceItems) {
t.Errorf("Invoices.Get\nInvoiceItems: got=%#v\nwant=%#v", actualItems, expectedInvoiceItems)
}
expectedMeta := &Meta{Total: 2}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Invoices.Get\nMeta: got=%#v\nwant=%#v", resp.Meta, expectedMeta)
}
}
func TestInvoices_ListInvoices(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/customers/my/invoices", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{
"invoices": [
{
"invoice_uuid": "example-invoice-uuid-1",
"amount": "12.34",
"invoice_period": "2020-01"
},
{
"invoice_uuid": "example-invoice-uuid-2",
"amount": "23.45",
"invoice_period": "2019-12"
}
],
"invoice_preview": {
"invoice_uuid": "example-invoice-uuid-preview",
"amount": "34.56",
"invoice_period": "2020-02",
"updated_at": "2020-02-05T05:43:10Z"
},
"meta": {
"total": 2
}
}`)
})
invoiceListResponse, resp, err := client.Invoices.List(ctx, nil)
if err != nil {
t.Errorf("Invoices.List returned error: %v", err)
}
expectedInvoiceListItems := []InvoiceListItem{
{
InvoiceUUID: "example-invoice-uuid-1",
Amount: "12.34",
InvoicePeriod: "2020-01",
},
{
InvoiceUUID: "example-invoice-uuid-2",
Amount: "23.45",
InvoicePeriod: "2019-12",
},
}
actualItems := invoiceListResponse.Invoices
if !reflect.DeepEqual(actualItems, expectedInvoiceListItems) {
t.Errorf("Invoices.List\nInvoiceListItems: got=%#v\nwant=%#v", actualItems, expectedInvoiceListItems)
}
expectedPreview := InvoiceListItem{
InvoiceUUID: "example-invoice-uuid-preview",
Amount: "34.56",
InvoicePeriod: "2020-02",
UpdatedAt: time.Date(2020, 2, 5, 5, 43, 10, 0, time.UTC),
}
if !reflect.DeepEqual(invoiceListResponse.InvoicePreview, expectedPreview) {
t.Errorf("Invoices.List\nInvoicePreview: got=%#v\nwant=%#v", invoiceListResponse.InvoicePreview, expectedPreview)
}
expectedMeta := &Meta{Total: 2}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Invoices.List\nMeta: got=%#v\nwant=%#v", resp.Meta, expectedMeta)
}
}
func TestInvoices_GetSummary(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/customers/my/invoices/example-invoice-uuid/summary", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{
"invoice_uuid": "example-invoice-uuid",
"billing_period": "2020-01",
"amount": "27.13",
"user_name": "Frodo Baggins",
"user_billing_address": {
"address_line1": "101 Bagshot Row",
"address_line2": "#2",
"city": "Hobbiton",
"region": "Shire",
"postal_code": "12345",
"country_iso2_code": "ME",
"created_at": "2018-06-20T08:44:38Z",
"updated_at": "2018-06-21T08:44:38Z"
},
"user_company": "DigitalOcean",
"user_email": "fbaggins@example.com",
"product_charges": {
"name": "Product usage charges",
"amount": "12.34",
"items": [
{
"amount": "10.00",
"name": "Spaces Subscription",
"count": "1"
},
{
"amount": "2.34",
"name": "Database Clusters",
"count": "1"
}
]
},
"overages": {
"name": "Overages",
"amount": "3.45"
},
"taxes": {
"name": "Taxes",
"amount": "4.56"
},
"credits_and_adjustments": {
"name": "Credits & adjustments",
"amount": "6.78"
}
}`)
})
invoiceSummaryResponse, _, err := client.Invoices.GetSummary(ctx, "example-invoice-uuid")
if err != nil {
t.Errorf("Invoices.GetSummary returned error: %v", err)
}
expectedSummary := InvoiceSummary{
InvoiceUUID: "example-invoice-uuid",
BillingPeriod: "2020-01",
Amount: "27.13",
UserName: "Frodo Baggins",
UserBillingAddress: Address{
AddressLine1: "101 Bagshot Row",
AddressLine2: "#2",
City: "Hobbiton",
Region: "Shire",
PostalCode: "12345",
CountryISO2Code: "ME",
CreatedAt: time.Date(2018, 6, 20, 8, 44, 38, 0, time.UTC),
UpdatedAt: time.Date(2018, 6, 21, 8, 44, 38, 0, time.UTC),
},
UserCompany: "DigitalOcean",
UserEmail: "fbaggins@example.com",
ProductCharges: InvoiceSummaryBreakdown{
Name: "Product usage charges",
Amount: "12.34",
Items: []InvoiceSummaryBreakdownItem{
{
Name: "Spaces Subscription",
Amount: "10.00",
Count: "1",
},
{
Name: "Database Clusters",
Amount: "2.34",
Count: "1",
},
},
},
Overages: InvoiceSummaryBreakdown{
Name: "Overages",
Amount: "3.45",
},
Taxes: InvoiceSummaryBreakdown{
Name: "Taxes",
Amount: "4.56",
},
CreditsAndAdjustments: InvoiceSummaryBreakdown{
Name: "Credits & adjustments",
Amount: "6.78",
},
}
if !reflect.DeepEqual(invoiceSummaryResponse, &expectedSummary) {
t.Errorf("Invoices.GetSummary\nInvoiceSummary: got=%#v\nwant=%#v", invoiceSummaryResponse, &expectedSummary)
}
}
func TestInvoices_GetPDF(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/customers/my/invoices/example-invoice-uuid/pdf", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `some pdf content`)
})
invoicePDFResponse, _, err := client.Invoices.GetPDF(ctx, "example-invoice-uuid")
if err != nil {
t.Errorf("Invoices.GetPDF returned error: %v", err)
}
expected := []byte("some pdf content")
if !bytes.Equal(invoicePDFResponse, expected) {
t.Errorf("Invoices.GetPDF\n got=%#v\nwant=%#v", invoicePDFResponse, expected)
}
}
func TestInvoices_GetCSV(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/customers/my/invoices/example-invoice-uuid/csv", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `some csv content`)
})
invoiceCSVResponse, _, err := client.Invoices.GetCSV(ctx, "example-invoice-uuid")
if err != nil {
t.Errorf("Invoices.GetCSV returned error: %v", err)
}
expected := []byte("some csv content")
if !bytes.Equal(invoiceCSVResponse, expected) {
t.Errorf("Invoices.GetCSV\n got=%#v\nwant=%#v", invoiceCSVResponse, expected)
}
}

View File

@ -1,968 +0,0 @@
package godo
import (
"bytes"
"context"
"encoding"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
const (
kubernetesBasePath = "/v2/kubernetes"
kubernetesClustersPath = kubernetesBasePath + "/clusters"
kubernetesOptionsPath = kubernetesBasePath + "/options"
)
// KubernetesService is an interface for interfacing with the Kubernetes endpoints
// of the DigitalOcean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Kubernetes
type KubernetesService interface {
Create(context.Context, *KubernetesClusterCreateRequest) (*KubernetesCluster, *Response, error)
Get(context.Context, string) (*KubernetesCluster, *Response, error)
GetUser(context.Context, string) (*KubernetesClusterUser, *Response, error)
GetUpgrades(context.Context, string) ([]*KubernetesVersion, *Response, error)
GetKubeConfig(context.Context, string) (*KubernetesClusterConfig, *Response, error)
GetKubeConfigWithExpiry(context.Context, string, int64) (*KubernetesClusterConfig, *Response, error)
GetCredentials(context.Context, string, *KubernetesClusterCredentialsGetRequest) (*KubernetesClusterCredentials, *Response, error)
List(context.Context, *ListOptions) ([]*KubernetesCluster, *Response, error)
Update(context.Context, string, *KubernetesClusterUpdateRequest) (*KubernetesCluster, *Response, error)
Upgrade(context.Context, string, *KubernetesClusterUpgradeRequest) (*Response, error)
Delete(context.Context, string) (*Response, error)
DeleteSelective(context.Context, string, *KubernetesClusterDeleteSelectiveRequest) (*Response, error)
DeleteDangerous(context.Context, string) (*Response, error)
ListAssociatedResourcesForDeletion(context.Context, string) (*KubernetesAssociatedResources, *Response, error)
CreateNodePool(ctx context.Context, clusterID string, req *KubernetesNodePoolCreateRequest) (*KubernetesNodePool, *Response, error)
GetNodePool(ctx context.Context, clusterID, poolID string) (*KubernetesNodePool, *Response, error)
ListNodePools(ctx context.Context, clusterID string, opts *ListOptions) ([]*KubernetesNodePool, *Response, error)
UpdateNodePool(ctx context.Context, clusterID, poolID string, req *KubernetesNodePoolUpdateRequest) (*KubernetesNodePool, *Response, error)
// RecycleNodePoolNodes is DEPRECATED please use DeleteNode
// The method will be removed in godo 2.0.
RecycleNodePoolNodes(ctx context.Context, clusterID, poolID string, req *KubernetesNodePoolRecycleNodesRequest) (*Response, error)
DeleteNodePool(ctx context.Context, clusterID, poolID string) (*Response, error)
DeleteNode(ctx context.Context, clusterID, poolID, nodeID string, req *KubernetesNodeDeleteRequest) (*Response, error)
GetOptions(context.Context) (*KubernetesOptions, *Response, error)
AddRegistry(ctx context.Context, req *KubernetesClusterRegistryRequest) (*Response, error)
RemoveRegistry(ctx context.Context, req *KubernetesClusterRegistryRequest) (*Response, error)
RunClusterlint(ctx context.Context, clusterID string, req *KubernetesRunClusterlintRequest) (string, *Response, error)
GetClusterlintResults(ctx context.Context, clusterID string, req *KubernetesGetClusterlintRequest) ([]*ClusterlintDiagnostic, *Response, error)
}
var _ KubernetesService = &KubernetesServiceOp{}
// KubernetesServiceOp handles communication with Kubernetes methods of the DigitalOcean API.
type KubernetesServiceOp struct {
client *Client
}
// KubernetesClusterCreateRequest represents a request to create a Kubernetes cluster.
type KubernetesClusterCreateRequest struct {
Name string `json:"name,omitempty"`
RegionSlug string `json:"region,omitempty"`
VersionSlug string `json:"version,omitempty"`
Tags []string `json:"tags,omitempty"`
VPCUUID string `json:"vpc_uuid,omitempty"`
// Create cluster with highly available control plane
HA bool `json:"ha"`
NodePools []*KubernetesNodePoolCreateRequest `json:"node_pools,omitempty"`
MaintenancePolicy *KubernetesMaintenancePolicy `json:"maintenance_policy"`
AutoUpgrade bool `json:"auto_upgrade"`
SurgeUpgrade bool `json:"surge_upgrade"`
}
// KubernetesClusterUpdateRequest represents a request to update a Kubernetes cluster.
type KubernetesClusterUpdateRequest struct {
Name string `json:"name,omitempty"`
Tags []string `json:"tags,omitempty"`
MaintenancePolicy *KubernetesMaintenancePolicy `json:"maintenance_policy,omitempty"`
AutoUpgrade *bool `json:"auto_upgrade,omitempty"`
SurgeUpgrade bool `json:"surge_upgrade,omitempty"`
}
// KubernetesClusterDeleteSelectiveRequest represents a delete selective request to delete a cluster and it's associated resources.
type KubernetesClusterDeleteSelectiveRequest struct {
Volumes []string `json:"volumes"`
VolumeSnapshots []string `json:"volume_snapshots"`
LoadBalancers []string `json:"load_balancers"`
}
// KubernetesClusterUpgradeRequest represents a request to upgrade a Kubernetes cluster.
type KubernetesClusterUpgradeRequest struct {
VersionSlug string `json:"version,omitempty"`
}
// Taint represents a Kubernetes taint that can be associated with a node pool
// (and, transitively, with all nodes of that pool).
type Taint struct {
Key string
Value string
Effect string
}
func (t Taint) String() string {
if t.Value == "" {
return fmt.Sprintf("%s:%s", t.Key, t.Effect)
}
return fmt.Sprintf("%s=%s:%s", t.Key, t.Value, t.Effect)
}
// KubernetesNodePoolCreateRequest represents a request to create a node pool for a
// Kubernetes cluster.
type KubernetesNodePoolCreateRequest struct {
Name string `json:"name,omitempty"`
Size string `json:"size,omitempty"`
Count int `json:"count,omitempty"`
Tags []string `json:"tags,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Taints []Taint `json:"taints,omitempty"`
AutoScale bool `json:"auto_scale,omitempty"`
MinNodes int `json:"min_nodes,omitempty"`
MaxNodes int `json:"max_nodes,omitempty"`
}
// KubernetesNodePoolUpdateRequest represents a request to update a node pool in a
// Kubernetes cluster.
type KubernetesNodePoolUpdateRequest struct {
Name string `json:"name,omitempty"`
Count *int `json:"count,omitempty"`
Tags []string `json:"tags,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Taints *[]Taint `json:"taints,omitempty"`
AutoScale *bool `json:"auto_scale,omitempty"`
MinNodes *int `json:"min_nodes,omitempty"`
MaxNodes *int `json:"max_nodes,omitempty"`
}
// KubernetesNodePoolRecycleNodesRequest is DEPRECATED please use DeleteNode
// The type will be removed in godo 2.0.
type KubernetesNodePoolRecycleNodesRequest struct {
Nodes []string `json:"nodes,omitempty"`
}
// KubernetesNodeDeleteRequest is a request to delete a specific node in a node pool.
type KubernetesNodeDeleteRequest struct {
// Replace will cause a new node to be created to replace the deleted node.
Replace bool `json:"replace,omitempty"`
// SkipDrain skips draining the node before deleting it.
SkipDrain bool `json:"skip_drain,omitempty"`
}
// KubernetesClusterCredentialsGetRequest is a request to get cluster credentials.
type KubernetesClusterCredentialsGetRequest struct {
ExpirySeconds *int `json:"expiry_seconds,omitempty"`
}
// KubernetesClusterRegistryRequest represents clusters to integrate with docr registry
type KubernetesClusterRegistryRequest struct {
ClusterUUIDs []string `json:"cluster_uuids,omitempty"`
}
type KubernetesRunClusterlintRequest struct {
IncludeGroups []string `json:"include_groups"`
ExcludeGroups []string `json:"exclude_groups"`
IncludeChecks []string `json:"include_checks"`
ExcludeChecks []string `json:"exclude_checks"`
}
type KubernetesGetClusterlintRequest struct {
RunId string `json:"run_id"`
}
// KubernetesCluster represents a Kubernetes cluster.
type KubernetesCluster struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
RegionSlug string `json:"region,omitempty"`
VersionSlug string `json:"version,omitempty"`
ClusterSubnet string `json:"cluster_subnet,omitempty"`
ServiceSubnet string `json:"service_subnet,omitempty"`
IPv4 string `json:"ipv4,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
Tags []string `json:"tags,omitempty"`
VPCUUID string `json:"vpc_uuid,omitempty"`
// Cluster runs a highly available control plane
HA bool `json:"ha,omitempty"`
NodePools []*KubernetesNodePool `json:"node_pools,omitempty"`
MaintenancePolicy *KubernetesMaintenancePolicy `json:"maintenance_policy,omitempty"`
AutoUpgrade bool `json:"auto_upgrade,omitempty"`
SurgeUpgrade bool `json:"surge_upgrade,omitempty"`
RegistryEnabled bool `json:"registry_enabled,omitempty"`
Status *KubernetesClusterStatus `json:"status,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}
// URN returns the Kubernetes cluster's ID in the format of DigitalOcean URN.
func (kc KubernetesCluster) URN() string {
return ToURN("Kubernetes", kc.ID)
}
// KubernetesClusterUser represents a Kubernetes cluster user.
type KubernetesClusterUser struct {
Username string `json:"username,omitempty"`
Groups []string `json:"groups,omitempty"`
}
// KubernetesClusterCredentials represents Kubernetes cluster credentials.
type KubernetesClusterCredentials struct {
Server string `json:"server"`
CertificateAuthorityData []byte `json:"certificate_authority_data"`
ClientCertificateData []byte `json:"client_certificate_data"`
ClientKeyData []byte `json:"client_key_data"`
Token string `json:"token"`
ExpiresAt time.Time `json:"expires_at"`
}
// KubernetesMaintenancePolicy is a configuration to set the maintenance window
// of a cluster
type KubernetesMaintenancePolicy struct {
StartTime string `json:"start_time"`
Duration string `json:"duration"`
Day KubernetesMaintenancePolicyDay `json:"day"`
}
// KubernetesMaintenancePolicyDay represents the possible days of a maintenance
// window
type KubernetesMaintenancePolicyDay int
const (
// KubernetesMaintenanceDayAny sets the KubernetesMaintenancePolicyDay to any
// day of the week
KubernetesMaintenanceDayAny KubernetesMaintenancePolicyDay = iota
// KubernetesMaintenanceDayMonday sets the KubernetesMaintenancePolicyDay to
// Monday
KubernetesMaintenanceDayMonday
// KubernetesMaintenanceDayTuesday sets the KubernetesMaintenancePolicyDay to
// Tuesday
KubernetesMaintenanceDayTuesday
// KubernetesMaintenanceDayWednesday sets the KubernetesMaintenancePolicyDay to
// Wednesday
KubernetesMaintenanceDayWednesday
// KubernetesMaintenanceDayThursday sets the KubernetesMaintenancePolicyDay to
// Thursday
KubernetesMaintenanceDayThursday
// KubernetesMaintenanceDayFriday sets the KubernetesMaintenancePolicyDay to
// Friday
KubernetesMaintenanceDayFriday
// KubernetesMaintenanceDaySaturday sets the KubernetesMaintenancePolicyDay to
// Saturday
KubernetesMaintenanceDaySaturday
// KubernetesMaintenanceDaySunday sets the KubernetesMaintenancePolicyDay to
// Sunday
KubernetesMaintenanceDaySunday
)
var (
days = [...]string{
"any",
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday",
"sunday",
}
toDay = map[string]KubernetesMaintenancePolicyDay{
"any": KubernetesMaintenanceDayAny,
"monday": KubernetesMaintenanceDayMonday,
"tuesday": KubernetesMaintenanceDayTuesday,
"wednesday": KubernetesMaintenanceDayWednesday,
"thursday": KubernetesMaintenanceDayThursday,
"friday": KubernetesMaintenanceDayFriday,
"saturday": KubernetesMaintenanceDaySaturday,
"sunday": KubernetesMaintenanceDaySunday,
}
)
// KubernetesMaintenanceToDay returns the appropriate KubernetesMaintenancePolicyDay for the given string.
func KubernetesMaintenanceToDay(day string) (KubernetesMaintenancePolicyDay, error) {
d, ok := toDay[day]
if !ok {
return 0, fmt.Errorf("unknown day: %q", day)
}
return d, nil
}
func (k KubernetesMaintenancePolicyDay) String() string {
if KubernetesMaintenanceDayAny <= k && k <= KubernetesMaintenanceDaySunday {
return days[k]
}
return fmt.Sprintf("%d !Weekday", k)
}
// UnmarshalJSON parses the JSON string into KubernetesMaintenancePolicyDay
func (k *KubernetesMaintenancePolicyDay) UnmarshalJSON(data []byte) error {
var val string
if err := json.Unmarshal(data, &val); err != nil {
return err
}
parsed, err := KubernetesMaintenanceToDay(val)
if err != nil {
return err
}
*k = parsed
return nil
}
// MarshalJSON returns the JSON string for KubernetesMaintenancePolicyDay
func (k KubernetesMaintenancePolicyDay) MarshalJSON() ([]byte, error) {
if KubernetesMaintenanceDayAny <= k && k <= KubernetesMaintenanceDaySunday {
return json.Marshal(days[k])
}
return nil, fmt.Errorf("invalid day: %d", k)
}
// Possible states for a cluster.
const (
KubernetesClusterStatusProvisioning = KubernetesClusterStatusState("provisioning")
KubernetesClusterStatusRunning = KubernetesClusterStatusState("running")
KubernetesClusterStatusDegraded = KubernetesClusterStatusState("degraded")
KubernetesClusterStatusError = KubernetesClusterStatusState("error")
KubernetesClusterStatusDeleted = KubernetesClusterStatusState("deleted")
KubernetesClusterStatusUpgrading = KubernetesClusterStatusState("upgrading")
KubernetesClusterStatusInvalid = KubernetesClusterStatusState("invalid")
)
// KubernetesClusterStatusState represents states for a cluster.
type KubernetesClusterStatusState string
var _ encoding.TextUnmarshaler = (*KubernetesClusterStatusState)(nil)
// UnmarshalText unmarshals the state.
func (s *KubernetesClusterStatusState) UnmarshalText(text []byte) error {
switch KubernetesClusterStatusState(strings.ToLower(string(text))) {
case KubernetesClusterStatusProvisioning:
*s = KubernetesClusterStatusProvisioning
case KubernetesClusterStatusRunning:
*s = KubernetesClusterStatusRunning
case KubernetesClusterStatusDegraded:
*s = KubernetesClusterStatusDegraded
case KubernetesClusterStatusError:
*s = KubernetesClusterStatusError
case KubernetesClusterStatusDeleted:
*s = KubernetesClusterStatusDeleted
case KubernetesClusterStatusUpgrading:
*s = KubernetesClusterStatusUpgrading
case "", KubernetesClusterStatusInvalid:
*s = KubernetesClusterStatusInvalid
default:
return fmt.Errorf("unknown cluster state %q", string(text))
}
return nil
}
// KubernetesClusterStatus describes the status of a cluster.
type KubernetesClusterStatus struct {
State KubernetesClusterStatusState `json:"state,omitempty"`
Message string `json:"message,omitempty"`
}
// KubernetesNodePool represents a node pool in a Kubernetes cluster.
type KubernetesNodePool struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Size string `json:"size,omitempty"`
Count int `json:"count,omitempty"`
Tags []string `json:"tags,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Taints []Taint `json:"taints,omitempty"`
AutoScale bool `json:"auto_scale,omitempty"`
MinNodes int `json:"min_nodes,omitempty"`
MaxNodes int `json:"max_nodes,omitempty"`
Nodes []*KubernetesNode `json:"nodes,omitempty"`
}
// KubernetesNode represents a Node in a node pool in a Kubernetes cluster.
type KubernetesNode struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Status *KubernetesNodeStatus `json:"status,omitempty"`
DropletID string `json:"droplet_id,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}
// KubernetesNodeStatus represents the status of a particular Node in a Kubernetes cluster.
type KubernetesNodeStatus struct {
State string `json:"state,omitempty"`
Message string `json:"message,omitempty"`
}
// KubernetesOptions represents options available for creating Kubernetes clusters.
type KubernetesOptions struct {
Versions []*KubernetesVersion `json:"versions,omitempty"`
Regions []*KubernetesRegion `json:"regions,omitempty"`
Sizes []*KubernetesNodeSize `json:"sizes,omitempty"`
}
// KubernetesVersion is a DigitalOcean Kubernetes release.
type KubernetesVersion struct {
Slug string `json:"slug,omitempty"`
KubernetesVersion string `json:"kubernetes_version,omitempty"`
SupportedFeatures []string `json:"supported_features,omitempty"`
}
// KubernetesNodeSize is a node sizes supported for Kubernetes clusters.
type KubernetesNodeSize struct {
Name string `json:"name"`
Slug string `json:"slug"`
}
// KubernetesRegion is a region usable by Kubernetes clusters.
type KubernetesRegion struct {
Name string `json:"name"`
Slug string `json:"slug"`
}
// ClusterlintDiagnostic is a diagnostic returned from clusterlint.
type ClusterlintDiagnostic struct {
CheckName string `json:"check_name"`
Severity string `json:"severity"`
Message string `json:"message"`
Object *ClusterlintObject `json:"object"`
}
// ClusterlintObject is the object a clusterlint diagnostic refers to.
type ClusterlintObject struct {
Kind string `json:"kind"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Owners []*ClusterlintOwner `json:"owners,omitempty"`
}
// ClusterlintOwner indicates the resource that owns the offending object.
type ClusterlintOwner struct {
Kind string `json:"kind"`
Name string `json:"name"`
}
// KubernetesAssociatedResources represents a cluster's associated resources
type KubernetesAssociatedResources struct {
Volumes []*AssociatedResource `json:"volumes"`
VolumeSnapshots []*AssociatedResource `json:"volume_snapshots"`
LoadBalancers []*AssociatedResource `json:"load_balancers"`
}
// AssociatedResource is the object to represent a Kubernetes cluster associated resource's ID and Name.
type AssociatedResource struct {
ID string `json:"id"`
Name string `json:"name"`
}
type kubernetesClustersRoot struct {
Clusters []*KubernetesCluster `json:"kubernetes_clusters,omitempty"`
Links []*LinkAction `json:"links,omitempty"`
Meta *Meta `json:"meta"`
}
type kubernetesClusterRoot struct {
Cluster *KubernetesCluster `json:"kubernetes_cluster,omitempty"`
}
type kubernetesClusterUserRoot struct {
User *KubernetesClusterUser `json:"kubernetes_cluster_user,omitempty"`
}
type kubernetesNodePoolRoot struct {
NodePool *KubernetesNodePool `json:"node_pool,omitempty"`
}
type kubernetesNodePoolsRoot struct {
NodePools []*KubernetesNodePool `json:"node_pools,omitempty"`
Links []LinkAction `json:"links,omitempty"`
}
type kubernetesUpgradesRoot struct {
AvailableUpgradeVersions []*KubernetesVersion `json:"available_upgrade_versions,omitempty"`
}
// Get retrieves the details of a Kubernetes cluster.
func (svc *KubernetesServiceOp) Get(ctx context.Context, clusterID string) (*KubernetesCluster, *Response, error) {
path := fmt.Sprintf("%s/%s", kubernetesClustersPath, clusterID)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(kubernetesClusterRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Cluster, resp, nil
}
// GetUser retrieves the details of a Kubernetes cluster user.
func (svc *KubernetesServiceOp) GetUser(ctx context.Context, clusterID string) (*KubernetesClusterUser, *Response, error) {
path := fmt.Sprintf("%s/%s/user", kubernetesClustersPath, clusterID)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(kubernetesClusterUserRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.User, resp, nil
}
// GetUpgrades retrieves versions a Kubernetes cluster can be upgraded to. An
// upgrade can be requested using `Upgrade`.
func (svc *KubernetesServiceOp) GetUpgrades(ctx context.Context, clusterID string) ([]*KubernetesVersion, *Response, error) {
path := fmt.Sprintf("%s/%s/upgrades", kubernetesClustersPath, clusterID)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(kubernetesUpgradesRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, nil, err
}
return root.AvailableUpgradeVersions, resp, nil
}
// Create creates a Kubernetes cluster.
func (svc *KubernetesServiceOp) Create(ctx context.Context, create *KubernetesClusterCreateRequest) (*KubernetesCluster, *Response, error) {
path := kubernetesClustersPath
req, err := svc.client.NewRequest(ctx, http.MethodPost, path, create)
if err != nil {
return nil, nil, err
}
root := new(kubernetesClusterRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Cluster, resp, nil
}
// Delete deletes a Kubernetes cluster. There is no way to recover a cluster
// once it has been destroyed.
func (svc *KubernetesServiceOp) Delete(ctx context.Context, clusterID string) (*Response, error) {
path := fmt.Sprintf("%s/%s", kubernetesClustersPath, clusterID)
req, err := svc.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := svc.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
// DeleteSelective deletes a Kubernetes cluster and the specified associated resources.
// Users can choose to delete specific volumes, volume snapshots or load balancers along with the cluster
// There is no way to recover a cluster or the specified resources once destroyed.
func (svc *KubernetesServiceOp) DeleteSelective(ctx context.Context, clusterID string, request *KubernetesClusterDeleteSelectiveRequest) (*Response, error) {
path := fmt.Sprintf("%s/%s/destroy_with_associated_resources/selective", kubernetesClustersPath, clusterID)
req, err := svc.client.NewRequest(ctx, http.MethodDelete, path, request)
if err != nil {
return nil, err
}
resp, err := svc.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
// DeleteDangerous deletes a Kubernetes cluster and all its associated resources. There is no way to recover a cluster
// or it's associated resources once destroyed.
func (svc *KubernetesServiceOp) DeleteDangerous(ctx context.Context, clusterID string) (*Response, error) {
path := fmt.Sprintf("%s/%s/destroy_with_associated_resources/dangerous", kubernetesClustersPath, clusterID)
req, err := svc.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := svc.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
// ListAssociatedResourcesForDeletion lists a Kubernetes cluster's resources that can be selected
// for deletion along with the cluster. See DeleteSelective
// Associated resources include volumes, volume snapshots and load balancers.
func (svc *KubernetesServiceOp) ListAssociatedResourcesForDeletion(ctx context.Context, clusterID string) (*KubernetesAssociatedResources, *Response, error) {
path := fmt.Sprintf("%s/%s/destroy_with_associated_resources", kubernetesClustersPath, clusterID)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(KubernetesAssociatedResources)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root, resp, nil
}
// List returns a list of the Kubernetes clusters visible with the caller's API token.
func (svc *KubernetesServiceOp) List(ctx context.Context, opts *ListOptions) ([]*KubernetesCluster, *Response, error) {
path := kubernetesClustersPath
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(kubernetesClustersRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Clusters, resp, nil
}
// KubernetesClusterConfig is the content of a Kubernetes config file, which can be
// used to interact with your Kubernetes cluster using `kubectl`.
// See: https://kubernetes.io/docs/tasks/tools/install-kubectl/
type KubernetesClusterConfig struct {
KubeconfigYAML []byte
}
// GetKubeConfig returns a Kubernetes config file for the specified cluster.
func (svc *KubernetesServiceOp) GetKubeConfig(ctx context.Context, clusterID string) (*KubernetesClusterConfig, *Response, error) {
path := fmt.Sprintf("%s/%s/kubeconfig", kubernetesClustersPath, clusterID)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
configBytes := bytes.NewBuffer(nil)
resp, err := svc.client.Do(ctx, req, configBytes)
if err != nil {
return nil, resp, err
}
res := &KubernetesClusterConfig{
KubeconfigYAML: configBytes.Bytes(),
}
return res, resp, nil
}
// GetKubeConfigWithExpiry returns a Kubernetes config file for the specified cluster with expiry_seconds.
func (svc *KubernetesServiceOp) GetKubeConfigWithExpiry(ctx context.Context, clusterID string, expirySeconds int64) (*KubernetesClusterConfig, *Response, error) {
path := fmt.Sprintf("%s/%s/kubeconfig", kubernetesClustersPath, clusterID)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
q := req.URL.Query()
q.Add("expiry_seconds", fmt.Sprintf("%d", expirySeconds))
req.URL.RawQuery = q.Encode()
configBytes := bytes.NewBuffer(nil)
resp, err := svc.client.Do(ctx, req, configBytes)
if err != nil {
return nil, resp, err
}
res := &KubernetesClusterConfig{
KubeconfigYAML: configBytes.Bytes(),
}
return res, resp, nil
}
// GetCredentials returns a Kubernetes API server credentials for the specified cluster.
func (svc *KubernetesServiceOp) GetCredentials(ctx context.Context, clusterID string, get *KubernetesClusterCredentialsGetRequest) (*KubernetesClusterCredentials, *Response, error) {
path := fmt.Sprintf("%s/%s/credentials", kubernetesClustersPath, clusterID)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
q := req.URL.Query()
if get.ExpirySeconds != nil {
q.Add("expiry_seconds", strconv.Itoa(*get.ExpirySeconds))
}
req.URL.RawQuery = q.Encode()
credentials := new(KubernetesClusterCredentials)
resp, err := svc.client.Do(ctx, req, credentials)
if err != nil {
return nil, nil, err
}
return credentials, resp, nil
}
// Update updates a Kubernetes cluster's properties.
func (svc *KubernetesServiceOp) Update(ctx context.Context, clusterID string, update *KubernetesClusterUpdateRequest) (*KubernetesCluster, *Response, error) {
path := fmt.Sprintf("%s/%s", kubernetesClustersPath, clusterID)
req, err := svc.client.NewRequest(ctx, http.MethodPut, path, update)
if err != nil {
return nil, nil, err
}
root := new(kubernetesClusterRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Cluster, resp, nil
}
// Upgrade upgrades a Kubernetes cluster to a new version. Valid upgrade
// versions for a given cluster can be retrieved with `GetUpgrades`.
func (svc *KubernetesServiceOp) Upgrade(ctx context.Context, clusterID string, upgrade *KubernetesClusterUpgradeRequest) (*Response, error) {
path := fmt.Sprintf("%s/%s/upgrade", kubernetesClustersPath, clusterID)
req, err := svc.client.NewRequest(ctx, http.MethodPost, path, upgrade)
if err != nil {
return nil, err
}
return svc.client.Do(ctx, req, nil)
}
// CreateNodePool creates a new node pool in an existing Kubernetes cluster.
func (svc *KubernetesServiceOp) CreateNodePool(ctx context.Context, clusterID string, create *KubernetesNodePoolCreateRequest) (*KubernetesNodePool, *Response, error) {
path := fmt.Sprintf("%s/%s/node_pools", kubernetesClustersPath, clusterID)
req, err := svc.client.NewRequest(ctx, http.MethodPost, path, create)
if err != nil {
return nil, nil, err
}
root := new(kubernetesNodePoolRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.NodePool, resp, nil
}
// GetNodePool retrieves an existing node pool in a Kubernetes cluster.
func (svc *KubernetesServiceOp) GetNodePool(ctx context.Context, clusterID, poolID string) (*KubernetesNodePool, *Response, error) {
path := fmt.Sprintf("%s/%s/node_pools/%s", kubernetesClustersPath, clusterID, poolID)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(kubernetesNodePoolRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.NodePool, resp, nil
}
// ListNodePools lists all the node pools found in a Kubernetes cluster.
func (svc *KubernetesServiceOp) ListNodePools(ctx context.Context, clusterID string, opts *ListOptions) ([]*KubernetesNodePool, *Response, error) {
path := fmt.Sprintf("%s/%s/node_pools", kubernetesClustersPath, clusterID)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(kubernetesNodePoolsRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.NodePools, resp, nil
}
// UpdateNodePool updates the details of an existing node pool.
func (svc *KubernetesServiceOp) UpdateNodePool(ctx context.Context, clusterID, poolID string, update *KubernetesNodePoolUpdateRequest) (*KubernetesNodePool, *Response, error) {
path := fmt.Sprintf("%s/%s/node_pools/%s", kubernetesClustersPath, clusterID, poolID)
req, err := svc.client.NewRequest(ctx, http.MethodPut, path, update)
if err != nil {
return nil, nil, err
}
root := new(kubernetesNodePoolRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.NodePool, resp, nil
}
// RecycleNodePoolNodes is DEPRECATED please use DeleteNode
// The method will be removed in godo 2.0.
func (svc *KubernetesServiceOp) RecycleNodePoolNodes(ctx context.Context, clusterID, poolID string, recycle *KubernetesNodePoolRecycleNodesRequest) (*Response, error) {
path := fmt.Sprintf("%s/%s/node_pools/%s/recycle", kubernetesClustersPath, clusterID, poolID)
req, err := svc.client.NewRequest(ctx, http.MethodPost, path, recycle)
if err != nil {
return nil, err
}
resp, err := svc.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
// DeleteNodePool deletes a node pool, and subsequently all the nodes in that pool.
func (svc *KubernetesServiceOp) DeleteNodePool(ctx context.Context, clusterID, poolID string) (*Response, error) {
path := fmt.Sprintf("%s/%s/node_pools/%s", kubernetesClustersPath, clusterID, poolID)
req, err := svc.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := svc.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
// DeleteNode deletes a specific node in a node pool.
func (svc *KubernetesServiceOp) DeleteNode(ctx context.Context, clusterID, poolID, nodeID string, deleteReq *KubernetesNodeDeleteRequest) (*Response, error) {
path := fmt.Sprintf("%s/%s/node_pools/%s/nodes/%s", kubernetesClustersPath, clusterID, poolID, nodeID)
if deleteReq != nil {
v := make(url.Values)
if deleteReq.SkipDrain {
v.Set("skip_drain", "1")
}
if deleteReq.Replace {
v.Set("replace", "1")
}
if query := v.Encode(); query != "" {
path = path + "?" + query
}
}
req, err := svc.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := svc.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
type kubernetesOptionsRoot struct {
Options *KubernetesOptions `json:"options,omitempty"`
Links []LinkAction `json:"links,omitempty"`
}
// GetOptions returns options about the Kubernetes service, such as the versions available for
// cluster creation.
func (svc *KubernetesServiceOp) GetOptions(ctx context.Context) (*KubernetesOptions, *Response, error) {
path := kubernetesOptionsPath
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(kubernetesOptionsRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Options, resp, nil
}
// AddRegistry integrates docr registry with all the specified clusters
func (svc *KubernetesServiceOp) AddRegistry(ctx context.Context, req *KubernetesClusterRegistryRequest) (*Response, error) {
path := fmt.Sprintf("%s/registry", kubernetesBasePath)
request, err := svc.client.NewRequest(ctx, http.MethodPost, path, req)
if err != nil {
return nil, err
}
resp, err := svc.client.Do(ctx, request, nil)
if err != nil {
return resp, err
}
return resp, nil
}
// RemoveRegistry removes docr registry support for all the specified clusters
func (svc *KubernetesServiceOp) RemoveRegistry(ctx context.Context, req *KubernetesClusterRegistryRequest) (*Response, error) {
path := fmt.Sprintf("%s/registry", kubernetesBasePath)
request, err := svc.client.NewRequest(ctx, http.MethodDelete, path, req)
if err != nil {
return nil, err
}
resp, err := svc.client.Do(ctx, request, nil)
if err != nil {
return resp, err
}
return resp, nil
}
type runClusterlintRoot struct {
RunID string `json:"run_id"`
}
// RunClusterlint schedules a clusterlint run for the specified cluster
func (svc *KubernetesServiceOp) RunClusterlint(ctx context.Context, clusterID string, req *KubernetesRunClusterlintRequest) (string, *Response, error) {
path := fmt.Sprintf("%s/%s/clusterlint", kubernetesClustersPath, clusterID)
request, err := svc.client.NewRequest(ctx, http.MethodPost, path, req)
if err != nil {
return "", nil, err
}
root := new(runClusterlintRoot)
resp, err := svc.client.Do(ctx, request, root)
if err != nil {
return "", resp, err
}
return root.RunID, resp, nil
}
type clusterlintDiagnosticsRoot struct {
Diagnostics []*ClusterlintDiagnostic
}
// GetClusterlintResults fetches the diagnostics after clusterlint run completes
func (svc *KubernetesServiceOp) GetClusterlintResults(ctx context.Context, clusterID string, req *KubernetesGetClusterlintRequest) ([]*ClusterlintDiagnostic, *Response, error) {
path := fmt.Sprintf("%s/%s/clusterlint", kubernetesClustersPath, clusterID)
if req != nil {
v := make(url.Values)
if req.RunId != "" {
v.Set("run_id", req.RunId)
}
if query := v.Encode(); query != "" {
path = path + "?" + query
}
}
request, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(clusterlintDiagnosticsRoot)
resp, err := svc.client.Do(ctx, request, root)
if err != nil {
return nil, resp, err
}
return root.Diagnostics, resp, nil
}

File diff suppressed because it is too large Load Diff

View File

@ -1,345 +0,0 @@
package godo
import (
"context"
"fmt"
"net/http"
)
const loadBalancersBasePath = "/v2/load_balancers"
const forwardingRulesPath = "forwarding_rules"
const dropletsPath = "droplets"
// LoadBalancersService is an interface for managing load balancers with the DigitalOcean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Load-Balancers
type LoadBalancersService interface {
Get(context.Context, string) (*LoadBalancer, *Response, error)
List(context.Context, *ListOptions) ([]LoadBalancer, *Response, error)
Create(context.Context, *LoadBalancerRequest) (*LoadBalancer, *Response, error)
Update(ctx context.Context, lbID string, lbr *LoadBalancerRequest) (*LoadBalancer, *Response, error)
Delete(ctx context.Context, lbID string) (*Response, error)
AddDroplets(ctx context.Context, lbID string, dropletIDs ...int) (*Response, error)
RemoveDroplets(ctx context.Context, lbID string, dropletIDs ...int) (*Response, error)
AddForwardingRules(ctx context.Context, lbID string, rules ...ForwardingRule) (*Response, error)
RemoveForwardingRules(ctx context.Context, lbID string, rules ...ForwardingRule) (*Response, error)
}
// LoadBalancer represents a DigitalOcean load balancer configuration.
// Tags can only be provided upon the creation of a Load Balancer.
type LoadBalancer struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
IP string `json:"ip,omitempty"`
// SizeSlug is mutually exclusive with SizeUnit. Only one should be specified
SizeSlug string `json:"size,omitempty"`
// SizeUnit is mutually exclusive with SizeSlug. Only one should be specified
SizeUnit uint32 `json:"size_unit,omitempty"`
Algorithm string `json:"algorithm,omitempty"`
Status string `json:"status,omitempty"`
Created string `json:"created_at,omitempty"`
ForwardingRules []ForwardingRule `json:"forwarding_rules,omitempty"`
HealthCheck *HealthCheck `json:"health_check,omitempty"`
StickySessions *StickySessions `json:"sticky_sessions,omitempty"`
Region *Region `json:"region,omitempty"`
DropletIDs []int `json:"droplet_ids,omitempty"`
Tag string `json:"tag,omitempty"`
Tags []string `json:"tags,omitempty"`
RedirectHttpToHttps bool `json:"redirect_http_to_https,omitempty"`
EnableProxyProtocol bool `json:"enable_proxy_protocol,omitempty"`
EnableBackendKeepalive bool `json:"enable_backend_keepalive,omitempty"`
VPCUUID string `json:"vpc_uuid,omitempty"`
DisableLetsEncryptDNSRecords *bool `json:"disable_lets_encrypt_dns_records,omitempty"`
ValidateOnly bool `json:"validate_only,omitempty"`
}
// String creates a human-readable description of a LoadBalancer.
func (l LoadBalancer) String() string {
return Stringify(l)
}
// URN returns the load balancer ID in a valid DO API URN form.
func (l LoadBalancer) URN() string {
return ToURN("LoadBalancer", l.ID)
}
// AsRequest creates a LoadBalancerRequest that can be submitted to Update with the current values of the LoadBalancer.
// Modifying the returned LoadBalancerRequest will not modify the original LoadBalancer.
func (l LoadBalancer) AsRequest() *LoadBalancerRequest {
r := LoadBalancerRequest{
Name: l.Name,
Algorithm: l.Algorithm,
SizeSlug: l.SizeSlug,
SizeUnit: l.SizeUnit,
ForwardingRules: append([]ForwardingRule(nil), l.ForwardingRules...),
DropletIDs: append([]int(nil), l.DropletIDs...),
Tag: l.Tag,
RedirectHttpToHttps: l.RedirectHttpToHttps,
EnableProxyProtocol: l.EnableProxyProtocol,
EnableBackendKeepalive: l.EnableBackendKeepalive,
HealthCheck: l.HealthCheck,
VPCUUID: l.VPCUUID,
DisableLetsEncryptDNSRecords: l.DisableLetsEncryptDNSRecords,
ValidateOnly: l.ValidateOnly,
}
if l.DisableLetsEncryptDNSRecords != nil {
*r.DisableLetsEncryptDNSRecords = *l.DisableLetsEncryptDNSRecords
}
if l.HealthCheck != nil {
r.HealthCheck = &HealthCheck{}
*r.HealthCheck = *l.HealthCheck
}
if l.StickySessions != nil {
r.StickySessions = &StickySessions{}
*r.StickySessions = *l.StickySessions
}
if l.Region != nil {
r.Region = l.Region.Slug
}
return &r
}
// ForwardingRule represents load balancer forwarding rules.
type ForwardingRule struct {
EntryProtocol string `json:"entry_protocol,omitempty"`
EntryPort int `json:"entry_port,omitempty"`
TargetProtocol string `json:"target_protocol,omitempty"`
TargetPort int `json:"target_port,omitempty"`
CertificateID string `json:"certificate_id,omitempty"`
TlsPassthrough bool `json:"tls_passthrough,omitempty"`
}
// String creates a human-readable description of a ForwardingRule.
func (f ForwardingRule) String() string {
return Stringify(f)
}
// HealthCheck represents optional load balancer health check rules.
type HealthCheck struct {
Protocol string `json:"protocol,omitempty"`
Port int `json:"port,omitempty"`
Path string `json:"path,omitempty"`
CheckIntervalSeconds int `json:"check_interval_seconds,omitempty"`
ResponseTimeoutSeconds int `json:"response_timeout_seconds,omitempty"`
HealthyThreshold int `json:"healthy_threshold,omitempty"`
UnhealthyThreshold int `json:"unhealthy_threshold,omitempty"`
}
// String creates a human-readable description of a HealthCheck.
func (h HealthCheck) String() string {
return Stringify(h)
}
// StickySessions represents optional load balancer session affinity rules.
type StickySessions struct {
Type string `json:"type,omitempty"`
CookieName string `json:"cookie_name,omitempty"`
CookieTtlSeconds int `json:"cookie_ttl_seconds,omitempty"`
}
// String creates a human-readable description of a StickySessions instance.
func (s StickySessions) String() string {
return Stringify(s)
}
// LoadBalancerRequest represents the configuration to be applied to an existing or a new load balancer.
type LoadBalancerRequest struct {
Name string `json:"name,omitempty"`
Algorithm string `json:"algorithm,omitempty"`
Region string `json:"region,omitempty"`
// SizeSlug is mutually exclusive with SizeUnit. Only one should be specified
SizeSlug string `json:"size,omitempty"`
// SizeUnit is mutually exclusive with SizeSlug. Only one should be specified
SizeUnit uint32 `json:"size_unit,omitempty"`
ForwardingRules []ForwardingRule `json:"forwarding_rules,omitempty"`
HealthCheck *HealthCheck `json:"health_check,omitempty"`
StickySessions *StickySessions `json:"sticky_sessions,omitempty"`
DropletIDs []int `json:"droplet_ids,omitempty"`
Tag string `json:"tag,omitempty"`
Tags []string `json:"tags,omitempty"`
RedirectHttpToHttps bool `json:"redirect_http_to_https,omitempty"`
EnableProxyProtocol bool `json:"enable_proxy_protocol,omitempty"`
EnableBackendKeepalive bool `json:"enable_backend_keepalive,omitempty"`
VPCUUID string `json:"vpc_uuid,omitempty"`
DisableLetsEncryptDNSRecords *bool `json:"disable_lets_encrypt_dns_records,omitempty"`
ValidateOnly bool `json:"validate_only,omitempty"`
}
// String creates a human-readable description of a LoadBalancerRequest.
func (l LoadBalancerRequest) String() string {
return Stringify(l)
}
type forwardingRulesRequest struct {
Rules []ForwardingRule `json:"forwarding_rules,omitempty"`
}
func (l forwardingRulesRequest) String() string {
return Stringify(l)
}
type dropletIDsRequest struct {
IDs []int `json:"droplet_ids,omitempty"`
}
func (l dropletIDsRequest) String() string {
return Stringify(l)
}
type loadBalancersRoot struct {
LoadBalancers []LoadBalancer `json:"load_balancers"`
Links []*LinkAction `json:"links"`
Meta *Meta `json:"meta"`
}
type loadBalancerRoot struct {
LoadBalancer *LoadBalancer `json:"load_balancer"`
}
// LoadBalancersServiceOp handles communication with load balancer-related methods of the DigitalOcean API.
type LoadBalancersServiceOp struct {
client *Client
}
var _ LoadBalancersService = &LoadBalancersServiceOp{}
// Get an existing load balancer by its identifier.
func (l *LoadBalancersServiceOp) Get(ctx context.Context, lbID string) (*LoadBalancer, *Response, error) {
path := fmt.Sprintf("%s/%s", loadBalancersBasePath, lbID)
req, err := l.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(loadBalancerRoot)
resp, err := l.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.LoadBalancer, resp, err
}
// List load balancers, with optional pagination.
func (l *LoadBalancersServiceOp) List(ctx context.Context, opt *ListOptions) ([]LoadBalancer, *Response, error) {
path, err := addOptions(loadBalancersBasePath, opt)
if err != nil {
return nil, nil, err
}
req, err := l.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(loadBalancersRoot)
resp, err := l.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.LoadBalancers, resp, err
}
// Create a new load balancer with a given configuration.
func (l *LoadBalancersServiceOp) Create(ctx context.Context, lbr *LoadBalancerRequest) (*LoadBalancer, *Response, error) {
req, err := l.client.NewRequest(ctx, http.MethodPost, loadBalancersBasePath, lbr)
if err != nil {
return nil, nil, err
}
root := new(loadBalancerRoot)
resp, err := l.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.LoadBalancer, resp, err
}
// Update an existing load balancer with new configuration.
func (l *LoadBalancersServiceOp) Update(ctx context.Context, lbID string, lbr *LoadBalancerRequest) (*LoadBalancer, *Response, error) {
path := fmt.Sprintf("%s/%s", loadBalancersBasePath, lbID)
req, err := l.client.NewRequest(ctx, "PUT", path, lbr)
if err != nil {
return nil, nil, err
}
root := new(loadBalancerRoot)
resp, err := l.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.LoadBalancer, resp, err
}
// Delete a load balancer by its identifier.
func (l *LoadBalancersServiceOp) Delete(ctx context.Context, ldID string) (*Response, error) {
path := fmt.Sprintf("%s/%s", loadBalancersBasePath, ldID)
req, err := l.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return l.client.Do(ctx, req, nil)
}
// AddDroplets adds droplets to a load balancer.
func (l *LoadBalancersServiceOp) AddDroplets(ctx context.Context, lbID string, dropletIDs ...int) (*Response, error) {
path := fmt.Sprintf("%s/%s/%s", loadBalancersBasePath, lbID, dropletsPath)
req, err := l.client.NewRequest(ctx, http.MethodPost, path, &dropletIDsRequest{IDs: dropletIDs})
if err != nil {
return nil, err
}
return l.client.Do(ctx, req, nil)
}
// RemoveDroplets removes droplets from a load balancer.
func (l *LoadBalancersServiceOp) RemoveDroplets(ctx context.Context, lbID string, dropletIDs ...int) (*Response, error) {
path := fmt.Sprintf("%s/%s/%s", loadBalancersBasePath, lbID, dropletsPath)
req, err := l.client.NewRequest(ctx, http.MethodDelete, path, &dropletIDsRequest{IDs: dropletIDs})
if err != nil {
return nil, err
}
return l.client.Do(ctx, req, nil)
}
// AddForwardingRules adds forwarding rules to a load balancer.
func (l *LoadBalancersServiceOp) AddForwardingRules(ctx context.Context, lbID string, rules ...ForwardingRule) (*Response, error) {
path := fmt.Sprintf("%s/%s/%s", loadBalancersBasePath, lbID, forwardingRulesPath)
req, err := l.client.NewRequest(ctx, http.MethodPost, path, &forwardingRulesRequest{Rules: rules})
if err != nil {
return nil, err
}
return l.client.Do(ctx, req, nil)
}
// RemoveForwardingRules removes forwarding rules from a load balancer.
func (l *LoadBalancersServiceOp) RemoveForwardingRules(ctx context.Context, lbID string, rules ...ForwardingRule) (*Response, error) {
path := fmt.Sprintf("%s/%s/%s", loadBalancersBasePath, lbID, forwardingRulesPath)
req, err := l.client.NewRequest(ctx, http.MethodDelete, path, &forwardingRulesRequest{Rules: rules})
if err != nil {
return nil, err
}
return l.client.Do(ctx, req, nil)
}

File diff suppressed because it is too large Load Diff

View File

@ -1,361 +0,0 @@
package godo
import (
"context"
"fmt"
"net/http"
"time"
"github.com/digitalocean/godo/metrics"
)
const (
monitoringBasePath = "v2/monitoring"
alertPolicyBasePath = monitoringBasePath + "/alerts"
dropletMetricsBasePath = monitoringBasePath + "/metrics/droplet"
DropletCPUUtilizationPercent = "v1/insights/droplet/cpu"
DropletMemoryUtilizationPercent = "v1/insights/droplet/memory_utilization_percent"
DropletDiskUtilizationPercent = "v1/insights/droplet/disk_utilization_percent"
DropletPublicOutboundBandwidthRate = "v1/insights/droplet/public_outbound_bandwidth"
DropletPublicInboundBandwidthRate = "v1/insights/droplet/public_inbound_bandwidth"
DropletPrivateOutboundBandwidthRate = "v1/insights/droplet/private_outbound_bandwidth"
DropletPrivateInboundBandwidthRate = "v1/insights/droplet/private_inbound_bandwidth"
DropletDiskReadRate = "v1/insights/droplet/disk_read"
DropletDiskWriteRate = "v1/insights/droplet/disk_write"
DropletOneMinuteLoadAverage = "v1/insights/droplet/load_1"
DropletFiveMinuteLoadAverage = "v1/insights/droplet/load_5"
DropletFifteenMinuteLoadAverage = "v1/insights/droplet/load_15"
LoadBalancerCPUUtilizationPercent = "v1/insights/lbaas/avg_cpu_utilization_percent"
LoadBalancerConnectionUtilizationPercent = "v1/insights/lbaas/connection_utilization_percent"
LoadBalancerDropletHealth = "v1/insights/lbaas/droplet_health"
LoadBalancerTLSUtilizationPercent = "v1/insights/lbaas/tls_connections_per_second_utilization_percent"
)
// MonitoringService is an interface for interfacing with the
// monitoring endpoints of the DigitalOcean API
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Monitoring
type MonitoringService interface {
ListAlertPolicies(context.Context, *ListOptions) ([]AlertPolicy, *Response, error)
GetAlertPolicy(context.Context, string) (*AlertPolicy, *Response, error)
CreateAlertPolicy(context.Context, *AlertPolicyCreateRequest) (*AlertPolicy, *Response, error)
UpdateAlertPolicy(context.Context, string, *AlertPolicyUpdateRequest) (*AlertPolicy, *Response, error)
DeleteAlertPolicy(context.Context, string) (*Response, error)
GetDropletBandwidth(context.Context, *DropletBandwidthMetricsRequest) (*MetricsResponse, *Response, error)
GetDropletAvailableMemory(context.Context, *DropletMetricsRequest) (*MetricsResponse, *Response, error)
GetDropletCPU(context.Context, *DropletMetricsRequest) (*MetricsResponse, *Response, error)
GetDropletFilesystemFree(context.Context, *DropletMetricsRequest) (*MetricsResponse, *Response, error)
GetDropletFilesystemSize(context.Context, *DropletMetricsRequest) (*MetricsResponse, *Response, error)
GetDropletLoad1(context.Context, *DropletMetricsRequest) (*MetricsResponse, *Response, error)
GetDropletLoad5(context.Context, *DropletMetricsRequest) (*MetricsResponse, *Response, error)
GetDropletLoad15(context.Context, *DropletMetricsRequest) (*MetricsResponse, *Response, error)
GetDropletCachedMemory(context.Context, *DropletMetricsRequest) (*MetricsResponse, *Response, error)
GetDropletFreeMemory(context.Context, *DropletMetricsRequest) (*MetricsResponse, *Response, error)
GetDropletTotalMemory(context.Context, *DropletMetricsRequest) (*MetricsResponse, *Response, error)
}
// MonitoringServiceOp handles communication with monitoring related methods of the
// DigitalOcean API.
type MonitoringServiceOp struct {
client *Client
}
var _ MonitoringService = &MonitoringServiceOp{}
// AlertPolicy represents a DigitalOcean alert policy
type AlertPolicy struct {
UUID string `json:"uuid"`
Type string `json:"type"`
Description string `json:"description"`
Compare AlertPolicyComp `json:"compare"`
Value float32 `json:"value"`
Window string `json:"window"`
Entities []string `json:"entities"`
Tags []string `json:"tags"`
Alerts Alerts `json:"alerts"`
Enabled bool `json:"enabled"`
}
// Alerts represents the alerts section of an alert policy
type Alerts struct {
Slack []SlackDetails `json:"slack"`
Email []string `json:"email"`
}
// SlackDetails represents the details required to send a slack alert
type SlackDetails struct {
URL string `json:"url"`
Channel string `json:"channel"`
}
// AlertPolicyComp represents an alert policy comparison operation
type AlertPolicyComp string
const (
// GreaterThan is the comparison >
GreaterThan AlertPolicyComp = "GreaterThan"
// LessThan is the comparison <
LessThan AlertPolicyComp = "LessThan"
)
// AlertPolicyCreateRequest holds the info for creating a new alert policy
type AlertPolicyCreateRequest struct {
Type string `json:"type"`
Description string `json:"description"`
Compare AlertPolicyComp `json:"compare"`
Value float32 `json:"value"`
Window string `json:"window"`
Entities []string `json:"entities"`
Tags []string `json:"tags"`
Alerts Alerts `json:"alerts"`
Enabled *bool `json:"enabled"`
}
// AlertPolicyUpdateRequest holds the info for updating an existing alert policy
type AlertPolicyUpdateRequest struct {
Type string `json:"type"`
Description string `json:"description"`
Compare AlertPolicyComp `json:"compare"`
Value float32 `json:"value"`
Window string `json:"window"`
Entities []string `json:"entities"`
Tags []string `json:"tags"`
Alerts Alerts `json:"alerts"`
Enabled *bool `json:"enabled"`
}
type alertPoliciesRoot struct {
AlertPolicies []AlertPolicy `json:"policies"`
Links []*LinkAction `json:"links"`
Meta *Meta `json:"meta"`
}
type alertPolicyRoot struct {
AlertPolicy *AlertPolicy `json:"policy,omitempty"`
}
// DropletMetricsRequest holds the information needed to retrieve Droplet various metrics.
type DropletMetricsRequest struct {
HostID string
Start time.Time
End time.Time
}
// DropletBandwidthMetricsRequest holds the information needed to retrieve Droplet bandwidth metrics.
type DropletBandwidthMetricsRequest struct {
DropletMetricsRequest
Interface string
Direction string
}
// MetricsResponse holds a Metrics query response.
type MetricsResponse struct {
Status string `json:"status"`
Data MetricsData `json:"data"`
}
// MetricsData holds the data portion of a Metrics response.
type MetricsData struct {
ResultType string `json:"resultType"`
Result []metrics.SampleStream `json:"result"`
}
// ListAlertPolicies all alert policies
func (s *MonitoringServiceOp) ListAlertPolicies(ctx context.Context, opt *ListOptions) ([]AlertPolicy, *Response, error) {
path := alertPolicyBasePath
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(alertPoliciesRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.AlertPolicies, resp, err
}
// GetAlertPolicy gets a single alert policy
func (s *MonitoringServiceOp) GetAlertPolicy(ctx context.Context, uuid string) (*AlertPolicy, *Response, error) {
path := fmt.Sprintf("%s/%s", alertPolicyBasePath, uuid)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(alertPolicyRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.AlertPolicy, resp, err
}
// CreateAlertPolicy creates a new alert policy
func (s *MonitoringServiceOp) CreateAlertPolicy(ctx context.Context, createRequest *AlertPolicyCreateRequest) (*AlertPolicy, *Response, error) {
if createRequest == nil {
return nil, nil, NewArgError("createRequest", "cannot be nil")
}
req, err := s.client.NewRequest(ctx, http.MethodPost, alertPolicyBasePath, createRequest)
if err != nil {
return nil, nil, err
}
root := new(alertPolicyRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.AlertPolicy, resp, err
}
// UpdateAlertPolicy updates an existing alert policy
func (s *MonitoringServiceOp) UpdateAlertPolicy(ctx context.Context, uuid string, updateRequest *AlertPolicyUpdateRequest) (*AlertPolicy, *Response, error) {
if uuid == "" {
return nil, nil, NewArgError("uuid", "cannot be empty")
}
if updateRequest == nil {
return nil, nil, NewArgError("updateRequest", "cannot be nil")
}
path := fmt.Sprintf("%s/%s", alertPolicyBasePath, uuid)
req, err := s.client.NewRequest(ctx, http.MethodPut, path, updateRequest)
if err != nil {
return nil, nil, err
}
root := new(alertPolicyRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.AlertPolicy, resp, err
}
// DeleteAlertPolicy deletes an existing alert policy
func (s *MonitoringServiceOp) DeleteAlertPolicy(ctx context.Context, uuid string) (*Response, error) {
if uuid == "" {
return nil, NewArgError("uuid", "cannot be empty")
}
path := fmt.Sprintf("%s/%s", alertPolicyBasePath, uuid)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(ctx, req, nil)
return resp, err
}
// GetDropletBandwidth retrieves Droplet bandwidth metrics.
func (s *MonitoringServiceOp) GetDropletBandwidth(ctx context.Context, args *DropletBandwidthMetricsRequest) (*MetricsResponse, *Response, error) {
path := dropletMetricsBasePath + "/bandwidth"
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
q := req.URL.Query()
q.Add("host_id", args.HostID)
q.Add("interface", args.Interface)
q.Add("direction", args.Direction)
q.Add("start", fmt.Sprintf("%d", args.Start.Unix()))
q.Add("end", fmt.Sprintf("%d", args.End.Unix()))
req.URL.RawQuery = q.Encode()
root := new(MetricsResponse)
resp, err := s.client.Do(ctx, req, root)
return root, resp, err
}
// GetDropletCPU retrieves Droplet CPU metrics.
func (s *MonitoringServiceOp) GetDropletCPU(ctx context.Context, args *DropletMetricsRequest) (*MetricsResponse, *Response, error) {
return s.getDropletMetrics(ctx, "/cpu", args)
}
// GetDropletFilesystemFree retrieves Droplet filesystem free metrics.
func (s *MonitoringServiceOp) GetDropletFilesystemFree(ctx context.Context, args *DropletMetricsRequest) (*MetricsResponse, *Response, error) {
return s.getDropletMetrics(ctx, "/filesystem_free", args)
}
// GetDropletFilesystemSize retrieves Droplet filesystem size metrics.
func (s *MonitoringServiceOp) GetDropletFilesystemSize(ctx context.Context, args *DropletMetricsRequest) (*MetricsResponse, *Response, error) {
return s.getDropletMetrics(ctx, "/filesystem_size", args)
}
// GetDropletLoad1 retrieves Droplet load 1 metrics.
func (s *MonitoringServiceOp) GetDropletLoad1(ctx context.Context, args *DropletMetricsRequest) (*MetricsResponse, *Response, error) {
return s.getDropletMetrics(ctx, "/load_1", args)
}
// GetDropletLoad5 retrieves Droplet load 5 metrics.
func (s *MonitoringServiceOp) GetDropletLoad5(ctx context.Context, args *DropletMetricsRequest) (*MetricsResponse, *Response, error) {
return s.getDropletMetrics(ctx, "/load_5", args)
}
// GetDropletLoad15 retrieves Droplet load 15 metrics.
func (s *MonitoringServiceOp) GetDropletLoad15(ctx context.Context, args *DropletMetricsRequest) (*MetricsResponse, *Response, error) {
return s.getDropletMetrics(ctx, "/load_15", args)
}
// GetDropletCachedMemory retrieves Droplet cached memory metrics.
func (s *MonitoringServiceOp) GetDropletCachedMemory(ctx context.Context, args *DropletMetricsRequest) (*MetricsResponse, *Response, error) {
return s.getDropletMetrics(ctx, "/memory_cached", args)
}
// GetDropletFreeMemory retrieves Droplet free memory metrics.
func (s *MonitoringServiceOp) GetDropletFreeMemory(ctx context.Context, args *DropletMetricsRequest) (*MetricsResponse, *Response, error) {
return s.getDropletMetrics(ctx, "/memory_free", args)
}
// GetDropletTotalMemory retrieves Droplet total memory metrics.
func (s *MonitoringServiceOp) GetDropletTotalMemory(ctx context.Context, args *DropletMetricsRequest) (*MetricsResponse, *Response, error) {
return s.getDropletMetrics(ctx, "/memory_total", args)
}
// GetDropletAvailableMemory retrieves Droplet available memory metrics.
func (s *MonitoringServiceOp) GetDropletAvailableMemory(ctx context.Context, args *DropletMetricsRequest) (*MetricsResponse, *Response, error) {
return s.getDropletMetrics(ctx, "/memory_available", args)
}
func (s *MonitoringServiceOp) getDropletMetrics(ctx context.Context, path string, args *DropletMetricsRequest) (*MetricsResponse, *Response, error) {
fullPath := dropletMetricsBasePath + path
req, err := s.client.NewRequest(ctx, http.MethodGet, fullPath, nil)
if err != nil {
return nil, nil, err
}
q := req.URL.Query()
q.Add("host_id", args.HostID)
q.Add("start", fmt.Sprintf("%d", args.Start.Unix()))
q.Add("end", fmt.Sprintf("%d", args.End.Unix()))
req.URL.RawQuery = q.Encode()
root := new(MetricsResponse)
resp, err := s.client.Do(ctx, req, root)
return root, resp, err
}

File diff suppressed because it is too large Load Diff

View File

@ -1,309 +0,0 @@
package godo
import (
"context"
"encoding/json"
"fmt"
"net/http"
"path"
)
const (
// DefaultProject is the ID you should use if you are working with your
// default project.
DefaultProject = "default"
projectsBasePath = "/v2/projects"
)
// ProjectsService is an interface for creating and managing Projects with the DigitalOcean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Projects
type ProjectsService interface {
List(context.Context, *ListOptions) ([]Project, *Response, error)
GetDefault(context.Context) (*Project, *Response, error)
Get(context.Context, string) (*Project, *Response, error)
Create(context.Context, *CreateProjectRequest) (*Project, *Response, error)
Update(context.Context, string, *UpdateProjectRequest) (*Project, *Response, error)
Delete(context.Context, string) (*Response, error)
ListResources(context.Context, string, *ListOptions) ([]ProjectResource, *Response, error)
AssignResources(context.Context, string, ...interface{}) ([]ProjectResource, *Response, error)
}
// ProjectsServiceOp handles communication with Projects methods of the DigitalOcean API.
type ProjectsServiceOp struct {
client *Client
}
// Project represents a DigitalOcean Project configuration.
type Project struct {
ID string `json:"id"`
OwnerUUID string `json:"owner_uuid"`
OwnerID uint64 `json:"owner_id"`
Name string `json:"name"`
Description string `json:"description"`
Purpose string `json:"purpose"`
Environment string `json:"environment"`
IsDefault bool `json:"is_default"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
// String creates a human-readable description of a Project.
func (p Project) String() string {
return Stringify(p)
}
// CreateProjectRequest represents the request to create a new project.
type CreateProjectRequest struct {
Name string `json:"name"`
Description string `json:"description"`
Purpose string `json:"purpose"`
Environment string `json:"environment"`
}
// UpdateProjectRequest represents the request to update project information.
// This type expects certain attribute types, but is built this way to allow
// nil values as well. See `updateProjectRequest` for the "real" types.
type UpdateProjectRequest struct {
Name interface{}
Description interface{}
Purpose interface{}
Environment interface{}
IsDefault interface{}
}
type updateProjectRequest struct {
Name *string `json:"name"`
Description *string `json:"description"`
Purpose *string `json:"purpose"`
Environment *string `json:"environment"`
IsDefault *bool `json:"is_default"`
}
// MarshalJSON takes an UpdateRequest and converts it to the "typed" request
// which is sent to the projects API. This is a PATCH request, which allows
// partial attributes, so `null` values are OK.
func (upr *UpdateProjectRequest) MarshalJSON() ([]byte, error) {
d := &updateProjectRequest{}
if str, ok := upr.Name.(string); ok {
d.Name = &str
}
if str, ok := upr.Description.(string); ok {
d.Description = &str
}
if str, ok := upr.Purpose.(string); ok {
d.Purpose = &str
}
if str, ok := upr.Environment.(string); ok {
d.Environment = &str
}
if val, ok := upr.IsDefault.(bool); ok {
d.IsDefault = &val
}
return json.Marshal(d)
}
type assignResourcesRequest struct {
Resources []string `json:"resources"`
}
// ProjectResource is the projects API's representation of a resource.
type ProjectResource struct {
URN string `json:"urn"`
AssignedAt string `json:"assigned_at"`
Links *ProjectResourceLinks `json:"links"`
Status string `json:"status,omitempty"`
}
// ProjectResourceLinks specify the link for more information about the resource.
type ProjectResourceLinks struct {
Self string `json:"self"`
}
type projectsRoot struct {
Projects []Project `json:"projects"`
Links []*LinkAction `json:"links"`
Meta *Meta `json:"meta"`
}
type projectRoot struct {
Project *Project `json:"project"`
}
type projectResourcesRoot struct {
Resources []ProjectResource `json:"resources"`
Links []*LinkAction `json:"links,omitempty"`
Meta *Meta `json:"meta"`
}
var _ ProjectsService = &ProjectsServiceOp{}
// List Projects.
func (p *ProjectsServiceOp) List(ctx context.Context, opts *ListOptions) ([]Project, *Response, error) {
path, err := addOptions(projectsBasePath, opts)
if err != nil {
return nil, nil, err
}
req, err := p.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(projectsRoot)
resp, err := p.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Projects, resp, err
}
// GetDefault project.
func (p *ProjectsServiceOp) GetDefault(ctx context.Context) (*Project, *Response, error) {
return p.getHelper(ctx, "default")
}
// Get retrieves a single project by its ID.
func (p *ProjectsServiceOp) Get(ctx context.Context, projectID string) (*Project, *Response, error) {
return p.getHelper(ctx, projectID)
}
// Create a new project.
func (p *ProjectsServiceOp) Create(ctx context.Context, cr *CreateProjectRequest) (*Project, *Response, error) {
req, err := p.client.NewRequest(ctx, http.MethodPost, projectsBasePath, cr)
if err != nil {
return nil, nil, err
}
root := new(projectRoot)
resp, err := p.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Project, resp, err
}
// Update an existing project.
func (p *ProjectsServiceOp) Update(ctx context.Context, projectID string, ur *UpdateProjectRequest) (*Project, *Response, error) {
path := path.Join(projectsBasePath, projectID)
req, err := p.client.NewRequest(ctx, http.MethodPatch, path, ur)
if err != nil {
return nil, nil, err
}
root := new(projectRoot)
resp, err := p.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Project, resp, err
}
// Delete an existing project. You cannot have any resources in a project
// before deleting it. See the API documentation for more details.
func (p *ProjectsServiceOp) Delete(ctx context.Context, projectID string) (*Response, error) {
path := path.Join(projectsBasePath, projectID)
req, err := p.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return p.client.Do(ctx, req, nil)
}
// ListResources lists all resources in a project.
func (p *ProjectsServiceOp) ListResources(ctx context.Context, projectID string, opts *ListOptions) ([]ProjectResource, *Response, error) {
basePath := path.Join(projectsBasePath, projectID, "resources")
path, err := addOptions(basePath, opts)
if err != nil {
return nil, nil, err
}
req, err := p.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(projectResourcesRoot)
resp, err := p.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Resources, resp, err
}
// AssignResources assigns one or more resources to a project. AssignResources
// accepts resources in two possible formats:
// 1. The resource type, like `&Droplet{ID: 1}` or `&FloatingIP{IP: "1.2.3.4"}`
// 2. A valid DO URN as a string, like "do:droplet:1234"
//
// There is no unassign. To move a resource to another project, just assign
// it to that other project.
func (p *ProjectsServiceOp) AssignResources(ctx context.Context, projectID string, resources ...interface{}) ([]ProjectResource, *Response, error) {
path := path.Join(projectsBasePath, projectID, "resources")
ar := &assignResourcesRequest{
Resources: make([]string, len(resources)),
}
for i, resource := range resources {
switch resource := resource.(type) {
case ResourceWithURN:
ar.Resources[i] = resource.URN()
case string:
ar.Resources[i] = resource
default:
return nil, nil, fmt.Errorf("%T must either be a string or have a valid URN method", resource)
}
}
req, err := p.client.NewRequest(ctx, http.MethodPost, path, ar)
if err != nil {
return nil, nil, err
}
root := new(projectResourcesRoot)
resp, err := p.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
return root.Resources, resp, err
}
func (p *ProjectsServiceOp) getHelper(ctx context.Context, projectID string) (*Project, *Response, error) {
path := path.Join(projectsBasePath, projectID)
req, err := p.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(projectRoot)
resp, err := p.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Project, resp, err
}

View File

@ -1,634 +0,0 @@
package godo
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"strings"
"testing"
)
func TestProjects_List(t *testing.T) {
setup()
defer teardown()
expectedProjects := []Project{
{
ID: "project-1",
Name: "project-1",
},
{
ID: "project-2",
Name: "project-2",
},
}
mux.HandleFunc("/v2/projects", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
resp, _ := json.Marshal(expectedProjects)
fmt.Fprint(w, fmt.Sprintf(`{"projects":%s, "meta": {"total": 2}}`, string(resp)))
})
projects, resp, err := client.Projects.List(ctx, nil)
if err != nil {
t.Errorf("Projects.List returned error: %v", err)
}
if !reflect.DeepEqual(projects, expectedProjects) {
t.Errorf("Projects.List returned projects %+v, expected %+v", projects, expectedProjects)
}
expectedMeta := &Meta{Total: 2}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Projects.List returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}
func TestProjects_ListWithMultiplePages(t *testing.T) {
setup()
defer teardown()
mockResp := `
{
"projects": [
{
"uuid": "project-1",
"name": "project-1"
},
{
"uuid": "project-2",
"name": "project-2"
}
],
"links": []
}`
mux.HandleFunc("/v2/projects", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, mockResp)
})
_, _, err := client.Projects.List(ctx, nil)
if err != nil {
t.Errorf("Projects.List returned error: %v", err)
}
}
func TestProjects_ListWithPageNumber(t *testing.T) {
setup()
defer teardown()
mockResp := `
{
"projects": [
{
"uuid": "project-1",
"name": "project-1"
},
{
"uuid": "project-2",
"name": "project-2"
}
],
"links": []
}`
mux.HandleFunc("/v2/projects", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, mockResp)
})
_, _, err := client.Projects.List(ctx, &ListOptions{Page: 2})
if err != nil {
t.Errorf("Projects.List returned error: %v", err)
}
}
func TestProjects_GetDefault(t *testing.T) {
setup()
defer teardown()
project := &Project{
ID: "project-1",
Name: "project-1",
}
mux.HandleFunc("/v2/projects/default", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
resp, _ := json.Marshal(project)
fmt.Fprint(w, fmt.Sprintf(`{"project":%s}`, string(resp)))
})
resp, _, err := client.Projects.GetDefault(ctx)
if err != nil {
t.Errorf("Projects.GetDefault returned error: %v", err)
}
if !reflect.DeepEqual(resp, project) {
t.Errorf("Projects.GetDefault returned %+v, expected %+v", resp, project)
}
}
func TestProjects_GetWithUUID(t *testing.T) {
setup()
defer teardown()
project := &Project{
ID: "project-1",
Name: "project-1",
}
mux.HandleFunc("/v2/projects/project-1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
resp, _ := json.Marshal(project)
fmt.Fprint(w, fmt.Sprintf(`{"project":%s}`, string(resp)))
})
resp, _, err := client.Projects.Get(ctx, "project-1")
if err != nil {
t.Errorf("Projects.Get returned error: %v", err)
}
if !reflect.DeepEqual(resp, project) {
t.Errorf("Projects.Get returned %+v, expected %+v", resp, project)
}
}
func TestProjects_Create(t *testing.T) {
setup()
defer teardown()
createRequest := &CreateProjectRequest{
Name: "my project",
Description: "for my stuff",
Purpose: "Just trying out DigitalOcean",
Environment: "Production",
}
createResp := &Project{
ID: "project-id",
Name: createRequest.Name,
Description: createRequest.Description,
Purpose: createRequest.Purpose,
Environment: createRequest.Environment,
}
mux.HandleFunc("/v2/projects", func(w http.ResponseWriter, r *http.Request) {
v := new(CreateProjectRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, createRequest) {
t.Errorf("Request body = %+v, expected %+v", v, createRequest)
}
resp, _ := json.Marshal(createResp)
fmt.Fprintf(w, fmt.Sprintf(`{"project":%s}`, string(resp)))
})
project, _, err := client.Projects.Create(ctx, createRequest)
if err != nil {
t.Errorf("Projects.Create returned error: %v", err)
}
if !reflect.DeepEqual(project, createResp) {
t.Errorf("Projects.Create returned %+v, expected %+v", project, createResp)
}
}
func TestProjects_UpdateWithOneAttribute(t *testing.T) {
setup()
defer teardown()
updateRequest := &UpdateProjectRequest{
Name: "my-great-project",
}
updateResp := &Project{
ID: "project-id",
Name: updateRequest.Name.(string),
Description: "some-other-description",
Purpose: "some-other-purpose",
Environment: "some-other-env",
IsDefault: false,
}
mux.HandleFunc("/v2/projects/project-1", func(w http.ResponseWriter, r *http.Request) {
reqBytes, respErr := ioutil.ReadAll(r.Body)
if respErr != nil {
t.Error("projects mock didn't work")
}
req := strings.TrimSuffix(string(reqBytes), "\n")
expectedReq := `{"name":"my-great-project","description":null,"purpose":null,"environment":null,"is_default":null}`
if req != expectedReq {
t.Errorf("projects req didn't match up:\n expected %+v\n got %+v\n", expectedReq, req)
}
resp, _ := json.Marshal(updateResp)
fmt.Fprintf(w, fmt.Sprintf(`{"project":%s}`, string(resp)))
})
project, _, err := client.Projects.Update(ctx, "project-1", updateRequest)
if err != nil {
t.Errorf("Projects.Update returned error: %v", err)
}
if !reflect.DeepEqual(project, updateResp) {
t.Errorf("Projects.Update returned %+v, expected %+v", project, updateResp)
}
}
func TestProjects_UpdateWithAllAttributes(t *testing.T) {
setup()
defer teardown()
updateRequest := &UpdateProjectRequest{
Name: "my-great-project",
Description: "some-description",
Purpose: "some-purpose",
Environment: "some-env",
IsDefault: true,
}
updateResp := &Project{
ID: "project-id",
Name: updateRequest.Name.(string),
Description: updateRequest.Description.(string),
Purpose: updateRequest.Purpose.(string),
Environment: updateRequest.Environment.(string),
IsDefault: updateRequest.IsDefault.(bool),
}
mux.HandleFunc("/v2/projects/project-1", func(w http.ResponseWriter, r *http.Request) {
reqBytes, respErr := ioutil.ReadAll(r.Body)
if respErr != nil {
t.Error("projects mock didn't work")
}
req := strings.TrimSuffix(string(reqBytes), "\n")
expectedReq := `{"name":"my-great-project","description":"some-description","purpose":"some-purpose","environment":"some-env","is_default":true}`
if req != expectedReq {
t.Errorf("projects req didn't match up:\n expected %+v\n got %+v\n", expectedReq, req)
}
resp, _ := json.Marshal(updateResp)
fmt.Fprintf(w, fmt.Sprintf(`{"project":%s}`, string(resp)))
})
project, _, err := client.Projects.Update(ctx, "project-1", updateRequest)
if err != nil {
t.Errorf("Projects.Update returned error: %v", err)
}
if !reflect.DeepEqual(project, updateResp) {
t.Errorf("Projects.Update returned %+v, expected %+v", project, updateResp)
}
}
func TestProjects_Destroy(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/projects/project-1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := client.Projects.Delete(ctx, "project-1")
if err != nil {
t.Errorf("Projects.Delete returned error: %v", err)
}
}
func TestProjects_ListResources(t *testing.T) {
setup()
defer teardown()
expectedResources := []ProjectResource{
{
URN: "do:droplet:1",
AssignedAt: "2018-09-27 00:00:00",
Links: &ProjectResourceLinks{
Self: "http://example.com/v2/droplets/1",
},
},
{
URN: "do:floatingip:1.2.3.4",
AssignedAt: "2018-09-27 00:00:00",
Links: &ProjectResourceLinks{
Self: "http://example.com/v2/floating_ips/1.2.3.4",
},
},
{
URN: "do:reservedip:1.2.3.4",
AssignedAt: "2018-09-27 00:00:00",
Links: &ProjectResourceLinks{
Self: "http://example.com/v2/reserved_ips/1.2.3.4",
},
},
}
mux.HandleFunc("/v2/projects/project-1/resources", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
resp, _ := json.Marshal(expectedResources)
fmt.Fprint(w, fmt.Sprintf(`{"resources":%s, "meta": {"total": 2}}`, string(resp)))
})
resources, resp, err := client.Projects.ListResources(ctx, "project-1", nil)
if err != nil {
t.Errorf("Projects.List returned error: %v", err)
}
if !reflect.DeepEqual(resources, expectedResources) {
t.Errorf("Projects.ListResources returned resources %+v, expected %+v", resources, expectedResources)
}
expectedMeta := &Meta{Total: 2}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Projects.ListResources returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}
func TestProjects_ListResourcesWithMultiplePages(t *testing.T) {
setup()
defer teardown()
mockResp := `
{
"resources": [
{
"urn": "do:droplet:1",
"assigned_at": "2018-09-27 00:00:00",
"links": {
"self": "http://example.com/v2/droplets/1"
}
},
{
"urn": "do:floatingip:1.2.3.4",
"assigned_at": "2018-09-27 00:00:00",
"links": {
"self": "http://example.com/v2/floating_ips/1.2.3.4"
}
},
{
"urn": "do:reservedip:1.2.3.4",
"assigned_at": "2018-09-27 00:00:00",
"links": {
"self": "http://example.com/v2/reserved_ips/1.2.3.4"
}
}
],
"links": []
}`
mux.HandleFunc("/v2/projects/project-1/resources", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, mockResp)
})
_, _, err := client.Projects.ListResources(ctx, "project-1", nil)
if err != nil {
t.Errorf("Projects.ListResources returned error: %v", err)
}
}
func TestProjects_ListResourcesWithPageNumber(t *testing.T) {
setup()
defer teardown()
mockResp := `
{
"resources": [
{
"urn": "do:droplet:1",
"assigned_at": "2018-09-27 00:00:00",
"links": {
"self": "http://example.com/v2/droplets/1"
}
},
{
"urn": "do:floatingip:1.2.3.4",
"assigned_at": "2018-09-27 00:00:00",
"links": {
"self": "http://example.com/v2/floating_ips/1.2.3.4"
}
},
{
"urn": "do:reservedip:1.2.3.4",
"assigned_at": "2018-09-27 00:00:00",
"links": {
"self": "http://example.com/v2/reserved_ips/1.2.3.4"
}
}
],
"links": []
}`
mux.HandleFunc("/v2/projects/project-1/resources", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, mockResp)
})
_, _, err := client.Projects.ListResources(ctx, "project-1", &ListOptions{Page: 2})
if err != nil {
t.Errorf("Projects.ListResources returned error: %v", err)
}
}
func TestProjects_AssignFleetResourcesWithTypes(t *testing.T) {
setup()
defer teardown()
assignableResources := []interface{}{
&Droplet{ID: 1234},
&FloatingIP{IP: "1.2.3.4"},
&ReservedIP{IP: "1.2.3.4"},
}
mockResp := `
{
"resources": [
{
"urn": "do:droplet:1234",
"assigned_at": "2018-09-27 00:00:00",
"links": {
"self": "http://example.com/v2/droplets/1"
}
},
{
"urn": "do:floatingip:1.2.3.4",
"assigned_at": "2018-09-27 00:00:00",
"links": {
"self": "http://example.com/v2/floating_ips/1.2.3.4"
}
},
{
"urn": "do:reservedip:1.2.3.4",
"assigned_at": "2018-09-27 00:00:00",
"links": {
"self": "http://example.com/v2/reserved_ips/1.2.3.4"
}
}
]
}`
mux.HandleFunc("/v2/projects/project-1/resources", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
reqBytes, respErr := ioutil.ReadAll(r.Body)
if respErr != nil {
t.Error("projects mock didn't work")
}
req := strings.TrimSuffix(string(reqBytes), "\n")
expectedReq := `{"resources":["do:droplet:1234","do:floatingip:1.2.3.4","do:reservedip:1.2.3.4"]}`
if req != expectedReq {
t.Errorf("projects assign req didn't match up:\n expected %+v\n got %+v\n", expectedReq, req)
}
fmt.Fprint(w, mockResp)
})
_, _, err := client.Projects.AssignResources(ctx, "project-1", assignableResources...)
if err != nil {
t.Errorf("Projects.AssignResources returned error: %v", err)
}
}
func TestProjects_AssignFleetResourcesWithStrings(t *testing.T) {
setup()
defer teardown()
assignableResources := []interface{}{
"do:droplet:1234",
"do:floatingip:1.2.3.4",
"do:reservedip:1.2.3.4",
}
mockResp := `
{
"resources": [
{
"urn": "do:droplet:1234",
"assigned_at": "2018-09-27 00:00:00",
"links": {
"self": "http://example.com/v2/droplets/1"
}
},
{
"urn": "do:floatingip:1.2.3.4",
"assigned_at": "2018-09-27 00:00:00",
"links": {
"self": "http://example.com/v2/floating_ips/1.2.3.4"
}
},
{
"urn": "do:reservedip:1.2.3.4",
"assigned_at": "2018-09-27 00:00:00",
"links": {
"self": "http://example.com/v2/reserved_ips/1.2.3.4"
}
}
]
}`
mux.HandleFunc("/v2/projects/project-1/resources", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
reqBytes, respErr := ioutil.ReadAll(r.Body)
if respErr != nil {
t.Error("projects mock didn't work")
}
req := strings.TrimSuffix(string(reqBytes), "\n")
expectedReq := `{"resources":["do:droplet:1234","do:floatingip:1.2.3.4","do:reservedip:1.2.3.4"]}`
if req != expectedReq {
t.Errorf("projects assign req didn't match up:\n expected %+v\n got %+v\n", expectedReq, req)
}
fmt.Fprint(w, mockResp)
})
_, _, err := client.Projects.AssignResources(ctx, "project-1", assignableResources...)
if err != nil {
t.Errorf("Projects.AssignResources returned error: %v", err)
}
}
func TestProjects_AssignFleetResourcesWithStringsAndTypes(t *testing.T) {
setup()
defer teardown()
assignableResources := []interface{}{
"do:droplet:1234",
&FloatingIP{IP: "1.2.3.4"},
&ReservedIP{IP: "1.2.3.4"},
}
mockResp := `
{
"resources": [
{
"urn": "do:droplet:1234",
"assigned_at": "2018-09-27 00:00:00",
"links": {
"self": "http://example.com/v2/droplets/1"
}
},
{
"urn": "do:floatingip:1.2.3.4",
"assigned_at": "2018-09-27 00:00:00",
"links": {
"self": "http://example.com/v2/floating_ips/1.2.3.4"
}
},
{
"urn": "do:reservedip:1.2.3.4",
"assigned_at": "2018-09-27 00:00:00",
"links": {
"self": "http://example.com/v2/reserved_ips/1.2.3.4"
}
}
]
}`
mux.HandleFunc("/v2/projects/project-1/resources", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
reqBytes, respErr := ioutil.ReadAll(r.Body)
if respErr != nil {
t.Error("projects mock didn't work")
}
req := strings.TrimSuffix(string(reqBytes), "\n")
expectedReq := `{"resources":["do:droplet:1234","do:floatingip:1.2.3.4","do:reservedip:1.2.3.4"]}`
if req != expectedReq {
t.Errorf("projects assign req didn't match up:\n expected %+v\n got %+v\n", expectedReq, req)
}
fmt.Fprint(w, mockResp)
})
_, _, err := client.Projects.AssignResources(ctx, "project-1", assignableResources...)
if err != nil {
t.Errorf("Projects.AssignResources returned error: %v", err)
}
}
func TestProjects_AssignFleetResourcesWithTypeWithoutURNReturnsError(t *testing.T) {
setup()
defer teardown()
type fakeType struct{}
assignableResources := []interface{}{
fakeType{},
}
_, _, err := client.Projects.AssignResources(ctx, "project-1", assignableResources...)
if err == nil {
t.Errorf("expected Projects.AssignResources to error, but it did not")
}
if err.Error() != "godo.fakeType must either be a string or have a valid URN method" {
t.Errorf("Projects.AssignResources returned the wrong error: %v", err)
}
}

View File

@ -1,591 +0,0 @@
package godo
import (
"bytes"
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
)
const (
registryPath = "/v2/registry"
// RegistryServer is the hostname of the DigitalOcean registry service
RegistryServer = "registry.digitalocean.com"
)
// RegistryService is an interface for interfacing with the Registry endpoints
// of the DigitalOcean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Container-Registry
type RegistryService interface {
Create(context.Context, *RegistryCreateRequest) (*Registry, *Response, error)
Get(context.Context) (*Registry, *Response, error)
Delete(context.Context) (*Response, error)
DockerCredentials(context.Context, *RegistryDockerCredentialsRequest) (*DockerCredentials, *Response, error)
ListRepositories(context.Context, string, *ListOptions) ([]*Repository, *Response, error)
ListRepositoriesV2(context.Context, string, *TokenListOptions) ([]*RepositoryV2, *Response, error)
ListRepositoryTags(context.Context, string, string, *ListOptions) ([]*RepositoryTag, *Response, error)
DeleteTag(context.Context, string, string, string) (*Response, error)
ListRepositoryManifests(context.Context, string, string, *ListOptions) ([]*RepositoryManifest, *Response, error)
DeleteManifest(context.Context, string, string, string) (*Response, error)
StartGarbageCollection(context.Context, string, ...*StartGarbageCollectionRequest) (*GarbageCollection, *Response, error)
GetGarbageCollection(context.Context, string) (*GarbageCollection, *Response, error)
ListGarbageCollections(context.Context, string, *ListOptions) ([]*GarbageCollection, *Response, error)
UpdateGarbageCollection(context.Context, string, string, *UpdateGarbageCollectionRequest) (*GarbageCollection, *Response, error)
GetOptions(context.Context) (*RegistryOptions, *Response, error)
GetSubscription(context.Context) (*RegistrySubscription, *Response, error)
UpdateSubscription(context.Context, *RegistrySubscriptionUpdateRequest) (*RegistrySubscription, *Response, error)
}
var _ RegistryService = &RegistryServiceOp{}
// RegistryServiceOp handles communication with Registry methods of the DigitalOcean API.
type RegistryServiceOp struct {
client *Client
}
// RegistryCreateRequest represents a request to create a registry.
type RegistryCreateRequest struct {
Name string `json:"name,omitempty"`
SubscriptionTierSlug string `json:"subscription_tier_slug,omitempty"`
Region string `json:"region,omitempty"`
}
// RegistryDockerCredentialsRequest represents a request to retrieve docker
// credentials for a registry.
type RegistryDockerCredentialsRequest struct {
ReadWrite bool `json:"read_write"`
ExpirySeconds *int `json:"expiry_seconds,omitempty"`
}
// Registry represents a registry.
type Registry struct {
Name string `json:"name,omitempty"`
StorageUsageBytes uint64 `json:"storage_usage_bytes,omitempty"`
StorageUsageBytesUpdatedAt time.Time `json:"storage_usage_bytes_updated_at,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
Region string `json:"region,omitempty"`
}
// Repository represents a repository
type Repository struct {
RegistryName string `json:"registry_name,omitempty"`
Name string `json:"name,omitempty"`
LatestTag *RepositoryTag `json:"latest_tag,omitempty"`
TagCount uint64 `json:"tag_count,omitempty"`
}
// RepositoryV2 represents a repository in the V2 format
type RepositoryV2 struct {
RegistryName string `json:"registry_name,omitempty"`
Name string `json:"name,omitempty"`
TagCount uint64 `json:"tag_count,omitempty"`
ManifestCount uint64 `json:"manifest_count,omitempty"`
LatestManifest *RepositoryManifest `json:"latest_manifest,omitempty"`
}
// RepositoryTag represents a repository tag
type RepositoryTag struct {
RegistryName string `json:"registry_name,omitempty"`
Repository string `json:"repository,omitempty"`
Tag string `json:"tag,omitempty"`
ManifestDigest string `json:"manifest_digest,omitempty"`
CompressedSizeBytes uint64 `json:"compressed_size_bytes,omitempty"`
SizeBytes uint64 `json:"size_bytes,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}
// RepositoryManifest represents a repository manifest
type RepositoryManifest struct {
RegistryName string `json:"registry_name,omitempty"`
Repository string `json:"repository,omitempty"`
Digest string `json:"digest,omitempty"`
CompressedSizeBytes uint64 `json:"compressed_size_bytes,omitempty"`
SizeBytes uint64 `json:"size_bytes,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
Tags []string `json:"tags,omitempty"`
Blobs []*Blob `json:"blobs,omitempty"`
}
// Blob represents a registry blob
type Blob struct {
Digest string `json:"digest,omitempty"`
CompressedSizeBytes uint64 `json:"compressed_size_bytes,omitempty"`
}
type registryRoot struct {
Registry *Registry `json:"registry,omitempty"`
}
type repositoriesRoot struct {
Repositories []*Repository `json:"repositories,omitempty"`
Links []*LinkAction `json:"links,omitempty"`
Meta *Meta `json:"meta"`
}
type repositoriesV2Root struct {
Repositories []*RepositoryV2 `json:"repositories,omitempty"`
Links []*LinkAction `json:"links,omitempty"`
Meta *Meta `json:"meta"`
}
type repositoryTagsRoot struct {
Tags []*RepositoryTag `json:"tags,omitempty"`
Links []*LinkAction `json:"links,omitempty"`
Meta *Meta `json:"meta"`
}
type repositoryManifestsRoot struct {
Manifests []*RepositoryManifest `json:"manifests,omitempty"`
Links []*LinkAction `json:"links,omitempty"`
Meta *Meta `json:"meta"`
}
// GarbageCollection represents a garbage collection.
type GarbageCollection struct {
UUID string `json:"uuid"`
RegistryName string `json:"registry_name"`
Status string `json:"status"`
Type GarbageCollectionType `json:"type"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
BlobsDeleted uint64 `json:"blobs_deleted"`
FreedBytes uint64 `json:"freed_bytes"`
}
type garbageCollectionRoot struct {
GarbageCollection *GarbageCollection `json:"garbage_collection,omitempty"`
}
type garbageCollectionsRoot struct {
GarbageCollections []*GarbageCollection `json:"garbage_collections,omitempty"`
Links []*LinkAction `json:"links,omitempty"`
Meta *Meta `json:"meta"`
}
type GarbageCollectionType string
const (
// GCTypeUntaggedManifestsOnly indicates that a garbage collection should
// only delete untagged manifests.
GCTypeUntaggedManifestsOnly = GarbageCollectionType("untagged manifests only")
// GCTypeUnreferencedBlobsOnly indicates that a garbage collection should
// only delete unreferenced blobs.
GCTypeUnreferencedBlobsOnly = GarbageCollectionType("unreferenced blobs only")
// GCTypeUntaggedManifestsAndUnreferencedBlobs indicates that a garbage
// collection should delete both untagged manifests and unreferenced blobs.
GCTypeUntaggedManifestsAndUnreferencedBlobs = GarbageCollectionType("untagged manifests and unreferenced blobs")
)
// StartGarbageCollectionRequest represents options to a garbage collection
// start request.
type StartGarbageCollectionRequest struct {
Type GarbageCollectionType `json:"type"`
}
// UpdateGarbageCollectionRequest represents a request to update a garbage
// collection.
type UpdateGarbageCollectionRequest struct {
Cancel bool `json:"cancel"`
}
// RegistryOptions are options for users when creating or updating a registry.
type RegistryOptions struct {
SubscriptionTiers []*RegistrySubscriptionTier `json:"subscription_tiers,omitempty"`
AvailableRegions []string `json:"available_regions"`
}
type registryOptionsRoot struct {
Options *RegistryOptions `json:"options"`
}
// RegistrySubscriptionTier is a subscription tier for container registry.
type RegistrySubscriptionTier struct {
Name string `json:"name"`
Slug string `json:"slug"`
IncludedRepositories uint64 `json:"included_repositories"`
IncludedStorageBytes uint64 `json:"included_storage_bytes"`
AllowStorageOverage bool `json:"allow_storage_overage"`
IncludedBandwidthBytes uint64 `json:"included_bandwidth_bytes"`
MonthlyPriceInCents uint64 `json:"monthly_price_in_cents"`
Eligible bool `json:"eligible,omitempty"`
// EligibilityReasons is included when Eligible is false, and indicates the
// reasons why this tier is not available to the user.
EligibilityReasons []string `json:"eligibility_reasons,omitempty"`
}
// RegistrySubscription is a user's subscription.
type RegistrySubscription struct {
Tier *RegistrySubscriptionTier `json:"tier"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type registrySubscriptionRoot struct {
Subscription *RegistrySubscription `json:"subscription"`
}
// RegistrySubscriptionUpdateRequest represents a request to update the
// subscription plan for a registry.
type RegistrySubscriptionUpdateRequest struct {
TierSlug string `json:"tier_slug"`
}
// Get retrieves the details of a Registry.
func (svc *RegistryServiceOp) Get(ctx context.Context) (*Registry, *Response, error) {
req, err := svc.client.NewRequest(ctx, http.MethodGet, registryPath, nil)
if err != nil {
return nil, nil, err
}
root := new(registryRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Registry, resp, nil
}
// Create creates a registry.
func (svc *RegistryServiceOp) Create(ctx context.Context, create *RegistryCreateRequest) (*Registry, *Response, error) {
req, err := svc.client.NewRequest(ctx, http.MethodPost, registryPath, create)
if err != nil {
return nil, nil, err
}
root := new(registryRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Registry, resp, nil
}
// Delete deletes a registry. There is no way to recover a registry once it has
// been destroyed.
func (svc *RegistryServiceOp) Delete(ctx context.Context) (*Response, error) {
req, err := svc.client.NewRequest(ctx, http.MethodDelete, registryPath, nil)
if err != nil {
return nil, err
}
resp, err := svc.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
// DockerCredentials is the content of a Docker config file
// that is used by the docker CLI
// See: https://docs.docker.com/engine/reference/commandline/cli/#configjson-properties
type DockerCredentials struct {
DockerConfigJSON []byte
}
// DockerCredentials retrieves a Docker config file containing the registry's credentials.
func (svc *RegistryServiceOp) DockerCredentials(ctx context.Context, request *RegistryDockerCredentialsRequest) (*DockerCredentials, *Response, error) {
path := fmt.Sprintf("%s/%s", registryPath, "docker-credentials")
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
q := req.URL.Query()
q.Add("read_write", strconv.FormatBool(request.ReadWrite))
if request.ExpirySeconds != nil {
q.Add("expiry_seconds", strconv.Itoa(*request.ExpirySeconds))
}
req.URL.RawQuery = q.Encode()
var buf bytes.Buffer
resp, err := svc.client.Do(ctx, req, &buf)
if err != nil {
return nil, resp, err
}
dc := &DockerCredentials{
DockerConfigJSON: buf.Bytes(),
}
return dc, resp, nil
}
// ListRepositories returns a list of the Repositories visible with the registry's credentials.
func (svc *RegistryServiceOp) ListRepositories(ctx context.Context, registry string, opts *ListOptions) ([]*Repository, *Response, error) {
path := fmt.Sprintf("%s/%s/repositories", registryPath, registry)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(repositoriesRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Repositories, resp, nil
}
// ListRepositoriesV2 returns a list of the Repositories in a registry.
func (svc *RegistryServiceOp) ListRepositoriesV2(ctx context.Context, registry string, opts *TokenListOptions) ([]*RepositoryV2, *Response, error) {
path := fmt.Sprintf("%s/%s/repositoriesV2", registryPath, registry)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(repositoriesV2Root)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
resp.Links = root.Links
resp.Meta = root.Meta
return root.Repositories, resp, nil
}
// ListRepositoryTags returns a list of the RepositoryTags available within the given repository.
func (svc *RegistryServiceOp) ListRepositoryTags(ctx context.Context, registry, repository string, opts *ListOptions) ([]*RepositoryTag, *Response, error) {
path := fmt.Sprintf("%s/%s/repositories/%s/tags", registryPath, registry, url.PathEscape(repository))
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(repositoryTagsRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Tags, resp, nil
}
// DeleteTag deletes a tag within a given repository.
func (svc *RegistryServiceOp) DeleteTag(ctx context.Context, registry, repository, tag string) (*Response, error) {
path := fmt.Sprintf("%s/%s/repositories/%s/tags/%s", registryPath, registry, url.PathEscape(repository), tag)
req, err := svc.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := svc.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
// ListRepositoryManifests returns a list of the RepositoryManifests available within the given repository.
func (svc *RegistryServiceOp) ListRepositoryManifests(ctx context.Context, registry, repository string, opts *ListOptions) ([]*RepositoryManifest, *Response, error) {
path := fmt.Sprintf("%s/%s/repositories/%s/digests", registryPath, registry, url.PathEscape(repository))
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(repositoryManifestsRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
resp.Links = root.Links
resp.Meta = root.Meta
return root.Manifests, resp, nil
}
// DeleteManifest deletes a manifest by its digest within a given repository.
func (svc *RegistryServiceOp) DeleteManifest(ctx context.Context, registry, repository, digest string) (*Response, error) {
path := fmt.Sprintf("%s/%s/repositories/%s/digests/%s", registryPath, registry, url.PathEscape(repository), digest)
req, err := svc.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := svc.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
// StartGarbageCollection requests a garbage collection for the specified
// registry.
func (svc *RegistryServiceOp) StartGarbageCollection(ctx context.Context, registry string, request ...*StartGarbageCollectionRequest) (*GarbageCollection, *Response, error) {
path := fmt.Sprintf("%s/%s/garbage-collection", registryPath, registry)
var requestParams interface{}
if len(request) < 1 {
// default to only garbage collecting unreferenced blobs for backwards
// compatibility
requestParams = &StartGarbageCollectionRequest{
Type: GCTypeUnreferencedBlobsOnly,
}
} else {
requestParams = request[0]
}
req, err := svc.client.NewRequest(ctx, http.MethodPost, path, requestParams)
if err != nil {
return nil, nil, err
}
root := new(garbageCollectionRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.GarbageCollection, resp, err
}
// GetGarbageCollection retrieves the currently-active garbage collection for
// the specified registry; if there are no active garbage collections, then
// return a 404/NotFound error. There can only be one active garbage
// collection on a registry.
func (svc *RegistryServiceOp) GetGarbageCollection(ctx context.Context, registry string) (*GarbageCollection, *Response, error) {
path := fmt.Sprintf("%s/%s/garbage-collection", registryPath, registry)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(garbageCollectionRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.GarbageCollection, resp, nil
}
// ListGarbageCollections retrieves all garbage collections (active and
// inactive) for the specified registry.
func (svc *RegistryServiceOp) ListGarbageCollections(ctx context.Context, registry string, opts *ListOptions) ([]*GarbageCollection, *Response, error) {
path := fmt.Sprintf("%s/%s/garbage-collections", registryPath, registry)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(garbageCollectionsRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if root.Links != nil {
resp.Links = root.Links
}
if root.Meta != nil {
resp.Meta = root.Meta
}
return root.GarbageCollections, resp, nil
}
// UpdateGarbageCollection updates the specified garbage collection for the
// specified registry. While only the currently-active garbage collection can
// be updated we still require the exact garbage collection to be specified to
// avoid race conditions that might may arise from issuing an update to the
// implicit "currently-active" garbage collection. Returns the updated garbage
// collection.
func (svc *RegistryServiceOp) UpdateGarbageCollection(ctx context.Context, registry, gcUUID string, request *UpdateGarbageCollectionRequest) (*GarbageCollection, *Response, error) {
path := fmt.Sprintf("%s/%s/garbage-collection/%s", registryPath, registry, gcUUID)
req, err := svc.client.NewRequest(ctx, http.MethodPut, path, request)
if err != nil {
return nil, nil, err
}
root := new(garbageCollectionRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.GarbageCollection, resp, nil
}
// GetOptions returns options the user can use when creating or updating a
// registry.
func (svc *RegistryServiceOp) GetOptions(ctx context.Context) (*RegistryOptions, *Response, error) {
path := fmt.Sprintf("%s/options", registryPath)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(registryOptionsRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Options, resp, nil
}
// GetSubscription retrieves the user's subscription.
func (svc *RegistryServiceOp) GetSubscription(ctx context.Context) (*RegistrySubscription, *Response, error) {
path := fmt.Sprintf("%s/subscription", registryPath)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(registrySubscriptionRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Subscription, resp, nil
}
// UpdateSubscription updates the user's registry subscription.
func (svc *RegistryServiceOp) UpdateSubscription(ctx context.Context, request *RegistrySubscriptionUpdateRequest) (*RegistrySubscription, *Response, error) {
path := fmt.Sprintf("%s/subscription", registryPath)
req, err := svc.client.NewRequest(ctx, http.MethodPost, path, request)
if err != nil {
return nil, nil, err
}
root := new(registrySubscriptionRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Subscription, resp, nil
}

View File

@ -1,885 +0,0 @@
package godo
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
testRegistry = "test-registry"
testRegion = "r1"
testRepository = "test/repository"
testEncodedRepository = "test%2Frepository"
testTag = "test-tag"
testDigest = "sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f"
testCompressedSize = 2789669
testSize = 5843968
testGCBlobsDeleted = 42
testGCFreedBytes = 666
testGCStatus = "requested"
testGCUUID = "mew-mew-id"
testGCType = GCTypeUnreferencedBlobsOnly
)
var (
testTime = time.Date(2020, 4, 1, 0, 0, 0, 0, time.UTC)
testTimeString = testTime.Format(time.RFC3339)
testGarbageCollection = &GarbageCollection{
UUID: testGCUUID,
RegistryName: testRegistry,
Status: testGCStatus,
CreatedAt: testTime,
UpdatedAt: testTime,
BlobsDeleted: testGCBlobsDeleted,
FreedBytes: testGCFreedBytes,
Type: testGCType,
}
)
func TestRegistry_Create(t *testing.T) {
setup()
defer teardown()
want := &Registry{
Name: testRegistry,
StorageUsageBytes: 0,
StorageUsageBytesUpdatedAt: testTime,
CreatedAt: testTime,
Region: testRegion,
}
createRequest := &RegistryCreateRequest{
Name: want.Name,
SubscriptionTierSlug: "basic",
Region: testRegion,
}
createResponseJSON := `
{
"registry": {
"name": "` + testRegistry + `",
"storage_usage_bytes": 0,
"storage_usage_bytes_updated_at": "` + testTimeString + `",
"created_at": "` + testTimeString + `",
"region": "` + testRegion + `"
},
"subscription": {
"tier": {
"name": "Basic",
"slug": "basic",
"included_repositories": 5,
"included_storage_bytes": 5368709120,
"allow_storage_overage": true,
"included_bandwidth_bytes": 5368709120,
"monthly_price_in_cents": 500
},
"created_at": "` + testTimeString + `",
"updated_at": "` + testTimeString + `"
}
}`
mux.HandleFunc("/v2/registry", func(w http.ResponseWriter, r *http.Request) {
v := new(RegistryCreateRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPost)
require.Equal(t, v, createRequest)
fmt.Fprint(w, createResponseJSON)
})
got, _, err := client.Registry.Create(ctx, createRequest)
require.NoError(t, err)
require.Equal(t, want, got)
}
func TestRegistry_Get(t *testing.T) {
setup()
defer teardown()
want := &Registry{
Name: testRegistry,
StorageUsageBytes: 0,
StorageUsageBytesUpdatedAt: testTime,
CreatedAt: testTime,
Region: testRegion,
}
getResponseJSON := `
{
"registry": {
"name": "` + testRegistry + `",
"storage_usage_bytes": 0,
"storage_usage_bytes_updated_at": "` + testTimeString + `",
"created_at": "` + testTimeString + `",
"region": "` + testRegion + `"
}
}`
mux.HandleFunc("/v2/registry", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, getResponseJSON)
})
got, _, err := client.Registry.Get(ctx)
require.NoError(t, err)
require.Equal(t, want, got)
}
func TestRegistry_Delete(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/registry", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := client.Registry.Delete(ctx)
require.NoError(t, err)
}
func TestRegistry_DockerCredentials(t *testing.T) {
returnedConfig := "this could be a docker config"
tests := []struct {
name string
params *RegistryDockerCredentialsRequest
expectedReadWrite string
expectedExpirySeconds string
}{
{
name: "read-only (default)",
params: &RegistryDockerCredentialsRequest{},
expectedReadWrite: "false",
},
{
name: "read/write",
params: &RegistryDockerCredentialsRequest{ReadWrite: true},
expectedReadWrite: "true",
},
{
name: "read-only + custom expiry",
params: &RegistryDockerCredentialsRequest{ExpirySeconds: intPtr(60 * 60)},
expectedReadWrite: "false",
expectedExpirySeconds: "3600",
},
{
name: "read/write + custom expiry",
params: &RegistryDockerCredentialsRequest{ReadWrite: true, ExpirySeconds: intPtr(60 * 60)},
expectedReadWrite: "true",
expectedExpirySeconds: "3600",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/registry/docker-credentials", func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, test.expectedReadWrite, r.URL.Query().Get("read_write"))
require.Equal(t, test.expectedExpirySeconds, r.URL.Query().Get("expiry_seconds"))
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, returnedConfig)
})
got, _, err := client.Registry.DockerCredentials(ctx, test.params)
require.NoError(t, err)
require.Equal(t, []byte(returnedConfig), got.DockerConfigJSON)
})
}
}
func TestRepository_List(t *testing.T) {
setup()
defer teardown()
wantRepositories := []*Repository{
{
RegistryName: testRegistry,
Name: testRepository,
TagCount: 1,
LatestTag: &RepositoryTag{
RegistryName: testRegistry,
Repository: testRepository,
Tag: testTag,
ManifestDigest: testDigest,
CompressedSizeBytes: testCompressedSize,
SizeBytes: testSize,
UpdatedAt: testTime,
},
},
}
getResponseJSON := `{
"repositories": [
{
"registry_name": "` + testRegistry + `",
"name": "` + testRepository + `",
"tag_count": 1,
"latest_tag": {
"registry_name": "` + testRegistry + `",
"repository": "` + testRepository + `",
"tag": "` + testTag + `",
"manifest_digest": "` + testDigest + `",
"compressed_size_bytes": ` + fmt.Sprintf("%d", testCompressedSize) + `,
"size_bytes": ` + fmt.Sprintf("%d", testSize) + `,
"updated_at": "` + testTimeString + `"
}
}
],
"links": [],
"meta": {
"total": 2
}
}`
mux.HandleFunc(fmt.Sprintf("/v2/registry/%s/repositories", testRegistry), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
testFormValues(t, r, map[string]string{"page": "1", "per_page": "1"})
fmt.Fprint(w, getResponseJSON)
})
got, response, err := client.Registry.ListRepositories(ctx, testRegistry, &ListOptions{Page: 1, PerPage: 1})
require.NoError(t, err)
require.Equal(t, wantRepositories, got)
gotRespLinks := response.Links
wantRespLinks := []*LinkAction{}
assert.Equal(t, wantRespLinks, gotRespLinks)
gotRespMeta := response.Meta
wantRespMeta := &Meta{
Total: 2,
}
assert.Equal(t, wantRespMeta, gotRespMeta)
}
func TestRepository_ListV2(t *testing.T) {
setup()
defer teardown()
wantRepositories := []*RepositoryV2{
{
RegistryName: testRegistry,
Name: testRepository,
TagCount: 2,
ManifestCount: 1,
LatestManifest: &RepositoryManifest{
Digest: "sha256:abc",
RegistryName: testRegistry,
Repository: testRepository,
CompressedSizeBytes: testCompressedSize,
SizeBytes: testSize,
UpdatedAt: testTime,
Tags: []string{"v1", "v2"},
Blobs: []*Blob{
{
Digest: "sha256:blob1",
CompressedSizeBytes: 100,
},
{
Digest: "sha256:blob2",
CompressedSizeBytes: 200,
},
},
},
},
}
getResponseJSON := `{
"repositories": [
{
"registry_name": "` + testRegistry + `",
"name": "` + testRepository + `",
"tag_count": 2,
"manifest_count": 1,
"latest_manifest": {
"digest": "sha256:abc",
"registry_name": "` + testRegistry + `",
"repository": "` + testRepository + `",
"compressed_size_bytes": ` + fmt.Sprintf("%d", testCompressedSize) + `,
"size_bytes": ` + fmt.Sprintf("%d", testSize) + `,
"updated_at": "` + testTimeString + `",
"tags": [
"v1",
"v2"
],
"blobs": [
{
"digest": "sha256:blob1",
"compressed_size_bytes": 100
},
{
"digest": "sha256:blob2",
"compressed_size_bytes": 200
}
]
}
}
],
"links": [],
"meta": {
"total": 5
}
}`
mux.HandleFunc(fmt.Sprintf("/v2/registry/%s/repositoriesV2", testRegistry), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
testFormValues(t, r, map[string]string{"page": "3", "per_page": "1", "page_token": "bbb"})
fmt.Fprint(w, getResponseJSON)
})
got, response, err := client.Registry.ListRepositoriesV2(ctx, testRegistry, &TokenListOptions{Page: 3, PerPage: 1, Token: "bbb"})
require.NoError(t, err)
require.Equal(t, wantRepositories, got)
gotRespLinks := response.Links
wantRespLinks := []*LinkAction{}
assert.Equal(t, wantRespLinks, gotRespLinks)
gotRespMeta := response.Meta
wantRespMeta := &Meta{
Total: 5,
}
assert.Equal(t, wantRespMeta, gotRespMeta)
}
func TestRepository_ListTags(t *testing.T) {
setup()
defer teardown()
wantTags := []*RepositoryTag{
{
RegistryName: testRegistry,
Repository: testRepository,
Tag: testTag,
ManifestDigest: testDigest,
CompressedSizeBytes: testCompressedSize,
SizeBytes: testSize,
UpdatedAt: testTime,
},
}
getResponseJSON := `{
"tags": [
{
"registry_name": "` + testRegistry + `",
"repository": "` + testRepository + `",
"tag": "` + testTag + `",
"manifest_digest": "` + testDigest + `",
"compressed_size_bytes": ` + fmt.Sprintf("%d", testCompressedSize) + `,
"size_bytes": ` + fmt.Sprintf("%d", testSize) + `,
"updated_at": "` + testTimeString + `"
}
],
"links": [],
"meta": {
"total": 2
}
}`
mux.HandleFunc(fmt.Sprintf("/v2/registry/%s/repositories/%s/tags", testRegistry, testRepository), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
testFormValues(t, r, map[string]string{"page": "1", "per_page": "1"})
fmt.Fprint(w, getResponseJSON)
})
got, response, err := client.Registry.ListRepositoryTags(ctx, testRegistry, testRepository, &ListOptions{Page: 1, PerPage: 1})
require.NoError(t, err)
require.Equal(t, wantTags, got)
gotRespLinks := response.Links
wantRespLinks := []*LinkAction{}
assert.Equal(t, wantRespLinks, gotRespLinks)
gotRespMeta := response.Meta
wantRespMeta := &Meta{
Total: 2,
}
assert.Equal(t, wantRespMeta, gotRespMeta)
}
func TestRegistry_DeleteTag(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc(fmt.Sprintf("/v2/registry/%s/repositories/%s/tags/%s", testRegistry, testRepository, testTag), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := client.Registry.DeleteTag(ctx, testRegistry, testRepository, testTag)
require.NoError(t, err)
}
func TestRegistry_ListManifests(t *testing.T) {
setup()
defer teardown()
wantTags := []*RepositoryManifest{
{
RegistryName: testRegistry,
Repository: testRepository,
Digest: testDigest,
CompressedSizeBytes: testCompressedSize,
SizeBytes: testSize,
UpdatedAt: testTime,
Tags: []string{"latest", "v1", "v2"},
Blobs: []*Blob{
{
Digest: "sha256:blob1",
CompressedSizeBytes: 998,
},
{
Digest: "sha256:blob2",
CompressedSizeBytes: 1,
},
},
},
}
getResponseJSON := `{
"manifests": [
{
"registry_name": "` + testRegistry + `",
"repository": "` + testRepository + `",
"digest": "` + testDigest + `",
"compressed_size_bytes": ` + fmt.Sprintf("%d", testCompressedSize) + `,
"size_bytes": ` + fmt.Sprintf("%d", testSize) + `,
"updated_at": "` + testTimeString + `",
"tags": [ "latest", "v1", "v2" ],
"blobs": [
{
"digest": "sha256:blob1",
"compressed_size_bytes": 998
},
{
"digest": "sha256:blob2",
"compressed_size_bytes": 1
}
]
}
],
"links": [],
"meta": {
"total": 5
}
}`
mux.HandleFunc(fmt.Sprintf("/v2/registry/%s/repositories/%s/digests", testRegistry, testRepository), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
testFormValues(t, r, map[string]string{"page": "3", "per_page": "1"})
fmt.Fprint(w, getResponseJSON)
})
got, response, err := client.Registry.ListRepositoryManifests(ctx, testRegistry, testRepository, &ListOptions{Page: 3, PerPage: 1})
require.NoError(t, err)
require.Equal(t, wantTags, got)
gotRespLinks := response.Links
wantRespLinks := []*LinkAction{}
assert.Equal(t, wantRespLinks, gotRespLinks)
gotRespMeta := response.Meta
wantRespMeta := &Meta{
Total: 5,
}
assert.Equal(t, wantRespMeta, gotRespMeta)
}
func TestRegistry_DeleteManifest(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc(fmt.Sprintf("/v2/registry/%s/repositories/%s/digests/%s", testRegistry, testRepository, testDigest), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := client.Registry.DeleteManifest(ctx, testRegistry, testRepository, testDigest)
require.NoError(t, err)
}
func reifyTemplateStr(t *testing.T, tmplStr string, v interface{}) string {
tmpl, err := template.New("meow").Parse(tmplStr)
require.NoError(t, err)
s := &strings.Builder{}
err = tmpl.Execute(s, v)
require.NoError(t, err)
return s.String()
}
func TestGarbageCollection_Start(t *testing.T) {
setup()
defer teardown()
want := testGarbageCollection
requestResponseJSONTmpl := `
{
"garbage_collection": {
"uuid": "{{.UUID}}",
"registry_name": "{{.RegistryName}}",
"status": "{{.Status}}",
"type": "{{.Type}}",
"created_at": "{{.CreatedAt.Format "2006-01-02T15:04:05Z07:00"}}",
"updated_at": "{{.UpdatedAt.Format "2006-01-02T15:04:05Z07:00"}}",
"blobs_deleted": {{.BlobsDeleted}},
"freed_bytes": {{.FreedBytes}}
}
}`
requestResponseJSON := reifyTemplateStr(t, requestResponseJSONTmpl, want)
createRequest := &StartGarbageCollectionRequest{
Type: GCTypeUnreferencedBlobsOnly,
}
mux.HandleFunc("/v2/registry/"+testRegistry+"/garbage-collection",
func(w http.ResponseWriter, r *http.Request) {
v := new(StartGarbageCollectionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPost)
require.Equal(t, v, createRequest)
fmt.Fprint(w, requestResponseJSON)
})
got, _, err := client.Registry.StartGarbageCollection(ctx, testRegistry)
require.NoError(t, err)
require.Equal(t, want, got)
}
func TestGarbageCollection_Get(t *testing.T) {
setup()
defer teardown()
want := testGarbageCollection
requestResponseJSONTmpl := `
{
"garbage_collection": {
"uuid": "{{.UUID}}",
"registry_name": "{{.RegistryName}}",
"status": "{{.Status}}",
"type": "{{.Type}}",
"created_at": "{{.CreatedAt.Format "2006-01-02T15:04:05Z07:00"}}",
"updated_at": "{{.UpdatedAt.Format "2006-01-02T15:04:05Z07:00"}}",
"blobs_deleted": {{.BlobsDeleted}},
"freed_bytes": {{.FreedBytes}}
}
}`
requestResponseJSON := reifyTemplateStr(t, requestResponseJSONTmpl, want)
mux.HandleFunc("/v2/registry/"+testRegistry+"/garbage-collection",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, requestResponseJSON)
})
got, _, err := client.Registry.GetGarbageCollection(ctx, testRegistry)
require.NoError(t, err)
require.Equal(t, want, got)
}
func TestGarbageCollection_List(t *testing.T) {
setup()
defer teardown()
want := []*GarbageCollection{testGarbageCollection}
requestResponseJSONTmpl := `
{
"garbage_collections": [
{
"uuid": "{{.UUID}}",
"registry_name": "{{.RegistryName}}",
"status": "{{.Status}}",
"type": "{{.Type}}",
"created_at": "{{.CreatedAt.Format "2006-01-02T15:04:05Z07:00"}}",
"updated_at": "{{.UpdatedAt.Format "2006-01-02T15:04:05Z07:00"}}",
"blobs_deleted": {{.BlobsDeleted}},
"freed_bytes": {{.FreedBytes}}
}
],
"links": [],
"meta": {
"total": 2
}
}`
requestResponseJSON := reifyTemplateStr(t, requestResponseJSONTmpl, testGarbageCollection)
mux.HandleFunc("/v2/registry/"+testRegistry+"/garbage-collections",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
testFormValues(t, r, map[string]string{"page": "1", "per_page": "1"})
fmt.Fprint(w, requestResponseJSON)
})
got, resp, err := client.Registry.ListGarbageCollections(ctx, testRegistry, &ListOptions{Page: 1, PerPage: 1})
require.NoError(t, err)
require.Equal(t, want, got)
gotRespLinks := resp.Links
wantRespLinks := []*LinkAction{}
assert.Equal(t, wantRespLinks, gotRespLinks)
gotRespMeta := resp.Meta
wantRespMeta := &Meta{
Total: 2,
}
assert.Equal(t, wantRespMeta, gotRespMeta)
}
func TestGarbageCollection_Update(t *testing.T) {
setup()
defer teardown()
updateRequest := &UpdateGarbageCollectionRequest{
Cancel: true,
}
want := testGarbageCollection
requestResponseJSONTmpl := `
{
"garbage_collection": {
"uuid": "{{.UUID}}",
"registry_name": "{{.RegistryName}}",
"status": "{{.Status}}",
"type": "{{.Type}}",
"created_at": "{{.CreatedAt.Format "2006-01-02T15:04:05Z07:00"}}",
"updated_at": "{{.UpdatedAt.Format "2006-01-02T15:04:05Z07:00"}}",
"blobs_deleted": {{.BlobsDeleted}},
"freed_bytes": {{.FreedBytes}}
}
}`
requestResponseJSON := reifyTemplateStr(t, requestResponseJSONTmpl, want)
mux.HandleFunc("/v2/registry/"+testRegistry+"/garbage-collection/"+testGCUUID,
func(w http.ResponseWriter, r *http.Request) {
v := new(UpdateGarbageCollectionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPut)
require.Equal(t, v, updateRequest)
fmt.Fprint(w, requestResponseJSON)
})
got, _, err := client.Registry.UpdateGarbageCollection(ctx, testRegistry, testGCUUID, updateRequest)
require.NoError(t, err)
require.Equal(t, want, got)
}
func TestRegistry_GetOptions(t *testing.T) {
responseJSON := `
{
"options": {
"available_regions": [
"r1",
"r2"
],
"subscription_tiers": [
{
"name": "Starter",
"slug": "starter",
"included_repositories": 1,
"included_storage_bytes": 524288000,
"allow_storage_overage": false,
"included_bandwidth_bytes": 524288000,
"monthly_price_in_cents": 0,
"eligible": false,
"eligibility_reasons": [
"OverStorageLimit",
"OverRepositoryLimit"
]
},
{
"name": "Basic",
"slug": "basic",
"included_repositories": 5,
"included_storage_bytes": 5368709120,
"allow_storage_overage": true,
"included_bandwidth_bytes": 5368709120,
"monthly_price_in_cents": 500,
"eligible": false,
"eligibility_reasons": [
"OverRepositoryLimit"
]
},
{
"name": "Professional",
"slug": "professional",
"included_repositories": 0,
"included_storage_bytes": 107374182400,
"allow_storage_overage": true,
"included_bandwidth_bytes": 107374182400,
"monthly_price_in_cents": 2000,
"eligible": true
}
]
}
}`
want := &RegistryOptions{
AvailableRegions: []string{
"r1",
"r2",
},
SubscriptionTiers: []*RegistrySubscriptionTier{
{
Name: "Starter",
Slug: "starter",
IncludedRepositories: 1,
IncludedStorageBytes: 524288000,
AllowStorageOverage: false,
IncludedBandwidthBytes: 524288000,
MonthlyPriceInCents: 0,
Eligible: false,
EligibilityReasons: []string{
"OverStorageLimit",
"OverRepositoryLimit",
},
},
{
Name: "Basic",
Slug: "basic",
IncludedRepositories: 5,
IncludedStorageBytes: 5368709120,
AllowStorageOverage: true,
IncludedBandwidthBytes: 5368709120,
MonthlyPriceInCents: 500,
Eligible: false,
EligibilityReasons: []string{
"OverRepositoryLimit",
},
},
{
Name: "Professional",
Slug: "professional",
IncludedRepositories: 0,
IncludedStorageBytes: 107374182400,
AllowStorageOverage: true,
IncludedBandwidthBytes: 107374182400,
MonthlyPriceInCents: 2000,
Eligible: true,
},
},
}
setup()
defer teardown()
mux.HandleFunc("/v2/registry/options", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, responseJSON)
})
got, _, err := client.Registry.GetOptions(ctx)
require.NoError(t, err)
require.Equal(t, want, got)
}
func TestRegistry_GetSubscription(t *testing.T) {
setup()
defer teardown()
want := &RegistrySubscription{
Tier: &RegistrySubscriptionTier{
Name: "Basic",
Slug: "basic",
IncludedRepositories: 5,
IncludedStorageBytes: 5368709120,
AllowStorageOverage: true,
IncludedBandwidthBytes: 5368709120,
MonthlyPriceInCents: 500,
},
CreatedAt: testTime,
UpdatedAt: testTime,
}
getResponseJSON := `
{
"subscription": {
"tier": {
"name": "Basic",
"slug": "basic",
"included_repositories": 5,
"included_storage_bytes": 5368709120,
"allow_storage_overage": true,
"included_bandwidth_bytes": 5368709120,
"monthly_price_in_cents": 500
},
"created_at": "` + testTimeString + `",
"updated_at": "` + testTimeString + `"
}
}
`
mux.HandleFunc("/v2/registry/subscription", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, getResponseJSON)
})
got, _, err := client.Registry.GetSubscription(ctx)
require.NoError(t, err)
require.Equal(t, want, got)
}
func TestRegistry_UpdateSubscription(t *testing.T) {
setup()
defer teardown()
updateRequest := &RegistrySubscriptionUpdateRequest{
TierSlug: "professional",
}
want := &RegistrySubscription{
Tier: &RegistrySubscriptionTier{
Name: "Professional",
Slug: "professional",
IncludedRepositories: 0,
IncludedStorageBytes: 107374182400,
AllowStorageOverage: true,
IncludedBandwidthBytes: 107374182400,
MonthlyPriceInCents: 2000,
Eligible: true,
},
CreatedAt: testTime,
UpdatedAt: testTime,
}
updateResponseJSON := `{
"subscription": {
"tier": {
"name": "Professional",
"slug": "professional",
"included_repositories": 0,
"included_storage_bytes": 107374182400,
"allow_storage_overage": true,
"included_bandwidth_bytes": 107374182400,
"monthly_price_in_cents": 2000,
"eligible": true
},
"created_at": "` + testTimeString + `",
"updated_at": "` + testTimeString + `"
}
}`
mux.HandleFunc("/v2/registry/subscription",
func(w http.ResponseWriter, r *http.Request) {
v := new(RegistrySubscriptionUpdateRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPost)
require.Equal(t, v, updateRequest)
fmt.Fprint(w, updateResponseJSON)
})
got, _, err := client.Registry.UpdateSubscription(ctx, updateRequest)
require.NoError(t, err)
require.Equal(t, want, got)
}

View File

@ -1,145 +0,0 @@
package godo
import (
"context"
"fmt"
"net/http"
)
const resourceType = "ReservedIP"
const reservedIPsBasePath = "v2/reserved_ips"
// ReservedIPsService is an interface for interfacing with the reserved IPs
// endpoints of the Digital Ocean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Reserved-IPs
type ReservedIPsService interface {
List(context.Context, *ListOptions) ([]ReservedIP, *Response, error)
Get(context.Context, string) (*ReservedIP, *Response, error)
Create(context.Context, *ReservedIPCreateRequest) (*ReservedIP, *Response, error)
Delete(context.Context, string) (*Response, error)
}
// ReservedIPsServiceOp handles communication with the reserved IPs related methods of the
// DigitalOcean API.
type ReservedIPsServiceOp struct {
client *Client
}
var _ ReservedIPsService = &ReservedIPsServiceOp{}
// ReservedIP represents a Digital Ocean reserved IP.
type ReservedIP struct {
Region *Region `json:"region"`
Droplet *Droplet `json:"droplet"`
IP string `json:"ip"`
}
func (f ReservedIP) String() string {
return Stringify(f)
}
// URN returns the reserved IP in a valid DO API URN form.
func (f ReservedIP) URN() string {
return ToURN(resourceType, f.IP)
}
type reservedIPsRoot struct {
ReservedIPs []ReservedIP `json:"reserved_ips"`
Links []*LinkAction `json:"links"`
Meta *Meta `json:"meta"`
}
type reservedIPRoot struct {
ReservedIP *ReservedIP `json:"reserved_ip"`
Links []*LinkAction `json:"links,omitempty"`
}
// ReservedIPCreateRequest represents a request to create a reserved IP.
// Specify DropletID to assign the reserved IP to a Droplet or Region
// to reserve it to the region.
type ReservedIPCreateRequest struct {
Region string `json:"region,omitempty"`
DropletID int `json:"droplet_id,omitempty"`
}
// List all reserved IPs.
func (r *ReservedIPsServiceOp) List(ctx context.Context, opt *ListOptions) ([]ReservedIP, *Response, error) {
path := reservedIPsBasePath
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
req, err := r.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(reservedIPsRoot)
resp, err := r.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.ReservedIPs, resp, err
}
// Get an individual reserved IP.
func (r *ReservedIPsServiceOp) Get(ctx context.Context, ip string) (*ReservedIP, *Response, error) {
path := fmt.Sprintf("%s/%s", reservedIPsBasePath, ip)
req, err := r.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(reservedIPRoot)
resp, err := r.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.ReservedIP, resp, err
}
// Create a reserved IP. If the DropletID field of the request is not empty,
// the reserved IP will also be assigned to the droplet.
func (r *ReservedIPsServiceOp) Create(ctx context.Context, createRequest *ReservedIPCreateRequest) (*ReservedIP, *Response, error) {
path := reservedIPsBasePath
req, err := r.client.NewRequest(ctx, http.MethodPost, path, createRequest)
if err != nil {
return nil, nil, err
}
root := new(reservedIPRoot)
resp, err := r.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
return root.ReservedIP, resp, err
}
// Delete a reserved IP.
func (r *ReservedIPsServiceOp) Delete(ctx context.Context, ip string) (*Response, error) {
path := fmt.Sprintf("%s/%s", reservedIPsBasePath, ip)
req, err := r.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := r.client.Do(ctx, req, nil)
return resp, err
}

View File

@ -1,109 +0,0 @@
package godo
import (
"context"
"fmt"
"net/http"
)
// ReservedIPActionsService is an interface for interfacing with the
// reserved IPs actions endpoints of the Digital Ocean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Reserved-IP-Actions
type ReservedIPActionsService interface {
Assign(ctx context.Context, ip string, dropletID int) (*Action, *Response, error)
Unassign(ctx context.Context, ip string) (*Action, *Response, error)
Get(ctx context.Context, ip string, actionID int) (*Action, *Response, error)
List(ctx context.Context, ip string, opt *ListOptions) ([]Action, *Response, error)
}
// ReservedIPActionsServiceOp handles communication with the reserved IPs
// action related methods of the DigitalOcean API.
type ReservedIPActionsServiceOp struct {
client *Client
}
// Assign a reserved IP to a droplet.
func (s *ReservedIPActionsServiceOp) Assign(ctx context.Context, ip string, dropletID int) (*Action, *Response, error) {
request := &ActionRequest{
"type": "assign",
"droplet_id": dropletID,
}
return s.doAction(ctx, ip, request)
}
// Unassign a rerserved IP from the droplet it is currently assigned to.
func (s *ReservedIPActionsServiceOp) Unassign(ctx context.Context, ip string) (*Action, *Response, error) {
request := &ActionRequest{"type": "unassign"}
return s.doAction(ctx, ip, request)
}
// Get an action for a particular reserved IP by id.
func (s *ReservedIPActionsServiceOp) Get(ctx context.Context, ip string, actionID int) (*Action, *Response, error) {
path := fmt.Sprintf("%s/%d", reservedIPActionPath(ip), actionID)
return s.get(ctx, path)
}
// List the actions for a particular reserved IP.
func (s *ReservedIPActionsServiceOp) List(ctx context.Context, ip string, opt *ListOptions) ([]Action, *Response, error) {
path := reservedIPActionPath(ip)
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
return s.list(ctx, path)
}
func (s *ReservedIPActionsServiceOp) doAction(ctx context.Context, ip string, request *ActionRequest) (*Action, *Response, error) {
path := reservedIPActionPath(ip)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, request)
if err != nil {
return nil, nil, err
}
root := new(actionRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Event, resp, err
}
func (s *ReservedIPActionsServiceOp) get(ctx context.Context, path string) (*Action, *Response, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(actionRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Event, resp, err
}
func (s *ReservedIPActionsServiceOp) list(ctx context.Context, path string) ([]Action, *Response, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(actionsRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
return root.Actions, resp, err
}
func reservedIPActionPath(ip string) string {
return fmt.Sprintf("%s/%s/actions", reservedIPsBasePath, ip)
}

View File

@ -1,156 +0,0 @@
package godo
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestReservedIPsActions_Assign(t *testing.T) {
setup()
defer teardown()
dropletID := 12345
assignRequest := &ActionRequest{
"droplet_id": float64(dropletID), // encoding/json decodes numbers as floats
"type": "assign",
}
mux.HandleFunc("/v2/reserved_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, assignRequest) {
t.Errorf("Request body = %#v, expected %#v", v, assignRequest)
}
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})
assign, _, err := client.ReservedIPActions.Assign(ctx, "192.168.0.1", 12345)
if err != nil {
t.Errorf("ReservedIPsActions.Assign returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(assign, expected) {
t.Errorf("ReservedIPsActions.Assign returned %+v, expected %+v", assign, expected)
}
}
func TestReservedIPsActions_Unassign(t *testing.T) {
setup()
defer teardown()
unassignRequest := &ActionRequest{
"type": "unassign",
}
mux.HandleFunc("/v2/reserved_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, unassignRequest) {
t.Errorf("Request body = %+v, expected %+v", v, unassignRequest)
}
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})
action, _, err := client.ReservedIPActions.Unassign(ctx, "192.168.0.1")
if err != nil {
t.Errorf("ReservedIPsActions.Get returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(action, expected) {
t.Errorf("ReservedIPsActions.Get returned %+v, expected %+v", action, expected)
}
}
func TestReservedIPsActions_Get(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/reserved_ips/192.168.0.1/actions/456", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})
action, _, err := client.ReservedIPActions.Get(ctx, "192.168.0.1", 456)
if err != nil {
t.Errorf("ReservedIPsActions.Get returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(action, expected) {
t.Errorf("ReservedIPsActions.Get returned %+v, expected %+v", action, expected)
}
}
func TestReservedIPsActions_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/reserved_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprintf(w, `{"actions":[{"status":"in-progress"}]}`)
})
actions, _, err := client.ReservedIPActions.List(ctx, "192.168.0.1", nil)
if err != nil {
t.Errorf("ReservedIPsActions.List returned error: %v", err)
}
expected := []Action{{Status: "in-progress"}}
if !reflect.DeepEqual(actions, expected) {
t.Errorf("ReservedIPsActions.List returned %+v, expected %+v", actions, expected)
}
}
func TestReservedIPsActions_ListMultiplePages(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/reserved_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"actions":[{"status":"in-progress"}], "links":[]}`)
})
_, _, err := client.ReservedIPActions.List(ctx, "192.168.0.1", nil)
if err != nil {
t.Errorf("ReservedIPsActions.List returned error: %v", err)
}
}
func TestReservedIPsActions_ListPageByNumber(t *testing.T) {
setup()
defer teardown()
jBlob := `
{
"actions":[{"status":"in-progress"}],
"links":[]
}`
mux.HandleFunc("/v2/reserved_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})
opt := &ListOptions{Page: 2}
_, _, err := client.ReservedIPActions.List(ctx, "192.168.0.1", opt)
if err != nil {
t.Errorf("ReservedIPsActions.List returned error: %v", err)
}
}

View File

@ -1,145 +0,0 @@
package godo
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestReservedIPs_ListReservedIPs(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/reserved_ips", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"reserved_ips": [{"region":{"slug":"nyc3"},"droplet":{"id":1},"ip":"192.168.0.1"},{"region":{"slug":"nyc3"},"droplet":{"id":2},"ip":"192.168.0.2"}],"meta":{"total":2}}`)
})
reservedIPs, resp, err := client.ReservedIPs.List(ctx, nil)
if err != nil {
t.Errorf("ReservedIPs.List returned error: %v", err)
}
expectedReservedIPs := []ReservedIP{
{Region: &Region{Slug: "nyc3"}, Droplet: &Droplet{ID: 1}, IP: "192.168.0.1"},
{Region: &Region{Slug: "nyc3"}, Droplet: &Droplet{ID: 2}, IP: "192.168.0.2"},
}
if !reflect.DeepEqual(reservedIPs, expectedReservedIPs) {
t.Errorf("ReservedIPs.List returned reserved IPs %+v, expected %+v", reservedIPs, expectedReservedIPs)
}
expectedMeta := &Meta{
Total: 2,
}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("ReservedIPs.List returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}
func TestReservedIPs_ListReservedIPsMultiplePages(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/reserved_ips", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"reserved_ips": [{"region":{"slug":"nyc3"},"droplet":{"id":1},"ip":"192.168.0.1"},{"region":{"slug":"nyc3"},"droplet":{"id":2},"ip":"192.168.0.2"}], "links":[]}`)
})
_, _, err := client.ReservedIPs.List(ctx, nil)
if err != nil {
t.Fatal(err)
}
}
func TestReservedIPs_RetrievePageByNumber(t *testing.T) {
setup()
defer teardown()
jBlob := `
{
"reserved_ips": [{"region":{"slug":"nyc3"},"droplet":{"id":1},"ip":"192.168.0.1"},{"region":{"slug":"nyc3"},"droplet":{"id":2},"ip":"192.168.0.2"}],
"links":[]
}`
mux.HandleFunc("/v2/reserved_ips", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})
opt := &ListOptions{Page: 2}
_, _, err := client.ReservedIPs.List(ctx, opt)
if err != nil {
t.Fatal(err)
}
}
func TestReservedIPs_Get(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/reserved_ips/192.168.0.1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"reserved_ip":{"region":{"slug":"nyc3"},"droplet":{"id":1},"ip":"192.168.0.1"}}`)
})
reservedIP, _, err := client.ReservedIPs.Get(ctx, "192.168.0.1")
if err != nil {
t.Errorf("domain.Get returned error: %v", err)
}
expected := &ReservedIP{Region: &Region{Slug: "nyc3"}, Droplet: &Droplet{ID: 1}, IP: "192.168.0.1"}
if !reflect.DeepEqual(reservedIP, expected) {
t.Errorf("ReservedIPs.Get returned %+v, expected %+v", reservedIP, expected)
}
}
func TestReservedIPs_Create(t *testing.T) {
setup()
defer teardown()
createRequest := &ReservedIPCreateRequest{
Region: "nyc3",
DropletID: 1,
}
mux.HandleFunc("/v2/reserved_ips", func(w http.ResponseWriter, r *http.Request) {
v := new(ReservedIPCreateRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, createRequest) {
t.Errorf("Request body = %+v, expected %+v", v, createRequest)
}
fmt.Fprint(w, `{"reserved_ip":{"region":{"slug":"nyc3"},"droplet":{"id":1},"ip":"192.168.0.1"}}`)
})
reservedIP, _, err := client.ReservedIPs.Create(ctx, createRequest)
if err != nil {
t.Errorf("ReservedIPs.Create returned error: %v", err)
}
expected := &ReservedIP{Region: &Region{Slug: "nyc3"}, Droplet: &Droplet{ID: 1}, IP: "192.168.0.1"}
if !reflect.DeepEqual(reservedIP, expected) {
t.Errorf("ReservedIPs.Create returned %+v, expected %+v", reservedIP, expected)
}
}
func TestReservedIPs_Destroy(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/reserved_ips/192.168.0.1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := client.ReservedIPs.Delete(ctx, "192.168.0.1")
if err != nil {
t.Errorf("ReservedIPs.Delete returned error: %v", err)
}
}

View File

@ -1,142 +0,0 @@
package godo
import (
"context"
"fmt"
"net/http"
)
const snapshotBasePath = "v2/snapshots"
// SnapshotsService is an interface for interfacing with the snapshots
// endpoints of the DigitalOcean API
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Snapshots
type SnapshotsService interface {
List(context.Context, *ListOptions) ([]Snapshot, *Response, error)
ListVolume(context.Context, *ListOptions) ([]Snapshot, *Response, error)
ListDroplet(context.Context, *ListOptions) ([]Snapshot, *Response, error)
Get(context.Context, string) (*Snapshot, *Response, error)
Delete(context.Context, string) (*Response, error)
}
// SnapshotsServiceOp handles communication with the snapshot related methods of the
// DigitalOcean API.
type SnapshotsServiceOp struct {
client *Client
}
var _ SnapshotsService = &SnapshotsServiceOp{}
// Snapshot represents a DigitalOcean Snapshot
type Snapshot struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
ResourceID string `json:"resource_id,omitempty"`
ResourceType string `json:"resource_type,omitempty"`
Regions []string `json:"regions,omitempty"`
MinDiskSize int `json:"min_disk_size,omitempty"`
SizeGigaBytes float64 `json:"size_gigabytes,omitempty"`
Created string `json:"created_at,omitempty"`
Tags []string `json:"tags,omitempty"`
}
type snapshotRoot struct {
Snapshot *Snapshot `json:"snapshot"`
}
type snapshotsRoot struct {
Snapshots []Snapshot `json:"snapshots"`
Links []*LinkAction `json:"links,omitempty"`
Meta *Meta `json:"meta,omitempty"`
}
type listSnapshotOptions struct {
ResourceType string `url:"resource_type,omitempty"`
}
func (s Snapshot) String() string {
return Stringify(s)
}
// List lists all the snapshots available.
func (s *SnapshotsServiceOp) List(ctx context.Context, opt *ListOptions) ([]Snapshot, *Response, error) {
return s.list(ctx, opt, nil)
}
// ListDroplet lists all the Droplet snapshots.
func (s *SnapshotsServiceOp) ListDroplet(ctx context.Context, opt *ListOptions) ([]Snapshot, *Response, error) {
listOpt := listSnapshotOptions{ResourceType: "droplet"}
return s.list(ctx, opt, &listOpt)
}
// ListVolume lists all the volume snapshots.
func (s *SnapshotsServiceOp) ListVolume(ctx context.Context, opt *ListOptions) ([]Snapshot, *Response, error) {
listOpt := listSnapshotOptions{ResourceType: "volume"}
return s.list(ctx, opt, &listOpt)
}
// Get retrieves a snapshot by id.
func (s *SnapshotsServiceOp) Get(ctx context.Context, snapshotID string) (*Snapshot, *Response, error) {
return s.get(ctx, snapshotID)
}
// Delete an snapshot.
func (s *SnapshotsServiceOp) Delete(ctx context.Context, snapshotID string) (*Response, error) {
path := fmt.Sprintf("%s/%s", snapshotBasePath, snapshotID)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(ctx, req, nil)
return resp, err
}
// Helper method for getting an individual snapshot
func (s *SnapshotsServiceOp) get(ctx context.Context, ID string) (*Snapshot, *Response, error) {
path := fmt.Sprintf("%s/%s", snapshotBasePath, ID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(snapshotRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Snapshot, resp, err
}
// Helper method for listing snapshots
func (s *SnapshotsServiceOp) list(ctx context.Context, opt *ListOptions, listOpt *listSnapshotOptions) ([]Snapshot, *Response, error) {
path := snapshotBasePath
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
path, err = addOptions(path, listOpt)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(snapshotsRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
return root.Snapshots, resp, err
}

View File

@ -1,177 +0,0 @@
package godo
import (
"context"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestSnapshots_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/snapshots", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"snapshots":[{"id":"1"},{"id":"2", "size_gigabytes": 4.84}]}`)
})
ctx := context.Background()
snapshots, _, err := client.Snapshots.List(ctx, nil)
if err != nil {
t.Errorf("Snapshots.List returned error: %v", err)
}
expected := []Snapshot{{ID: "1"}, {ID: "2", SizeGigaBytes: 4.84}}
if !reflect.DeepEqual(snapshots, expected) {
t.Errorf("Snapshots.List returned %+v, expected %+v", snapshots, expected)
}
}
func TestSnapshots_ListVolume(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/snapshots", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
expected := "volume"
actual := r.URL.Query().Get("resource_type")
if actual != expected {
t.Errorf("'type' query = %v, expected %v", actual, expected)
}
fmt.Fprint(w, `{"snapshots":[{"id":"1"},{"id":"2"}]}`)
})
ctx := context.Background()
snapshots, _, err := client.Snapshots.ListVolume(ctx, nil)
if err != nil {
t.Errorf("Snapshots.ListVolume returned error: %v", err)
}
expected := []Snapshot{{ID: "1"}, {ID: "2"}}
if !reflect.DeepEqual(snapshots, expected) {
t.Errorf("Snapshots.ListVolume returned %+v, expected %+v", snapshots, expected)
}
}
func TestSnapshots_ListDroplet(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/snapshots", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
expected := "droplet"
actual := r.URL.Query().Get("resource_type")
if actual != expected {
t.Errorf("'resource_type' query = %v, expected %v", actual, expected)
}
fmt.Fprint(w, `{"snapshots":[{"id":"1"},{"id":"2", "size_gigabytes": 4.84}]}`)
})
ctx := context.Background()
snapshots, _, err := client.Snapshots.ListDroplet(ctx, nil)
if err != nil {
t.Errorf("Snapshots.ListDroplet returned error: %v", err)
}
expected := []Snapshot{{ID: "1"}, {ID: "2", SizeGigaBytes: 4.84}}
if !reflect.DeepEqual(snapshots, expected) {
t.Errorf("Snapshots.ListDroplet returned %+v, expected %+v", snapshots, expected)
}
}
func TestSnapshots_ListSnapshotsMultiplePages(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/snapshots", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"snapshots": [{"id":"1"},{"id":"2"}], "links":[]}`)
})
ctx := context.Background()
_, _, err := client.Snapshots.List(ctx, &ListOptions{Page: 2})
if err != nil {
t.Fatal(err)
}
}
func TestSnapshots_RetrievePageByNumber(t *testing.T) {
setup()
defer teardown()
jBlob := `
{
"snapshots": [{"id":"1"},{"id":"2"}],
"links":[]
}`
mux.HandleFunc("/v2/snapshots", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})
ctx := context.Background()
opt := &ListOptions{Page: 2}
_, _, err := client.Snapshots.List(ctx, opt)
if err != nil {
t.Fatal(err)
}
}
func TestSnapshots_GetSnapshotByID(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/snapshots/12345", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"snapshot":{"id":"12345"}}`)
})
ctx := context.Background()
snapshots, _, err := client.Snapshots.Get(ctx, "12345")
if err != nil {
t.Errorf("Snapshot.GetByID returned error: %v", err)
}
expected := &Snapshot{ID: "12345"}
if !reflect.DeepEqual(snapshots, expected) {
t.Errorf("Snapshots.GetByID returned %+v, expected %+v", snapshots, expected)
}
}
func TestSnapshots_Destroy(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/snapshots/12345", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
ctx := context.Background()
_, err := client.Snapshots.Delete(ctx, "12345")
if err != nil {
t.Errorf("Snapshot.Delete returned error: %v", err)
}
}
func TestSnapshot_String(t *testing.T) {
snapshot := &Snapshot{
ID: "1",
Name: "Snapsh176ot",
ResourceID: "0",
ResourceType: "droplet",
Regions: []string{"one"},
MinDiskSize: 20,
SizeGigaBytes: 4.84,
Created: "2013-11-27T09:24:55Z",
Tags: []string{"one", "two"},
}
stringified := snapshot.String()
expected := `godo.Snapshot{ID:"1", Name:"Snapsh176ot", ResourceID:"0", ResourceType:"droplet", Regions:["one"], MinDiskSize:20, SizeGigaBytes:4.84, Created:"2013-11-27T09:24:55Z", Tags:["one" "two"]}`
if expected != stringified {
t.Errorf("Snapshot.String returned %+v, expected %+v", stringified, expected)
}
}

View File

@ -1,262 +0,0 @@
package godo
import (
"context"
"fmt"
"net/http"
"time"
)
const (
storageBasePath = "v2"
storageAllocPath = storageBasePath + "/volumes"
storageSnapPath = storageBasePath + "/snapshots"
)
// StorageService is an interface for interfacing with the storage
// endpoints of the Digital Ocean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Block-Storage
type StorageService interface {
ListVolumes(context.Context, *ListVolumeParams) ([]Volume, *Response, error)
GetVolume(context.Context, string) (*Volume, *Response, error)
CreateVolume(context.Context, *VolumeCreateRequest) (*Volume, *Response, error)
DeleteVolume(context.Context, string) (*Response, error)
ListSnapshots(ctx context.Context, volumeID string, opts *ListOptions) ([]Snapshot, *Response, error)
GetSnapshot(context.Context, string) (*Snapshot, *Response, error)
CreateSnapshot(context.Context, *SnapshotCreateRequest) (*Snapshot, *Response, error)
DeleteSnapshot(context.Context, string) (*Response, error)
}
// StorageServiceOp handles communication with the storage volumes related methods of the
// DigitalOcean API.
type StorageServiceOp struct {
client *Client
}
// ListVolumeParams stores the options you can set for a ListVolumeCall
type ListVolumeParams struct {
Region string `json:"region"`
Name string `json:"name"`
ListOptions *ListOptions `json:"list_options,omitempty"`
}
var _ StorageService = &StorageServiceOp{}
// Volume represents a Digital Ocean block store volume.
type Volume struct {
ID string `json:"id"`
Region *Region `json:"region"`
Name string `json:"name"`
SizeGigaBytes int64 `json:"size_gigabytes"`
Description string `json:"description"`
DropletIDs []int `json:"droplet_ids"`
CreatedAt time.Time `json:"created_at"`
FilesystemType string `json:"filesystem_type"`
FilesystemLabel string `json:"filesystem_label"`
Tags []string `json:"tags"`
}
func (f Volume) String() string {
return Stringify(f)
}
// URN returns the volume ID as a valid DO API URN
func (f Volume) URN() string {
return ToURN("Volume", f.ID)
}
type storageVolumesRoot struct {
Volumes []Volume `json:"volumes"`
Links []*LinkAction `json:"links"`
Meta *Meta `json:"meta"`
}
type storageVolumeRoot struct {
Volume *Volume `json:"volume"`
Links []*LinkAction `json:"links,omitempty"`
}
// VolumeCreateRequest represents a request to create a block store
// volume.
type VolumeCreateRequest struct {
Region string `json:"region"`
Name string `json:"name"`
Description string `json:"description"`
SizeGigaBytes int64 `json:"size_gigabytes"`
SnapshotID string `json:"snapshot_id"`
FilesystemType string `json:"filesystem_type"`
FilesystemLabel string `json:"filesystem_label"`
Tags []string `json:"tags"`
}
// ListVolumes lists all storage volumes.
func (svc *StorageServiceOp) ListVolumes(ctx context.Context, params *ListVolumeParams) ([]Volume, *Response, error) {
path := storageAllocPath
if params != nil {
if params.Region != "" && params.Name != "" {
path = fmt.Sprintf("%s?name=%s&region=%s", path, params.Name, params.Region)
} else if params.Region != "" {
path = fmt.Sprintf("%s?region=%s", path, params.Region)
} else if params.Name != "" {
path = fmt.Sprintf("%s?name=%s", path, params.Name)
}
if params.ListOptions != nil {
var err error
path, err = addOptions(path, params.ListOptions)
if err != nil {
return nil, nil, err
}
}
}
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(storageVolumesRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Volumes, resp, nil
}
// CreateVolume creates a storage volume. The name must be unique.
func (svc *StorageServiceOp) CreateVolume(ctx context.Context, createRequest *VolumeCreateRequest) (*Volume, *Response, error) {
path := storageAllocPath
req, err := svc.client.NewRequest(ctx, http.MethodPost, path, createRequest)
if err != nil {
return nil, nil, err
}
root := new(storageVolumeRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Volume, resp, nil
}
// GetVolume retrieves an individual storage volume.
func (svc *StorageServiceOp) GetVolume(ctx context.Context, id string) (*Volume, *Response, error) {
path := fmt.Sprintf("%s/%s", storageAllocPath, id)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(storageVolumeRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Volume, resp, nil
}
// DeleteVolume deletes a storage volume.
func (svc *StorageServiceOp) DeleteVolume(ctx context.Context, id string) (*Response, error) {
path := fmt.Sprintf("%s/%s", storageAllocPath, id)
req, err := svc.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return svc.client.Do(ctx, req, nil)
}
// SnapshotCreateRequest represents a request to create a block store
// volume.
type SnapshotCreateRequest struct {
VolumeID string `json:"volume_id"`
Name string `json:"name"`
Description string `json:"description"`
Tags []string `json:"tags"`
}
// ListSnapshots lists all snapshots related to a storage volume.
func (svc *StorageServiceOp) ListSnapshots(ctx context.Context, volumeID string, opt *ListOptions) ([]Snapshot, *Response, error) {
path := fmt.Sprintf("%s/%s/snapshots", storageAllocPath, volumeID)
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(snapshotsRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Snapshots, resp, nil
}
// CreateSnapshot creates a snapshot of a storage volume.
func (svc *StorageServiceOp) CreateSnapshot(ctx context.Context, createRequest *SnapshotCreateRequest) (*Snapshot, *Response, error) {
path := fmt.Sprintf("%s/%s/snapshots", storageAllocPath, createRequest.VolumeID)
req, err := svc.client.NewRequest(ctx, http.MethodPost, path, createRequest)
if err != nil {
return nil, nil, err
}
root := new(snapshotRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Snapshot, resp, nil
}
// GetSnapshot retrieves an individual snapshot.
func (svc *StorageServiceOp) GetSnapshot(ctx context.Context, id string) (*Snapshot, *Response, error) {
path := fmt.Sprintf("%s/%s", storageSnapPath, id)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(snapshotRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Snapshot, resp, nil
}
// DeleteSnapshot deletes a snapshot.
func (svc *StorageServiceOp) DeleteSnapshot(ctx context.Context, id string) (*Response, error) {
path := fmt.Sprintf("%s/%s", storageSnapPath, id)
req, err := svc.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return svc.client.Do(ctx, req, nil)
}

View File

@ -1,132 +0,0 @@
package godo
import (
"context"
"fmt"
"net/http"
)
// StorageActionsService is an interface for interfacing with the
// storage actions endpoints of the Digital Ocean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Block-Storage-Actions
type StorageActionsService interface {
Attach(ctx context.Context, volumeID string, dropletID int) (*Action, *Response, error)
DetachByDropletID(ctx context.Context, volumeID string, dropletID int) (*Action, *Response, error)
Get(ctx context.Context, volumeID string, actionID int) (*Action, *Response, error)
List(ctx context.Context, volumeID string, opt *ListOptions) ([]Action, *Response, error)
Resize(ctx context.Context, volumeID string, sizeGigabytes int, regionSlug string) (*Action, *Response, error)
}
// StorageActionsServiceOp handles communication with the storage volumes
// action related methods of the DigitalOcean API.
type StorageActionsServiceOp struct {
client *Client
}
// StorageAttachment represents the attachment of a block storage
// volume to a specific Droplet under the device name.
type StorageAttachment struct {
DropletID int `json:"droplet_id"`
}
// Attach a storage volume to a Droplet.
func (s *StorageActionsServiceOp) Attach(ctx context.Context, volumeID string, dropletID int) (*Action, *Response, error) {
request := &ActionRequest{
"type": "attach",
"droplet_id": dropletID,
}
return s.doAction(ctx, volumeID, request)
}
// DetachByDropletID a storage volume from a Droplet by Droplet ID.
func (s *StorageActionsServiceOp) DetachByDropletID(ctx context.Context, volumeID string, dropletID int) (*Action, *Response, error) {
request := &ActionRequest{
"type": "detach",
"droplet_id": dropletID,
}
return s.doAction(ctx, volumeID, request)
}
// Get an action for a particular storage volume by id.
func (s *StorageActionsServiceOp) Get(ctx context.Context, volumeID string, actionID int) (*Action, *Response, error) {
path := fmt.Sprintf("%s/%d", storageAllocationActionPath(volumeID), actionID)
return s.get(ctx, path)
}
// List the actions for a particular storage volume.
func (s *StorageActionsServiceOp) List(ctx context.Context, volumeID string, opt *ListOptions) ([]Action, *Response, error) {
path := storageAllocationActionPath(volumeID)
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
return s.list(ctx, path)
}
// Resize a storage volume.
func (s *StorageActionsServiceOp) Resize(ctx context.Context, volumeID string, sizeGigabytes int, regionSlug string) (*Action, *Response, error) {
request := &ActionRequest{
"type": "resize",
"size_gigabytes": sizeGigabytes,
"region": regionSlug,
}
return s.doAction(ctx, volumeID, request)
}
func (s *StorageActionsServiceOp) doAction(ctx context.Context, volumeID string, request *ActionRequest) (*Action, *Response, error) {
path := storageAllocationActionPath(volumeID)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, request)
if err != nil {
return nil, nil, err
}
root := new(actionRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Event, resp, err
}
func (s *StorageActionsServiceOp) get(ctx context.Context, path string) (*Action, *Response, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(actionRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Event, resp, err
}
func (s *StorageActionsServiceOp) list(ctx context.Context, path string) ([]Action, *Response, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(actionsRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Actions, resp, err
}
func storageAllocationActionPath(volumeID string) string {
return fmt.Sprintf("%s/%s/actions", storageAllocPath, volumeID)
}

View File

@ -1,163 +0,0 @@
package godo
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestStoragesActions_Attach(t *testing.T) {
setup()
defer teardown()
const (
volumeID = "98d414c6-295e-4e3a-ac58-eb9456c1e1d1"
dropletID = 12345
)
attachRequest := &ActionRequest{
"type": "attach",
"droplet_id": float64(dropletID), // encoding/json decodes numbers as floats
}
mux.HandleFunc("/v2/volumes/"+volumeID+"/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, attachRequest) {
t.Errorf("want=%#v", attachRequest)
t.Errorf("got=%#v", v)
}
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})
_, _, err := client.StorageActions.Attach(ctx, volumeID, dropletID)
if err != nil {
t.Errorf("StoragesActions.Attach returned error: %v", err)
}
}
func TestStoragesActions_DetachByDropletID(t *testing.T) {
setup()
defer teardown()
volumeID := "98d414c6-295e-4e3a-ac58-eb9456c1e1d1"
dropletID := 123456
detachByDropletIDRequest := &ActionRequest{
"type": "detach",
"droplet_id": float64(dropletID), // encoding/json decodes numbers as floats
}
mux.HandleFunc("/v2/volumes/"+volumeID+"/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, detachByDropletIDRequest) {
t.Errorf("want=%#v", detachByDropletIDRequest)
t.Errorf("got=%#v", v)
}
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})
_, _, err := client.StorageActions.DetachByDropletID(ctx, volumeID, dropletID)
if err != nil {
t.Errorf("StoragesActions.DetachByDropletID returned error: %v", err)
}
}
func TestStorageActions_Get(t *testing.T) {
setup()
defer teardown()
volumeID := "98d414c6-295e-4e3a-ac58-eb9456c1e1d1"
mux.HandleFunc("/v2/volumes/"+volumeID+"/actions/456", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})
action, _, err := client.StorageActions.Get(ctx, volumeID, 456)
if err != nil {
t.Errorf("StorageActions.Get returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(action, expected) {
t.Errorf("StorageActions.Get returned %+v, expected %+v", action, expected)
}
}
func TestStorageActions_List(t *testing.T) {
setup()
defer teardown()
volumeID := "98d414c6-295e-4e3a-ac58-eb9456c1e1d1"
mux.HandleFunc("/v2/volumes/"+volumeID+"/actions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprintf(w, `{
"actions": [
{
"status": "in-progress"
}
],
"meta": {
"total": 1
}
}`)
})
actions, resp, err := client.StorageActions.List(ctx, volumeID, nil)
if err != nil {
t.Errorf("StorageActions.List returned error: %v", err)
}
expectedActions := []Action{{Status: "in-progress"}}
if !reflect.DeepEqual(actions, expectedActions) {
t.Errorf("StorageActions.List returned actions %+v, expected %+v", actions, expectedActions)
}
expectedMeta := &Meta{Total: 1}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("StorageActions.List returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}
func TestStoragesActions_Resize(t *testing.T) {
setup()
defer teardown()
volumeID := "98d414c6-295e-4e3a-ac58-eb9456c1e1d1"
resizeRequest := &ActionRequest{
"type": "resize",
"size_gigabytes": float64(500),
"region": "nyc1",
}
mux.HandleFunc("/v2/volumes/"+volumeID+"/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, resizeRequest) {
t.Errorf("want=%#v", resizeRequest)
t.Errorf("got=%#v", v)
}
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})
_, _, err := client.StorageActions.Resize(ctx, volumeID, 500, "nyc1")
if err != nil {
t.Errorf("StoragesActions.Resize returned error: %v", err)
}
}

View File

@ -1,704 +0,0 @@
package godo
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
"time"
)
func TestStorageVolumes_ListStorageVolumes(t *testing.T) {
setup()
defer teardown()
jBlob := `
{
"volumes": [
{
"user_id": 42,
"region": {"slug": "nyc3"},
"id": "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
"name": "my volume",
"description": "my description",
"size_gigabytes": 100,
"droplet_ids": [10],
"created_at": "2002-10-02T15:00:00.05Z",
"filesystem_type": "",
"filesystem_label": "",
"tags": ["tag1", "tag2"]
},
{
"user_id": 42,
"region": {"slug": "nyc3"},
"id": "96d414c6-295e-4e3a-ac59-eb9456c1e1d1",
"name": "my other volume",
"description": "my other description",
"size_gigabytes": 100,
"created_at": "2012-10-03T15:00:01.05Z",
"filesystem_type": "ext4",
"filesystem_label": "my-volume",
"tags": []
}
],
"links": [],
"meta": {
"total": 28
}
}`
mux.HandleFunc("/v2/volumes/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})
volumes, resp, err := client.Storage.ListVolumes(ctx, nil)
if err != nil {
t.Errorf("Storage.ListVolumes returned error: %v", err)
}
expectedVolume := []Volume{
{
Region: &Region{Slug: "nyc3"},
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Name: "my volume",
Description: "my description",
SizeGigaBytes: 100,
DropletIDs: []int{10},
CreatedAt: time.Date(2002, 10, 02, 15, 00, 00, 50000000, time.UTC),
Tags: []string{"tag1", "tag2"},
},
{
Region: &Region{Slug: "nyc3"},
ID: "96d414c6-295e-4e3a-ac59-eb9456c1e1d1",
Name: "my other volume",
Description: "my other description",
SizeGigaBytes: 100,
CreatedAt: time.Date(2012, 10, 03, 15, 00, 01, 50000000, time.UTC),
FilesystemType: "ext4",
FilesystemLabel: "my-volume",
Tags: []string{},
},
}
if !reflect.DeepEqual(volumes, expectedVolume) {
t.Errorf("Storage.ListVolumes returned volumes %+v, expected %+v", volumes, expectedVolume)
}
expectedMeta := &Meta{
Total: 28,
}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Storage.ListVolumes returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}
func TestStorageVolumes_Get(t *testing.T) {
setup()
defer teardown()
want := &Volume{
Region: &Region{Slug: "nyc3"},
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Name: "my volume",
Description: "my description",
SizeGigaBytes: 100,
CreatedAt: time.Date(2002, 10, 02, 15, 00, 00, 50000000, time.UTC),
FilesystemType: "xfs",
FilesystemLabel: "my-vol",
Tags: []string{"tag1", "tag2"},
}
jBlob := `{
"volume":{
"region": {"slug":"nyc3"},
"attached_to_droplet": null,
"id": "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
"name": "my volume",
"description": "my description",
"size_gigabytes": 100,
"created_at": "2002-10-02T15:00:00.05Z",
"filesystem_type": "xfs",
"filesystem_label": "my-vol",
"tags": ["tag1", "tag2"]
}
}`
mux.HandleFunc("/v2/volumes/80d414c6-295e-4e3a-ac58-eb9456c1e1d1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})
got, _, err := client.Storage.GetVolume(ctx, "80d414c6-295e-4e3a-ac58-eb9456c1e1d1")
if err != nil {
t.Errorf("Storage.GetVolume returned error: %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Storage.GetVolume returned %+v, want %+v", got, want)
}
}
func TestStorageVolumes_ListVolumesByName(t *testing.T) {
setup()
defer teardown()
jBlob :=
`{
"volumes": [
{
"region": {"slug": "nyc3"},
"id": "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
"name": "myvolume",
"description": "my description",
"size_gigabytes": 100,
"droplet_ids": [10],
"created_at": "2002-10-02T15:00:00.05Z",
"filesystem_type": "",
"filesystem_label": "",
"tags": ["tag1", "tag2"]
}
],
"links": [],
"meta": {
"total": 1
}
}`
expectedVolumes := []Volume{
{
Region: &Region{Slug: "nyc3"},
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Name: "myvolume",
Description: "my description",
SizeGigaBytes: 100,
DropletIDs: []int{10},
CreatedAt: time.Date(2002, 10, 02, 15, 00, 00, 50000000, time.UTC),
Tags: []string{"tag1", "tag2"},
},
}
mux.HandleFunc("/v2/volumes", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("name") != "myvolume" || r.URL.Query().Get("region") != "" {
t.Errorf("Storage.ListVolumeByName did not request the correct name or region")
}
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})
options := &ListVolumeParams{
Name: "myvolume",
}
volumes, resp, err := client.Storage.ListVolumes(ctx, options)
if err != nil {
t.Errorf("Storage.ListVolumeByName returned error: %v", err)
}
if !reflect.DeepEqual(volumes, expectedVolumes) {
t.Errorf("Storage.ListVolumeByName returned volumes %+v, expected %+v", volumes, expectedVolumes)
}
expectedMeta := &Meta{
Total: 1,
}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Storage.ListVolumeByName returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}
func TestStorageVolumes_ListVolumesByRegion(t *testing.T) {
setup()
defer teardown()
jBlob :=
`{
"volumes": [
{
"region": {"slug": "nyc3"},
"id": "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
"name": "myvolume",
"description": "my description",
"size_gigabytes": 100,
"droplet_ids": [10],
"created_at": "2002-10-02T15:00:00.05Z",
"filesystem_type": "",
"filesystem_label": "",
"tags": ["tag1", "tag2"]
}
],
"links": [],
"meta": {
"total": 1
}
}`
expectedVolumes := []Volume{
{
Region: &Region{Slug: "nyc3"},
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Name: "myvolume",
Description: "my description",
SizeGigaBytes: 100,
DropletIDs: []int{10},
CreatedAt: time.Date(2002, 10, 02, 15, 00, 00, 50000000, time.UTC),
Tags: []string{"tag1", "tag2"},
},
}
mux.HandleFunc("/v2/volumes", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("region") != "nyc3" || r.URL.Query().Get("name") != "" {
t.Errorf("Storage.ListVolumeByName did not request the correct name or region")
}
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})
options := &ListVolumeParams{
Region: "nyc3",
}
volumes, resp, err := client.Storage.ListVolumes(ctx, options)
if err != nil {
t.Errorf("Storage.ListVolumeByName returned error: %v", err)
}
if !reflect.DeepEqual(volumes, expectedVolumes) {
t.Errorf("Storage.ListVolumeByName returned volumes %+v, expected %+v", volumes, expectedVolumes)
}
expectedMeta := &Meta{
Total: 1,
}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Storage.ListVolumeByName returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}
func TestStorageVolumes_ListVolumesByNameAndRegion(t *testing.T) {
setup()
defer teardown()
jBlob :=
`{
"volumes": [
{
"region": {"slug": "nyc3"},
"id": "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
"name": "myvolume",
"description": "my description",
"size_gigabytes": 100,
"droplet_ids": [10],
"created_at": "2002-10-02T15:00:00.05Z",
"filesystem_type": "",
"filesystem_label": "",
"tags": ["tag1", "tag2"]
}
],
"links": [],
"meta": {
"total": 1
}
}`
expectedVolumes := []Volume{
{
Region: &Region{Slug: "nyc3"},
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Name: "myvolume",
Description: "my description",
SizeGigaBytes: 100,
DropletIDs: []int{10},
CreatedAt: time.Date(2002, 10, 02, 15, 00, 00, 50000000, time.UTC),
Tags: []string{"tag1", "tag2"},
},
}
mux.HandleFunc("/v2/volumes", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("region") != "nyc3" || r.URL.Query().Get("name") != "myvolume" {
t.Errorf("Storage.ListVolumeByName did not request the correct name or region")
}
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})
options := &ListVolumeParams{
Region: "nyc3",
Name: "myvolume",
}
volumes, resp, err := client.Storage.ListVolumes(ctx, options)
if err != nil {
t.Errorf("Storage.ListVolumeByName returned error: %v", err)
}
if !reflect.DeepEqual(volumes, expectedVolumes) {
t.Errorf("Storage.ListVolumeByName returned volumes %+v, expected %+v", volumes, expectedVolumes)
}
expectedMeta := &Meta{
Total: 1,
}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Storage.ListVolumeByName returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}
func TestStorageVolumes_Create(t *testing.T) {
setup()
defer teardown()
createRequest := &VolumeCreateRequest{
Region: "nyc3",
Name: "my volume",
Description: "my description",
SizeGigaBytes: 100,
Tags: []string{"tag1", "tag2"},
}
want := &Volume{
Region: &Region{Slug: "nyc3"},
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Name: "my volume",
Description: "my description",
SizeGigaBytes: 100,
CreatedAt: time.Date(2002, 10, 02, 15, 00, 00, 50000000, time.UTC),
Tags: []string{"tag1", "tag2"},
}
jBlob := `{
"volume":{
"region": {"slug":"nyc3"},
"id": "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
"name": "my volume",
"description": "my description",
"size_gigabytes": 100,
"created_at": "2002-10-02T15:00:00.05Z",
"tags": ["tag1", "tag2"]
},
"links": []
}`
mux.HandleFunc("/v2/volumes", func(w http.ResponseWriter, r *http.Request) {
v := new(VolumeCreateRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, createRequest) {
t.Errorf("Request body = %+v, expected %+v", v, createRequest)
}
fmt.Fprint(w, jBlob)
})
got, _, err := client.Storage.CreateVolume(ctx, createRequest)
if err != nil {
t.Errorf("Storage.CreateVolume returned error: %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Storage.CreateVolume returned %+v, want %+v", got, want)
}
}
func TestStorageVolumes_CreateFormatted(t *testing.T) {
setup()
defer teardown()
createRequest := &VolumeCreateRequest{
Region: "nyc3",
Name: "my volume",
Description: "my description",
SizeGigaBytes: 100,
FilesystemType: "xfs",
Tags: []string{"tag1", "tag2"},
}
want := &Volume{
Region: &Region{Slug: "nyc3"},
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Name: "my volume",
Description: "my description",
SizeGigaBytes: 100,
CreatedAt: time.Date(2002, 10, 02, 15, 00, 00, 50000000, time.UTC),
FilesystemType: "xfs",
Tags: []string{"tag1", "tag2"},
}
jBlob := `{
"volume":{
"region": {"slug":"nyc3"},
"id": "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
"name": "my volume",
"description": "my description",
"size_gigabytes": 100,
"created_at": "2002-10-02T15:00:00.05Z",
"filesystem_type": "xfs",
"filesystem_label": "",
"tags": ["tag1", "tag2"]
},
"links": []
}`
mux.HandleFunc("/v2/volumes", func(w http.ResponseWriter, r *http.Request) {
v := new(VolumeCreateRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, createRequest) {
t.Errorf("Request body = %+v, expected %+v", v, createRequest)
}
fmt.Fprint(w, jBlob)
})
got, _, err := client.Storage.CreateVolume(ctx, createRequest)
if err != nil {
t.Errorf("Storage.CreateVolume returned error: %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Storage.CreateVolume returned %+v, want %+v", got, want)
}
}
func TestStorageVolumes_CreateFromSnapshot(t *testing.T) {
setup()
defer teardown()
createRequest := &VolumeCreateRequest{
Name: "my-volume-from-a-snapshot",
Description: "my description",
SizeGigaBytes: 100,
SnapshotID: "0d165eff-0b4c-11e7-9093-0242ac110207",
Tags: []string{"tag1", "tag2"},
}
want := &Volume{
Region: &Region{Slug: "nyc3"},
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Name: "my-volume-from-a-snapshot",
Description: "my description",
SizeGigaBytes: 100,
CreatedAt: time.Date(2002, 10, 02, 15, 00, 00, 50000000, time.UTC),
Tags: []string{"tag1", "tag2"},
}
jBlob := `{
"volume":{
"region": {"slug":"nyc3"},
"id": "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
"name": "my-volume-from-a-snapshot",
"description": "my description",
"size_gigabytes": 100,
"created_at": "2002-10-02T15:00:00.05Z",
"tags": ["tag1", "tag2"]
},
"links": []
}`
mux.HandleFunc("/v2/volumes", func(w http.ResponseWriter, r *http.Request) {
v := new(VolumeCreateRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, createRequest) {
t.Errorf("Request body = %+v, expected %+v", v, createRequest)
}
fmt.Fprint(w, jBlob)
})
got, _, err := client.Storage.CreateVolume(ctx, createRequest)
if err != nil {
t.Errorf("Storage.CreateVolume returned error: %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Storage.CreateVolume returned %+v, want %+v", got, want)
}
}
func TestStorageVolumes_Destroy(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/volumes/80d414c6-295e-4e3a-ac58-eb9456c1e1d1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := client.Storage.DeleteVolume(ctx, "80d414c6-295e-4e3a-ac58-eb9456c1e1d1")
if err != nil {
t.Errorf("Storage.DeleteVolume returned error: %v", err)
}
}
func TestStorageSnapshots_ListStorageSnapshots(t *testing.T) {
setup()
defer teardown()
jBlob := `
{
"snapshots": [
{
"regions": ["nyc3"],
"id": "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
"name": "my snapshot",
"size_gigabytes": 100,
"created_at": "2002-10-02T15:00:00.05Z"
},
{
"regions": ["nyc3"],
"id": "96d414c6-295e-4e3a-ac59-eb9456c1e1d1",
"name": "my other snapshot",
"size_gigabytes": 100,
"created_at": "2012-10-03T15:00:01.05Z"
}
],
"links": [],
"meta": {
"total": 28
}
}`
mux.HandleFunc("/v2/volumes/98d414c6-295e-4e3a-ac58-eb9456c1e1d1/snapshots", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})
volumes, resp, err := client.Storage.ListSnapshots(ctx, "98d414c6-295e-4e3a-ac58-eb9456c1e1d1", nil)
if err != nil {
t.Errorf("Storage.ListSnapshots returned error: %v", err)
}
expectedSnapshots := []Snapshot{
{
Regions: []string{"nyc3"},
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Name: "my snapshot",
SizeGigaBytes: 100,
Created: "2002-10-02T15:00:00.05Z",
},
{
Regions: []string{"nyc3"},
ID: "96d414c6-295e-4e3a-ac59-eb9456c1e1d1",
Name: "my other snapshot",
SizeGigaBytes: 100,
Created: "2012-10-03T15:00:01.05Z",
},
}
if !reflect.DeepEqual(volumes, expectedSnapshots) {
t.Errorf("Storage.ListSnapshots returned snapshots %+v, expected %+v", volumes, expectedSnapshots)
}
expectedMeta := &Meta{
Total: 28,
}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Storage.ListSnapshots returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}
func TestStorageSnapshots_Get(t *testing.T) {
setup()
defer teardown()
want := &Snapshot{
Regions: []string{"nyc3"},
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Name: "my snapshot",
SizeGigaBytes: 100,
Created: "2002-10-02T15:00:00.05Z",
}
jBlob := `{
"snapshot":{
"regions": ["nyc3"],
"id": "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
"name": "my snapshot",
"size_gigabytes": 100,
"created_at": "2002-10-02T15:00:00.05Z"
}
}`
mux.HandleFunc("/v2/snapshots/80d414c6-295e-4e3a-ac58-eb9456c1e1d1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})
got, _, err := client.Storage.GetSnapshot(ctx, "80d414c6-295e-4e3a-ac58-eb9456c1e1d1")
if err != nil {
t.Errorf("Storage.GetSnapshot returned error: %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Storage.GetSnapshot returned %+v, want %+v", got, want)
}
}
func TestStorageSnapshots_Create(t *testing.T) {
setup()
defer teardown()
createRequest := &SnapshotCreateRequest{
VolumeID: "98d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Name: "my snapshot",
Description: "my description",
Tags: []string{"one", "two"},
}
want := &Snapshot{
Regions: []string{"nyc3"},
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Name: "my snapshot",
SizeGigaBytes: 100,
Created: "2002-10-02T15:00:00.05Z",
Tags: []string{"one", "two"},
}
jBlob := `{
"snapshot":{
"regions": ["nyc3"],
"id": "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
"name": "my snapshot",
"description": "my description",
"size_gigabytes": 100,
"created_at": "2002-10-02T15:00:00.05Z",
"tags": ["one", "two"]
},
"links": [],
"meta": {
"total": 28
}
}`
mux.HandleFunc("/v2/volumes/98d414c6-295e-4e3a-ac58-eb9456c1e1d1/snapshots", func(w http.ResponseWriter, r *http.Request) {
v := new(SnapshotCreateRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, createRequest) {
t.Errorf("Request body = %+v, expected %+v", v, createRequest)
}
fmt.Fprint(w, jBlob)
})
got, _, err := client.Storage.CreateSnapshot(ctx, createRequest)
if err != nil {
t.Errorf("Storage.CreateSnapshot returned error: %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Storage.CreateSnapshot returned %+v, want %+v", got, want)
}
}
func TestStorageSnapshots_Destroy(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/snapshots/80d414c6-295e-4e3a-ac58-eb9456c1e1d1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := client.Storage.DeleteSnapshot(ctx, "80d414c6-295e-4e3a-ac58-eb9456c1e1d1")
if err != nil {
t.Errorf("Storage.DeleteSnapshot returned error: %v", err)
}
}

265
vpcs.go
View File

@ -1,265 +0,0 @@
package godo
import (
"context"
"net/http"
"time"
)
const vpcsBasePath = "/v2/vpcs"
// VPCsService is an interface for managing Virtual Private Cloud configurations with the
// DigitalOcean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/VPCs
type VPCsService interface {
Create(context.Context, *VPCCreateRequest) (*VPC, *Response, error)
Get(context.Context, string) (*VPC, *Response, error)
List(context.Context, *ListOptions) ([]*VPC, *Response, error)
ListMembers(context.Context, string, *VPCListMembersRequest, *ListOptions) ([]*VPCMember, *Response, error)
Update(context.Context, string, *VPCUpdateRequest) (*VPC, *Response, error)
Set(context.Context, string, ...VPCSetField) (*VPC, *Response, error)
Delete(context.Context, string) (*Response, error)
}
var _ VPCsService = &VPCsServiceOp{}
// VPCsServiceOp interfaces with VPC endpoints in the DigitalOcean API.
type VPCsServiceOp struct {
client *Client
}
// VPCCreateRequest represents a request to create a Virtual Private Cloud.
type VPCCreateRequest struct {
Name string `json:"name,omitempty"`
RegionSlug string `json:"region,omitempty"`
Description string `json:"description,omitempty"`
IPRange string `json:"ip_range,omitempty"`
}
// VPCUpdateRequest represents a request to update a Virtual Private Cloud.
type VPCUpdateRequest struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Default *bool `json:"default,omitempty"`
}
// VPCSetField allows one to set individual fields within a VPC configuration.
type VPCSetField interface {
vpcSetField(map[string]interface{})
}
// VPCSetName is used when one want to set the `name` field of a VPC.
// Ex.: VPCs.Set(..., VPCSetName("new-name"))
type VPCSetName string
// VPCSetDescription is used when one want to set the `description` field of a VPC.
// Ex.: VPCs.Set(..., VPCSetDescription("vpc description"))
type VPCSetDescription string
// VPCSetDefault is used when one wants to enable the `default` field of a VPC, to
// set a VPC as the default one in the region
// Ex.: VPCs.Set(..., VPCSetDefault())
func VPCSetDefault() VPCSetField {
return &vpcSetDefault{}
}
// vpcSetDefault satisfies the VPCSetField interface
type vpcSetDefault struct{}
// VPC represents a DigitalOcean Virtual Private Cloud configuration.
type VPC struct {
ID string `json:"id,omitempty"`
URN string `json:"urn"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
IPRange string `json:"ip_range,omitempty"`
RegionSlug string `json:"region,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
Default bool `json:"default,omitempty"`
}
type VPCListMembersRequest struct {
ResourceType string `url:"resource_type,omitempty"`
}
type VPCMember struct {
URN string `json:"urn,omitempty"`
Name string `json:"name,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
}
type vpcRoot struct {
VPC *VPC `json:"vpc"`
}
type vpcsRoot struct {
VPCs []*VPC `json:"vpcs"`
Links []*LinkAction `json:"links"`
Meta *Meta `json:"meta"`
}
type vpcMembersRoot struct {
Members []*VPCMember `json:"members"`
Links []*LinkAction `json:"links"`
Meta *Meta `json:"meta"`
}
// Get returns the details of a Virtual Private Cloud.
func (v *VPCsServiceOp) Get(ctx context.Context, id string) (*VPC, *Response, error) {
path := vpcsBasePath + "/" + id
req, err := v.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(vpcRoot)
resp, err := v.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.VPC, resp, nil
}
// Create creates a new Virtual Private Cloud.
func (v *VPCsServiceOp) Create(ctx context.Context, create *VPCCreateRequest) (*VPC, *Response, error) {
path := vpcsBasePath
req, err := v.client.NewRequest(ctx, http.MethodPost, path, create)
if err != nil {
return nil, nil, err
}
root := new(vpcRoot)
resp, err := v.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.VPC, resp, nil
}
// List returns a list of the caller's VPCs, with optional pagination.
func (v *VPCsServiceOp) List(ctx context.Context, opt *ListOptions) ([]*VPC, *Response, error) {
path, err := addOptions(vpcsBasePath, opt)
if err != nil {
return nil, nil, err
}
req, err := v.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(vpcsRoot)
resp, err := v.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.VPCs, resp, nil
}
// Update updates a Virtual Private Cloud's properties.
func (v *VPCsServiceOp) Update(ctx context.Context, id string, update *VPCUpdateRequest) (*VPC, *Response, error) {
path := vpcsBasePath + "/" + id
req, err := v.client.NewRequest(ctx, http.MethodPut, path, update)
if err != nil {
return nil, nil, err
}
root := new(vpcRoot)
resp, err := v.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.VPC, resp, nil
}
func (n VPCSetName) vpcSetField(in map[string]interface{}) {
in["name"] = n
}
func (n VPCSetDescription) vpcSetField(in map[string]interface{}) {
in["description"] = n
}
func (*vpcSetDefault) vpcSetField(in map[string]interface{}) {
in["default"] = true
}
// Set updates specific properties of a Virtual Private Cloud.
func (v *VPCsServiceOp) Set(ctx context.Context, id string, fields ...VPCSetField) (*VPC, *Response, error) {
path := vpcsBasePath + "/" + id
update := make(map[string]interface{}, len(fields))
for _, field := range fields {
field.vpcSetField(update)
}
req, err := v.client.NewRequest(ctx, http.MethodPatch, path, update)
if err != nil {
return nil, nil, err
}
root := new(vpcRoot)
resp, err := v.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.VPC, resp, nil
}
// Delete deletes a Virtual Private Cloud. There is no way to recover a VPC once it has been
// destroyed.
func (v *VPCsServiceOp) Delete(ctx context.Context, id string) (*Response, error) {
path := vpcsBasePath + "/" + id
req, err := v.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := v.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
func (v *VPCsServiceOp) ListMembers(ctx context.Context, id string, request *VPCListMembersRequest, opt *ListOptions) ([]*VPCMember, *Response, error) {
path := vpcsBasePath + "/" + id + "/members"
pathWithResourceType, err := addOptions(path, request)
if err != nil {
return nil, nil, err
}
pathWithOpts, err := addOptions(pathWithResourceType, opt)
if err != nil {
return nil, nil, err
}
req, err := v.client.NewRequest(ctx, http.MethodGet, pathWithOpts, nil)
if err != nil {
return nil, nil, err
}
root := new(vpcMembersRoot)
resp, err := v.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Members, resp, nil
}

View File

@ -1,424 +0,0 @@
package godo
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var vTestObj = &VPC{
ID: "880b7f98-f062-404d-b33c-458d545696f6",
URN: "do:vpc:880b7f98-f062-404d-b33c-458d545696f6",
Name: "my-new-vpc",
Description: "vpc description",
IPRange: "10.122.0.0/20",
RegionSlug: "s2r7",
CreatedAt: time.Date(2019, 2, 4, 21, 48, 40, 995304079, time.UTC),
Default: false,
}
var vTestJSON = `
{
"id":"880b7f98-f062-404d-b33c-458d545696f6",
"urn":"do:vpc:880b7f98-f062-404d-b33c-458d545696f6",
"name":"my-new-vpc",
"description":"vpc description",
"ip_range":"10.122.0.0/20",
"region":"s2r7",
"created_at":"2019-02-04T21:48:40.995304079Z",
"default":false
}
`
func TestVPCs_Get(t *testing.T) {
setup()
defer teardown()
svc := client.VPCs
path := "/v2/vpcs"
want := vTestObj
id := "880b7f98-f062-404d-b33c-458d545696f6"
jsonBlob := `
{
"vpc":
` + vTestJSON + `
}
`
mux.HandleFunc(path+"/"+id, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jsonBlob)
})
got, _, err := svc.Get(ctx, id)
require.NoError(t, err)
require.Equal(t, want, got)
}
func TestVPCs_List(t *testing.T) {
setup()
defer teardown()
svc := client.VPCs
path := "/v2/vpcs"
want := []*VPC{
vTestObj,
}
links := []*LinkAction{}
meta := &Meta{
Total: 3,
}
jsonBlob := `
{
"vpcs": [
` + vTestJSON + `
],
"links": [],
"meta": {"total": 3}
}
`
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jsonBlob)
})
got, resp, err := svc.List(ctx, nil)
require.NoError(t, err)
assert.Equal(t, want, got)
assert.Equal(t, resp.Links, links)
assert.Equal(t, resp.Meta, meta)
}
func TestVPCs_Create(t *testing.T) {
setup()
defer teardown()
svc := client.VPCs
path := "/v2/vpcs"
want := vTestObj
req := &VPCCreateRequest{
Name: "my-new-vpc",
RegionSlug: "s2r7",
}
jsonBlob := `
{
"vpc":
` + vTestJSON + `
}
`
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
c := new(VPCCreateRequest)
err := json.NewDecoder(r.Body).Decode(c)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPost)
require.Equal(t, c, req)
fmt.Fprint(w, jsonBlob)
})
got, _, err := svc.Create(ctx, req)
require.NoError(t, err)
require.Equal(t, want, got)
}
func TestVPCs_Update(t *testing.T) {
tests := []struct {
desc string
id string
req *VPCUpdateRequest
mockResponse string
expectedRequestBody string
expectedUpdatedVPC *VPC
}{
{
desc: "setting name and description without default argument",
id: "880b7f98-f062-404d-b33c-458d545696f6",
req: &VPCUpdateRequest{
Name: "my-new-vpc",
Description: "vpc description",
},
mockResponse: `
{
"vpc":
` + vTestJSON + `
}
`,
expectedRequestBody: `{"name":"my-new-vpc","description":"vpc description"}`,
expectedUpdatedVPC: vTestObj,
},
{
desc: "setting the default vpc option",
id: "880b7f98-f062-404d-b33c-458d545696f6",
req: &VPCUpdateRequest{
Name: "my-new-vpc",
Description: "vpc description",
Default: boolPtr(false),
},
mockResponse: `
{
"vpc":
` + vTestJSON + `
}
`,
expectedRequestBody: `{"name":"my-new-vpc","description":"vpc description","default":false}`,
expectedUpdatedVPC: vTestObj,
},
{
desc: "setting the default vpc option",
id: "880b7f98-f062-404d-b33c-458d545696f6",
req: &VPCUpdateRequest{
Name: "my-new-vpc",
Description: "vpc description",
Default: boolPtr(true),
},
mockResponse: `
{
"vpc":
` + vTestJSON + `
}
`,
expectedRequestBody: `{"name":"my-new-vpc","description":"vpc description","default":true}`,
expectedUpdatedVPC: vTestObj,
},
}
for _, tt := range tests {
setup()
mux.HandleFunc("/v2/vpcs/"+tt.id, func(w http.ResponseWriter, r *http.Request) {
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
require.Equal(t, tt.expectedRequestBody, strings.TrimSpace(buf.String()))
v := new(VPCUpdateRequest)
err := json.NewDecoder(buf).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPut)
fmt.Fprint(w, tt.mockResponse)
})
got, _, err := client.VPCs.Update(ctx, tt.id, tt.req)
teardown()
require.NoError(t, err)
require.Equal(t, tt.expectedUpdatedVPC, got)
}
}
func TestVPCs_Set(t *testing.T) {
tests := []struct {
desc string
id string
updateFields []VPCSetField
mockResponse string
expectedRequestBody string
expectedUpdatedVPC *VPC
}{
{
desc: "setting name and description",
id: "880b7f98-f062-404d-b33c-458d545696f6",
updateFields: []VPCSetField{
VPCSetName("my-new-vpc"),
VPCSetDescription("vpc description"),
},
mockResponse: `
{
"vpc":
` + vTestJSON + `
}
`,
expectedRequestBody: `{"description":"vpc description","name":"my-new-vpc"}`,
expectedUpdatedVPC: vTestObj,
},
{
desc: "setting the default vpc option",
id: "880b7f98-f062-404d-b33c-458d545696f6",
updateFields: []VPCSetField{
VPCSetName("my-new-vpc"),
VPCSetDescription("vpc description"),
VPCSetDefault(),
},
mockResponse: `
{
"vpc":
` + vTestJSON + `
}
`,
expectedRequestBody: `{"default":true,"description":"vpc description","name":"my-new-vpc"}`,
expectedUpdatedVPC: vTestObj,
},
}
for _, tt := range tests {
setup()
mux.HandleFunc("/v2/vpcs/"+tt.id, func(w http.ResponseWriter, r *http.Request) {
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
require.Equal(t, tt.expectedRequestBody, strings.TrimSpace(buf.String()))
v := new(VPCUpdateRequest)
err := json.NewDecoder(buf).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPatch)
fmt.Fprint(w, tt.mockResponse)
})
got, _, err := client.VPCs.Set(ctx, tt.id, tt.updateFields...)
teardown()
require.NoError(t, err)
require.Equal(t, tt.expectedUpdatedVPC, got)
}
}
func TestVPCs_Delete(t *testing.T) {
setup()
defer teardown()
svc := client.VPCs
path := "/v2/vpcs"
id := "880b7f98-f062-404d-b33c-458d545696f6"
mux.HandleFunc(path+"/"+id, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := svc.Delete(ctx, id)
require.NoError(t, err)
}
func TestVPCs_ListMembers(t *testing.T) {
tests := []struct {
desc string
expectedQuery string
query *VPCListMembersRequest
resp string
want []*VPCMember
}{
{
desc: "list all members",
expectedQuery: "",
query: nil,
resp: `{
"members": [
{
"urn": "do:loadbalancer:fb294d78-d193-4cb2-8737-ea620993591b",
"name": "nyc1-load-balancer-01",
"created_at": "2020-03-16T19:30:48Z"
},
{
"urn": "do:dbaas:13f7a2f6-43df-4c4a-8129-8733267ddeea",
"name": "db-postgresql-nyc1-55986",
"created_at": "2020-03-15T19:30:48Z"
},
{
"urn": "do:kubernetes:da39d893-96e1-4e4d-971d-1fdda33a46b1",
"name": "k8s-nyc1-1584127772221",
"created_at": "2020-03-14T19:30:48Z"
},
{
"urn": "do:droplet:86e29982-03a7-4946-8a07-a0114dff8754",
"name": "ubuntu-s-1vcpu-1gb-nyc1-01",
"created_at": "2020-03-13T19:30:48Z"
}
],
"links": [],
"meta": {
"total": 4
}
}`,
want: []*VPCMember{
{
URN: "do:loadbalancer:fb294d78-d193-4cb2-8737-ea620993591b",
Name: "nyc1-load-balancer-01",
CreatedAt: time.Date(2020, 3, 16, 19, 30, 48, 0, time.UTC),
},
{
URN: "do:dbaas:13f7a2f6-43df-4c4a-8129-8733267ddeea",
Name: "db-postgresql-nyc1-55986",
CreatedAt: time.Date(2020, 3, 15, 19, 30, 48, 0, time.UTC),
},
{
URN: "do:kubernetes:da39d893-96e1-4e4d-971d-1fdda33a46b1",
Name: "k8s-nyc1-1584127772221",
CreatedAt: time.Date(2020, 3, 14, 19, 30, 48, 0, time.UTC),
},
{
URN: "do:droplet:86e29982-03a7-4946-8a07-a0114dff8754",
Name: "ubuntu-s-1vcpu-1gb-nyc1-01",
CreatedAt: time.Date(2020, 3, 13, 19, 30, 48, 0, time.UTC),
},
},
},
{
desc: "list droplet members",
expectedQuery: "droplet",
query: &VPCListMembersRequest{ResourceType: "droplet"},
resp: `{
"members": [
{
"urn": "do:droplet:86e29982-03a7-4946-8a07-a0114dff8754",
"name": "ubuntu-s-1vcpu-1gb-nyc1-01",
"created_at": "2020-03-13T19:30:48Z"
}
],
"links": [],
"meta": {
"total": 1
}
}`,
want: []*VPCMember{
{
URN: "do:droplet:86e29982-03a7-4946-8a07-a0114dff8754",
Name: "ubuntu-s-1vcpu-1gb-nyc1-01",
CreatedAt: time.Date(2020, 3, 13, 19, 30, 48, 0, time.UTC),
},
},
},
}
id := "880b7f98-f062-404d-b33c-458d545696f6"
path := "/v2/vpcs/" + id + "/members"
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
require.Equal(t, tt.expectedQuery, r.URL.Query().Get("resource_type"))
fmt.Fprint(w, tt.resp)
})
got, _, err := client.VPCs.ListMembers(ctx, id, tt.query, nil)
require.NoError(t, err)
require.Equal(t, tt.want, got)
})
}
}