@@ -19,12 +19,15 @@ import (
1919const apiVersion = "v4"
2020
2121type fakeAPI struct {
22- t * testing.T
23- nb map [string ]* linodego.NodeBalancer
24- nbc map [string ]* linodego.NodeBalancerConfig
25- nbn map [string ]* linodego.NodeBalancerNode
26- fw map [int ]* linodego.Firewall // map of firewallID -> firewall
27- fwd map [int ]map [int ]* linodego.FirewallDevice // map of firewallID -> firewallDeviceID:FirewallDevice
22+ t * testing.T
23+ nb map [string ]* linodego.NodeBalancer
24+ nbc map [string ]* linodego.NodeBalancerConfig
25+ nbn map [string ]* linodego.NodeBalancerNode
26+ fw map [int ]* linodego.Firewall // map of firewallID -> firewall
27+ fwd map [int ]map [int ]* linodego.FirewallDevice // map of firewallID -> firewallDeviceID:FirewallDevice
28+ nbvpcc map [string ]* linodego.NodeBalancerVPCConfig
29+ vpc map [int ]* linodego.VPC
30+ subnet map [int ]* linodego.VPCSubnet
2831
2932 requests map [fakeRequest ]struct {}
3033 mux * http.ServeMux
@@ -44,6 +47,9 @@ func newFake(t *testing.T) *fakeAPI {
4447 nbn : make (map [string ]* linodego.NodeBalancerNode ),
4548 fw : make (map [int ]* linodego.Firewall ),
4649 fwd : make (map [int ]map [int ]* linodego.FirewallDevice ),
50+ nbvpcc : make (map [string ]* linodego.NodeBalancerVPCConfig ),
51+ vpc : make (map [int ]* linodego.VPC ),
52+ subnet : make (map [int ]* linodego.VPCSubnet ),
4753 requests : make (map [fakeRequest ]struct {}),
4854 mux : http .NewServeMux (),
4955 }
@@ -117,6 +123,54 @@ func (f *fakeAPI) setupRoutes() {
117123 _ , _ = w .Write (rr )
118124 })
119125
126+ f .mux .HandleFunc ("GET /v4/vpcs" , func (w http.ResponseWriter , r * http.Request ) {
127+ res := 0
128+ data := []linodego.VPC {}
129+ filter := r .Header .Get ("X-Filter" )
130+ if filter == "" {
131+ for _ , v := range f .vpc {
132+ data = append (data , * v )
133+ }
134+ } else {
135+ var fs map [string ]string
136+ err := json .Unmarshal ([]byte (filter ), & fs )
137+ if err != nil {
138+ f .t .Fatal (err )
139+ }
140+ for _ , v := range f .vpc {
141+ if v .Label != "" && fs ["label" ] != "" && v .Label == fs ["label" ] {
142+ data = append (data , * v )
143+ }
144+ }
145+ }
146+
147+ resp := paginatedResponse [linodego.VPC ]{
148+ Page : 1 ,
149+ Pages : 1 ,
150+ Results : res ,
151+ Data : data ,
152+ }
153+ rr , _ := json .Marshal (resp )
154+ _ , _ = w .Write (rr )
155+ })
156+
157+ f .mux .HandleFunc ("GET /v4/vpcs/{vpcId}/subnets" , func (w http.ResponseWriter , r * http.Request ) {
158+ res := 0
159+ vpcID , err := strconv .Atoi (r .PathValue ("vpcId" ))
160+ if err != nil {
161+ f .t .Fatal (err )
162+ }
163+
164+ resp := paginatedResponse [linodego.VPCSubnet ]{
165+ Page : 1 ,
166+ Pages : 1 ,
167+ Results : res ,
168+ Data : f .vpc [vpcID ].Subnets ,
169+ }
170+ rr , _ := json .Marshal (resp )
171+ _ , _ = w .Write (rr )
172+ })
173+
120174 f .mux .HandleFunc ("GET /v4/nodebalancers/{nodeBalancerId}" , func (w http.ResponseWriter , r * http.Request ) {
121175 nb , found := f .nb [r .PathValue ("nodeBalancerId" )]
122176 if ! found {
@@ -462,6 +516,54 @@ func (f *fakeAPI) setupRoutes() {
462516 _ , _ = w .Write (resp )
463517 })
464518
519+ f .mux .HandleFunc ("POST /v4/vpcs" , func (w http.ResponseWriter , r * http.Request ) {
520+ vco := linodego.VPCCreateOptions {}
521+ if err := json .NewDecoder (r .Body ).Decode (& vco ); err != nil {
522+ f .t .Fatal (err )
523+ }
524+
525+ subnets := []linodego.VPCSubnet {}
526+ for _ , s := range vco .Subnets {
527+ subnet := linodego.VPCSubnet {
528+ ID : rand .Intn (9999 ),
529+ IPv4 : s .IPv4 ,
530+ Label : s .Label ,
531+ }
532+ subnets = append (subnets , subnet )
533+ f .subnet [subnet .ID ] = & subnet
534+ }
535+ vpc := linodego.VPC {
536+ ID : rand .Intn (9999 ),
537+ Label : vco .Label ,
538+ Description : vco .Description ,
539+ Region : vco .Region ,
540+ Subnets : subnets ,
541+ }
542+
543+ f .vpc [vpc .ID ] = & vpc
544+ resp , err := json .Marshal (vpc )
545+ if err != nil {
546+ f .t .Fatal (err )
547+ }
548+ _ , _ = w .Write (resp )
549+ })
550+
551+ f .mux .HandleFunc ("DELETE /v4/vpcs/{vpcId}" , func (w http.ResponseWriter , r * http.Request ) {
552+ vpcid , err := strconv .Atoi (r .PathValue ("vpcId" ))
553+ if err != nil {
554+ f .t .Fatal (err )
555+ }
556+
557+ for k , v := range f .vpc {
558+ if v .ID == vpcid {
559+ for _ , s := range v .Subnets {
560+ delete (f .subnet , s .ID )
561+ }
562+ delete (f .vpc , k )
563+ }
564+ }
565+ })
566+
465567 f .mux .HandleFunc ("POST /v4/networking/firewalls/{firewallId}/devices" , func (w http.ResponseWriter , r * http.Request ) {
466568 fdco := linodego.FirewallDeviceCreateOptions {}
467569 if err := json .NewDecoder (r .Body ).Decode (& fdco ); err != nil {
0 commit comments