Skip to content

Commit

Permalink
Added parameters validation
Browse files Browse the repository at this point in the history
Added parameters validation
  • Loading branch information
ruslangabitov committed Oct 4, 2014
1 parent a870bf1 commit 2edf9b0
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 54 deletions.
4 changes: 4 additions & 0 deletions clients/imageClient/imageClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func GetImageList() (ImageList, error){
}

func ResolveImageName(imageName string) (error) {
if len(imageName) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "imageName")
}

imageList, err := GetImageList()
if err != nil {
return err
Expand Down
14 changes: 9 additions & 5 deletions clients/locationClient/locationClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,27 @@ const (
invalidLocationError = "Invalid location. Available locations: %s"
)

func ResolveLocation(specifiedLocation string) (error) {
func ResolveLocation(location string) (error) {
if len(location) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "location")
}

locations, err := GetLocationList()
if err != nil {
return err
}

for _, location := range locations.Locations {
if location.Name != specifiedLocation {
for _, existingLocation := range locations.Locations {
if existingLocation.Name != location {
continue
}

return nil
}

var availableLocations bytes.Buffer
for _, location := range locations.Locations {
availableLocations.WriteString(location.Name + ", ")
for _, existingLocation := range locations.Locations {
availableLocations.WriteString(existingLocation.Name + ", ")
}

return errors.New(fmt.Sprintf(invalidLocationError, strings.Trim(availableLocations.String(), ", ")))
Expand Down
15 changes: 15 additions & 0 deletions clients/storageServiceClient/storageServiceClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func GetStorageServiceList() (*StorageServiceList, error){
}

func GetStorageServiceByName(serviceName string) (*StorageService, error){
if len(serviceName) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "serviceName")
}

storageService := new(StorageService)
requestURL := fmt.Sprintf(azureStorageServiceURL, serviceName)
response, err := azure.SendAzureGetRequest(requestURL)
Expand All @@ -50,6 +54,10 @@ func GetStorageServiceByName(serviceName string) (*StorageService, error){
}

func GetStorageServiceByLocation(location string) (*StorageService, error) {
if len(location) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "location")
}

storageService := new(StorageService)
storageServiceList, err := GetStorageServiceList()
if err != nil {
Expand All @@ -68,6 +76,13 @@ func GetStorageServiceByLocation(location string) (*StorageService, error) {
}

func CreateStorageService(name, location string) (*StorageService, error){
if len(name) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "name")
}
if len(location) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "location")
}

storageDeploymentConfig := createStorageServiceDeploymentConf(name, location)
deploymentBytes, err := xml.Marshal(storageDeploymentConfig)
if err != nil {
Expand Down
155 changes: 136 additions & 19 deletions clients/vmClient/vmClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,22 @@ const (
dockerPrivateConfig = "{ \"ca\": \"%s\", \"server-cert\": \"%s\", \"server-key\": \"%s\" }"
dockerDirExistsMessage = "Docker directory exists"

missingDockerCertsError = "You should generate docker certificates first. Info can be found here: https://docs.docker.com/articles/https/"
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/"
provisioningConfDoesNotExistsError = "You should set azure VM provisioning config first"
invalidCertExtensionError = "Certificate %s is invalid. Please specify %s certificate."
invalidOSError = "You must specify correct OS param. Valid values are 'Linux' and 'Windows'"
)

// REGION PUBLIC METHODS STARTS
//Region public methods starts

func CreateAzureVM(role *Role, dnsName, location string) error {

if len(dnsName) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "dnsName")
}
if len(location) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "location")
}

err := locationClient.ResolveLocation(location)
if err != nil {
return err
Expand Down Expand Up @@ -90,7 +96,13 @@ func CreateAzureVM(role *Role, dnsName, location string) error {
}

func CreateHostedService(dnsName, location string) (string, error) {

if len(dnsName) == 0 {
return "", fmt.Errorf(azure.ParamNotSpecifiedError, "dnsName")
}
if len(location) == 0 {
return "", fmt.Errorf(azure.ParamNotSpecifiedError, "location")
}

err := locationClient.ResolveLocation(location)
if err != nil {
return "", err
Expand All @@ -112,7 +124,10 @@ func CreateHostedService(dnsName, location string) (string, error) {
}

func DeleteHostedService(dnsName string) error {

if len(dnsName) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "dnsName")
}

requestURL := fmt.Sprintf(azureHostedServiceURL, dnsName)
requestId, err := azure.SendAzureDeleteRequest(requestURL)
if err != nil {
Expand All @@ -124,6 +139,19 @@ func DeleteHostedService(dnsName string) error {
}

func CreateAzureVMConfiguration(name, instanceSize, imageName, location string) (*Role, error) {
if len(name) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "name")
}
if len(instanceSize) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "instanceSize")
}
if len(imageName) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "imageName")
}
if len(location) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "location")
}

fmt.Println("Creating azure VM configuration... ")

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

func AddAzureLinuxProvisioningConfig(azureVMConfig *Role, userName, password, certPath string) (*Role, error) {
if len(userName) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "userName")
}
if len(password) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "password")
}
if len(certPath) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "certPath")
}

fmt.Println("Adding azure provisioning configuration... ")

