Skip to content

Commit

Permalink
Added new VM operations
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslangabitov committed Oct 1, 2014
1 parent 753dde6 commit 3f2caf9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
5 changes: 5 additions & 0 deletions clients/vmClient/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ type StartRoleOperation struct {
OperationType string
}

type ShutdownRoleOperation struct {
Xmlns string `xml:"xmlns,attr"`
OperationType string
}

type RestartRoleOperation struct {
Xmlns string `xml:"xmlns,attr"`
OperationType string
Expand Down
73 changes: 68 additions & 5 deletions clients/vmClient/vmClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
const (
azureXmlns = "http://schemas.microsoft.com/windowsazure"
azureDeploymentListURL = "services/hostedservices/%s/deployments"
azureHostedServicesURL = "services/hostedservices"
azureHostedServiceListURL = "services/hostedservices"
azureHostedServiceURL = "services/hostedservices/%s"
azureDeploymentURL = "services/hostedservices/%s/deployments/%s"
azureRoleURL = "services/hostedservices/%s/deployments/%s/roles/%s"
azureOperationsURL = "services/hostedservices/%s/deployments/%s/roleinstances/%s/Operations"
Expand Down Expand Up @@ -101,7 +102,7 @@ func CreateHostedService(dnsName, location string) (string, error) {
return "", err
}

requestURL := azureHostedServicesURL
requestURL := azureHostedServiceListURL
requestId, azureErr := azure.SendAzurePostRequest(requestURL, hostedServiceBytes)
if azureErr != nil {
return "", err
Expand All @@ -110,6 +111,18 @@ func CreateHostedService(dnsName, location string) (string, error) {
return requestId, nil
}

func DeleteHostedService(dnsName string) error {

requestURL := fmt.Sprintf(azureHostedServiceURL, dnsName)
requestId, err := azure.SendAzureDeleteRequest(requestURL)
if err != nil {
return err
}

azure.WaitAsyncOperation(requestId)
return nil
}

func CreateAzureVMConfiguration(name, instanceSize, imageName, location string) (*Role, error) {
fmt.Println("Creating azure VM configuration... ")

Expand Down Expand Up @@ -211,7 +224,7 @@ func SetAzureDockerVMExtension(azureVMConfiguration *Role, dockerCertDir string,
func GetVMDeployment(cloudserviceName, deploymentName string) (*VMDeployment, error) {
deployment := new(VMDeployment)

requestURL := fmt.Sprintf(azureDeploymentURL, cloudserviceName, deploymentName)
requestURL := fmt.Sprintf(azureDeploymentURL, cloudserviceName, deploymentName)
response, azureErr := azure.SendAzureGetRequest(requestURL)
if azureErr != nil {
if strings.Contains(azureErr.Error(), "Code: ResourceNotFound") {
Expand All @@ -229,6 +242,18 @@ func GetVMDeployment(cloudserviceName, deploymentName string) (*VMDeployment, er
return deployment, nil
}

func DeleteVMDeployment(cloudserviceName, deploymentName string) error {

requestURL := fmt.Sprintf(azureDeploymentURL, cloudserviceName, deploymentName)
requestId, err := azure.SendAzureDeleteRequest(requestURL)
if err != nil {
return err
}

azure.WaitAsyncOperation(requestId)
return nil
}

func GetRole(cloudserviceName, deploymentName, roleName string) (*Role, error) {
role := new(Role)

Expand Down Expand Up @@ -258,7 +283,7 @@ func StartRole(cloudserviceName, deploymentName, roleName string) (error) {
return err
}

requestURL := fmt.Sprintf(azureOperationsURL, cloudserviceName, deploymentName, roleName)
requestURL := fmt.Sprintf(azureOperationsURL, cloudserviceName, deploymentName, roleName)
requestId, azureErr := azure.SendAzurePostRequest(requestURL, startRoleOperationBytes)
if azureErr != nil {
return azureErr
Expand All @@ -268,6 +293,24 @@ func StartRole(cloudserviceName, deploymentName, roleName string) (error) {
return nil
}

func ShutdownRole(cloudserviceName, deploymentName, roleName string) (error) {
shutdownRoleOperation := createShutdowRoleOperation()

shutdownRoleOperationBytes, err := xml.Marshal(shutdownRoleOperation)
if err != nil {
return err
}

requestURL := fmt.Sprintf(azureOperationsURL, cloudserviceName, deploymentName, roleName)
requestId, azureErr := azure.SendAzurePostRequest(requestURL, shutdownRoleOperationBytes)
if azureErr != nil {
return azureErr
}

azure.WaitAsyncOperation(requestId)
return nil
}

func RestartRole(cloudserviceName, deploymentName, roleName string) (error) {
restartRoleOperation := createRestartRoleOperation()

Expand All @@ -286,6 +329,17 @@ func RestartRole(cloudserviceName, deploymentName, roleName string) (error) {
return nil
}

func DeleteRole(cloudserviceName, deploymentName, roleName string) (error) {
requestURL := fmt.Sprintf(azureRoleURL, cloudserviceName, deploymentName, roleName)
requestId, azureErr := azure.SendAzureDeleteRequest(requestURL)
if azureErr != nil {
return azureErr
}

azure.WaitAsyncOperation(requestId)
return nil
}

// REGION PUBLIC METHODS ENDS


Expand All @@ -299,6 +353,14 @@ func createStartRoleOperation() StartRoleOperation {
return startRoleOperation
}

func createShutdowRoleOperation() ShutdownRoleOperation {
shutdownRoleOperation := ShutdownRoleOperation{}
shutdownRoleOperation.OperationType = "ShutdownRoleOperation"
shutdownRoleOperation.Xmlns = azureXmlns

return shutdownRoleOperation
}

func createRestartRoleOperation() RestartRoleOperation {
startRoleOperation := RestartRoleOperation{}
startRoleOperation.OperationType = "RestartRoleOperation"
Expand Down Expand Up @@ -561,14 +623,15 @@ func getServiceCertFingerprint(certPath string) (string, error) {
if readErr != nil {
return "", readErr
}

block, rest := pem.Decode(certData)
if block == nil {
return "", errors.New(string(rest))
}

sha1sum := sha1.Sum(block.Bytes)
fingerprint := fmt.Sprintf("%X", sha1sum)
fmt.Println(fingerprint)
return fingerprint, nil
}

Expand Down
10 changes: 10 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ func SendAzurePostRequest(url string, data []byte) (string, error){
return requestId[0], nil
}

func SendAzureDeleteRequest(url string) ([]byte, error){
response, err := SendAzureRequest(url, "DELETE", nil)
if err != nil {
return "", err
}

requestId := response.Header[requestIdHeader]
return requestId[0], nil
}

func SendAzureRequest(url string, requestType string, data []byte) (*http.Response, error){
client := createHttpClient()

Expand Down

0 comments on commit 3f2caf9

Please sign in to comment.