Esempio n. 1
0
// NewClient returns a Client that is backed by a storage.Client created with
// storage.NewClient
func NewClient(accountName, accountKey, blobServiceBaseURL, apiVersion string, useHTTPS bool) (Client, error) {
	client, err := storage.NewClient(accountName, accountKey, blobServiceBaseURL, apiVersion, useHTTPS)
	if err != nil {
		return nil, errors.Trace(err)
	}
	return clientWrapper{client}, nil
}
func (az *Cloud) getBlobClient(accountName, accountKey string) (*azs.BlobStorageClient, error) {
	client, err := azs.NewClient(accountName, accountKey, az.Environment.StorageEndpointSuffix, azs.DefaultAPIVersion, useHTTPS)
	if err != nil {
		return nil, fmt.Errorf("error creating azure client: %v", err)
	}
	b := client.GetBlobService()
	return &b, nil
}
Esempio n. 3
0
// NewAzureClient function
// NewClient constructs a StorageClient and blobStorageClinet.
// This should be used if the caller wants to specify
// whether to use HTTPS, a specific REST API version or a
// custom storage endpoint than Azure Public Cloud.
// Recommended API version "2014-02-14"
// synax :
// AzurestorageAccountName, AzurestorageAccountKey, "core.chinacloudapi.cn", "2014-02-14", true
func NewAzureClient(accountName, accountKey, blobServiceBaseUrl, apiVersion string, useHttps bool) (*AzureClient, error) {
	cli, err := storage.NewClient(accountName, accountKey, blobServiceBaseUrl, apiVersion, useHttps)
	if err != nil {
		return nil, err
	}
	return &AzureClient{
		client:     &cli,
		blobClient: cli.GetBlobService(),
	}, nil
}
Esempio n. 4
0
// New initializes a new fs.FileProvider with a specific Azure account and key
func New(account, secret string) (fs.FileProvider, error) {
	cli, err := storage.NewClient(account, secret, storage.DefaultBaseURL, storage.DefaultAPIVersion, true)
	if err != nil {
		return nil, err
	}

	return &azureFS{
		id:                   nil,
		client:               cli.GetBlobService(),
		currentRealDirectory: "",
	}, nil
}
Esempio n. 5
0
// NewAzureBlob - Create a new AzureBlob crud type.
func NewAzureBlob(config AzureConfig) (Type, error) {
	client, err := azure.NewClient(
		config.Account,
		config.Secret,
		azure.DefaultBaseURL,
		azure.DefaultAPIVersion,
		true)
	if err != nil {
		return nil, err
	}
	blobStorage := client.GetBlobService()
	// Ensure the container exists
	var accessType azure.ContainerAccessType
	switch config.AccessType {
	case "":
		accessType = azure.ContainerAccessTypePrivate
	case "blob":
		accessType = azure.ContainerAccessTypeBlob
	case "container":
		accessType = azure.ContainerAccessTypeContainer
	default:
		err := fmt.Errorf(
			"azure container access_type: '%s' is invalid",
			config.AccessType)
		return nil, err
	}
	b := backoff.NewExponentialBackOff()
	b.InitialInterval = 500 * time.Second
	b.RandomizationFactor = 0.5
	b.Multiplier = 1.5
	b.MaxInterval = 20
	b.MaxElapsedTime = 1 * time.Minute
	err = backoff.Retry(func() error {
		_, err := blobStorage.CreateContainerIfNotExists(config.Container, accessType)
		return err
	}, b)
	if err != nil {
		return nil, err
	}
	// Return AzureBlob
	return &AzureBlob{
		config:      config,
		blobStorage: blobStorage,
	}, nil
}
Esempio n. 6
0
// New constructs a new Driver with the given Azure Storage Account credentials
func New(accountName, accountKey, container, realm string) (*Driver, error) {
	api, err := azure.NewClient(accountName, accountKey, realm, azure.DefaultAPIVersion, true)
	if err != nil {
		return nil, err
	}

	blobClient := api.GetBlobService()

	// Create registry container
	if _, err = blobClient.CreateContainerIfNotExists(container, azure.ContainerAccessTypePrivate); err != nil {
		return nil, err
	}

	d := &driver{
		client:    blobClient,
		container: container}
	return &Driver{baseEmbed: baseEmbed{Base: base.Base{StorageDriver: d}}}, nil
}
Esempio n. 7
0
func (armClient *ArmClient) getQueueServiceClientForStorageAccount(resourceGroupName, storageAccountName string) (*mainStorage.QueueServiceClient, bool, error) {
	key, accountExists, err := armClient.getKeyForStorageAccount(resourceGroupName, storageAccountName)
	if err != nil {
		return nil, accountExists, err
	}
	if accountExists == false {
		return nil, false, nil
	}

	storageClient, err := mainStorage.NewClient(storageAccountName, key, armClient.environment.StorageEndpointSuffix,
		mainStorage.DefaultAPIVersion, true)
	if err != nil {
		return nil, true, fmt.Errorf("Error creating storage client for storage account %q: %s", storageAccountName, err)
	}

	queueClient := storageClient.GetQueueService()
	return &queueClient, true, nil
}
Esempio n. 8
0
func (a AzureClient) removeOSDiskBlob(resourceGroup, vmName, vhdURL string) error {
	// NOTE(ahmetalpbalkan) Currently Azure APIs do not offer a Delete Virtual
	// Machine functionality which deletes the attached disks along with the VM
	// as well. Therefore we find out the storage account from OS disk URL and
	// fetch storage account keys to delete the container containing the disk.
	log.Debug("Attempting to remove OS disk.", logutil.Fields{"vm": vmName})
	log.Debugf("OS Disk vhd URL: %q", vhdURL)

	vhdContainer := osDiskStorageContainerName(vmName)

	storageAccount, blobServiceBaseURL := extractStorageAccountFromVHDURL(vhdURL)
	if storageAccount == "" {
		log.Warn("Could not extract the storage account name from URL. Please clean up the disk yourself.")
		return nil
	}
	log.Debug("Fetching storage account keys.", logutil.Fields{
		"account":     storageAccount,
		"storageBase": blobServiceBaseURL,
	})
	resp, err := a.storageAccountsClient().ListKeys(resourceGroup, storageAccount)
	if err != nil {
		return err
	}

	if resp.Keys == nil || len(*resp.Keys) < 1 {
		return errors.New("Returned storage keys list response does not contain any keys")
	}
	storageAccountKey := to.String(((*resp.Keys)[0]).Value)
	bs, err := blobstorage.NewClient(storageAccount, storageAccountKey, blobServiceBaseURL, defaultStorageAPIVersion, true)
	if err != nil {
		return fmt.Errorf("Error constructing blob storage client :%v", err)
	}

	f := logutil.Fields{
		"account":   storageAccount,
		"container": vhdContainer}
	log.Debug("Removing container of disk blobs.", f)
	ok, err := bs.GetBlobService().DeleteContainerIfExists(vhdContainer) // HTTP round-trip will not be inspected
	if err != nil {
		log.Debugf("Container remove happened: %v", ok)
	}
	return err
}
func newVolumeDriver(accountName, accountKey, storageBase, mountpoint, metadataRoot string, removeShares bool) (*volumeDriver, error) {
	storageClient, err := azure.NewClient(accountName, accountKey, storageBase, azure.DefaultAPIVersion, true)
	if err != nil {
		return nil, fmt.Errorf("error creating azure client: %v", err)
	}
	metaDriver, err := newMetadataDriver(metadataRoot)
	if err != nil {
		return nil, fmt.Errorf("cannot initialize metadata driver: %v", err)
	}
	return &volumeDriver{
		cl:           storageClient.GetFileService(),
		meta:         metaDriver,
		accountName:  accountName,
		accountKey:   accountKey,
		storageBase:  storageBase,
		mountpoint:   mountpoint,
		removeShares: removeShares,
	}, nil
}
Esempio n. 10
0
func validateStorageAccount(config *Config, client management.Client) (string, error) {
	ssc := storageservice.NewClient(client)

	sa, err := ssc.GetStorageService(config.StorageAccount)
	if err != nil {
		return "", err
	}

	if sa.StorageServiceProperties.Location != config.Location {
		return "", fmt.Errorf("Storage Account %q is not in location %q, but in location %q.",
			config.StorageAccount, sa.StorageServiceProperties.Location, config.Location)
	}

	var blobEndpoint string
	for _, uri := range sa.StorageServiceProperties.Endpoints {
		if strings.Contains(uri, ".blob.") {
			blobEndpoint = uri
		}
	}
	if blobEndpoint == "" {
		return "", fmt.Errorf("Could not find blob endpoint for account %q in %v",
			sa.ServiceName, sa.StorageServiceProperties.Endpoints)
	}
	log.Printf("Blob endpoint: %s", blobEndpoint)

	log.Print("Getting key for storage account...")
	keys, err := ssc.GetStorageServiceKeys(config.StorageAccount)
	if err != nil {
		return "", fmt.Errorf("Could not retrieve key for storage account %q", config.StorageAccount)
	}
	config.storageAccountKey = keys.PrimaryKey

	config.storageClient, err = storage.NewClient(config.StorageAccount, config.storageAccountKey,
		strings.TrimSuffix(blobEndpoint[strings.Index(blobEndpoint, ".blob.")+6:], "/"), storage.DefaultAPIVersion, true)
	if err != nil {
		return "", fmt.Errorf("Could not create storage client for account %q", config.StorageAccount)
	}

	return fmt.Sprintf("%s%s/%s.vhd", blobEndpoint, config.StorageContainer, config.tmpVmName), nil
}
Esempio n. 11
0
func NewAzureClient(subscriptionID, resourceGroupName, storageAccountName string,
	cloud *azure.Environment,
	servicePrincipalToken, servicePrincipalTokenVault *azure.ServicePrincipalToken) (*AzureClient, error) {

	var azureClient = &AzureClient{}

	maxlen := getInspectorMaxLength()

	azureClient.DeploymentsClient = resources.NewDeploymentsClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
	azureClient.DeploymentsClient.Authorizer = servicePrincipalToken
	azureClient.DeploymentsClient.RequestInspector = withInspection(maxlen)
	azureClient.DeploymentsClient.ResponseInspector = byInspecting(maxlen)
	azureClient.DeploymentsClient.UserAgent += packerUserAgent

	azureClient.GroupsClient = resources.NewGroupsClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
	azureClient.GroupsClient.Authorizer = servicePrincipalToken
	azureClient.GroupsClient.RequestInspector = withInspection(maxlen)
	azureClient.GroupsClient.ResponseInspector = byInspecting(maxlen)
	azureClient.GroupsClient.UserAgent += packerUserAgent

	azureClient.InterfacesClient = network.NewInterfacesClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
	azureClient.InterfacesClient.Authorizer = servicePrincipalToken
	azureClient.InterfacesClient.RequestInspector = withInspection(maxlen)
	azureClient.InterfacesClient.ResponseInspector = byInspecting(maxlen)
	azureClient.InterfacesClient.UserAgent += packerUserAgent

	azureClient.SubnetsClient = network.NewSubnetsClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
	azureClient.SubnetsClient.Authorizer = servicePrincipalToken
	azureClient.SubnetsClient.RequestInspector = withInspection(maxlen)
	azureClient.SubnetsClient.ResponseInspector = byInspecting(maxlen)
	azureClient.SubnetsClient.UserAgent += packerUserAgent

	azureClient.VirtualNetworksClient = network.NewVirtualNetworksClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
	azureClient.VirtualNetworksClient.Authorizer = servicePrincipalToken
	azureClient.VirtualNetworksClient.RequestInspector = withInspection(maxlen)
	azureClient.VirtualNetworksClient.ResponseInspector = byInspecting(maxlen)
	azureClient.VirtualNetworksClient.UserAgent += packerUserAgent

	azureClient.PublicIPAddressesClient = network.NewPublicIPAddressesClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
	azureClient.PublicIPAddressesClient.Authorizer = servicePrincipalToken
	azureClient.PublicIPAddressesClient.RequestInspector = withInspection(maxlen)
	azureClient.PublicIPAddressesClient.ResponseInspector = byInspecting(maxlen)
	azureClient.PublicIPAddressesClient.UserAgent += packerUserAgent

	azureClient.VirtualMachinesClient = compute.NewVirtualMachinesClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
	azureClient.VirtualMachinesClient.Authorizer = servicePrincipalToken
	azureClient.VirtualMachinesClient.RequestInspector = withInspection(maxlen)
	azureClient.VirtualMachinesClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), templateCapture(azureClient))
	azureClient.VirtualMachinesClient.UserAgent += packerUserAgent

	azureClient.AccountsClient = armStorage.NewAccountsClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
	azureClient.AccountsClient.Authorizer = servicePrincipalToken
	azureClient.AccountsClient.RequestInspector = withInspection(maxlen)
	azureClient.AccountsClient.ResponseInspector = byInspecting(maxlen)
	azureClient.AccountsClient.UserAgent += packerUserAgent

	keyVaultURL, err := url.Parse(cloud.KeyVaultEndpoint)
	if err != nil {
		return nil, err
	}

	azureClient.VaultClient = common.NewVaultClient(*keyVaultURL)
	azureClient.VaultClient.Authorizer = servicePrincipalTokenVault
	azureClient.VaultClient.RequestInspector = withInspection(maxlen)
	azureClient.VaultClient.ResponseInspector = byInspecting(maxlen)
	azureClient.VaultClient.UserAgent += packerUserAgent

	accountKeys, err := azureClient.AccountsClient.ListKeys(resourceGroupName, storageAccountName)
	if err != nil {
		return nil, err
	}

	storageClient, err := storage.NewClient(
		storageAccountName,
		*(*accountKeys.Keys)[0].Value,
		cloud.StorageEndpointSuffix,
		storage.DefaultAPIVersion,
		true /*useHttps*/)

	if err != nil {
		return nil, err
	}

	azureClient.BlobStorageClient = storageClient.GetBlobService()
	return azureClient, nil
}