Skip to content

Commit

Permalink
Merge pull request digitalocean#220 from digitalocean/jheimann/vpc
Browse files Browse the repository at this point in the history
roll out vpc functionality
  • Loading branch information
table-delete authored Apr 3, 2019
2 parents 87f69d1 + 6760694 commit c7d55c2
Show file tree
Hide file tree
Showing 9 changed files with 492 additions and 9 deletions.
3 changes: 3 additions & 0 deletions droplets.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Droplet struct {
Kernel *Kernel `json:"kernel,omitempty"`
Tags []string `json:"tags,omitempty"`
VolumeIDs []string `json:"volume_ids"`
VPCUUID string `json:"vpc_uuid,omitempty"`
}

// PublicIPv4 returns the public IPv4 address for the Droplet.
Expand Down Expand Up @@ -222,6 +223,7 @@ type DropletCreateRequest struct {
UserData string `json:"user_data,omitempty"`
Volumes []DropletCreateVolume `json:"volumes,omitempty"`
Tags []string `json:"tags"`
VPCUUID string `json:"vpc_uuid,omitempty"`
}

// DropletMultiCreateRequest is a request to create multiple Droplets.
Expand All @@ -237,6 +239,7 @@ type DropletMultiCreateRequest struct {
Monitoring bool `json:"monitoring"`
UserData string `json:"user_data,omitempty"`
Tags []string `json:"tags"`
VPCUUID string `json:"vpc_uuid,omitempty"`
}

func (d DropletCreateRequest) String() string {
Expand Down
70 changes: 63 additions & 7 deletions droplets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ func TestDroplets_Create(t *testing.T) {
{ID: "hello-im-another-volume"},
{Name: "hello-im-still-a-volume", ID: "should be ignored due to Name"},
},
Tags: []string{"one", "two"},
Tags: []string{"one", "two"},
VPCUUID: "880b7f98-f062-404d-b33c-458d545696f6",
}

mux.HandleFunc("/v2/droplets", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -171,8 +172,26 @@ func TestDroplets_Create(t *testing.T) {
map[string]interface{}{"id": "hello-im-another-volume"},
map[string]interface{}{"name": "hello-im-still-a-volume"},
},
"tags": []interface{}{"one", "two"},
"tags": []interface{}{"one", "two"},
"vpc_uuid": "880b7f98-f062-404d-b33c-458d545696f6",
}
jsonBlob := `
{
"droplet": {
"id": 1,
"vpc_uuid": "880b7f98-f062-404d-b33c-458d545696f6"
},
"links": {
"actions": [
{
"id": 1,
"href": "http://example.com",
"rel": "create"
}
]
}
}
`

var v map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&v)
Expand All @@ -184,7 +203,7 @@ func TestDroplets_Create(t *testing.T) {
t.Errorf("Request body\n got=%#v\nwant=%#v", v, expected)
}

fmt.Fprintf(w, `{"droplet":{"id":1}, "links":{"actions": [{"id": 1, "href": "http://example.com", "rel": "create"}]}}`)
fmt.Fprintf(w, jsonBlob)
})

droplet, resp, err := client.Droplets.Create(ctx, createRequest)
Expand All @@ -196,6 +215,11 @@ func TestDroplets_Create(t *testing.T) {
t.Errorf("expected id '%d', received '%d'", 1, id)
}

vpcid := "880b7f98-f062-404d-b33c-458d545696f6"
if id := droplet.VPCUUID; id != vpcid {
t.Errorf("expected VPC uuid '%s', received '%s'", vpcid, id)
}

if a := resp.Links.Actions[0]; a.ID != 1 {
t.Errorf("expected action id '%d', received '%d'", 1, a.ID)
}
Expand All @@ -212,7 +236,8 @@ func TestDroplets_CreateMultiple(t *testing.T) {
Image: DropletCreateImage{
ID: 1,
},
Tags: []string{"one", "two"},
Tags: []string{"one", "two"},
VPCUUID: "880b7f98-f062-404d-b33c-458d545696f6",
}

