This commit is contained in:
Ruslan Gabitov 2014-10-01 16:46:05 -07:00
Родитель 753dde68bf
Коммит 3f2caf97b2
3 изменённых файлов: 83 добавлений и 5 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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
}

Просмотреть файл

@ -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()