Skip to content

Commit 2edf9b0

Browse files
committed
Added parameters validation
Added parameters validation
1 parent a870bf1 commit 2edf9b0

File tree

6 files changed

+259
-54
lines changed

6 files changed

+259
-54
lines changed

clients/imageClient/imageClient.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ func GetImageList() (ImageList, error){
2525
}
2626

2727
func ResolveImageName(imageName string) (error) {
28+
if len(imageName) == 0 {
29+
return fmt.Errorf(azure.ParamNotSpecifiedError, "imageName")
30+
}
31+
2832
imageList, err := GetImageList()
2933
if err != nil {
3034
return err

clients/locationClient/locationClient.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,27 @@ const (
1414
invalidLocationError = "Invalid location. Available locations: %s"
1515
)
1616

17-
func ResolveLocation(specifiedLocation string) (error) {
17+
func ResolveLocation(location string) (error) {
18+
if len(location) == 0 {
19+
return fmt.Errorf(azure.ParamNotSpecifiedError, "location")
20+
}
21+
1822
locations, err := GetLocationList()
1923
if err != nil {
2024
return err
2125
}
2226

23-
for _, location := range locations.Locations {
24-
if location.Name != specifiedLocation {
27+
for _, existingLocation := range locations.Locations {
28+
if existingLocation.Name != location {
2529
continue
2630
}
2731

2832
return nil
2933
}
3034

3135
var availableLocations bytes.Buffer
32-
for _, location := range locations.Locations {
33-
availableLocations.WriteString(location.Name + ", ")
36+
for _, existingLocation := range locations.Locations {
37+
availableLocations.WriteString(existingLocation.Name + ", ")
3438
}
3539

3640
return errors.New(fmt.Sprintf(invalidLocationError, strings.Trim(availableLocations.String(), ", ")))

clients/storageServiceClient/storageServiceClient.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ func GetStorageServiceList() (*StorageServiceList, error){
3434
}
3535

3636
func GetStorageServiceByName(serviceName string) (*StorageService, error){
37+
if len(serviceName) == 0 {
38+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "serviceName")
39+
}
40+
3741
storageService := new(StorageService)
3842
requestURL := fmt.Sprintf(azureStorageServiceURL, serviceName)
3943
response, err := azure.SendAzureGetRequest(requestURL)
@@ -50,6 +54,10 @@ func GetStorageServiceByName(serviceName string) (*StorageService, error){
5054
}
5155

5256
func GetStorageServiceByLocation(location string) (*StorageService, error) {
57+
if len(location) == 0 {
58+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "location")
59+
}
60+
5361
storageService := new(StorageService)
5462
storageServiceList, err := GetStorageServiceList()
5563
if err != nil {
@@ -68,6 +76,13 @@ func GetStorageServiceByLocation(location string) (*StorageService, error) {
6876
}
6977

7078
func CreateStorageService(name, location string) (*StorageService, error){
79+
if len(name) == 0 {
80+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "name")
81+
}
82+
if len(location) == 0 {
83+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "location")
84+
}
85+
7186
storageDeploymentConfig := createStorageServiceDeploymentConf(name, location)
7287
deploymentBytes, err := xml.Marshal(storageDeploymentConfig)
7388
if err != nil {

clients/vmClient/vmClient.go

Lines changed: 136 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,22 @@ const (
3838
dockerPrivateConfig = "{ \"ca\": \"%s\", \"server-cert\": \"%s\", \"server-key\": \"%s\" }"
3939
dockerDirExistsMessage = "Docker directory exists"
4040

41-
missingDockerCertsError = "You should generate docker certificates first. Info can be found here: https://docs.docker.com/articles/https/"
41+
missingDockerCertsError = "Can not find docker certificates folder %s. You should generate docker certificates first. Info can be found here: https://docs.docker.com/articles/https/"
4242
provisioningConfDoesNotExistsError = "You should set azure VM provisioning config first"
4343
invalidCertExtensionError = "Certificate %s is invalid. Please specify %s certificate."
4444
invalidOSError = "You must specify correct OS param. Valid values are 'Linux' and 'Windows'"
4545
)
4646

47-
// REGION PUBLIC METHODS STARTS
47+
//Region public methods starts
4848

4949
func CreateAzureVM(role *Role, dnsName, location string) error {
50-
50+
if len(dnsName) == 0 {
51+
return fmt.Errorf(azure.ParamNotSpecifiedError, "dnsName")
52+
}
53+
if len(location) == 0 {
54+
return fmt.Errorf(azure.ParamNotSpecifiedError, "location")
55+
}
56+
5157
err := locationClient.ResolveLocation(location)
5258
if err != nil {
5359
return err
@@ -90,7 +96,13 @@ func CreateAzureVM(role *Role, dnsName, location string) error {
9096
}
9197

9298
func CreateHostedService(dnsName, location string) (string, error) {
93-
99+
if len(dnsName) == 0 {
100+
return "", fmt.Errorf(azure.ParamNotSpecifiedError, "dnsName")
101+
}
102+
if len(location) == 0 {
103+
return "", fmt.Errorf(azure.ParamNotSpecifiedError, "location")
104+
}
105+
94106
err := locationClient.ResolveLocation(location)
95107
if err != nil {
96108
return "", err
@@ -112,7 +124,10 @@ func CreateHostedService(dnsName, location string) (string, error) {
112124
}
113125

114126
func DeleteHostedService(dnsName string) error {
115-
127+
if len(dnsName) == 0 {
128+
return fmt.Errorf(azure.ParamNotSpecifiedError, "dnsName")
129+
}
130+
116131
requestURL := fmt.Sprintf(azureHostedServiceURL, dnsName)
117132
requestId, err := azure.SendAzureDeleteRequest(requestURL)
118133
if err != nil {
@@ -124,6 +139,19 @@ func DeleteHostedService(dnsName string) error {
124139
}
125140

126141
func CreateAzureVMConfiguration(name, instanceSize, imageName, location string) (*Role, error) {
142+
if len(name) == 0 {
143+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "name")
144+
}
145+
if len(instanceSize) == 0 {
146+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "instanceSize")
147+
}
148+
if len(imageName) == 0 {
149+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "imageName")
150+
}
151+
if len(location) == 0 {
152+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "location")
153+
}
154+
127155
fmt.Println("Creating azure VM configuration... ")
128156

129157
err := locationClient.ResolveLocation(location)
@@ -140,6 +168,16 @@ func CreateAzureVMConfiguration(name, instanceSize, imageName, location string)
140168
}
141169

142170
func AddAzureLinuxProvisioningConfig(azureVMConfig *Role, userName, password, certPath string) (*Role, error) {
171+
if len(userName) == 0 {
172+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "userName")
173+
}
174+
if len(password) == 0 {
175+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "password")
176+
}
177+
if len(certPath) == 0 {
178+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "certPath")
179+
}
180+
143181
fmt.Println("Adding azure provisioning configuration... ")
144182

145183
configurationSets := ConfigurationSets{}
@@ -168,7 +206,20 @@ func AddAzureLinuxProvisioningConfig(azureVMConfig *Role, userName, password, ce
168206
return azureVMConfig, nil
169207
}
170208

171-
func SetAzureVMExtension(azureVMConfiguration *Role, name string, publisher string, version string, referenceName string, state string, publicConfigurationValue string, privateConfigurationValue string) (*Role) {
209+
func SetAzureVMExtension(azureVMConfiguration *Role, name string, publisher string, version string, referenceName string, state string, publicConfigurationValue string, privateConfigurationValue string) (*Role, error) {
210+
if len(name) == 0 {
211+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "name")
212+
}
213+
if len(publisher) == 0 {
214+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "publisher")
215+
}
216+
if len(version) == 0 {
217+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "version")
218+
}
219+
if len(referenceName) == 0 {
220+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "referenceName")
221+
}
222+
172223
fmt.Printf("Setting azure VM extension: %s... \n", name)
173224

174225
extension := ResourceExtensionReference{}
@@ -198,10 +249,14 @@ func SetAzureVMExtension(azureVMConfiguration *Role, name string, publisher stri
198249

199250
azureVMConfiguration.ResourceExtensionReferences.ResourceExtensionReference = append(azureVMConfiguration.ResourceExtensionReferences.ResourceExtensionReference, extension)
200251

201-
return azureVMConfiguration
252+
return azureVMConfiguration, nil
202253
}
203254

204255
func SetAzureDockerVMExtension(azureVMConfiguration *Role, dockerCertDir string, dockerPort int, version string) (*Role, error) {
256+
if len(dockerCertDir) == 0 {
257+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "dockerCertDir")
258+
}
259+
205260
if len(version) == 0 {
206261
version = "0.3"
207262
}
@@ -217,11 +272,18 @@ func SetAzureDockerVMExtension(azureVMConfiguration *Role, dockerCertDir string,
217272
return nil, err
218273
}
219274

220-
azureVMConfiguration = SetAzureVMExtension(azureVMConfiguration, "DockerExtension", "MSOpenTech.Extensions", version, "DockerExtension", "enable", publicConfiguration, privateConfiguration)
275+
azureVMConfiguration, err = SetAzureVMExtension(azureVMConfiguration, "DockerExtension", "MSOpenTech.Extensions", version, "DockerExtension", "enable", publicConfiguration, privateConfiguration)
221276
return azureVMConfiguration, nil
222277
}
223278

224279
func GetVMDeployment(cloudserviceName, deploymentName string) (*VMDeployment, error) {
280+
if len(cloudserviceName) == 0 {
281+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "cloudserviceName")
282+
}
283+
if len(deploymentName) == 0 {
284+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "deploymentName")
285+
}
286+
225287
deployment := new(VMDeployment)
226288

227289
requestURL := fmt.Sprintf(azureDeploymentURL, cloudserviceName, deploymentName)
@@ -239,7 +301,13 @@ func GetVMDeployment(cloudserviceName, deploymentName string) (*VMDeployment, er
239301
}
240302

241303
func DeleteVMDeployment(cloudserviceName, deploymentName string) error {
242-
304+
if len(cloudserviceName) == 0 {
305+
return fmt.Errorf(azure.ParamNotSpecifiedError, "cloudserviceName")
306+
}
307+
if len(deploymentName) == 0 {
308+
return fmt.Errorf(azure.ParamNotSpecifiedError, "deploymentName")
309+
}
310+
243311
requestURL := fmt.Sprintf(azureDeploymentURL, cloudserviceName, deploymentName)
244312
requestId, err := azure.SendAzureDeleteRequest(requestURL)
245313
if err != nil {
@@ -251,6 +319,16 @@ func DeleteVMDeployment(cloudserviceName, deploymentName string) error {
251319
}
252320

253321
func GetRole(cloudserviceName, deploymentName, roleName string) (*Role, error) {
322+
if len(cloudserviceName) == 0 {
323+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "cloudserviceName")
324+
}
325+
if len(deploymentName) == 0 {
326+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "deploymentName")
327+
}
328+
if len(roleName) == 0 {
329+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "roleName")
330+
}
331+
254332
role := new(Role)
255333

256334
requestURL := fmt.Sprintf(azureRoleURL, cloudserviceName, deploymentName, roleName)
@@ -267,7 +345,17 @@ func GetRole(cloudserviceName, deploymentName, roleName string) (*Role, error) {
267345
return role, nil
268346
}
269347

270-
func StartRole(cloudserviceName, deploymentName, roleName string) (error) {
348+
func StartRole(cloudserviceName, deploymentName, roleName string) error {
349+
if len(cloudserviceName) == 0 {
350+
return fmt.Errorf(azure.ParamNotSpecifiedError, "cloudserviceName")
351+
}
352+
if len(deploymentName) == 0 {
353+
return fmt.Errorf(azure.ParamNotSpecifiedError, "deploymentName")
354+
}
355+
if len(roleName) == 0 {
356+
return fmt.Errorf(azure.ParamNotSpecifiedError, "roleName")
357+
}
358+
271359
startRoleOperation := createStartRoleOperation()
272360

273361
startRoleOperationBytes, err := xml.Marshal(startRoleOperation)
@@ -285,7 +373,17 @@ func StartRole(cloudserviceName, deploymentName, roleName string) (error) {
285373
return nil
286374
}
287375

288-
func ShutdownRole(cloudserviceName, deploymentName, roleName string) (error) {
376+
func ShutdownRole(cloudserviceName, deploymentName, roleName string) error {
377+
if len(cloudserviceName) == 0 {
378+
return fmt.Errorf(azure.ParamNotSpecifiedError, "cloudserviceName")
379+
}
380+
if len(deploymentName) == 0 {
381+
return fmt.Errorf(azure.ParamNotSpecifiedError, "deploymentName")
382+
}
383+
if len(roleName) == 0 {
384+
return fmt.Errorf(azure.ParamNotSpecifiedError, "roleName")
385+
}
386+
289387
shutdownRoleOperation := createShutdowRoleOperation()
290388

291389
shutdownRoleOperationBytes, err := xml.Marshal(shutdownRoleOperation)
@@ -303,7 +401,17 @@ func ShutdownRole(cloudserviceName, deploymentName, roleName string) (error) {
303401
return nil
304402
}
305403

306-
func RestartRole(cloudserviceName, deploymentName, roleName string) (error) {
404+
func RestartRole(cloudserviceName, deploymentName, roleName string) error {
405+
if len(cloudserviceName) == 0 {
406+
return fmt.Errorf(azure.ParamNotSpecifiedError, "cloudserviceName")
407+
}
408+
if len(deploymentName) == 0 {
409+
return fmt.Errorf(azure.ParamNotSpecifiedError, "deploymentName")
410+
}
411+
if len(roleName) == 0 {
412+
return fmt.Errorf(azure.ParamNotSpecifiedError, "roleName")
413+
}
414+
307415
restartRoleOperation := createRestartRoleOperation()
308416

309417
restartRoleOperationBytes, err := xml.Marshal(restartRoleOperation)
@@ -321,7 +429,17 @@ func RestartRole(cloudserviceName, deploymentName, roleName string) (error) {
321429
return nil
322430
}
323431

324-
func DeleteRole(cloudserviceName, deploymentName, roleName string) (error) {
432+
func DeleteRole(cloudserviceName, deploymentName, roleName string) error {
433+
if len(cloudserviceName) == 0 {
434+
return fmt.Errorf(azure.ParamNotSpecifiedError, "cloudserviceName")
435+
}
436+
if len(deploymentName) == 0 {
437+
return fmt.Errorf(azure.ParamNotSpecifiedError, "deploymentName")
438+
}
439+
if len(roleName) == 0 {
440+
return fmt.Errorf(azure.ParamNotSpecifiedError, "roleName")
441+
}
442+
325443
requestURL := fmt.Sprintf(azureRoleURL, cloudserviceName, deploymentName, roleName)
326444
requestId, azureErr := azure.SendAzureDeleteRequest(requestURL)
327445
if azureErr != nil {
@@ -332,10 +450,10 @@ func DeleteRole(cloudserviceName, deploymentName, roleName string) (error) {
332450
return nil
333451
}
334452

335-
// REGION PUBLIC METHODS ENDS
453+
//Region public methods ends
336454

337455

338-
// REGION PRIVATE METHODS STARTS
456+
//Region private methods starts
339457

340458
func createStartRoleOperation() StartRoleOperation {
341459
startRoleOperation := StartRoleOperation{}
@@ -377,7 +495,8 @@ func createDockerPrivateConfig(dockerCertDir string) (string, error) {
377495
if _, err := os.Stat(certDir); err == nil {
378496
fmt.Println(dockerDirExistsMessage)
379497
} else {
380-
return "", errors.New(missingDockerCertsError)
498+
errorMessage := fmt.Sprintf(missingDockerCertsError, certDir)
499+
return "", errors.New(errorMessage)
381500
}
382501

383502
caCert, err := parseFileToBase64String(path.Join(certDir, "ca.pem"))
@@ -666,6 +785,4 @@ func createEndpoint(name string, protocol string, extertalPort int, internalPort
666785
return endpoint
667786
}
668787

669-
// REGION PRIVATE METHODS ENDS
670-
671-
788+
//Region private methods ends

0 commit comments

Comments
 (0)