-
Notifications
You must be signed in to change notification settings - Fork 5k
New resource: azurerm_managed_lustre_file_system_auto_export_job
#29908
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
Open
jiaweitao001
wants to merge
20
commits into
hashicorp:main
Choose a base branch
from
jiaweitao001:storage_cache_autoexport
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+722
−5
Open
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
b75bbeb
New resource: azurerm_managed_lustre_file_system_auto_export_job
jiaweitao001 c7425fa
sort imports
jiaweitao001 7b4e382
add complete test
jiaweitao001 87af82d
fix acc test
jiaweitao001 4e97673
Address comments
jiaweitao001 16ec007
fix indent
jiaweitao001 e73602e
Increase delete/update timeout
5a17cc5
Update docs
9502dc4
Addressing comments
jiaweitao001 fa3d5f6
Add timeout time for deleting resource
jiaweitao001 a7ec3ca
Fix docs
jiaweitao001 05a6ded
Addressing comments
jiaweitao001 e9e0f83
Making auto_export_prefixes to force new
jiaweitao001 57eadd4
Removing import prefix from AMLFS
jiaweitao001 4303806
Add waiting logic for import job
jiaweitao001 32ea5c7
storagecache: wait for import jobs and handle adminStatus on auto exp…
jiaweitao001 d705489
Changing update method to avoid unexpected status code issue from API
jiaweitao001 0fc76ed
Fix docs and name validation
jiaweitao001 bd306e8
Fix acc tests
jiaweitao001 1bce4da
Increase Create and Update timeouts for auto export job resource
jiaweitao001 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
235 changes: 235 additions & 0 deletions
235
internal/services/storagecache/managed_lustre_file_system_auto_export_job_resource.go
This file contains hidden or 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,235 @@ | ||
| package storagecache | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "regexp" | ||
| "time" | ||
|
|
||
| "github.com/hashicorp/go-azure-helpers/lang/pointer" | ||
| "github.com/hashicorp/go-azure-helpers/lang/response" | ||
| "github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" | ||
| "github.com/hashicorp/go-azure-helpers/resourcemanager/location" | ||
| "github.com/hashicorp/go-azure-sdk/resource-manager/storagecache/2024-07-01/autoexportjob" | ||
| "github.com/hashicorp/go-azure-sdk/resource-manager/storagecache/2024-07-01/autoexportjobs" | ||
| "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" | ||
| ) | ||
|
|
||
| type ManagedLustreFileSystemAutoExportJobModel struct { | ||
| Name string `tfschema:"name"` | ||
| ManagedLustreFileSystemId string `tfschema:"managed_lustre_file_system_id"` | ||
| Location string `tfschema:"location"` | ||
| AutoExportPrefixes []string `tfschema:"auto_export_prefixes"` | ||
| AdminStatusEnabled bool `tfschema:"admin_status_enabled"` | ||
| Tags map[string]string `tfschema:"tags"` | ||
| } | ||
|
|
||
| type ManagedLustreFileSystemAutoExportJobResource struct{} | ||
|
|
||
| var _ sdk.ResourceWithUpdate = ManagedLustreFileSystemAutoExportJobResource{} | ||
|
|
||
| func (r ManagedLustreFileSystemAutoExportJobResource) Attributes() map[string]*pluginsdk.Schema { | ||
| return map[string]*pluginsdk.Schema{} | ||
| } | ||
|
|
||
| func (r ManagedLustreFileSystemAutoExportJobResource) ModelObject() interface{} { | ||
| return &ManagedLustreFileSystemAutoExportJobModel{} | ||
| } | ||
|
|
||
| func (r ManagedLustreFileSystemAutoExportJobResource) ResourceType() string { | ||
| return "azurerm_managed_lustre_file_system_auto_export_job" | ||
| } | ||
|
|
||
| func (r ManagedLustreFileSystemAutoExportJobResource) Arguments() map[string]*pluginsdk.Schema { | ||
| return map[string]*pluginsdk.Schema{ | ||
| "name": { | ||
| Type: pluginsdk.TypeString, | ||
| Required: true, | ||
| ForceNew: true, | ||
| ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[0-9a-zA-Z][-0-9a-zA-Z_]{0,78}[0-9a-zA-Z]$`), "name must be 3-80 characters long and can only contain alphanumeric characters, underscores, and hyphens, and must start and end with an alphanumeric character"), | ||
| }, | ||
|
|
||
| "managed_lustre_file_system_id": commonschema.ResourceIDReferenceRequiredForceNew(&autoexportjob.AmlFilesystemId{}), | ||
|
|
||
| "location": commonschema.Location(), | ||
|
|
||
| "auto_export_prefixes": { | ||
| Type: pluginsdk.TypeList, | ||
| Required: true, | ||
| MinItems: 1, | ||
| Elem: &pluginsdk.Schema{ | ||
| Type: pluginsdk.TypeString, | ||
| ValidateFunc: validation.StringIsNotEmpty, | ||
| }, | ||
| }, | ||
|
|
||
| "admin_status_enabled": { | ||
| Type: pluginsdk.TypeBool, | ||
| Optional: true, | ||
| Default: true, | ||
| }, | ||
|
|
||
| "tags": commonschema.Tags(), | ||
| } | ||
| } | ||
|
|
||
| func (r ManagedLustreFileSystemAutoExportJobResource) Create() sdk.ResourceFunc { | ||
| return sdk.ResourceFunc{ | ||
| Timeout: 60 * time.Minute, | ||
| Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
| var model ManagedLustreFileSystemAutoExportJobModel | ||
| if err := metadata.Decode(&model); err != nil { | ||
| return fmt.Errorf("decoding model: %w", err) | ||
| } | ||
|
|
||
| autoExportJobsClient := metadata.Client.StorageCache.AutoExportJobs | ||
| autoExportJobClient := metadata.Client.StorageCache.AutoExportJob | ||
|
|
||
| amlFileSystemId, err := autoexportjob.ParseAmlFilesystemID(model.ManagedLustreFileSystemId) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| id := autoexportjobs.NewAutoExportJobID(amlFileSystemId.SubscriptionId, amlFileSystemId.ResourceGroupName, amlFileSystemId.AmlFilesystemName, model.Name) | ||
|
|
||
| existing, err := autoExportJobsClient.Get(ctx, id) | ||
| if err != nil && !response.WasNotFound(existing.HttpResponse) { | ||
| return fmt.Errorf("checking for existing %s: %+v", id, err) | ||
| } | ||
|
|
||
| if !response.WasNotFound(existing.HttpResponse) { | ||
| return metadata.ResourceRequiresImport(r.ResourceType(), id) | ||
| } | ||
|
|
||
| props := autoexportjob.AutoExportJob{ | ||
| Location: location.Normalize(model.Location), | ||
| Properties: pointer.To(autoexportjob.AutoExportJobProperties{ | ||
| AutoExportPrefixes: pointer.To(model.AutoExportPrefixes), | ||
| }), | ||
| Tags: pointer.To(model.Tags), | ||
| } | ||
|
|
||
| if model.AdminStatusEnabled { | ||
| props.Properties.AdminStatus = pointer.To(autoexportjob.AutoExportJobAdminStatusEnable) | ||
| } else { | ||
| props.Properties.AdminStatus = pointer.To(autoexportjob.AutoExportJobAdminStatusDisable) | ||
| } | ||
|
|
||
| autoExportJobId := autoexportjob.NewAutoExportJobID(amlFileSystemId.SubscriptionId, amlFileSystemId.ResourceGroupName, amlFileSystemId.AmlFilesystemName, model.Name) | ||
| if err := autoExportJobClient.CreateOrUpdateThenPoll(ctx, autoExportJobId, props); err != nil { | ||
| return fmt.Errorf("creating %s: %+v", id, err) | ||
| } | ||
|
|
||
| metadata.SetID(autoExportJobId) | ||
| return nil | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (r ManagedLustreFileSystemAutoExportJobResource) Read() sdk.ResourceFunc { | ||
| return sdk.ResourceFunc{ | ||
| Timeout: 5 * time.Minute, | ||
| Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
| client := metadata.Client.StorageCache.AutoExportJobs | ||
|
|
||
| id, err := autoexportjobs.ParseAutoExportJobID(metadata.ResourceData.Id()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| resp, err := client.Get(ctx, *id) | ||
| if err != nil { | ||
| if response.WasNotFound(resp.HttpResponse) { | ||
| return metadata.MarkAsGone(id) | ||
| } | ||
|
|
||
| return fmt.Errorf("retrieving %s: %+v", id, err) | ||
| } | ||
|
|
||
| state := ManagedLustreFileSystemAutoExportJobModel{} | ||
| if model := resp.Model; model != nil { | ||
| state.Name = id.AutoExportJobName | ||
| state.ManagedLustreFileSystemId = autoexportjob.NewAmlFilesystemID(id.SubscriptionId, id.ResourceGroupName, id.AmlFilesystemName).ID() | ||
| state.Location = location.Normalize(model.Location) | ||
| state.Tags = pointer.From(model.Tags) | ||
|
|
||
| if props := model.Properties; props != nil { | ||
| state.AutoExportPrefixes = pointer.From(props.AutoExportPrefixes) | ||
| state.AdminStatusEnabled = pointer.From(props.AdminStatus) == autoexportjobs.AutoExportJobAdminStatusEnable | ||
| } | ||
| } | ||
|
|
||
| return metadata.Encode(&state) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (r ManagedLustreFileSystemAutoExportJobResource) Delete() sdk.ResourceFunc { | ||
| return sdk.ResourceFunc{ | ||
| Timeout: 90 * time.Minute, | ||
| Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
| client := metadata.Client.StorageCache.AutoExportJobs | ||
| id, err := autoexportjobs.ParseAutoExportJobID(metadata.ResourceData.Id()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := client.DeleteThenPoll(ctx, *id); err != nil { | ||
| return fmt.Errorf("deleting %s: %+v", id, err) | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (r ManagedLustreFileSystemAutoExportJobResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { | ||
| return autoexportjob.ValidateAutoExportJobID | ||
| } | ||
|
|
||
| func (r ManagedLustreFileSystemAutoExportJobResource) Update() sdk.ResourceFunc { | ||
| return sdk.ResourceFunc{ | ||
| Timeout: 60 * time.Minute, | ||
| Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
| client := metadata.Client.StorageCache.AutoExportJob | ||
|
|
||
| id, err := autoexportjob.ParseAutoExportJobID(metadata.ResourceData.Id()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var model ManagedLustreFileSystemAutoExportJobModel | ||
| if err := metadata.Decode(&model); err != nil { | ||
| return fmt.Errorf("decoding: %w", err) | ||
| } | ||
|
|
||
| props := autoexportjob.AutoExportJob{ | ||
| Properties: pointer.To(autoexportjob.AutoExportJobProperties{}), | ||
| } | ||
|
|
||
| if metadata.ResourceData.HasChange("auto_export_prefixes") { | ||
| props.Properties.AutoExportPrefixes = pointer.To(model.AutoExportPrefixes) | ||
| } | ||
|
|
||
| if metadata.ResourceData.HasChange("admin_status_enabled") { | ||
| if model.AdminStatusEnabled { | ||
| props.Properties.AdminStatus = pointer.To(autoexportjob.AutoExportJobAdminStatusEnable) | ||
| } else { | ||
| props.Properties.AdminStatus = pointer.To(autoexportjob.AutoExportJobAdminStatusDisable) | ||
| } | ||
| } | ||
|
|
||
| if metadata.ResourceData.HasChange("tags") { | ||
| props.Tags = pointer.To(model.Tags) | ||
| } | ||
|
|
||
| if err := client.CreateOrUpdateThenPoll(ctx, *id, props); err != nil { | ||
| return fmt.Errorf("updating %s: %+v", id, err) | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
| } | ||
140 changes: 140 additions & 0 deletions
140
internal/services/storagecache/managed_lustre_file_system_auto_export_job_resource_test.go
This file contains hidden or 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,140 @@ | ||
| package storagecache_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/go-azure-helpers/lang/pointer" | ||
| "github.com/hashicorp/go-azure-sdk/resource-manager/storagecache/2024-07-01/autoexportjobs" | ||
| "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/tf/pluginsdk" | ||
| ) | ||
|
|
||
| type ManagedLustreFileSystemAutoExportJobResource struct{} | ||
|
|
||
| func (r ManagedLustreFileSystemAutoExportJobResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { | ||
| id, err := autoexportjobs.ParseAutoExportJobID(state.ID) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add some more tests here, at least a
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do. |
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| client := clients.StorageCache.AutoExportJobs | ||
| resp, err := client.Get(ctx, *id) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("retrieving %s: %+v", id, err) | ||
| } | ||
| return pointer.To(resp.Model != nil), nil | ||
| } | ||
|
|
||
| func TestAccManagedLustreFileSystemExportJob_basic(t *testing.T) { | ||
| data := acceptance.BuildTestData(t, "azurerm_managed_lustre_file_system_auto_export_job", "test") | ||
| r := ManagedLustreFileSystemAutoExportJobResource{} | ||
|
|
||
| data.ResourceTest(t, r, []acceptance.TestStep{ | ||
| { | ||
| Config: r.basic(data), | ||
| Check: acceptance.ComposeTestCheckFunc( | ||
| check.That(data.ResourceName).ExistsInAzure(r), | ||
| ), | ||
| }, | ||
| data.ImportStep(), | ||
| }) | ||
| } | ||
|
|
||
| func TestAccManagedLustreFileSystemExportJob_complete(t *testing.T) { | ||
| data := acceptance.BuildTestData(t, "azurerm_managed_lustre_file_system_auto_export_job", "test") | ||
| r := ManagedLustreFileSystemAutoExportJobResource{} | ||
|
|
||
| data.ResourceTest(t, r, []acceptance.TestStep{ | ||
| { | ||
| Config: r.complete(data), | ||
| Check: acceptance.ComposeTestCheckFunc( | ||
| check.That(data.ResourceName).ExistsInAzure(r), | ||
| ), | ||
| }, | ||
| data.ImportStep(), | ||
| }) | ||
| } | ||
|
|
||
| func TestAccManagedLustreFileSystemExportJob_update(t *testing.T) { | ||
| data := acceptance.BuildTestData(t, "azurerm_managed_lustre_file_system_auto_export_job", "test") | ||
| r := ManagedLustreFileSystemAutoExportJobResource{} | ||
|
|
||
| data.ResourceTest(t, r, []acceptance.TestStep{ | ||
| { | ||
| Config: r.basic(data), | ||
| Check: acceptance.ComposeTestCheckFunc( | ||
| check.That(data.ResourceName).ExistsInAzure(r), | ||
| ), | ||
| }, | ||
| data.ImportStep(), | ||
| { | ||
| Config: r.complete(data), | ||
| Check: acceptance.ComposeTestCheckFunc( | ||
| check.That(data.ResourceName).ExistsInAzure(r), | ||
| ), | ||
| }, | ||
| data.ImportStep(), | ||
| }) | ||
| } | ||
|
|
||
| func TestAccManagedLustreFileSystemExportJob_requiresImport(t *testing.T) { | ||
| data := acceptance.BuildTestData(t, "azurerm_managed_lustre_file_system_auto_export_job", "test") | ||
| r := ManagedLustreFileSystemAutoExportJobResource{} | ||
|
|
||
| data.ResourceTest(t, r, []acceptance.TestStep{ | ||
| { | ||
| Config: r.basic(data), | ||
| Check: acceptance.ComposeTestCheckFunc( | ||
| check.That(data.ResourceName).ExistsInAzure(r), | ||
| ), | ||
| }, | ||
| data.RequiresImportErrorStep(r.requiresImport), | ||
| }) | ||
| } | ||
|
|
||
| func (r ManagedLustreFileSystemAutoExportJobResource) basic(data acceptance.TestData) string { | ||
| return fmt.Sprintf(` | ||
| %s | ||
|
|
||
| resource "azurerm_managed_lustre_file_system_auto_export_job" "test" { | ||
| name = "acctest-amlfsaej-%d" | ||
| managed_lustre_file_system_id = azurerm_managed_lustre_file_system.test.id | ||
| location = azurerm_resource_group.test.location | ||
|
|
||
| auto_export_prefixes = ["/"] | ||
| } | ||
| `, ManagedLustreFileSystemResource{}.complete(data), data.RandomInteger) | ||
| } | ||
|
|
||
| func (r ManagedLustreFileSystemAutoExportJobResource) complete(data acceptance.TestData) string { | ||
| return fmt.Sprintf(` | ||
| %s | ||
|
|
||
| resource "azurerm_managed_lustre_file_system_auto_export_job" "test" { | ||
| name = "acctest-amlfsaej-%d" | ||
| managed_lustre_file_system_id = azurerm_managed_lustre_file_system.test.id | ||
| location = azurerm_resource_group.test.location | ||
|
|
||
| auto_export_prefixes = ["/", "/export"] | ||
| admin_status_enabled = false | ||
| } | ||
| `, ManagedLustreFileSystemResource{}.complete(data), data.RandomInteger) | ||
| } | ||
|
|
||
| func (r ManagedLustreFileSystemAutoExportJobResource) requiresImport(data acceptance.TestData) string { | ||
| return fmt.Sprintf(` | ||
| %s | ||
|
|
||
| resource "azurerm_managed_lustre_file_system_auto_export_job" "import" { | ||
| name = azurerm_managed_lustre_file_system_auto_export_job.test.name | ||
| managed_lustre_file_system_id = azurerm_managed_lustre_file_system_auto_export_job.test.managed_lustre_file_system_id | ||
| location = azurerm_managed_lustre_file_system_auto_export_job.test.location | ||
|
|
||
| auto_export_prefixes = azurerm_managed_lustre_file_system_auto_export_job.test.auto_export_prefixes | ||
| } | ||
| `, r.basic(data)) | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Number of maximum allowed paths for now is 1, so we should enforce that in the schema. Since this is an array of
blob paths/prefixeswhich defaults to/can we also add a better validation function here as well?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.
Will change.