...

Source file src/github.com/Azure/azure-sdk-for-go/services/classic/management/client.go

Documentation: github.com/Azure/azure-sdk-for-go/services/classic/management

     1  // +build go1.7
     2  
     3  // Package management provides the main API client to construct other clients
     4  // and make requests to the Microsoft Azure Service Management REST API.
     5  package management
     6  
     7  // Copyright (c) Microsoft Corporation. All rights reserved.
     8  // Licensed under the MIT License. See License.txt in the project root for license information.
     9  
    10  import (
    11  	"errors"
    12  	"fmt"
    13  	"net/http"
    14  	"runtime"
    15  	"time"
    16  
    17  	"github.com/Azure/azure-sdk-for-go/version"
    18  )
    19  
    20  var (
    21  	DefaultUserAgent = userAgent()
    22  )
    23  
    24  const (
    25  	DefaultAzureManagementURL    = "https://management.core.windows.net"
    26  	DefaultOperationPollInterval = time.Second * 30
    27  	DefaultAPIVersion            = "2014-10-01"
    28  
    29  	errPublishSettingsConfiguration       = "PublishSettingsFilePath is set. Consequently ManagementCertificatePath and SubscriptionId must not be set."
    30  	errManagementCertificateConfiguration = "Both ManagementCertificatePath and SubscriptionId should be set, and PublishSettingsFilePath must not be set."
    31  	errParamNotSpecified                  = "Parameter %s is not specified."
    32  )
    33  
    34  type client struct {
    35  	publishSettings publishSettings
    36  	config          ClientConfig
    37  	httpClient      *http.Client
    38  }
    39  
    40  // Client is the base Azure Service Management API client instance that
    41  // can be used to construct client instances for various services.
    42  type Client interface {
    43  	// SendAzureGetRequest sends a request to the management API using the HTTP GET method
    44  	// and returns the response body or an error.
    45  	SendAzureGetRequest(url string) ([]byte, error)
    46  
    47  	// SendAzurePostRequest sends a request to the management API using the HTTP POST method
    48  	// and returns the request ID or an error.
    49  	SendAzurePostRequest(url string, data []byte) (OperationID, error)
    50  
    51  	// SendAzurePostRequestWithReturnedResponse sends a request to the management API using
    52  	// the HTTP POST method and returns the response body or an error.
    53  	SendAzurePostRequestWithReturnedResponse(url string, data []byte) ([]byte, error)
    54  
    55  	// SendAzurePutRequest sends a request to the management API using the HTTP PUT method
    56  	// and returns the request ID or an error. The content type can be specified, however
    57  	// if an empty string is passed, the default of "application/xml" will be used.
    58  	SendAzurePutRequest(url, contentType string, data []byte) (OperationID, error)
    59  
    60  	// SendAzureDeleteRequest sends a request to the management API using the HTTP DELETE method
    61  	// and returns the request ID or an error.
    62  	SendAzureDeleteRequest(url string) (OperationID, error)
    63  
    64  	// GetOperationStatus gets the status of operation with given Operation ID.
    65  	// WaitForOperation utility method can be used for polling for operation status.
    66  	GetOperationStatus(operationID OperationID) (GetOperationStatusResponse, error)
    67  
    68  	// WaitForOperation polls the Azure API for given operation ID indefinitely
    69  	// until the operation is completed with either success or failure.
    70  	// It is meant to be used for waiting for the result of the methods that
    71  	// return an OperationID value (meaning a long running operation has started).
    72  	//
    73  	// Cancellation of the polling loop (for instance, timing out) is done through
    74  	// cancel channel. If the user does not want to cancel, a nil chan can be provided.
    75  	// To cancel the method, it is recommended to close the channel provided to this
    76  	// method.
    77  	//
    78  	// If the operation was not successful or cancelling is signaled, an error
    79  	// is returned.
    80  	WaitForOperation(operationID OperationID, cancel chan struct{}) error
    81  }
    82  
    83  // ClientConfig provides a configuration for use by a Client.
    84  type ClientConfig struct {
    85  	ManagementURL         string
    86  	OperationPollInterval time.Duration
    87  	UserAgent             string
    88  	APIVersion            string
    89  }
    90  
    91  // NewAnonymousClient creates a new azure.Client with no credentials set.
    92  func NewAnonymousClient() Client {
    93  	return client{}
    94  }
    95  
    96  // DefaultConfig returns the default client configuration used to construct
    97  // a client. This value can be used to make modifications on the default API
    98  // configuration.
    99  func DefaultConfig() ClientConfig {
   100  	return ClientConfig{
   101  		ManagementURL:         DefaultAzureManagementURL,
   102  		OperationPollInterval: DefaultOperationPollInterval,
   103  		APIVersion:            DefaultAPIVersion,
   104  		UserAgent:             DefaultUserAgent,
   105  	}
   106  }
   107  
   108  // NewClient creates a new Client using the given subscription ID and
   109  // management certificate.
   110  func NewClient(subscriptionID string, managementCert []byte) (Client, error) {
   111  	return NewClientFromConfig(subscriptionID, managementCert, DefaultConfig())
   112  }
   113  
   114  // NewClientFromConfig creates a new Client using a given ClientConfig.
   115  func NewClientFromConfig(subscriptionID string, managementCert []byte, config ClientConfig) (Client, error) {
   116  	return makeClient(subscriptionID, managementCert, config)
   117  }
   118  
   119  func makeClient(subscriptionID string, managementCert []byte, config ClientConfig) (Client, error) {
   120  	var c client
   121  
   122  	if subscriptionID == "" {
   123  		return c, errors.New("azure: subscription ID required")
   124  	}
   125  
   126  	if len(managementCert) == 0 {
   127  		return c, errors.New("azure: management certificate required")
   128  	}
   129  
   130  	publishSettings := publishSettings{
   131  		SubscriptionID:   subscriptionID,
   132  		SubscriptionCert: managementCert,
   133  		SubscriptionKey:  managementCert,
   134  	}
   135  
   136  	// Validate client configuration
   137  	switch {
   138  	case config.ManagementURL == "":
   139  		return c, errors.New("azure: base URL required")
   140  	case config.OperationPollInterval <= 0:
   141  		return c, errors.New("azure: operation polling interval must be a positive duration")
   142  	case config.APIVersion == "":
   143  		return c, errors.New("azure: client configuration must specify an API version")
   144  	case config.UserAgent == "":
   145  		config.UserAgent = DefaultUserAgent
   146  	}
   147  
   148  	clientObj := client{
   149  		publishSettings: publishSettings,
   150  		config:          config,
   151  	}
   152  	var err error
   153  	clientObj.httpClient, err = clientObj.createHTTPClient()
   154  	if err != nil {
   155  		return nil, err
   156  	}
   157  	return clientObj, nil
   158  }
   159  
   160  func userAgent() string {
   161  	return fmt.Sprintf("Go/%s (%s-%s) Azure-SDK-For-Go/%s asm/%s",
   162  		runtime.Version(),
   163  		runtime.GOARCH,
   164  		runtime.GOOS,
   165  		version.Number,
   166  		DefaultAPIVersion)
   167  }
   168  

View as plain text