configurationSets := ConfigurationSets{}
Expand Down Expand Up @@ -168,7 +206,20 @@ func AddAzureLinuxProvisioningConfig(azureVMConfig *Role, userName, password, ce
return azureVMConfig, nil
}

func SetAzureVMExtension(azureVMConfiguration *Role, name string, publisher string, version string, referenceName string, state string, publicConfigurationValue string, privateConfigurationValue string) (*Role) {
func SetAzureVMExtension(azureVMConfiguration *Role, name string, publisher string, version string, referenceName string, state string, publicConfigurationValue string, privateConfigurationValue string) (*Role, error) {
if len(name) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "name")
}
if len(publisher) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "publisher")
}
if len(version) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "version")
}
if len(referenceName) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "referenceName")
}

fmt.Printf("Setting azure VM extension: %s... \n", name)

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

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

return azureVMConfiguration
return azureVMConfiguration, nil
}

func SetAzureDockerVMExtension(azureVMConfiguration *Role, dockerCertDir string, dockerPort int, version string) (*Role, error) {
if len(dockerCertDir) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "dockerCertDir")
}

if len(version) == 0 {
version = "0.3"
}
Expand All @@ -217,11 +272,18 @@ func SetAzureDockerVMExtension(azureVMConfiguration *Role, dockerCertDir string,
return nil, err
}

azureVMConfiguration = SetAzureVMExtension(azureVMConfiguration, "DockerExtension", "MSOpenTech.Extensions", version, "DockerExtension", "enable", publicConfiguration, privateConfiguration)
azureVMConfiguration, err = SetAzureVMExtension(azureVMConfiguration, "DockerExtension", "MSOpenTech.Extensions", version, "DockerExtension", "enable", publicConfiguration, privateConfiguration)
return azureVMConfiguration, nil
}

func GetVMDeployment(cloudserviceName, deploymentName string) (*VMDeployment, error) {
if len(cloudserviceName) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "cloudserviceName")
}
if len(deploymentName) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "deploymentName")
}

deployment := new(VMDeployment)

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

func DeleteVMDeployment(cloudserviceName, deploymentName string) error {

if len(cloudserviceName) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "cloudserviceName")
}
if len(deploymentName) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "deploymentName")
}

requestURL := fmt.Sprintf(azureDeploymentURL, cloudserviceName, deploymentName)
requestId, err := azure.SendAzureDeleteRequest(requestURL)
if err != nil {
Expand All @@ -251,6 +319,16 @@ func DeleteVMDeployment(cloudserviceName, deploymentName string) error {
}

func GetRole(cloudserviceName, deploymentName, roleName string) (*Role, error) {
if len(cloudserviceName) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "cloudserviceName")
}
if len(deploymentName) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "deploymentName")
}
if len(roleName) == 0 {
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "roleName")
}

role := new(Role)

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

func StartRole(cloudserviceName, deploymentName, roleName string) (error) {
func StartRole(cloudserviceName, deploymentName, roleName string) error {
if len(cloudserviceName) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "cloudserviceName")
}
if len(deploymentName) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "deploymentName")
}
if len(roleName) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "roleName")
}

startRoleOperation := createStartRoleOperation()

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

func ShutdownRole(cloudserviceName, deploymentName, roleName string) (error) {
func ShutdownRole(cloudserviceName, deploymentName, roleName string) error {
if len(cloudserviceName) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "cloudserviceName")
}
if len(deploymentName) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "deploymentName")
}
if len(roleName) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "roleName")
}

shutdownRoleOperation := createShutdowRoleOperation()

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

func RestartRole(cloudserviceName, deploymentName, roleName string) (error) {
func RestartRole(cloudserviceName, deploymentName, roleName string) error {
if len(cloudserviceName) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "cloudserviceName")
}
if len(deploymentName) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "deploymentName")
}
if len(roleName) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "roleName")
}

restartRoleOperation := createRestartRoleOperation()

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

func DeleteRole(cloudserviceName, deploymentName, roleName string) (error) {
func DeleteRole(cloudserviceName, deploymentName, roleName string) error {
if len(cloudserviceName) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "cloudserviceName")
}
if len(deploymentName) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "deploymentName")
}
if len(roleName) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "roleName")
}

requestURL := fmt.Sprintf(azureRoleURL, cloudserviceName, deploymentName, roleName)
requestId, azureErr := azure.SendAzureDeleteRequest(requestURL)
if azureErr != nil {
Expand All @@ -332,10 +450,10 @@ func DeleteRole(cloudserviceName, deploymentName, roleName string) (error) {
return nil
}

// REGION PUBLIC METHODS ENDS
//Region public methods ends


// REGION PRIVATE METHODS STARTS
//Region private methods starts

func createStartRoleOperation() StartRoleOperation {
startRoleOperation := StartRoleOperation{}
Expand Down Expand Up @@ -377,7 +495,8 @@ func createDockerPrivateConfig(dockerCertDir string) (string, error) {
if _, err := os.Stat(certDir); err == nil {
fmt.Println(dockerDirExistsMessage)
} else {
return "", errors.New(missingDockerCertsError)
errorMessage := fmt.Sprintf(missingDockerCertsError, certDir)
return "", errors.New(errorMessage)
}

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

// REGION PRIVATE METHODS ENDS


//Region private methods ends
Loading

0 comments on commit 2edf9b0

Please sign in to comment.