-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvcloud.go
More file actions
224 lines (199 loc) · 6.01 KB
/
vcloud.go
File metadata and controls
224 lines (199 loc) · 6.01 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"errors"
"fmt"
"net/url"
"strings"
"github.com/vmware/go-vcloud-director/v2/govcd"
"github.com/vmware/go-vcloud-director/v2/types/v56"
)
type vCloudConfig struct {
VcdClient *govcd.VCDClient
Client *govcd.Client
Org *govcd.Org
Vdc *govcd.Vdc
}
var vConfig vCloudConfig
func vcloudAuth(username, password string) error {
_, err := vConfig.VcdClient.GetAuthResponse(username, password, vOrg)
return err
}
func initVCD() error {
apiEndPoint, err := url.Parse(vHref)
if err != nil {
fmt.Println(err)
return err
}
fmt.Println("[vCloud] Creating VCD client...")
vcdClient := govcd.NewVCDClient(*apiEndPoint, vInsecure)
fmt.Println("[vCloud] Authenticating...")
err = vcdClient.Authenticate(codyConf.VCloudUsername, codyConf.VCloudPassword, vOrg)
if err != nil {
fmt.Println(err)
return err
}
fmt.Println("[vCloud] Getting org...")
org, err := vcdClient.GetOrgByName(vOrg)
if err != nil {
fmt.Println("entity", vOrg, "not found:", err)
return err
}
fmt.Println("[vCloud] Getting vdc...")
vdc, err := org.GetVDCByName(vVDC, true)
if err != nil {
fmt.Println(err)
return err
}
vConfig = vCloudConfig{
VcdClient: vcdClient,
Client: &vcdClient.Client,
Org: org,
Vdc: vdc,
}
return nil
}
func vappDeploy(vapp string, user string) (string, error) {
if !validateName(vapp) {
return "", errors.New("Invalid name")
}
vapp = strings.TrimSpace(vapp)
user = strings.TrimSpace(user)
name := user + "-" + vapp
storageProfileName := "Defsec"
networkName := "DefSec_01"
// get catalocg
catalog, err := vConfig.Org.FindCatalog("Other") // TODO change to DefSec_Lessons
if err != nil {
return "", err
} else if catalog.Catalog == nil {
return "", errors.New("Catalog not found")
}
item, err := catalog.GetCatalogItemByName(strings.TrimSpace(vapp), true)
if err != nil {
return "", err
} else if item == nil {
return "", errors.New("Catalog item not found")
}
fmt.Println(item)
// find vapp template
vappTemplate, err := item.GetVAppTemplate()
if err != nil {
panic(err)
}
var networks []*types.OrgVDCNetwork
net, err := vConfig.Vdc.GetOrgVdcNetworkByName(networkName, false)
if err != nil {
return "", fmt.Errorf("error finding network : %s, err: %s",
networkName, err)
}
networks = append(networks, net.OrgVDCNetwork)
// Get StorageProfileReference
storageProfileRef, err := vConfig.Vdc.FindStorageProfileReference(storageProfileName)
if err != nil {
return "", fmt.Errorf("error finding storage profile: %s", err)
}
task, err := vConfig.Vdc.ComposeVApp(networks, vappTemplate, storageProfileRef, name, "description", true)
if err != nil {
//return "", fmt.Errorf("error composing vapp: %s", err)
return "", errors.New("VApp already exists!")
}
// After a successful creation, the entity is added to the cleanup list.
// If something fails after this point, the entity will be removed
err = task.WaitTaskCompletion()
if err != nil {
return "", fmt.Errorf("error composing vapp: %s", err)
}
// Get VApp
vappLive, err := vConfig.Vdc.GetVAppByName(name, true)
if err != nil {
return "", fmt.Errorf("error getting vapp: %s", err)
}
err = vappLive.BlockWhileStatus("UNRESOLVED", vConfig.Client.MaxRetryTimeout)
if err != nil {
return "", fmt.Errorf("error waiting for created test vApp to have working state: %s", err)
}
if err != nil {
fmt.Println("error creating vApp: %s", err)
}
return "id? vapp obj?", nil
// DEPLOY VAPP TO USER
}
func vappPowerAndIPs(id string) (string, error) {
// POWER ON VAPP AND GET IPS.
return "ip1", nil
}
func takeBoolPointer(value bool) *bool {
return &value
}
func takeIntPointer(value int) *int {
return &value
}
/*
func testvCloud() {
vappName := "CoolBroMan"
vmName := "yep"
err := vConfig.Vdc.ComposeRawVApp(vappName)
if err != nil {
fmt.Println("error creating vApp: %s", err)
}
vapp, err := vConfig.Vdc.GetVAppByName(vappName, true)
if err != nil {
fmt.Println("unable to find vApp by name %s: %s", vappName, err)
}
// must wait until the vApp exits
err = vapp.BlockWhileStatus("UNRESOLVED", vConfig.VcdClient.Client.MaxRetryTimeout)
if err != nil {
fmt.Println("error waiting for created test vApp to have working state: %s", err)
}
desiredNetConfig := &types.NetworkConnectionSection{}
desiredNetConfig.PrimaryNetworkConnectionIndex = 2
desiredNetConfig.NetworkConnection = append(desiredNetConfig.NetworkConnection,
&types.NetworkConnection{
IsConnected: true,
IPAddressAllocationMode: types.IPAllocationModeNone,
Network: types.NoneNetwork,
NetworkConnectionIndex: 1,
},
&types.NetworkConnection{
IsConnected: true,
IPAddressAllocationMode: types.IPAllocationModeNone,
Network: types.NoneNetwork,
NetworkConnectionIndex: 2,
})
newDisk := types.DiskSettings{
AdapterType: "5",
SizeMb: int64(16384),
BusNumber: 0,
UnitNumber: 0,
ThinProvisioned: takeBoolPointer(true),
OverrideVmDefault: true,
}
requestDetails := &types.RecomposeVAppParamsForEmptyVm{
CreateItem: &types.CreateItem{
Name: vmName,
NetworkConnectionSection: desiredNetConfig,
Description: "created by test",
GuestCustomizationSection: nil,
VmSpecSection: &types.VmSpecSection{
Modified: takeBoolPointer(true),
Info: "Virtual Machine specification",
OsType: "debian10Guest",
NumCpus: takeIntPointer(2),
NumCoresPerSocket: takeIntPointer(1),
CpuResourceMhz: &types.CpuResourceMhz{Configured: 1},
MemoryResourceMb: &types.MemoryResourceMb{Configured: 512},
MediaSection: nil,
DiskSection: &types.DiskSection{DiskSettings: []*types.DiskSettings{&newDisk}},
HardwareVersion: &types.HardwareVersion{Value: "vmx-13"}, // need support older version vCD
VirtualCpuType: "VM32",
},
},
AllEULAsAccepted: true,
}
vm, err := vapp.AddEmptyVm(requestDetails)
if err != nil {
fmt.Println("error creating empty VM: %s", err)
}
fmt.Println(vm)
}
*/