Skip to content

Commit

Permalink
Merge pull request #2643 from terraform-providers/e-tracking-guid
Browse files Browse the repository at this point in the history
Support for specifying the Partner ID
  • Loading branch information
tombuildsstuff committed Jan 11, 2019
2 parents 253f3ec + b8ee7fe commit 8d5e9b3
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion azurerm/azurerm_sweeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func buildConfigForSweepers() (*ArmClient, error) {
return nil, fmt.Errorf("Error building ARM Client: %+v", err)
}

return getArmClient(config, false)
return getArmClient(config, false, "")
}

func shouldSweepAcceptanceTestResource(name string, resourceLocation string, region string) bool {
Expand Down
14 changes: 10 additions & 4 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type ArmClient struct {
clientId string
tenantId string
subscriptionId string
partnerId string
usingServicePrincipal bool
environment az.Environment
skipProviderRegistration bool
Expand Down Expand Up @@ -349,15 +350,15 @@ func clientRequestID() string {
}

func (c *ArmClient) configureClient(client *autorest.Client, auth autorest.Authorizer) {
setUserAgent(client)
setUserAgent(client, c.partnerId)
client.Authorizer = auth
//client.RequestInspector = azure.WithClientID(clientRequestID())
client.Sender = azure.BuildSender()
client.SkipResourceProviderRegistration = c.skipProviderRegistration
client.PollingDuration = 60 * time.Minute
}

func setUserAgent(client *autorest.Client) {
func setUserAgent(client *autorest.Client, partnerID string) {
// TODO: This is the SDK version not the CLI version, once we are on 0.12, should revisit
tfUserAgent := httpclient.UserAgentString()

Expand All @@ -370,12 +371,16 @@ func setUserAgent(client *autorest.Client) {
client.UserAgent = fmt.Sprintf("%s %s", client.UserAgent, azureAgent)
}

if partnerID != "" {
client.UserAgent = fmt.Sprintf("%s pid-%s", client.UserAgent, partnerID)
}

log.Printf("[DEBUG] AzureRM Client User Agent: %s\n", client.UserAgent)
}

// getArmClient is a helper method which returns a fully instantiated
// *ArmClient based on the Config's current settings.
func getArmClient(c *authentication.Config, skipProviderRegistration bool) (*ArmClient, error) {
func getArmClient(c *authentication.Config, skipProviderRegistration bool, partnerId string) (*ArmClient, error) {
env, err := authentication.DetermineEnvironment(c.Environment)
if err != nil {
return nil, err
Expand All @@ -386,6 +391,7 @@ func getArmClient(c *authentication.Config, skipProviderRegistration bool) (*Arm
clientId: c.ClientID,
tenantId: c.TenantID,
subscriptionId: c.SubscriptionID,
partnerId: partnerId,
environment: *env,
usingServicePrincipal: c.AuthenticatedAsAServicePrincipal,
skipProviderRegistration: skipProviderRegistration,
Expand Down Expand Up @@ -726,7 +732,7 @@ func (c *ArmClient) registerDatabases(endpoint, subscriptionId string, auth auto
c.sqlDatabasesClient = sqlDBClient

sqlDTDPClient := sql.NewDatabaseThreatDetectionPoliciesClientWithBaseURI(endpoint, subscriptionId)
setUserAgent(&sqlDTDPClient.Client)
setUserAgent(&sqlDTDPClient.Client, "")
sqlDTDPClient.Authorizer = auth
sqlDTDPClient.Sender = sender
sqlDTDPClient.SkipResourceProviderRegistration = c.skipProviderRegistration
Expand Down
13 changes: 12 additions & 1 deletion azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
)

// Provider returns a terraform.ResourceProvider.
Expand Down Expand Up @@ -74,6 +75,14 @@ func Provider() terraform.ResourceProvider {
DefaultFunc: schema.EnvDefaultFunc("ARM_MSI_ENDPOINT", ""),
},

// Managed Tracking GUID for User-agent
"partner_id": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("ARM_PARTNER_ID", ""),
ValidateFunc: validate.UUID,
},

// Advanced feature flags
"skip_credentials_validation": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -368,8 +377,10 @@ func providerConfigure(p *schema.Provider) schema.ConfigureFunc {
return nil, fmt.Errorf("Error building AzureRM Client: %s", err)
}

partnerId := d.Get("partner_id").(string)
skipProviderRegistration := d.Get("skip_provider_registration").(bool)
client, err := getArmClient(config, skipProviderRegistration)
client, err := getArmClient(config, skipProviderRegistration, partnerId)

if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion azurerm/required_resource_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestAccAzureRMEnsureRequiredResourceProvidersAreRegistered(t *testing.T) {
}

// this test intentionally checks all the RP's are registered - so this is intentional
armClient, err := getArmClient(config, true)
armClient, err := getArmClient(config, true, "")
if err != nil {
t.Fatalf("Error building ARM Client: %+v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion azurerm/resource_arm_container_registry_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestAccAzureRMContainerRegistryMigrateState(t *testing.T) {
return
}

client, err := getArmClient(config, false)
client, err := getArmClient(config, false, "")
if err != nil {
t.Fatal(fmt.Errorf("Error building ARM Client: %+v", err))
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestAccAzureRMDataLakeStoreFileMigrateState(t *testing.T) {
return
}

client, err := getArmClient(config, false)
client, err := getArmClient(config, false, "")
if err != nil {
t.Fatal(fmt.Errorf("Error building ARM Client: %+v", err))
return
Expand Down
2 changes: 1 addition & 1 deletion azurerm/resource_arm_storage_blob_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestAccAzureRMStorageBlobMigrateState(t *testing.T) {
return
}

client, err := getArmClient(config, false)
client, err := getArmClient(config, false, "")
if err != nil {
t.Fatal(fmt.Errorf("Error building ARM Client: %+v", err))
return
Expand Down
2 changes: 1 addition & 1 deletion azurerm/resource_arm_storage_container_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestAccAzureRMStorageContainerMigrateState(t *testing.T) {
return
}

client, err := getArmClient(config, false)
client, err := getArmClient(config, false, "")
if err != nil {
t.Fatal(fmt.Errorf("Error building ARM Client: %+v", err))
return
Expand Down
2 changes: 1 addition & 1 deletion azurerm/resource_arm_storage_queue_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestAccAzureRMStorageQueueMigrateState(t *testing.T) {
return
}

client, err := getArmClient(config, false)
client, err := getArmClient(config, false, "")
if err != nil {
t.Fatal(fmt.Errorf("Error building ARM Client: %+v", err))
return
Expand Down
2 changes: 1 addition & 1 deletion azurerm/resource_arm_storage_table_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestAccAzureRMStorageTableMigrateState(t *testing.T) {
return
}

client, err := getArmClient(config, false)
client, err := getArmClient(config, false, "")
if err != nil {
t.Fatal(fmt.Errorf("Error building ARM Client: %+v", err))
return
Expand Down
2 changes: 2 additions & 0 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ More information on [how to configure a Service Principal using Managed Service

For some advanced scenarios, such as where more granular permissions are necessary - the following properties can be set:

* `partner_id` - (Optional) A GUID/UUID provided by Microsoft that is added to the user agent to facilitate partner resource usage data collection. This can also be sourced from the `ARM_PARTNER_ID` Environment Variable.

* `skip_credentials_validation` - (Optional) Should the AzureRM Provider skip verifying the credentials being used are valid? This can also be sourced from the `ARM_SKIP_CREDENTIALS_VALIDATION` Environment Variable. Defaults to `false`.

* `skip_provider_registration` - (Optional) Should the AzureRM Provider skip registering any required Resource Providers? This can also be sourced from the `ARM_SKIP_PROVIDER_REGISTRATION` Environment Variable. Defaults to `false`.
Expand Down

0 comments on commit 8d5e9b3

Please sign in to comment.