github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/builtin/providers/azure/config.go (about)

     1  package azure
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"sync"
     7  
     8  	"github.com/Azure/azure-sdk-for-go/management"
     9  )
    10  
    11  // Config is the configuration structure used to instantiate a
    12  // new Azure management client.
    13  type Config struct {
    14  	SettingsFile   string
    15  	SubscriptionID string
    16  	Certificate    []byte
    17  	ManagementURL  string
    18  }
    19  
    20  // Client contains all the handles required for managing Azure services.
    21  type Client struct {
    22  	// unfortunately; because of how Azure's network API works; doing networking operations
    23  	// concurrently is very hazardous, and we need a mutex to guard the management.Client.
    24  	mutex      *sync.Mutex
    25  	mgmtClient management.Client
    26  }
    27  
    28  // NewClientFromSettingsFile returns a new Azure management
    29  // client created using a publish settings file.
    30  func (c *Config) NewClientFromSettingsFile() (*Client, error) {
    31  	if _, err := os.Stat(c.SettingsFile); os.IsNotExist(err) {
    32  		return nil, fmt.Errorf("Publish Settings file %q does not exist!", c.SettingsFile)
    33  	}
    34  
    35  	mc, err := management.ClientFromPublishSettingsFile(c.SettingsFile, c.SubscriptionID)
    36  	if err != nil {
    37  		return nil, nil
    38  	}
    39  
    40  	return &Client{
    41  		mutex:      &sync.Mutex{},
    42  		mgmtClient: mc,
    43  	}, nil
    44  }
    45  
    46  // NewClient returns a new Azure management client created
    47  // using a subscription ID and certificate.
    48  func (c *Config) NewClient() (*Client, error) {
    49  	mc, err := management.NewClient(c.SubscriptionID, c.Certificate)
    50  	if err != nil {
    51  		return nil, nil
    52  	}
    53  
    54  	return &Client{
    55  		mutex:      &sync.Mutex{},
    56  		mgmtClient: mc,
    57  	}, nil
    58  }