Skip to content

Commit

Permalink
feat: Add clideside lock and retry_count (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
uanid authored Nov 1, 2023
1 parent c997b57 commit a079824
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 107 deletions.
1 change: 1 addition & 0 deletions example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ terraform {

provider "msgraph" {
use_cli = true
use_clientside_lock = true
}

resource "msgraph_app_redirect_uris" "uris" {
Expand Down
17 changes: 12 additions & 5 deletions internal/clients/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package clients
import (
"context"
"fmt"
"sync"

"github.com/manicminer/hamilton/auth"
"github.com/manicminer/hamilton/environments"
Expand All @@ -14,11 +15,12 @@ import (
)

type ClientBuilder struct {
AuthConfig *auth.Config
AadAuthConfig *authentication.Config
EnableMsGraph bool
PartnerID string
TerraformVersion string
AuthConfig *auth.Config
AadAuthConfig *authentication.Config
EnableMsGraph bool
PartnerID string
TerraformVersion string
EnableClientsideLock bool
}

// Build is a helper method which returns a fully instantiated *Client based on the auth Config's current settings.
Expand Down Expand Up @@ -49,6 +51,11 @@ func (b *ClientBuilder) Build(ctx context.Context) (*Client, error) {
AuthenticatedAsAServicePrincipal: b.AadAuthConfig.AuthenticatedAsAServicePrincipal,
}

if b.EnableClientsideLock {
client.EnableResourceMutex = true
client.ResourceMutex = &sync.Mutex{}
}

if b.AuthConfig != nil {
client.Environment = b.AuthConfig.Environment
}
Expand Down
4 changes: 4 additions & 0 deletions internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package clients
import (
"context"
"fmt"
"sync"

"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
"github.com/manicminer/hamilton/auth"
Expand Down Expand Up @@ -36,6 +37,9 @@ type Client struct {
}
GroupsClient *msgraph.GroupsClient
AppClient *msgraph.ApplicationsClient

EnableResourceMutex bool
ResourceMutex *sync.Mutex
}

func (client *Client) build(ctx context.Context, o *common.ClientOptions) error { //nolint:unparam
Expand Down
22 changes: 16 additions & 6 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ func Provider() *schema.Provider {
DefaultFunc: schema.EnvDefaultFunc("ARM_DISABLE_TERRAFORM_PARTNER_ID", false),
Description: "Disable the Terraform Partner ID which is used if a custom `partner_id` isn't specified.",
},

"use_clientside_lock": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Use a client-side lock for all resources to prevent concurrent modification.",
},
},

ResourcesMap: resources,
Expand Down Expand Up @@ -216,22 +223,25 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc {
partnerId = terraformPartnerId
}

return buildClient(ctx, p, authConfig, aadBuilder, partnerId)
useClientsideLock := d.Get("use_clientside_lock").(bool)

return buildClient(ctx, p, authConfig, aadBuilder, partnerId, useClientsideLock)
}
}

// TODO: v2.0 pull out authentication.Builder and derived configuration
func buildClient(ctx context.Context, p *schema.Provider, authConfig *auth.Config, b *authentication.Builder, partnerId string) (*clients.Client, diag.Diagnostics) {
func buildClient(ctx context.Context, p *schema.Provider, authConfig *auth.Config, b *authentication.Builder, partnerId string, enableClientsideLock bool) (*clients.Client, diag.Diagnostics) {
aadConfig, err := b.Build()
if err != nil {
return nil, tf.ErrorDiagF(err, "Building AzureAD Client")
}

clientBuilder := clients.ClientBuilder{
AuthConfig: authConfig,
AadAuthConfig: aadConfig,
PartnerID: partnerId,
TerraformVersion: p.TerraformVersion,
AuthConfig: authConfig,
AadAuthConfig: aadConfig,
PartnerID: partnerId,
TerraformVersion: p.TerraformVersion,
EnableClientsideLock: enableClientsideLock,
}

stopCtx, ok := schema.StopContext(ctx) //nolint:SA1019
Expand Down
Loading

0 comments on commit a079824

Please sign in to comment.