-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
New Resource: azurerm_management_lock
#575
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5b802d4
Vendoring the Locks SDK
tombuildsstuff facd2a5
New Resource: `azurerm_management_lock`
tombuildsstuff 0f63744
Merge branch 'master' into locks
tombuildsstuff 8fe5b5f
Adding validation for the locks name
tombuildsstuff 032c7ac
Linting
tombuildsstuff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package azurerm | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccAzureRMManagementLock_importResourceGroupReadOnlyBasic(t *testing.T) { | ||
ri := acctest.RandInt() | ||
config := testAccAzureRMManagementLock_resourceGroupReadOnlyBasic(ri, testLocation()) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMManagementLockDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
}, | ||
{ | ||
ResourceName: "azurerm_management_lock.test", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAzureRMManagementLock_importResourceGroupReadOnlyComplete(t *testing.T) { | ||
ri := acctest.RandInt() | ||
config := testAccAzureRMManagementLock_resourceGroupReadOnlyComplete(ri, testLocation()) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMManagementLockDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
}, | ||
{ | ||
ResourceName: "azurerm_management_lock.test", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAzureRMManagementLock_importResourceGroupCanNotDeleteBasic(t *testing.T) { | ||
ri := acctest.RandInt() | ||
config := testAccAzureRMManagementLock_resourceGroupCanNotDeleteBasic(ri, testLocation()) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMManagementLockDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
}, | ||
{ | ||
ResourceName: "azurerm_management_lock.test", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAzureRMManagementLock_importResourceGroupCanNotDeleteComplete(t *testing.T) { | ||
ri := acctest.RandInt() | ||
config := testAccAzureRMManagementLock_resourceGroupCanNotDeleteComplete(ri, testLocation()) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMManagementLockDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
}, | ||
{ | ||
ResourceName: "azurerm_management_lock.test", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAzureRMManagementLock_importPublicIPCanNotDeleteBasic(t *testing.T) { | ||
ri := acctest.RandInt() | ||
config := testAccAzureRMManagementLock_publicIPCanNotDeleteBasic(ri, testLocation()) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMManagementLockDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
}, | ||
{ | ||
ResourceName: "azurerm_management_lock.test", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAzureRMManagementLock_importPublicIPReadOnlyBasic(t *testing.T) { | ||
ri := acctest.RandInt() | ||
config := testAccAzureRMManagementLock_publicIPReadOnlyBasic(ri, testLocation()) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMManagementLockDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
}, | ||
{ | ||
ResourceName: "azurerm_management_lock.test", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"regexp" | ||
|
||
"github.com/Azure/azure-sdk-for-go/arm/resources/locks" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmManagementLock() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmManagementLockCreateUpdate, | ||
Read: resourceArmManagementLockRead, | ||
Delete: resourceArmManagementLockDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validateArmManagementLockName, | ||
}, | ||
|
||
"scope": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"lock_level": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
string(locks.CanNotDelete), | ||
string(locks.ReadOnly), | ||
}, false), | ||
}, | ||
|
||
"notes": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringLenBetween(0, 512), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmManagementLockCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).managementLocksClient | ||
log.Printf("[INFO] preparing arguments for AzureRM Management Lock creation.") | ||
|
||
name := d.Get("name").(string) | ||
scope := d.Get("scope").(string) | ||
lockLevel := d.Get("lock_level").(string) | ||
notes := d.Get("notes").(string) | ||
|
||
lock := locks.ManagementLockObject{ | ||
ManagementLockProperties: &locks.ManagementLockProperties{ | ||
Level: locks.LockLevel(lockLevel), | ||
Notes: utils.String(notes), | ||
}, | ||
} | ||
|
||
_, err := client.CreateOrUpdateByScope(scope, name, lock) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
read, err := client.GetByScope(scope, name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if read.ID == nil { | ||
return fmt.Errorf("Cannot read ID of AzureRM Management Lock %q (Scope %q)", name, scope) | ||
} | ||
|
||
d.SetId(*read.ID) | ||
return resourceArmManagementLockRead(d, meta) | ||
} | ||
|
||
func resourceArmManagementLockRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).managementLocksClient | ||
|
||
id, err := parseAzureRMLockId(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := client.GetByScope(id.Scope, id.Name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error making Read request on AzureRM Management Lock %q (Scope %q): %+v", id.Name, id.Scope, err) | ||
} | ||
|
||
d.Set("name", resp.Name) | ||
d.Set("scope", id.Scope) | ||
|
||
if props := resp.ManagementLockProperties; props != nil { | ||
d.Set("lock_level", string(props.Level)) | ||
d.Set("notes", props.Notes) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmManagementLockDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).managementLocksClient | ||
|
||
id, err := parseAzureRMLockId(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := client.DeleteByScope(id.Scope, id.Name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp) { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("Error issuing AzureRM delete request for Management Lock %q (Scope %q): %+v", id.Name, id.Scope, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type AzureManagementLockId struct { | ||
Scope string | ||
Name string | ||
} | ||
|
||
func parseAzureRMLockId(id string) (*AzureManagementLockId, error) { | ||
segments := strings.Split(id, "/providers/Microsoft.Authorization/locks/") | ||
if len(segments) != 2 { | ||
return nil, fmt.Errorf("Expected ID to be in the format `{scope}/providers/Microsoft.Authorization/locks/{name} - got %d segments", len(segments)) | ||
} | ||
|
||
scope := segments[0] | ||
name := segments[1] | ||
lockId := AzureManagementLockId{ | ||
Scope: scope, | ||
Name: name, | ||
} | ||
return &lockId, nil | ||
} | ||
|
||
func validateArmManagementLockName(v interface{}, k string) (ws []string, es []error) { | ||
input := v.(string) | ||
|
||
if !regexp.MustCompile(`\A([A-Za-z0-9\-_]{1, 260})\z`).MatchString(input) { | ||
es = append(es, fmt.Errorf("%s can only consist of alphanumeric characters, dashes and underscores - and must be a maxiumum of 260 characters", k)) | ||
} | ||
|
||
return | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts on confirming we have two parts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed