Skip to content

Commit

Permalink
azurerm_role_definition: upgrade go-azure-sdk from `2018-01-01-pr…
Browse files Browse the repository at this point in the history
…eview` to `2022-05-01-preview` (hashicorp#25008)

* upgrade `authorization` from `2018-01-01-preview` to
`2022-05-01-preview`

* remove workaround not needed anymore

* make client consistent

* add lint ignore back
  • Loading branch information
ziyeqf authored and rizkybiz committed Feb 29, 2024
1 parent 0207cfc commit e50bdcc
Show file tree
Hide file tree
Showing 24 changed files with 85 additions and 185 deletions.
125 changes: 0 additions & 125 deletions internal/services/authorization/azuresdkhacks/definitions.go

This file was deleted.

11 changes: 1 addition & 10 deletions internal/services/authorization/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@ import (
"github.com/Azure/azure-sdk-for-go/services/preview/authorization/mgmt/2020-04-01-preview/authorization" // nolint: staticcheck // nolint: staticcheck

// To swap sdk for `azurerm_role_definition` without changing API version
oldRoleDefinitions "github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2018-01-01-preview/roledefinitions"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2020-10-01/roleassignmentscheduleinstances"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2020-10-01/roleassignmentschedulerequests"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2020-10-01/roleeligibilityscheduleinstances"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2020-10-01/roleeligibilityschedulerequests"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2022-04-01/roleassignments"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2022-04-01/roledefinitions"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2022-05-01-preview/roledefinitions"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
RoleAssignmentsClient *authorization.RoleAssignmentsClient
RoleDefinitionsClient *oldRoleDefinitions.RoleDefinitionsClient
RoleAssignmentScheduleRequestClient *roleassignmentschedulerequests.RoleAssignmentScheduleRequestsClient
RoleAssignmentScheduleInstancesClient *roleassignmentscheduleinstances.RoleAssignmentScheduleInstancesClient
RoleEligibilityScheduleRequestClient *roleeligibilityschedulerequests.RoleEligibilityScheduleRequestsClient
Expand All @@ -34,12 +32,6 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
roleAssignmentsClient := authorization.NewRoleAssignmentsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&roleAssignmentsClient.Client, o.ResourceManagerAuthorizer)

roleDefinitionsClient, err := oldRoleDefinitions.NewRoleDefinitionsClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("creating roleDefinitionsClient: %+v", err)
}
o.Configure(roleDefinitionsClient.Client, o.Authorizers.ResourceManager)

