diff --git a/clients/vmClient/entities.go b/clients/vmClient/entities.go index 8cb408de99cd..5b775b7887a7 100644 --- a/clients/vmClient/entities.go +++ b/clients/vmClient/entities.go @@ -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 diff --git a/clients/vmClient/vmClient.go b/clients/vmClient/vmClient.go index 8d99db702ff6..4e45e4f2ffa3 100644 --- a/clients/vmClient/vmClient.go +++ b/clients/vmClient/vmClient.go @@ -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" @@ -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 @@ -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... ") @@ -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") { @@ -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) @@ -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 @@ -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() @@ -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 @@ -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" @@ -561,7 +623,7 @@ func getServiceCertFingerprint(certPath string) (string, error) { if readErr != nil { return "", readErr } - + block, rest := pem.Decode(certData) if block == nil { return "", errors.New(string(rest)) @@ -569,6 +631,7 @@ func getServiceCertFingerprint(certPath string) (string, error) { sha1sum := sha1.Sum(block.Bytes) fingerprint := fmt.Sprintf("%X", sha1sum) + fmt.Println(fingerprint) return fingerprint, nil } diff --git a/common.go b/common.go index 4fe8ef86685e..f2543b8448fa 100644 --- a/common.go +++ b/common.go @@ -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()