Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vpcs: Support listing members of a VPC. #439

Merged
merged 2 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
vpcs: Support listing members of a VPC.
  • Loading branch information
andrewsomething committed Mar 25, 2021
commit c533cc33ec04565a4c3a02b25f19659d37a20599
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting that it's never been implemented!

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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Golang question: Is it a paradigm that interface functions return pointers and not actual values?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not necessarily, but it's the general pattern we're using in godo.

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

}
118 changes: 118 additions & 0 deletions vpcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,121 @@ 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 interface{}
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)
})

var req *VPCListMembersRequest
if tt.query != nil {
req = tt.query.(*VPCListMembersRequest)
andrewsomething marked this conversation as resolved.
Show resolved Hide resolved
}
got, _, err := client.VPCs.ListMembers(ctx, id, req, nil)

require.NoError(t, err)
require.Equal(t, tt.want, got)
})
}
}