github.com/coreos/mantle@v0.13.0/platform/api/azure/storage.go (about)

     1  // Copyright 2016 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package azure
    16  
    17  import (
    18  	"encoding/xml"
    19  	"fmt"
    20  	"net/url"
    21  	"path"
    22  	"strings"
    23  
    24  	"github.com/Azure/azure-sdk-for-go/arm/storage"
    25  	"github.com/Azure/azure-sdk-for-go/management"
    26  	"github.com/Azure/azure-sdk-for-go/management/storageservice"
    27  )
    28  
    29  var (
    30  	azureImageURL = "services/images"
    31  )
    32  
    33  func (a *API) GetStorageServiceKeys(account string) (storageservice.GetStorageServiceKeysResponse, error) {
    34  	return storageservice.NewClient(a.client).GetStorageServiceKeys(account)
    35  }
    36  
    37  func (a *API) GetStorageServiceKeysARM(account, resourceGroup string) (storage.AccountListKeysResult, error) {
    38  	return a.accClient.ListKeys(resourceGroup, account)
    39  }
    40  
    41  // https://msdn.microsoft.com/en-us/library/azure/jj157192.aspx
    42  func (a *API) AddOSImage(md *OSImage) error {
    43  	data, err := xml.Marshal(md)
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	op, err := a.client.SendAzurePostRequest(azureImageURL, data)
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	return a.client.WaitForOperation(op, nil)
    54  }
    55  
    56  func (a *API) OSImageExists(name string) (bool, error) {
    57  	url := fmt.Sprintf("%s/%s", azureImageURL, name)
    58  	response, err := a.client.SendAzureGetRequest(url)
    59  	if err != nil {
    60  		if management.IsResourceNotFoundError(err) {
    61  			return false, nil
    62  		}
    63  
    64  		return false, err
    65  	}
    66  
    67  	var image OSImage
    68  
    69  	if err := xml.Unmarshal(response, &image); err != nil {
    70  		return false, err
    71  	}
    72  
    73  	if image.Name == name {
    74  		return true, nil
    75  	}
    76  
    77  	return false, nil
    78  }
    79  
    80  func (a *API) UrlOfBlob(account, container, blob string) *url.URL {
    81  	return &url.URL{
    82  		Scheme: "https",
    83  		Host:   fmt.Sprintf("%s.blob.%s", account, a.opts.StorageEndpointSuffix),
    84  		Path:   path.Join(container, blob),
    85  	}
    86  }
    87  
    88  func (a *API) CreateStorageAccount(resourceGroup string) (string, error) {
    89  	// Only lower-case letters & numbers allowed in storage account names
    90  	name := strings.Replace(randomName("kolasa"), "-", "", -1)
    91  	parameters := storage.AccountCreateParameters{
    92  		Sku: &storage.Sku{
    93  			Name: "Standard_LRS",
    94  		},
    95  		Kind:     "Storage",
    96  		Location: &a.opts.Location,
    97  	}
    98  	_, err := a.accClient.Create(resourceGroup, name, parameters, nil)
    99  	if err != nil {
   100  		return "", fmt.Errorf("creating storage account: %v", err)
   101  	}
   102  	return name, nil
   103  }