~juju-qa/ubuntu/xenial/juju/2.0-rc2

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/provider/azure/storage.go

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
import (
7
7
        "fmt"
 
8
        "net/http"
8
9
        "strings"
9
10
 
10
11
        "github.com/Azure/azure-sdk-for-go/arm/compute"
14
15
        "github.com/Azure/go-autorest/autorest/to"
15
16
        "github.com/juju/errors"
16
17
        "github.com/juju/schema"
17
 
        "github.com/juju/utils"
18
18
        "gopkg.in/juju/names.v2"
19
19
 
20
 
        "github.com/juju/juju/environs"
21
20
        "github.com/juju/juju/instance"
 
21
        "github.com/juju/juju/provider/azure/internal/armtemplates"
22
22
        internalazurestorage "github.com/juju/juju/provider/azure/internal/azurestorage"
23
23
        "github.com/juju/juju/storage"
24
24
)
582
582
// virtualMachines returns a mapping of instance IDs to VirtualMachines and
583
583
// errors, for each of the specified instance IDs.
584
584
func (v *azureVolumeSource) virtualMachines(instanceIds []instance.Id) (map[instance.Id]*maybeVirtualMachine, error) {
585
 
        // Fetch all instances at once. Failure to find an instance should
586
 
        // not cause the entire method to fail.
 
585
        vmsClient := compute.VirtualMachinesClient{v.env.compute}
 
586
        var result compute.VirtualMachineListResult
 
587
        if err := v.env.callAPI(func() (autorest.Response, error) {
 
588
                var err error
 
589
                result, err = vmsClient.List(v.env.resourceGroup)
 
590
                return result.Response, err
 
591
        }); err != nil {
 
592
                return nil, errors.Annotate(err, "listing virtual machines")
 
593
        }
 
594
 
 
595
        all := make(map[instance.Id]*compute.VirtualMachine)
 
596
        if result.Value != nil {
 
597
                for _, vm := range *result.Value {
 
598
                        vmCopy := vm
 
599
                        all[instance.Id(to.String(vm.Name))] = &vmCopy
 
600
                }
 
601
        }
587
602
        results := make(map[instance.Id]*maybeVirtualMachine)
588
 
        instances, err := v.env.instances(
589
 
                v.env.resourceGroup,
590
 
                instanceIds,
591
 
                false, /* don't refresh addresses */
592
 
        )
593
 
        switch err {
594
 
        case nil, environs.ErrPartialInstances:
595
 
                for i, inst := range instances {
596
 
                        vm := &maybeVirtualMachine{}
597
 
                        if inst != nil {
598
 
                                vm.vm = &inst.(*azureInstance).VirtualMachine
599
 
                        } else {
600
 
                                vm.err = errors.NotFoundf("instance %v", instanceIds[i])
601
 
                        }
602
 
                        results[instanceIds[i]] = vm
603
 
                }
604
 
        case environs.ErrNoInstances:
605
 
                for _, instanceId := range instanceIds {
606
 
                        results[instanceId] = &maybeVirtualMachine{
607
 
                                err: errors.NotFoundf("instance %v", instanceId),
608
 
                        }
609
 
                }
610
 
        default:
611
 
                return nil, errors.Annotate(err, "getting instances")
 
603
        for _, id := range instanceIds {
 
604
                result := &maybeVirtualMachine{vm: all[id]}
 
605
                if result.vm == nil {
 
606
                        result.err = errors.NotFoundf("instance %v", id)
 
607
                }
 
608
                results[id] = result
612
609
        }
613
610
        return results, nil
614
611
}
739
736
        )
740
737
}
741
738
 
 
739
// getStorageAccountKey returns the key for the storage account.
742
740
func getStorageAccountKey(
743
741
        callAPI callAPIFunc,
744
742
        client armstorage.AccountsClient,
751
749
                listKeysResult, err = client.ListKeys(resourceGroup, accountName)
752
750
                return listKeysResult.Response, err
753
751
        }); err != nil {
 
752
                if listKeysResult.Response.Response != nil && listKeysResult.StatusCode == http.StatusNotFound {
 
753
                        return nil, errors.NewNotFound(err, "storage account keys not found")
 
754
                }
754
755
                return nil, errors.Annotate(err, "listing storage account keys")
755
756
        }
756
757
        if listKeysResult.Keys == nil {
778
779
        return fullKey, nil
779
780
}
780
781
 
781
 
// RandomStorageAccountName returns a random storage account name.
782
 
func RandomStorageAccountName() string {
783
 
        const maxStorageAccountNameLen = 24
784
 
        validRunes := append(utils.LowerAlpha, utils.Digits...)
785
 
        return utils.RandomString(maxStorageAccountNameLen, validRunes)
 
782
// storageAccountTemplateResource returns a template resource definition
 
783
// for creating a storage account.
 
784
func storageAccountTemplateResource(
 
785
        location string,
 
786
        envTags map[string]string,
 
787
        accountName, accountType string,
 
788
) armtemplates.Resource {
 
789
        return armtemplates.Resource{
 
790
                APIVersion: armstorage.APIVersion,
 
791
                Type:       "Microsoft.Storage/storageAccounts",
 
792
                Name:       accountName,
 
793
                Location:   location,
 
794
                Tags:       envTags,
 
795
                StorageSku: &armstorage.Sku{
 
796
                        Name: armstorage.SkuName(accountType),
 
797
                },
 
798
        }
786
799
}