roleAssignmentScheduleRequestsClient, err := roleassignmentschedulerequests.NewRoleAssignmentScheduleRequestsClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("creating roleAssignmentScheduleRequestsClient: %+v", err)
Expand Down Expand Up @@ -79,7 +71,6 @@ func NewClient(o *common.ClientOptions) (*Client, error) {

return &Client{
RoleAssignmentsClient: &roleAssignmentsClient,
RoleDefinitionsClient: roleDefinitionsClient,
RoleAssignmentScheduleRequestClient: roleAssignmentScheduleRequestsClient,
RoleAssignmentScheduleInstancesClient: roleAssignmentScheduleInstancesClient,
RoleEligibilityScheduleRequestClient: roleEligibilityScheduleRequestClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2022-04-01/roleassignments"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2022-04-01/roledefinitions"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2022-05-01-preview/roledefinitions"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/testclient"
Expand Down
6 changes: 3 additions & 3 deletions internal/services/authorization/role_assignment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/preview/authorization/mgmt/2020-04-01-preview/authorization" // nolint: staticcheck
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2018-01-01-preview/roledefinitions"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2022-05-01-preview/roledefinitions"
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2022-12-01/subscriptions"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
Expand Down Expand Up @@ -156,7 +156,7 @@ func resourceArmRoleAssignment() *pluginsdk.Resource {

func resourceArmRoleAssignmentCreate(d *pluginsdk.ResourceData, meta interface{}) error {
roleAssignmentsClient := meta.(*clients.Client).Authorization.RoleAssignmentsClient
roleDefinitionsClient := meta.(*clients.Client).Authorization.RoleDefinitionsClient
roleDefinitionsClient := meta.(*clients.Client).Authorization.ScopedRoleDefinitionsClient
subscriptionClient := meta.(*clients.Client).Subscription.SubscriptionsClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
Expand Down Expand Up @@ -271,7 +271,7 @@ func resourceArmRoleAssignmentCreate(d *pluginsdk.ResourceData, meta interface{}

func resourceArmRoleAssignmentRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Authorization.RoleAssignmentsClient
roleDefinitionsClient := meta.(*clients.Client).Authorization.RoleDefinitionsClient
roleDefinitionsClient := meta.(*clients.Client).Authorization.ScopedRoleDefinitionsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2022-04-01/roleassignments"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2022-04-01/roledefinitions"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2022-05-01-preview/roledefinitions"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ data "azurerm_client_config" "test" {
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-role-assigment-%d"
name = "acctestRG-role-assignment-%d"
location = "%s"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2018-01-01-preview/roledefinitions"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2022-05-01-preview/roledefinitions"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
Expand Down Expand Up @@ -145,7 +145,7 @@ func (a RoleDefinitionDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Authorization.RoleDefinitionsClient
client := metadata.Client.Authorization.ScopedRoleDefinitionsClient

var config RoleDefinitionDataSourceModel
if err := metadata.Decode(&config); err != nil {
Expand Down Expand Up @@ -218,7 +218,7 @@ func (a RoleDefinitionDataSource) Read() sdk.ResourceFunc {
}

// The sdk managed id start with two "/" when scope is tenant level (empty).
// So we use the id from response without parsing and reformating it.
// So we use the id from response without parsing and reformatting it.
// Tracked on https://github.com/hashicorp/pandora/issues/3257
metadata.ResourceData.SetId(*role.Id)
return metadata.Encode(&state)
Expand Down
20 changes: 9 additions & 11 deletions internal/services/authorization/role_definition_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import (
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2018-01-01-preview/roledefinitions"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2022-05-01-preview/roledefinitions"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/authorization/azuresdkhacks"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/authorization/migration"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/authorization/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down Expand Up @@ -164,7 +163,7 @@ func (r RoleDefinitionResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Authorization.RoleDefinitionsClient
client := metadata.Client.Authorization.ScopedRoleDefinitionsClient

var config RoleDefinitionModel
if err := metadata.Decode(&config); err != nil {
Expand Down Expand Up @@ -232,7 +231,7 @@ func (r RoleDefinitionResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Authorization.RoleDefinitionsClient
client := metadata.Client.Authorization.ScopedRoleDefinitionsClient

stateId, err := parse.RoleDefinitionId(metadata.ResourceData.Id())
if err != nil {
Expand Down Expand Up @@ -276,8 +275,7 @@ func (r RoleDefinitionResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 60 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
sdkClient := metadata.Client.Authorization.RoleDefinitionsClient
client := azuresdkhacks.NewRoleDefinitionsWorkaroundClient(sdkClient.Client)
client := metadata.Client.Authorization.ScopedRoleDefinitionsClient

stateId, err := parse.RoleDefinitionId(metadata.ResourceData.Id())
if err != nil {
Expand All @@ -291,16 +289,16 @@ func (r RoleDefinitionResource) Update() sdk.ResourceFunc {
return err
}

exisiting, err := client.Get(ctx, id)
existing, err := client.Get(ctx, id)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", stateId, err)
}

if exisiting.Model == nil {
if existing.Model == nil {
return fmt.Errorf("retrieving %s: model was nil", stateId)
}

model := *exisiting.Model
model := *existing.Model

if model.Properties == nil {
return fmt.Errorf("retrieving %s: properties was nil", stateId)
Expand Down Expand Up @@ -378,7 +376,7 @@ func (r RoleDefinitionResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Authorization.RoleDefinitionsClient
client := metadata.Client.Authorization.ScopedRoleDefinitionsClient

stateId, err := parse.RoleDefinitionId(metadata.ResourceData.Id())
if err != nil {
Expand Down Expand Up @@ -432,7 +430,7 @@ func (RoleDefinitionResource) StateUpgraders() sdk.StateUpgradeData {
}
}

func roleDefinitionEventualConsistencyUpdate(ctx context.Context, client azuresdkhacks.RoleDefinitionsWorkaroundClient, id roledefinitions.ScopedRoleDefinitionId, updateRequestDate string) pluginsdk.StateRefreshFunc {
func roleDefinitionEventualConsistencyUpdate(ctx context.Context, client *roledefinitions.RoleDefinitionsClient, id roledefinitions.ScopedRoleDefinitionId, updateRequestDate string) pluginsdk.StateRefreshFunc {
return func() (interface{}, string, error) {
resp, err := client.Get(ctx, id)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/google/uuid"
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2018-01-01-preview/roledefinitions"
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2022-05-01-preview/roledefinitions"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -196,7 +196,7 @@ func (RoleDefinitionResource) Exists(ctx context.Context, client *clients.Client
roleId := state.Attributes["role_definition_id"]

id := roledefinitions.NewScopedRoleDefinitionID(scope, roleId)
resp, err := client.Authorization.RoleDefinitionsClient.Get(ctx, id)
resp, err := client.Authorization.ScopedRoleDefinitionsClient.Get(ctx, id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return pointer.To(false), nil
Expand Down

This file was deleted.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e50bdcc

Please sign in to comment.