/
subnet_api.go
66 lines (57 loc) · 1.75 KB
/
subnet_api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package gcpiface
import (
"context"
"net/http"
compute "cloud.google.com/go/compute/apiv1"
"cloud.google.com/go/compute/apiv1/computepb"
"github.com/googleapis/gax-go/v2"
"google.golang.org/api/googleapi"
"google.golang.org/api/option"
)
type SubnetsApi interface {
Get(context.Context, *computepb.GetSubnetworkRequest, ...gax.CallOption) (*computepb.Subnetwork, error)
}
// GCP Client code below
type subnetsClient struct {
SubnetsApi
subnetsService *compute.SubnetworksClient
}
func NewSubnetsAPI(ctx context.Context, opt option.ClientOption) (SubnetsApi, error) {
subnetsRestClient, err := compute.NewSubnetworksRESTClient(ctx, opt)
if err != nil {
return nil, err
}
return &subnetsClient{
subnetsService: subnetsRestClient,
}, nil
}
func (c *subnetsClient) Get(ctx context.Context, req *computepb.GetSubnetworkRequest, opts ...gax.CallOption) (*computepb.Subnetwork, error) {
return c.subnetsService.Get(ctx, req, opts...)
}
// Mock Client code below
type MockSubnetsClient struct {
SubnetsApi
GetFn func(*computepb.GetSubnetworkRequest) (*computepb.Subnetwork, error)
GetFnTwo func(*computepb.GetSubnetworkRequest) (*computepb.Subnetwork, error)
call int
}
func GetMockSubnetsClient(modifyFn func(subnetClient *MockSubnetsClient)) *MockSubnetsClient {
mock := &MockSubnetsClient{
GetFn: func(req *computepb.GetSubnetworkRequest) (*computepb.Subnetwork, error) {
return nil, &googleapi.Error{
Code: http.StatusNotFound,
}
},
}
if modifyFn != nil {
modifyFn(mock)
}
return mock
}
func (m *MockSubnetsClient) Get(ctx context.Context, req *computepb.GetSubnetworkRequest, opts ...gax.CallOption) (*computepb.Subnetwork, error) {
m.call++
if m.GetFnTwo != nil && m.call > 1 {
return m.GetFnTwo(req)
}
return m.GetFn(req)
}