This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
virtualnetworks.go
117 lines (93 loc) · 3.94 KB
/
virtualnetworks.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
package packngo
import (
"path"
)
const virtualNetworkBasePath = "/virtual-networks"
// DevicePortService handles operations on a port which belongs to a particular device
type ProjectVirtualNetworkService interface {
List(projectID string, opts *ListOptions) (*VirtualNetworkListResponse, *Response, error)
Create(*VirtualNetworkCreateRequest) (*VirtualNetwork, *Response, error)
Get(string, *GetOptions) (*VirtualNetwork, *Response, error)
Delete(virtualNetworkID string) (*Response, error)
}
type VirtualNetwork struct {
ID string `json:"id"`
Description string `json:"description,omitempty"` // TODO: field can be null
VXLAN int `json:"vxlan,omitempty"`
FacilityCode string `json:"facility_code,omitempty"`
MetroCode string `json:"metro_code,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
Href string `json:"href"`
Project *Project `json:"assigned_to,omitempty"`
Facility *Facility `json:"facility,omitempty"`
Metro *Metro `json:"metro,omitempty"`
Instances []*Device `json:"instances,omitempty"`
}
type ProjectVirtualNetworkServiceOp struct {
client *Client
}
type VirtualNetworkListResponse struct {
VirtualNetworks []VirtualNetwork `json:"virtual_networks"`
}
func (i *ProjectVirtualNetworkServiceOp) List(projectID string, opts *ListOptions) (*VirtualNetworkListResponse, *Response, error) {
if validateErr := ValidateUUID(projectID); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(projectBasePath, projectID, virtualNetworkBasePath)
apiPathQuery := opts.WithQuery(endpointPath)
output := new(VirtualNetworkListResponse)
resp, err := i.client.DoRequest("GET", apiPathQuery, nil, output)
if err != nil {
return nil, nil, err
}
return output, resp, nil
}
type VirtualNetworkCreateRequest struct {
// ProjectID of the project where the VLAN will be made available.
ProjectID string `json:"project_id"`
// Description is a user supplied description of the VLAN.
Description string `json:"description"`
// TODO: default Description is null when not specified. Permitting *string here would require changing VirtualNetwork.Description to *string too.
// Facility in which to create the VLAN. Mutually exclusive with Metro.
Facility string `json:"facility,omitempty"`
// Metro in which to create the VLAN. Mutually exclusive with Facility.
Metro string `json:"metro,omitempty"`
// VXLAN is the VLAN ID. VXLAN may be specified when Metro is defined. It is remotely incremented otherwise. Must be unique per Metro.
VXLAN int `json:"vxlan,omitempty"`
}
func (i *ProjectVirtualNetworkServiceOp) Get(vlanID string, opts *GetOptions) (*VirtualNetwork, *Response, error) {
if validateErr := ValidateUUID(vlanID); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(virtualNetworkBasePath, vlanID)
apiPathQuery := opts.WithQuery(endpointPath)
vlan := new(VirtualNetwork)
resp, err := i.client.DoRequest("GET", apiPathQuery, nil, vlan)
if err != nil {
return nil, resp, err
}
return vlan, resp, err
}
func (i *ProjectVirtualNetworkServiceOp) Create(input *VirtualNetworkCreateRequest) (*VirtualNetwork, *Response, error) {
// TODO: May need to add timestamp to output from 'post' request
// for the 'created_at' attribute of VirtualNetwork struct since
// API response doesn't include it
apiPath := path.Join(projectBasePath, input.ProjectID, virtualNetworkBasePath)
output := new(VirtualNetwork)
resp, err := i.client.DoRequest("POST", apiPath, input, output)
if err != nil {
return nil, nil, err
}
return output, resp, nil
}
func (i *ProjectVirtualNetworkServiceOp) Delete(virtualNetworkID string) (*Response, error) {
if validateErr := ValidateUUID(virtualNetworkID); validateErr != nil {
return nil, validateErr
}
apiPath := path.Join(virtualNetworkBasePath, virtualNetworkID)
resp, err := i.client.DoRequest("DELETE", apiPath, nil, nil)
if err != nil {
return nil, err
}
return resp, nil
}