forked from vmware/go-vcloud-director
/
openapi_org_network_dhcp.go
186 lines (147 loc) · 6 KB
/
openapi_org_network_dhcp.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright 2021 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
*/
package govcd
import (
"fmt"
"net/url"
"github.com/vmware/go-vcloud-director/v2/types/v56"
)
// OpenApiOrgVdcNetwork uses OpenAPI endpoint to operate both - NSX-T and NSX-V Org VDC network DHCP settings
type OpenApiOrgVdcNetworkDhcp struct {
OpenApiOrgVdcNetworkDhcp *types.OpenApiOrgVdcNetworkDhcp
client *Client
}
// GetOpenApiOrgVdcNetworkDhcp allows to retrieve DHCP configuration for specific Org VDC network
func (orgVdcNet *OpenApiOrgVdcNetwork) GetOpenApiOrgVdcNetworkDhcp() (*OpenApiOrgVdcNetworkDhcp, error) {
if orgVdcNet == nil || orgVdcNet.client == nil {
return nil, fmt.Errorf("error ")
}
client := orgVdcNet.client
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworksDhcp
apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return nil, err
}
if orgVdcNet.OpenApiOrgVdcNetwork.ID == "" {
return nil, fmt.Errorf("empty Org VDC network ID")
}
urlRef, err := client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, orgVdcNet.OpenApiOrgVdcNetwork.ID))
if err != nil {
return nil, err
}
orgNetDhcp := &OpenApiOrgVdcNetworkDhcp{
OpenApiOrgVdcNetworkDhcp: &types.OpenApiOrgVdcNetworkDhcp{},
client: client,
}
err = client.OpenApiGetItem(apiVersion, urlRef, nil, orgNetDhcp.OpenApiOrgVdcNetworkDhcp, nil)
if err != nil {
return nil, err
}
return orgNetDhcp, nil
}
// GetOpenApiOrgVdcNetworkDhcp allows to retrieve DHCP configuration for specific Org VDC network
// ID specified as orgNetworkId using OpenAPI
func (vdc *Vdc) GetOpenApiOrgVdcNetworkDhcp(orgNetworkId string) (*OpenApiOrgVdcNetworkDhcp, error) {
client := vdc.client
// Inject Vdc ID filter to perform filtering on server side
params := url.Values{}
queryParameters := queryParameterFilterAnd("orgVdc.id=="+vdc.Vdc.ID, params)
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworksDhcp
apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return nil, err
}
if orgNetworkId == "" {
return nil, fmt.Errorf("empty Org VDC network ID")
}
urlRef, err := client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, orgNetworkId))
if err != nil {
return nil, err
}
orgNetDhcp := &OpenApiOrgVdcNetworkDhcp{
OpenApiOrgVdcNetworkDhcp: &types.OpenApiOrgVdcNetworkDhcp{},
client: client,
}
err = client.OpenApiGetItem(apiVersion, urlRef, queryParameters, orgNetDhcp.OpenApiOrgVdcNetworkDhcp, nil)
if err != nil {
return nil, err
}
return orgNetDhcp, nil
}
// UpdateOpenApiOrgVdcNetworkDhcp allows to update DHCP configuration for specific Org VDC network
// ID specified as orgNetworkId using OpenAPI
func (vdc *Vdc) UpdateOpenApiOrgVdcNetworkDhcp(orgNetworkId string, orgVdcNetworkDhcpConfig *types.OpenApiOrgVdcNetworkDhcp) (*OpenApiOrgVdcNetworkDhcp, error) {
return updateOrgNetworkDhcp(vdc.client, orgNetworkId, orgVdcNetworkDhcpConfig)
}
// DeleteOpenApiOrgVdcNetworkDhcp allows to perform HTTP DELETE request on DHCP pool configuration for specified Org VDC
// Network ID
func (vdc *Vdc) DeleteOpenApiOrgVdcNetworkDhcp(orgNetworkId string) error {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworksDhcp
apiVersion, err := vdc.client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return err
}
if orgNetworkId == "" {
return fmt.Errorf("cannot delete Org VDC network DHCP configuration without ID")
}
urlRef, err := vdc.client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, orgNetworkId))
if err != nil {
return err
}
err = vdc.client.OpenApiDeleteItem(apiVersion, urlRef, nil, nil)
if err != nil {
return fmt.Errorf("error deleting Org VDC network DHCP configuration: %s", err)
}
return nil
}
// DeletNetworkDhcp allows to perform HTTP DELETE request on DHCP pool configuration for Org network
func (orgVdcNet *OpenApiOrgVdcNetwork) DeletNetworkDhcp() error {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworksDhcp
apiVersion, err := orgVdcNet.client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return err
}
if orgVdcNet.OpenApiOrgVdcNetwork.ID == "" {
return fmt.Errorf("cannot delete Org VDC network DHCP configuration without ID")
}
urlRef, err := orgVdcNet.client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, orgVdcNet.OpenApiOrgVdcNetwork.ID))
if err != nil {
return err
}
err = orgVdcNet.client.OpenApiDeleteItem(apiVersion, urlRef, nil, nil)
if err != nil {
return fmt.Errorf("error deleting Org VDC network DHCP configuration: %s", err)
}
return nil
}
func updateOrgNetworkDhcp(client *Client, orgNetworkId string, orgVdcNetworkDhcpConfig *types.OpenApiOrgVdcNetworkDhcp) (*OpenApiOrgVdcNetworkDhcp, error) {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworksDhcp
apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return nil, err
}
urlRef, err := client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, orgNetworkId))
if err != nil {
return nil, err
}
orgNetDhcpResponse := &OpenApiOrgVdcNetworkDhcp{
OpenApiOrgVdcNetworkDhcp: &types.OpenApiOrgVdcNetworkDhcp{},
client: client,
}
// From v35.0 onwards, if orgVdcNetworkDhcpConfig.LeaseTime or orgVdcNetworkDhcpConfig.Mode are not explicitly
// passed, the API doesn't use any defaults returning an error. Previous API versions were setting
// LeaseTime to 86400 seconds and Mode to EDGE if these values were not supplied. These two conditional
// address the situation.
if orgVdcNetworkDhcpConfig.LeaseTime == nil {
orgVdcNetworkDhcpConfig.LeaseTime = takeIntAddress(86400)
}
if len(orgVdcNetworkDhcpConfig.Mode) == 0 {
orgVdcNetworkDhcpConfig.Mode = "EDGE"
}
err = client.OpenApiPutItem(apiVersion, urlRef, nil, orgVdcNetworkDhcpConfig, orgNetDhcpResponse.OpenApiOrgVdcNetworkDhcp, nil)
if err != nil {
return nil, fmt.Errorf("error updating Org VDC network DHCP configuration: %s", err)
}
return orgNetDhcpResponse, nil
}