forked from vmware/go-vcloud-director
/
vdc_network_profile.go
146 lines (115 loc) · 5.07 KB
/
vdc_network_profile.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
/*
* Copyright 2022 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
*/
package govcd
import (
"fmt"
"github.com/vmware/go-vcloud-director/v2/types/v56"
)
// VDC Network profiles have 1:1 mapping with VDC - each VDC has an option to configure VDC Network
// Profiles. types.VdcNetworkProfile holds more information about possible configurations
// GetVdcNetworkProfile retrieves VDC Network Profile configuration
// vdc.Vdc.ID must be set and valid present
func (vdc *Vdc) GetVdcNetworkProfile() (*types.VdcNetworkProfile, error) {
if vdc == nil || vdc.Vdc == nil || vdc.Vdc.ID == "" {
return nil, fmt.Errorf("cannot lookup VDC Network Profile configuration without VDC ID")
}
return getVdcNetworkProfile(vdc.client, vdc.Vdc.ID)
}
// GetVdcNetworkProfile retrieves VDC Network Profile configuration
// vdc.Vdc.ID must be set and valid present
func (adminVdc *AdminVdc) GetVdcNetworkProfile() (*types.VdcNetworkProfile, error) {
if adminVdc == nil || adminVdc.AdminVdc == nil || adminVdc.AdminVdc.ID == "" {
return nil, fmt.Errorf("cannot lookup VDC Network Profile configuration without VDC ID")
}
return getVdcNetworkProfile(adminVdc.client, adminVdc.AdminVdc.ID)
}
// UpdateVdcNetworkProfile updates the VDC Network Profile configuration
//
// Note. Whenever updating VDC Network Profile it is required to send all fields (not only the
// changed ones) as VCD will remove other configuration. Best practice is to fetch current
// configuration of VDC Network Profile using GetVdcNetworkProfile, alter it with new values and
// submit it to UpdateVdcNetworkProfile.
func (vdc *Vdc) UpdateVdcNetworkProfile(vdcNetworkProfileConfig *types.VdcNetworkProfile) (*types.VdcNetworkProfile, error) {
if vdc == nil || vdc.Vdc == nil || vdc.Vdc.ID == "" {
return nil, fmt.Errorf("cannot update VDC Network Profile configuration without ID")
}
return updateVdcNetworkProfile(vdc.client, vdc.Vdc.ID, vdcNetworkProfileConfig)
}
// UpdateVdcNetworkProfile updates the VDC Network Profile configuration
func (adminVdc *AdminVdc) UpdateVdcNetworkProfile(vdcNetworkProfileConfig *types.VdcNetworkProfile) (*types.VdcNetworkProfile, error) {
if adminVdc == nil || adminVdc.AdminVdc == nil || adminVdc.AdminVdc.ID == "" {
return nil, fmt.Errorf("cannot update VDC Network Profile configuration without ID")
}
return updateVdcNetworkProfile(adminVdc.client, adminVdc.AdminVdc.ID, vdcNetworkProfileConfig)
}
// DeleteVdcNetworkProfile deletes VDC Network Profile Configuration
func (vdc *Vdc) DeleteVdcNetworkProfile() error {
if vdc == nil || vdc.Vdc == nil || vdc.Vdc.ID == "" {
return fmt.Errorf("cannot lookup VDC Network Profile without VDC ID")
}
return deleteVdcNetworkProfile(vdc.client, vdc.Vdc.ID)
}
// DeleteVdcNetworkProfile deletes VDC Network Profile Configuration
func (adminVdc *AdminVdc) DeleteVdcNetworkProfile() error {
if adminVdc == nil || adminVdc.AdminVdc == nil || adminVdc.AdminVdc.ID == "" {
return fmt.Errorf("cannot lookup VDC Network Profile without VDC ID")
}
return deleteVdcNetworkProfile(adminVdc.client, adminVdc.AdminVdc.ID)
}
func getVdcNetworkProfile(client *Client, vdcId string) (*types.VdcNetworkProfile, error) {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointVdcNetworkProfile
apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return nil, err
}
if vdcId == "" {
return nil, fmt.Errorf("cannot lookup VDC Network Profile configuration without VDC ID")
}
urlRef, err := client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, vdcId))
if err != nil {
return nil, err
}
returnObject := &types.VdcNetworkProfile{}
err = client.OpenApiGetItem(apiVersion, urlRef, nil, returnObject, nil)
if err != nil {
return nil, err
}
return returnObject, nil
}
func updateVdcNetworkProfile(client *Client, vdcId string, vdcNetworkProfileConfig *types.VdcNetworkProfile) (*types.VdcNetworkProfile, error) {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointVdcNetworkProfile
apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return nil, err
}
if vdcId == "" {
return nil, fmt.Errorf("cannot update VDC Network Profile configuration without ID")
}
urlRef, err := client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, vdcId))
if err != nil {
return nil, err
}
returnObject := &types.VdcNetworkProfile{}
err = client.OpenApiPutItem(apiVersion, urlRef, nil, vdcNetworkProfileConfig, returnObject, nil)
if err != nil {
return nil, fmt.Errorf("error updating VDC Network Profile configuration: %s", err)
}
return returnObject, nil
}
func deleteVdcNetworkProfile(client *Client, vdcId string) error {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointVdcNetworkProfile
apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return err
}
urlRef, err := client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, vdcId))
if err != nil {
return err
}
err = client.OpenApiDeleteItem(apiVersion, urlRef, nil, nil)
if err != nil {
return fmt.Errorf("error deleting VDC Network Profile configuration: %s", err)
}
return nil
}