management/storage: Remove helpers and cleanup
Fixes #68. Removed some 'special logic' containing helper methods. Did some code cleanup and stopped returning pointers.
This commit is contained in:
Родитель
f70269e81f
Коммит
cc00a407f5
|
@ -3,7 +3,6 @@ package storageservice
|
|||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/Azure/azure-sdk-for-go/management"
|
||||
)
|
||||
|
@ -16,8 +15,7 @@ const (
|
|||
|
||||
azureXmlns = "http://schemas.microsoft.com/windowsazure"
|
||||
|
||||
errBlobEndpointNotFound = "Blob endpoint was not found in storage serice %s"
|
||||
errParamNotSpecified = "Parameter %s is not specified."
|
||||
errParamNotSpecified = "Parameter %s is not specified."
|
||||
)
|
||||
|
||||
// NewClient is used to instantiate a new StorageServiceClient from an Azure
|
||||
|
@ -26,79 +24,47 @@ func NewClient(s management.Client) StorageServiceClient {
|
|||
return StorageServiceClient{client: s}
|
||||
}
|
||||
|
||||
func (s StorageServiceClient) GetStorageServiceList() (*StorageServiceList, error) {
|
||||
storageServiceList := new(StorageServiceList)
|
||||
|
||||
func (s StorageServiceClient) GetStorageServiceList() (StorageServiceList, error) {
|
||||
var l StorageServiceList
|
||||
response, err := s.client.SendAzureGetRequest(azureStorageServiceListURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return l, err
|
||||
}
|
||||
|
||||
err = xml.Unmarshal(response, storageServiceList)
|
||||
if err != nil {
|
||||
return storageServiceList, err
|
||||
}
|
||||
|
||||
return storageServiceList, nil
|
||||
err = xml.Unmarshal(response, &l)
|
||||
return l, err
|
||||
}
|
||||
|
||||
func (s StorageServiceClient) GetStorageServiceByName(serviceName string) (*StorageService, error) {
|
||||
func (s StorageServiceClient) GetStorageService(serviceName string) (StorageServiceResponse, error) {
|
||||
var svc StorageServiceResponse
|
||||
if serviceName == "" {
|
||||
return nil, fmt.Errorf(errParamNotSpecified, "serviceName")
|
||||
return svc, fmt.Errorf(errParamNotSpecified, "serviceName")
|
||||
}
|
||||
|
||||
storageService := new(StorageService)
|
||||
requestURL := fmt.Sprintf(azureStorageServiceURL, serviceName)
|
||||
response, err := s.client.SendAzureGetRequest(requestURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return svc, err
|
||||
}
|
||||
|
||||
err = xml.Unmarshal(response, storageService)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return storageService, nil
|
||||
}
|
||||
|
||||
func (s StorageServiceClient) GetStorageServiceByLocation(location string) (*StorageService, error) {
|
||||
if location == "" {
|
||||
return nil, fmt.Errorf(errParamNotSpecified, "location")
|
||||
}
|
||||
|
||||
storageService := new(StorageService)
|
||||
storageServiceList, err := s.GetStorageServiceList()
|
||||
if err != nil {
|
||||
return storageService, err
|
||||
}
|
||||
|
||||
for _, storageService := range storageServiceList.StorageServices {
|
||||
if storageService.StorageServiceProperties.Location != location {
|
||||
continue
|
||||
}
|
||||
|
||||
return &storageService, nil
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
err = xml.Unmarshal(response, &svc)
|
||||
return svc, err
|
||||
}
|
||||
|
||||
func (s StorageServiceClient) GetStorageServiceKeys(serviceName string) (GetStorageServiceKeysResponse, error) {
|
||||
var r GetStorageServiceKeysResponse
|
||||
if serviceName == "" {
|
||||
return GetStorageServiceKeysResponse{}, fmt.Errorf(errParamNotSpecified, "serviceName")
|
||||
return r, fmt.Errorf(errParamNotSpecified, "serviceName")
|
||||
}
|
||||
|
||||
requestURL := fmt.Sprintf(azureStorageServiceKeysURL, serviceName)
|
||||
data, err := s.client.SendAzureGetRequest(requestURL)
|
||||
if err != nil {
|
||||
return GetStorageServiceKeysResponse{}, err
|
||||
return r, err
|
||||
}
|
||||
|
||||
response := GetStorageServiceKeysResponse{}
|
||||
err = xml.Unmarshal(data, &response)
|
||||
|
||||
return response, err
|
||||
err = xml.Unmarshal(data, &r)
|
||||
return r, err
|
||||
}
|
||||
|
||||
func (s StorageServiceClient) CreateAsync(parameters StorageAccountCreateParameters) (management.OperationID, error) {
|
||||
|
@ -111,32 +77,6 @@ func (s StorageServiceClient) CreateAsync(parameters StorageAccountCreateParamet
|
|||
return s.client.SendAzurePostRequest(azureStorageServiceListURL, data)
|
||||
}
|
||||
|
||||
func (s StorageServiceClient) Create(parameters StorageAccountCreateParameters) (*StorageService, error) {
|
||||
operationID, err := s.CreateAsync(parameters)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = s.client.WaitAsyncOperation(operationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.GetStorageServiceByName(parameters.ServiceName)
|
||||
}
|
||||
|
||||
func (s StorageServiceClient) GetBlobEndpoint(storageService *StorageService) (string, error) {
|
||||
for _, endpoint := range storageService.StorageServiceProperties.Endpoints {
|
||||
if !strings.Contains(endpoint, ".blob.core") {
|
||||
continue
|
||||
}
|
||||
|
||||
return endpoint, nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf(errBlobEndpointNotFound, storageService.ServiceName)
|
||||
}
|
||||
|
||||
// CheckStorageAccountNameAvailability checks to if the specified storage account
|
||||
// name is available.
|
||||
//
|
||||
|
|
|
@ -12,11 +12,10 @@ type StorageServiceClient struct {
|
|||
}
|
||||
|
||||
type StorageServiceList struct {
|
||||
XMLName xml.Name `xml:"StorageServices"`
|
||||
StorageServices []StorageService `xml:"StorageService"`
|
||||
StorageServices []StorageServiceResponse `xml:"StorageService"`
|
||||
}
|
||||
|
||||
type StorageService struct {
|
||||
type StorageServiceResponse struct {
|
||||
URL string `xml:"Url"`
|
||||
ServiceName string
|
||||
StorageServiceProperties StorageServiceProperties
|
||||
|
|
|
@ -204,10 +204,10 @@ func deleteHostedService(t *testing.T, client management.Client, vmname string)
|
|||
|
||||
// === utility funcs ===
|
||||
|
||||
func GetTestStorageAccount(t *testing.T, client management.Client) *storage.StorageService {
|
||||
func GetTestStorageAccount(t *testing.T, client management.Client) storage.StorageServiceResponse {
|
||||
t.Log("Retrieving storage account")
|
||||
sc := storage.NewClient(client)
|
||||
var sa *storage.StorageService
|
||||
var sa storage.StorageServiceResponse
|
||||
ssl, err := sc.GetStorageServiceList()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -226,7 +226,7 @@ func GetTestStorageAccount(t *testing.T, client management.Client) *storage.Stor
|
|||
|
||||
t.Logf("Location for new storage account: %s", loc)
|
||||
name := GenerateName()
|
||||
sa, err = sc.Create(storage.StorageAccountCreateParameters{
|
||||
op, err := sc.CreateAsync(storage.StorageAccountCreateParameters{
|
||||
ServiceName: name,
|
||||
Label: base64.StdEncoding.EncodeToString([]byte(name)),
|
||||
Location: loc,
|
||||
|
@ -234,9 +234,13 @@ func GetTestStorageAccount(t *testing.T, client management.Client) *storage.Stor
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := client.WaitAsyncOperation(op); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sa, err = sc.GetStorageService(name)
|
||||
} else {
|
||||
|
||||
sa = &ssl.StorageServices[rnd.Intn(len(ssl.StorageServices))]
|
||||
sa = ssl.StorageServices[rnd.Intn(len(ssl.StorageServices))]
|
||||
}
|
||||
|
||||
t.Logf("Selected storage account '%s' in location '%s'",
|
||||
|
|
Загрузка…
Ссылка в новой задаче