Skip to content

Commit

Permalink
vpcs: Support listing members of a VPC. (digitalocean#439)
Browse files Browse the repository at this point in the history
* vpcs: Support listing members of a VPC.

* Simplify tests.
  • Loading branch information
andrewsomething authored Mar 26, 2021
1 parent c3fc90e commit edf70d4
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
49 changes: 49 additions & 0 deletions vpcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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)
Expand Down Expand Up @@ -77,6 +78,16 @@ type VPC struct {
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"`
}
Expand All @@ -87,6 +98,12 @@ type vpcsRoot struct {
Meta *Meta `json:"meta"`
}

type vpcMembersRoot struct {
Members []*VPCMember `json:"members"`
Links *Links `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
Expand Down Expand Up @@ -214,3 +231,35 @@ func (v *VPCsServiceOp) Delete(ctx context.Context, id string) (*Response, error

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

}
114 changes: 114 additions & 0 deletions vpcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,117 @@ func TestVPCs_Delete(t *testing.T) {
_, 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)
})
}
}

0 comments on commit edf70d4

Please sign in to comment.