/
vpcs.go
121 lines (97 loc) · 2.78 KB
/
vpcs.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Copyright 2018 The Doctl Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package do
import (
"context"
"github.com/digitalocean/godo"
)
// VPC wraps a godo VPC.
type VPC struct {
*godo.VPC
}
// VPCs is a slice of VPC.
type VPCs []VPC
// VPCsService is the godo VPCsService interface.
type VPCsService interface {
Get(vpcUUID string) (*VPC, error)
List() (VPCs, error)
Create(vpcr *godo.VPCCreateRequest) (*VPC, error)
Update(vpcUUID string, vpcr *godo.VPCUpdateRequest) (*VPC, error)
PartialUpdate(vpcUUID string, options ...godo.VPCSetField) (*VPC, error)
Delete(vpcUUID string) error
}
var _ VPCsService = &vpcsService{}
type vpcsService struct {
client *godo.Client
}
// NewVPCsService builds an instance of VPCsService.
func NewVPCsService(client *godo.Client) VPCsService {
return &vpcsService{
client: client,
}
}
func (v *vpcsService) Get(vpcUUID string) (*VPC, error) {
vpc, _, err := v.client.VPCs.Get(context.TODO(), vpcUUID)
if err != nil {
return nil, err
}
return &VPC{VPC: vpc}, nil
}
func (v *vpcsService) List() (VPCs, error) {
f := func(opt *godo.ListOptions) ([]any, *godo.Response, error) {
list, resp, err := v.client.VPCs.List(context.TODO(), opt)
if err != nil {
return nil, nil, err
}
si := make([]any, len(list))
for i := range list {
si[i] = list[i]
}
return si, resp, err
}
si, err := PaginateResp(f)
if err != nil {
return nil, err
}
list := make([]VPC, len(si))
for i := range si {
a := si[i].(*godo.VPC)
list[i] = VPC{VPC: a}
}
return list, nil
}
func (v *vpcsService) Create(vpcr *godo.VPCCreateRequest) (*VPC, error) {
vpc, _, err := v.client.VPCs.Create(context.TODO(), vpcr)
if err != nil {
return nil, err
}
return &VPC{VPC: vpc}, nil
}
func (v *vpcsService) Update(vpcUUID string, vpcr *godo.VPCUpdateRequest) (*VPC, error) {
vpc, _, err := v.client.VPCs.Update(context.TODO(), vpcUUID, vpcr)
if err != nil {
return nil, err
}
return &VPC{VPC: vpc}, nil
}
func (v *vpcsService) PartialUpdate(vpcUUID string, options ...godo.VPCSetField) (*VPC, error) {
vpc, _, err := v.client.VPCs.Set(context.TODO(), vpcUUID, options...)
if err != nil {
return nil, err
}
return &VPC{VPC: vpc}, nil
}
func (v *vpcsService) Delete(vpcUUID string) error {
_, err := v.client.VPCs.Delete(context.TODO(), vpcUUID)
return err
}