Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/management_locks: refactoring to use hashicorp/go-azure-sdk #20387

Merged
merged 3 commits into from
Feb 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions internal/services/resource/client/client.go
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@ import (
providers "github.com/Azure/azure-sdk-for-go/profiles/2017-03-09/resources/mgmt/resources"
"github.com/Azure/azure-sdk-for-go/services/preview/resources/mgmt/2019-06-01-preview/templatespecs" // nolint: staticcheck
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2015-12-01/features" // nolint: staticcheck
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-09-01/locks" // nolint: staticcheck
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-06-01/resources" // nolint: staticcheck
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/managementlocks"
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-10-01/deploymentscripts"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)
@@ -15,7 +15,7 @@ type Client struct {
DeploymentScriptsClient *deploymentscripts.DeploymentScriptsClient
FeaturesClient *features.Client
GroupsClient *resources.GroupsClient
LocksClient *locks.ManagementLocksClient
LocksClient *managementlocks.ManagementLocksClient
ProvidersClient *providers.ProvidersClient
ResourceProvidersClient *resources.ProvidersClient
ResourcesClient *resources.Client
@@ -38,7 +38,7 @@ func NewClient(o *common.ClientOptions) *Client {
groupsClient := resources.NewGroupsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&groupsClient.Client, o.ResourceManagerAuthorizer)

locksClient := locks.NewManagementLocksClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
locksClient := managementlocks.NewManagementLocksClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&locksClient.Client, o.ResourceManagerAuthorizer)

// this has to come from the Profile since this is shared with Stack
56 changes: 27 additions & 29 deletions internal/services/resource/management_lock_resource.go
Original file line number Diff line number Diff line change
@@ -2,13 +2,12 @@ package resource

import (
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-09-01/locks" // nolint: staticcheck
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/managementlocks"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/resource/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/resource/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
@@ -18,12 +17,12 @@ import (

func resourceManagementLock() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceManagementLockCreateUpdate,
Create: resourceManagementLockCreate,
Read: resourceManagementLockRead,
Delete: resourceManagementLockDelete,

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.ParseManagementLockID(id)
_, err := managementlocks.ParseScopedLockID(id)
return err
}),

@@ -53,8 +52,8 @@ func resourceManagementLock() *pluginsdk.Resource {
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(locks.CanNotDelete),
string(locks.ReadOnly),
string(managementlocks.LockLevelCanNotDelete),
string(managementlocks.LockLevelReadOnly),
}, false),
},

@@ -68,34 +67,33 @@ func resourceManagementLock() *pluginsdk.Resource {
}
}

func resourceManagementLockCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
func resourceManagementLockCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Resource.LocksClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()
log.Printf("[INFO] preparing arguments for AzureRM Management Lock creation.")

id := parse.NewManagementLockID(d.Get("scope").(string), d.Get("name").(string))
id := managementlocks.NewScopedLockID(d.Get("scope").(string), d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.GetByScope(ctx, id.Scope, id.Name)
existing, err := client.GetByScope(ctx, id)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}

if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError("azurerm_management_lock", id.ID())
}
}

lock := locks.ManagementLockObject{
ManagementLockProperties: &locks.ManagementLockProperties{
Level: locks.LockLevel(d.Get("lock_level").(string)),
payload := managementlocks.ManagementLockObject{
Properties: managementlocks.ManagementLockProperties{
Level: managementlocks.LockLevel(d.Get("lock_level").(string)),
Notes: utils.String(d.Get("notes").(string)),
},
}

if _, err := client.CreateOrUpdateByScope(ctx, id.Scope, id.Name, lock); err != nil {
if _, err := client.CreateOrUpdateByScope(ctx, id, payload); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

@@ -108,27 +106,27 @@ func resourceManagementLockRead(d *pluginsdk.ResourceData, meta interface{}) err
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ParseManagementLockID(d.Id())
id, err := managementlocks.ParseScopedLockID(d.Id())
if err != nil {
return err
}

resp, err := client.GetByScope(ctx, id.Scope, id.Name)
resp, err := client.GetByScope(ctx, *id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
d.SetId("")
return nil
}

return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", id.Name)
d.Set("name", id.LockName)
d.Set("scope", id.Scope)

if props := resp.ManagementLockProperties; props != nil {
d.Set("lock_level", string(props.Level))
d.Set("notes", props.Notes)
if model := resp.Model; model != nil {
d.Set("lock_level", string(model.Properties.Level))
d.Set("notes", model.Properties.Notes)
}

return nil
@@ -139,15 +137,15 @@ func resourceManagementLockDelete(d *pluginsdk.ResourceData, meta interface{}) e
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ParseManagementLockID(d.Id())
id, err := managementlocks.ParseScopedLockID(d.Id())
if err != nil {
return err
}

if resp, err := client.DeleteByScope(ctx, id.Scope, id.Name); err != nil {
if resp, err := client.DeleteByScope(ctx, *id); err != nil {
// @tombuildsstuff: this is intentionally here in case the parent is gone, since we're under a scope
// which isn't ideal (as this shouldn't be present for most resources) but should for this one
if utils.ResponseWasNotFound(resp) {
// which isn't ideal (as this logic shouldn't be present for most resources) but should for this one
if response.WasNotFound(resp.HttpResponse) {
return nil
}

10 changes: 5 additions & 5 deletions internal/services/resource/management_lock_resource_test.go
Original file line number Diff line number Diff line change
@@ -6,10 +6,10 @@ import (
"os"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/managementlocks"
"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"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/resource/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
@@ -162,17 +162,17 @@ func TestAccManagementLock_subscriptionCanNotDeleteBasic(t *testing.T) {
}

func (t ManagementLockResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.ParseManagementLockID(state.ID)
id, err := managementlocks.ParseScopedLockID(state.ID)
if err != nil {
return nil, err
}

resp, err := clients.Resource.LocksClient.GetByScope(ctx, id.Scope, id.Name)
resp, err := clients.Resource.LocksClient.GetByScope(ctx, *id)
if err != nil {
return nil, fmt.Errorf("reading Management Lock (%s): %+v", id, err)
return nil, fmt.Errorf("reading %s: %+v", *id, err)
}

return utils.Bool(resp.ID != nil), nil
return utils.Bool(resp.Model != nil), nil
}

func (ManagementLockResource) resourceGroupReadOnlyBasic(data acceptance.TestData) string {
87 changes: 0 additions & 87 deletions internal/services/resource/parse/management_lock.go

This file was deleted.

This file was deleted.

This file was deleted.

Loading