Fixes for HostedServiceClient (closes #121)

1. CreateHostedService func now takes a struct input
   to stabilize the function signature.
2. CreateHostedService is actually not a long running operation,
   therefore just returning `error` value from that func now.
3. Changed CheckHostedServiceNameAvailabilityMethod to return
   the correct response struct to the user.
4. Updated example codes and tests to accomodate changes.
   (Please run the integration tests after merging this.)
This commit is contained in:
Ahmet Alp Balkan 2015-05-15 01:05:18 -07:00
Родитель b2dbe90be9
Коммит e32e816da5
5 изменённых файлов: 63 добавлений и 92 удалений

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

@ -20,56 +20,55 @@ Download publish settings file from https://manage.windowsazure.com/publishsetti
package main
import (
"fmt"
"encoding/base64"
"fmt"
"github.com/Azure/azure-sdk-for-go/management"
"github.com/Azure/azure-sdk-for-go/management/hostedservice"
"github.com/Azure/azure-sdk-for-go/management/virtualmachine"
"github.com/Azure/azure-sdk-for-go/management/vmutils"
"github.com/Azure/azure-sdk-for-go/management"
"github.com/Azure/azure-sdk-for-go/management/hostedservice"
"github.com/Azure/azure-sdk-for-go/management/virtualmachine"
"github.com/Azure/azure-sdk-for-go/management/vmutils"
)
func main() {
dnsName := "test-vm-from-go"
storageAccount := "mystorageaccount"
location := "West US"
vmSize := "Small"
vmImage := "b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-14_04-LTS-amd64-server-20140724-en-us-30GB"
userName := "testuser"
userPassword := "Test123"
dnsName := "test-vm-from-go"
storageAccount := "mystorageaccount"
location := "West US"
vmSize := "Small"
vmImage := "b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-14_04-LTS-amd64-server-20140724-en-us-30GB"
userName := "testuser"
userPassword := "Test123"
client, err := management.ClientFromPublishSettingsFile("path/to/downloaded.publishsettings", "")
if err != nil {
panic(err)
}
client, err := management.ClientFromPublishSettingsFile("path/to/downloaded.publishsettings", "")
if err != nil {
panic(err)
}
// create hosted service
requestId, err := hostedservice.NewClient(client).CreateHostedService(dnsName, location, "", dnsName, "")
if err != nil {
panic(err)
}
err = client.WaitAsyncOperation(requestId)
if err != nil {
panic(err)
}
// create hosted service
if err := hostedservice.NewClient(client).CreateHostedService(hostedservice.CreateHostedServiceParameters{
ServiceName: dnsName,
Location: location,
Label: base64.StdEncoding.EncodeToString([]byte(dnsName))}); err != nil {
panic(err)
}
// create virtual machine
role, err := vmutils.NewVmConfiguration(dnsName, vmSize)
if err != nil {
panic(err)
}
vmutils.ConfigureDeploymentFromPlatformImage(&role,
vmImage, fmt.Sprintf("http://%s.blob.core.windows.net/sdktest/%s.vhd", storageAccount, dnsName), "")
vmutils.ConfigureForLinux(&role, dnsName, userName, userPassword)
vmutils.ConfigureWithPublicSSH(&role)
// create virtual machine
role := vmutils.NewVMConfiguration(dnsName, vmSize)
vmutils.ConfigureDeploymentFromPlatformImage(
&role,
vmImage,
fmt.Sprintf("http://%s.blob.core.windows.net/sdktest/%s.vhd", storageAccount, dnsName),
"")
vmutils.ConfigureForLinux(&role, dnsName, userName, userPassword)
vmutils.ConfigureWithPublicSSH(&role)
requestId, err = virtualmachine.NewClient(client).CreateDeployment(role, dnsName)
if err != nil {
panic(err)
}
err = client.WaitAsyncOperation(requestId)
if err != nil {
panic(err)
}
operationID, err := virtualmachine.NewClient(client).
CreateDeployment(role, dnsName, virtualmachine.CreateDeploymentOptions{})
if err != nil {
panic(err)
}
if err := client.WaitAsyncOperation(operationID); err != nil {
panic(err)
}
}
```

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

@ -26,42 +26,30 @@ func NewClient(client management.Client) HostedServiceClient {
return HostedServiceClient{client: client}
}
func (h HostedServiceClient) CreateHostedService(dnsName, location string, reverseDNSFqdn string, label string, description string) (management.OperationID, error) {
if dnsName == "" {
return "", fmt.Errorf(errParamNotSpecified, "dnsName")
}
if location == "" {
return "", fmt.Errorf(errParamNotSpecified, "location")
}
hostedServiceDeployment := h.createHostedServiceDeploymentConfig(dnsName, location, reverseDNSFqdn, label, description)
hostedServiceBytes, err := xml.Marshal(hostedServiceDeployment)
func (h HostedServiceClient) CreateHostedService(params CreateHostedServiceParameters) error {
req, err := xml.Marshal(params)
if err != nil {
return "", err
return err
}
requestURL := azureHostedServiceListURL
return h.client.SendAzurePostRequest(requestURL, hostedServiceBytes)
_, err = h.client.SendAzurePostRequest(azureHostedServiceListURL, req) // not a long running operation
return err
}
func (h HostedServiceClient) CheckHostedServiceNameAvailability(dnsName string) (bool, string, error) {
func (h HostedServiceClient) CheckHostedServiceNameAvailability(dnsName string) (AvailabilityResponse, error) {
var r AvailabilityResponse
if dnsName == "" {
return false, "", fmt.Errorf(errParamNotSpecified, "dnsName")
return r, fmt.Errorf(errParamNotSpecified, "dnsName")
}
requestURL := fmt.Sprintf(azureHostedServiceAvailabilityURL, dnsName)
response, err := h.client.SendAzureGetRequest(requestURL)
if err != nil {
return false, "", err
return r, err
}
availabilityResponse := new(AvailabilityResponse)
err = xml.Unmarshal(response, availabilityResponse)
if err != nil {
return false, "", err
}
return availabilityResponse.Result, availabilityResponse.Reason, nil
err = xml.Unmarshal(response, &r)
return r, err
}
func (h HostedServiceClient) DeleteHostedService(dnsName string, deleteDisksAndBlobs bool) (management.OperationID, error) {
@ -114,19 +102,6 @@ func (h HostedServiceClient) ListHostedServices() (ListHostedServiceResponse, er
return response, err
}
func (h HostedServiceClient) createHostedServiceDeploymentConfig(dnsName, location string, reverseDNSFqdn string, label string, description string) CreateHostedService {
encodedLabel := base64.StdEncoding.EncodeToString([]byte(label))
deployment := CreateHostedService{
ServiceName: dnsName,
Label: encodedLabel,
Description: description,
Location: location,
ReverseDNSFqdn: reverseDNSFqdn,
Xmlns: azureXmlns,
}
return deployment
}
func (h HostedServiceClient) AddCertificate(dnsName string, certData []byte, certificateFormat CertificateFormat, password string) (management.OperationID, error) {
if dnsName == "" {
return "", fmt.Errorf(errParamNotSpecified, "dnsName")

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

@ -11,7 +11,7 @@ type HostedServiceClient struct {
client management.Client
}
type CreateHostedService struct {
type CreateHostedServiceParameters struct {
XMLName xml.Name
Xmlns string `xml:"xmlns,attr"`
ServiceName string

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

@ -1,6 +1,7 @@
package vmutils
import (
"encoding/base64"
"fmt"
"github.com/Azure/azure-sdk-for-go/management"
@ -23,13 +24,10 @@ func Example() {
}
// create hosted service
operationID, err := hostedservice.NewClient(client).
CreateHostedService(dnsName, location, "", dnsName, "")
if err != nil {
panic(err)
}
err = client.WaitAsyncOperation(operationID)
if err != nil {
if err := hostedservice.NewClient(client).CreateHostedService(hostedservice.CreateHostedServiceParameters{
ServiceName: dnsName,
Location: location,
Label: base64.StdEncoding.EncodeToString([]byte(dnsName))}); err != nil {
panic(err)
}
@ -43,13 +41,12 @@ func Example() {
ConfigureForLinux(&role, dnsName, userName, userPassword)
ConfigureWithPublicSSH(&role)
operationID, err = virtualmachine.NewClient(client).
operationID, err := virtualmachine.NewClient(client).
CreateDeployment(role, dnsName, virtualmachine.CreateDeploymentOptions{})
if err != nil {
panic(err)
}
err = client.WaitAsyncOperation(operationID)
if err != nil {
if err := client.WaitAsyncOperation(operationID); err != nil {
panic(err)
}
}

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

@ -180,9 +180,9 @@ func createRoleConfiguration(t *testing.T, client management.Client, role vm.Rol
hsc := hostedservice.NewClient(client)
vmname := role.RoleName
if err := Await(client, func() (management.OperationID, error) {
return hsc.CreateHostedService(vmname, location, "", vmname, "")
}); err != nil {
if err := hsc.CreateHostedService(hostedservice.CreateHostedServiceParameters{
ServiceName: vmname, Location: location,
Label: base64.StdEncoding.EncodeToString([]byte(vmname))}); err != nil {
t.Error(err)
}