mux.HandleFunc("/v2/droplets", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -227,7 +252,31 @@ func TestDroplets_CreateMultiple(t *testing.T) {
"private_networking": false,
"monitoring": false,
"tags": []interface{}{"one", "two"},
"vpc_uuid": "880b7f98-f062-404d-b33c-458d545696f6",
}
jsonBlob := `
{
"droplets": [
{
"id": 1,
"vpc_uuid": "880b7f98-f062-404d-b33c-458d545696f6"
},
{
"id": 2,
"vpc_uuid": "880b7f98-f062-404d-b33c-458d545696f6"
}
],
"links": {
"actions": [
{
"id": 1,
"href": "http://example.com",
"rel": "multiple_create"
}
]
}
}
`

var v map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&v)
Expand All @@ -239,7 +288,7 @@ func TestDroplets_CreateMultiple(t *testing.T) {
t.Errorf("Request body = %#v, expected %#v", v, expected)
}

fmt.Fprintf(w, `{"droplets":[{"id":1},{"id":2}], "links":{"actions": [{"id": 1, "href": "http://example.com", "rel": "multiple_create"}]}}`)
fmt.Fprintf(w, jsonBlob)
})

droplets, resp, err := client.Droplets.CreateMultiple(ctx, createRequest)
Expand All @@ -250,9 +299,16 @@ func TestDroplets_CreateMultiple(t *testing.T) {
if id := droplets[0].ID; id != 1 {
t.Errorf("expected id '%d', received '%d'", 1, id)
}

if id := droplets[1].ID; id != 2 {
t.Errorf("expected id '%d', received '%d'", 1, id)
t.Errorf("expected id '%d', received '%d'", 2, id)
}

vpcid := "880b7f98-f062-404d-b33c-458d545696f6"
if id := droplets[0].VPCUUID; id != vpcid {
t.Errorf("expected VPC uuid '%s', received '%s'", vpcid, id)
}
if id := droplets[1].VPCUUID; id != vpcid {
t.Errorf("expected VPC uuid '%s', received '%s'", vpcid, id)
}

if a := resp.Links.Actions[0]; a.ID != 1 {
Expand Down
2 changes: 2 additions & 0 deletions godo.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Client struct {
Projects ProjectsService
Kubernetes KubernetesService
Databases DatabasesService
VPCs VPCsService

// Optional function called after every successful request made to the DO APIs
onRequestCompleted RequestCompletionCallback
Expand Down Expand Up @@ -181,6 +182,7 @@ func NewClient(httpClient *http.Client) *Client {
c.Tags = &TagsServiceOp{client: c}
c.Kubernetes = &KubernetesServiceOp{client: c}
c.Databases = &DatabasesServiceOp{client: c}
c.VPCs = &VPCsServiceOp{client: c}

return c
}
Expand Down
4 changes: 3 additions & 1 deletion kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
kubernetesOptionsPath = kubernetesBasePath + "/options"
)

// KubernetesService is an interface for interfacing with the kubernetes endpoints
// KubernetesService is an interface for interfacing with the Kubernetes endpoints
// of the DigitalOcean API.
// See: https://developers.digitalocean.com/documentation/v2#kubernetes
type KubernetesService interface {
Expand Down Expand Up @@ -50,6 +50,7 @@ type KubernetesClusterCreateRequest struct {
RegionSlug string `json:"region,omitempty"`
VersionSlug string `json:"version,omitempty"`
Tags []string `json:"tags,omitempty"`
VPCUUID string `json:"vpc_uuid,omitempty"`

NodePools []*KubernetesNodePoolCreateRequest `json:"node_pools,omitempty"`
}
Expand Down Expand Up @@ -94,6 +95,7 @@ type KubernetesCluster struct {
IPv4 string `json:"ipv4,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
Tags []string `json:"tags,omitempty"`
VPCUUID string `json:"vpc_uuid,omitempty"`

NodePools []*KubernetesNodePool `json:"node_pools,omitempty"`

Expand Down
11 changes: 11 additions & 0 deletions kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestKubernetesClusters_ListClusters(t *testing.T) {
ServiceSubnet: "10.245.0.0/16",
IPv4: "",
Tags: []string(nil),
VPCUUID: "880b7f98-f062-404d-b33c-458d545696f6",
Status: &KubernetesClusterStatus{
State: KubernetesClusterStatusRunning,
},
Expand Down Expand Up @@ -64,6 +65,7 @@ func TestKubernetesClusters_ListClusters(t *testing.T) {
ClusterSubnet: "10.244.0.0/16",
ServiceSubnet: "10.245.0.0/16",
IPv4: "1.2.3.4",
VPCUUID: "880b7f98-f062-404d-b33c-458d545696f7",
Status: &KubernetesClusterStatus{
State: KubernetesClusterStatusRunning,
},
Expand Down Expand Up @@ -107,6 +109,7 @@ func TestKubernetesClusters_ListClusters(t *testing.T) {
"service_subnet": "10.245.0.0/16",
"ipv4": "",
"tags": null,
"vpc_uuid": "880b7f98-f062-404d-b33c-458d545696f6",
"status": {
"state": "running"
},
Expand Down Expand Up @@ -155,6 +158,7 @@ func TestKubernetesClusters_ListClusters(t *testing.T) {
"status": {
"state": "running"
},
"vpc_uuid": "880b7f98-f062-404d-b33c-458d545696f7",
"node_pools": [
{
"id": "deadbeef-dead-beef-dead-deadbeefb4b3",
Expand Down Expand Up @@ -214,6 +218,7 @@ func TestKubernetesClusters_Get(t *testing.T) {
ClusterSubnet: "10.244.0.0/16",
ServiceSubnet: "10.245.0.0/16",
IPv4: "1.2.3.4",
VPCUUID: "880b7f98-f062-404d-b33c-458d545696f6",
Status: &KubernetesClusterStatus{
State: KubernetesClusterStatusRunning,
},
Expand Down Expand Up @@ -255,6 +260,7 @@ func TestKubernetesClusters_Get(t *testing.T) {
"service_subnet": "10.245.0.0/16",
"ipv4": "1.2.3.4",
"tags": null,
"vpc_uuid": "880b7f98-f062-404d-b33c-458d545696f6",
"status": {
"state": "running"
},
Expand Down Expand Up @@ -332,6 +338,7 @@ func TestKubernetesClusters_Create(t *testing.T) {
ClusterSubnet: "10.244.0.0/16",
ServiceSubnet: "10.245.0.0/16",
Tags: []string{"cluster-tag-1", "cluster-tag-2"},
VPCUUID: "880b7f98-f062-404d-b33c-458d545696f6",
NodePools: []*KubernetesNodePool{
&KubernetesNodePool{
ID: "8d91899c-0739-4a1a-acc5-deadbeefbb8a",
Expand All @@ -347,6 +354,7 @@ func TestKubernetesClusters_Create(t *testing.T) {
RegionSlug: want.RegionSlug,
VersionSlug: want.VersionSlug,
Tags: want.Tags,
VPCUUID: want.VPCUUID,
NodePools: []*KubernetesNodePoolCreateRequest{
&KubernetesNodePoolCreateRequest{
Size: want.NodePools[0].Size,
Expand All @@ -370,6 +378,7 @@ func TestKubernetesClusters_Create(t *testing.T) {
"cluster-tag-1",
"cluster-tag-2"
],
"vpc_uuid": "880b7f98-f062-404d-b33c-458d545696f6",
"node_pools": [
{
"id": "8d91899c-0739-4a1a-acc5-deadbeefbb8a",
Expand Down Expand Up @@ -415,6 +424,7 @@ func TestKubernetesClusters_Update(t *testing.T) {
ClusterSubnet: "10.244.0.0/16",
ServiceSubnet: "10.245.0.0/16",
Tags: []string{"cluster-tag-1", "cluster-tag-2"},
VPCUUID: "880b7f98-f062-404d-b33c-458d545696f6",
NodePools: []*KubernetesNodePool{
&KubernetesNodePool{
ID: "8d91899c-0739-4a1a-acc5-deadbeefbb8a",
Expand Down Expand Up @@ -443,6 +453,7 @@ func TestKubernetesClusters_Update(t *testing.T) {
"cluster-tag-1",
"cluster-tag-2"
],
"vpc_uuid": "880b7f98-f062-404d-b33c-458d545696f6",
"node_pools": [
{
"id": "8d91899c-0739-4a1a-acc5-deadbeefbb8a",
Expand Down
3 changes: 3 additions & 0 deletions load_balancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type LoadBalancer struct {
Tags []string `json:"tags,omitempty"`
RedirectHttpToHttps bool `json:"redirect_http_to_https,omitempty"`
EnableProxyProtocol bool `json:"enable_proxy_protocol,omitempty"`
VPCUUID string `json:"vpc_uuid,omitempty"`
}

// String creates a human-readable description of a LoadBalancer.
Expand All @@ -66,6 +67,7 @@ func (l LoadBalancer) AsRequest() *LoadBalancerRequest {
RedirectHttpToHttps: l.RedirectHttpToHttps,
EnableProxyProtocol: l.EnableProxyProtocol,
HealthCheck: l.HealthCheck,
VPCUUID: l.VPCUUID,
}

if l.HealthCheck != nil {
Expand Down Expand Up @@ -138,6 +140,7 @@ type LoadBalancerRequest struct {
Tags []string `json:"tags,omitempty"`
RedirectHttpToHttps bool `json:"redirect_http_to_https,omitempty"`
EnableProxyProtocol bool `json:"enable_proxy_protocol,omitempty"`
VPCUUID string `json:"vpc_uuid,omitempty"`
}

// String creates a human-readable description of a LoadBalancerRequest.
Expand Down
7 changes: 6 additions & 1 deletion load_balancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ var lbCreateJSONResponse = `
2,
21
],
"redirect_http_to_https":true
"redirect_http_to_https":true,
"vpc_uuid":"880b7f98-f062-404d-b33c-458d545696f6"
}
}
`
Expand Down Expand Up @@ -369,6 +370,7 @@ func TestLoadBalancers_Create(t *testing.T) {
Tags: []string{"my-tag"},
DropletIDs: []int{2, 21},
RedirectHttpToHttps: true,
VPCUUID: "880b7f98-f062-404d-b33c-458d545696f6",
}

path := "/v2/load_balancers"
Expand Down Expand Up @@ -438,6 +440,7 @@ func TestLoadBalancers_Create(t *testing.T) {
Tags: []string{"my-tag"},
DropletIDs: []int{2, 21},
RedirectHttpToHttps: true,
VPCUUID: "880b7f98-f062-404d-b33c-458d545696f6",
}

assert.Equal(t, expected, loadBalancer)
Expand Down Expand Up @@ -825,6 +828,7 @@ func TestLoadBalancers_AsRequest(t *testing.T) {
},
RedirectHttpToHttps: true,
EnableProxyProtocol: true,
VPCUUID: "880b7f98-f062-404d-b33c-458d545696f6",
}
lb.DropletIDs = make([]int, 1, 2)
lb.DropletIDs[0] = 12345
Expand Down Expand Up @@ -863,6 +867,7 @@ func TestLoadBalancers_AsRequest(t *testing.T) {
DropletIDs: []int{12345},
RedirectHttpToHttps: true,
EnableProxyProtocol: true,
VPCUUID: "880b7f98-f062-404d-b33c-458d545696f6",
}

r := lb.AsRequest()
Expand Down
Loading

0 comments on commit c7d55c2

Please sign in to comment.