Skip to content

Commit

Permalink
Restructure resources into service packages
Browse files Browse the repository at this point in the history
- Provider and Client refactoring to accommodate additional API clients
- Split aadgraph service package into packages by object type
- Move AAD Graph helpers outside service package
- Create parse package per service
- Some schema deprecations in preparation for next major release
  • Loading branch information
manicminer committed Jan 13, 2021
1 parent 523eee9 commit 2a7d9eb
Show file tree
Hide file tree
Showing 82 changed files with 1,819 additions and 1,109 deletions.
2 changes: 1 addition & 1 deletion internal/acceptance/check/that.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func That(resourceName string) thatType {
// ExistsInAzure validates that the specified resource exists within Azure
func (t thatType) ExistsInAzure(testResource types.TestResource) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := acceptance.AzureADProvider.Meta().(*clients.AadClient)
client := acceptance.AzureADProvider.Meta().(*clients.Client)
return helpers.ExistsInAzure(client, testResource, t.resourceName)(s)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/acceptance/helpers/check_destroyed.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// CheckDestroyedFunc returns a TestCheckFunc which validates the resource no longer exists
func CheckDestroyedFunc(client *clients.AadClient, testResource types.TestResource, resourceType, resourceLabel string) func(state *terraform.State) error {
func CheckDestroyedFunc(client *clients.Client, testResource types.TestResource, resourceType, resourceLabel string) func(state *terraform.State) error {
return func(state *terraform.State) error {
ctx := client.StopContext

Expand Down
2 changes: 1 addition & 1 deletion internal/acceptance/helpers/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// DeleteResourceFunc returns a TestCheckFunc which deletes the resource within Azure
// this is only used within the Internal
func DeleteResourceFunc(client *clients.AadClient, testResource types.TestResourceVerifyingRemoved, resourceName string) func(state *terraform.State) error {
func DeleteResourceFunc(client *clients.Client, testResource types.TestResourceVerifyingRemoved, resourceName string) func(state *terraform.State) error {
return func(state *terraform.State) error {
ctx := client.StopContext

Expand Down
2 changes: 1 addition & 1 deletion internal/acceptance/helpers/exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/terraform-providers/terraform-provider-azuread/internal/clients"
)

func ExistsInAzure(client *clients.AadClient, testResource types.TestResource, resourceName string) resource.TestCheckFunc {
func ExistsInAzure(client *clients.Client, testResource types.TestResource, resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
ctx := client.StopContext

Expand Down
4 changes: 2 additions & 2 deletions internal/acceptance/testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ func PreCheck(t *testing.T) {
}
}

func buildClient() *clients.AadClient {
return AzureADProvider.Meta().(*clients.AadClient)
func buildClient() *clients.Client {
return AzureADProvider.Meta().(*clients.Client)
}
4 changes: 2 additions & 2 deletions internal/acceptance/types/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
)

type TestResource interface {
Exists(ctx context.Context, client *clients.AadClient, state *terraform.InstanceState) (*bool, error)
Exists(ctx context.Context, client *clients.Client, state *terraform.InstanceState) (*bool, error)
}

type TestResourceVerifyingRemoved interface {
TestResource
Destroy(ctx context.Context, client *clients.AadClient, state *terraform.InstanceState) (*bool, error)
Destroy(ctx context.Context, client *clients.Client, state *terraform.InstanceState) (*bool, error)
}
36 changes: 17 additions & 19 deletions internal/clients/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import (
"context"
"fmt"

"github.com/Azure/go-autorest/autorest"
"github.com/hashicorp/go-azure-helpers/authentication"
"github.com/hashicorp/go-azure-helpers/sender"

"github.com/terraform-providers/terraform-provider-azuread/internal/services"
aad "github.com/terraform-providers/terraform-provider-azuread/internal/services/aadgraph/client"
"github.com/terraform-providers/terraform-provider-azuread/internal/common"
)

type ClientBuilder struct {
Expand All @@ -18,8 +16,8 @@ type ClientBuilder struct {
TerraformVersion string
}

// Build is a helper method which returns a fully instantiated *AadClient based on the auth Config's current settings.
func (b *ClientBuilder) Build(ctx context.Context) (*AadClient, error) {
// Build is a helper method which returns a fully instantiated *Client based on the auth Config's current settings.
func (b *ClientBuilder) Build(ctx context.Context) (*Client, error) {
env, err := authentication.AzureEnvironmentByNameFromEndpoint(ctx, b.AuthConfig.MetadataHost, b.AuthConfig.Environment)
if err != nil {
return nil, err
Expand All @@ -36,7 +34,7 @@ func (b *ClientBuilder) Build(ctx context.Context) (*AadClient, error) {
}

// client declarations:
client := AadClient{
client := Client{
ClientID: b.AuthConfig.ClientID,
ObjectID: objectID,
TenantID: b.AuthConfig.TenantID,
Expand All @@ -53,25 +51,25 @@ func (b *ClientBuilder) Build(ctx context.Context) (*AadClient, error) {
return nil, err
}

o := &services.ClientOptions{
PartnerID: b.PartnerID,
TenantID: b.AuthConfig.TenantID,
TerraformVersion: b.TerraformVersion,
Environment: *env,
}

// Graph Endpoints
graphEndpoint := env.GraphEndpoint // todo this should become AadGraphEndpoint?
graphAuthorizer, err := b.AuthConfig.GetAuthorizationToken(sender, oauth, graphEndpoint)
aadGraphEndpoint := env.GraphEndpoint
aadGraphAuthorizer, err := b.AuthConfig.GetAuthorizationToken(sender, oauth, aadGraphEndpoint)
if err != nil {
return nil, err
}

client.AadGraph = aad.BuildClient(o, graphEndpoint, graphAuthorizer)

autorest.Count429AsRetry = false
o := &common.ClientOptions{
AadGraphAuthorizer: aadGraphAuthorizer,
AadGraphEndpoint: aadGraphEndpoint,
PartnerID: b.PartnerID,
TenantID: b.AuthConfig.TenantID,
TerraformVersion: b.TerraformVersion,
Environment: *env,
}

client.StopContext = ctx
if err := client.build(ctx, o); err != nil {
return nil, fmt.Errorf("Error building Client: %+v", err)
}

return &client, nil
}
33 changes: 27 additions & 6 deletions internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ package clients
import (
"context"

"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"

aad "github.com/terraform-providers/terraform-provider-azuread/internal/services/aadgraph/client"
"github.com/terraform-providers/terraform-provider-azuread/internal/common"
applications "github.com/terraform-providers/terraform-provider-azuread/internal/services/applications/client"
domains "github.com/terraform-providers/terraform-provider-azuread/internal/services/domains/client"
groups "github.com/terraform-providers/terraform-provider-azuread/internal/services/groups/client"
serviceprincipals "github.com/terraform-providers/terraform-provider-azuread/internal/services/serviceprincipals/client"
users "github.com/terraform-providers/terraform-provider-azuread/internal/services/users/client"
)

// AadClient contains the handles to all the specific Azure AD resource classes' respective clients
type AadClient struct {
// todo move this to an "Account" struct as in azurerm?
// Client contains the handles to all the specific Azure AD resource classes' respective clients
type Client struct {
ClientID string
ObjectID string
TenantID string
Expand All @@ -21,6 +26,22 @@ type AadClient struct {

StopContext context.Context

// Azure AD clients
AadGraph *aad.Client
Applications *applications.Client
Domains *domains.Client
Groups *groups.Client
ServicePrincipals *serviceprincipals.Client
Users *users.Client
}

func (client *Client) build(ctx context.Context, o *common.ClientOptions) error { //nolint:unparam
autorest.Count429AsRetry = false
client.StopContext = ctx

client.Applications = applications.NewClient(o)
client.Domains = domains.NewClient(o)
client.Groups = groups.NewClient(o)
client.ServicePrincipals = serviceprincipals.NewClient(o)
client.Users = users.NewClient(o)

return nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package services
package common

import (
"fmt"
Expand All @@ -21,6 +21,9 @@ type ClientOptions struct {
PartnerID string
TerraformVersion string

AadGraphAuthorizer autorest.Authorizer
AadGraphEndpoint string

SkipProviderReg bool
}

Expand Down
Loading

0 comments on commit 2a7d9eb

Please sign in to comment.