-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Resource :
azurerm_storage_mover
(#21000)
Co-authored-by: kt <kt@katbyte.me>
- Loading branch information
Showing
28 changed files
with
1,339 additions
and
0 deletions.
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
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
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,24 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/go-azure-sdk/resource-manager/storagemover/2023-03-01/storagemovers" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/common" | ||
) | ||
|
||
type Client struct { | ||
StorageMoversClient *storagemovers.StorageMoversClient | ||
} | ||
|
||
func NewClient(o *common.ClientOptions) (*Client, error) { | ||
storageMoversClient, err := storagemovers.NewStorageMoversClientWithBaseURI(o.Environment.ResourceManager) | ||
if err != nil { | ||
return nil, fmt.Errorf("building Storage Movers client: %+v", err) | ||
} | ||
o.Configure(storageMoversClient.Client, o.Authorizers.ResourceManager) | ||
|
||
return &Client{ | ||
StorageMoversClient: storageMoversClient, | ||
}, nil | ||
} |
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,35 @@ | ||
package storagemover | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk" | ||
) | ||
|
||
type Registration struct{} | ||
|
||
var ( | ||
_ sdk.TypedServiceRegistration = Registration{} | ||
) | ||
|
||
// Name is the name of this Service | ||
func (r Registration) Name() string { | ||
return "Storage Mover" | ||
} | ||
|
||
// WebsiteCategories returns a list of categories which can be used for the sidebar | ||
func (r Registration) WebsiteCategories() []string { | ||
return []string{ | ||
"Storage Mover", | ||
} | ||
} | ||
|
||
// DataSources returns a list of Data Sources supported by this Service | ||
func (r Registration) DataSources() []sdk.DataSource { | ||
return []sdk.DataSource{} | ||
} | ||
|
||
// Resources returns a list of Resources supported by this Service | ||
func (r Registration) Resources() []sdk.Resource { | ||
return []sdk.Resource{ | ||
StorageMoverResource{}, | ||
} | ||
} |
221 changes: 221 additions & 0 deletions
221
internal/services/storagemover/storage_mover_resource.go
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,221 @@ | ||
package storagemover | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"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/storagemover/2023-03-01/storagemovers" | ||
"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 StorageMoverModel struct { | ||
Name string `tfschema:"name"` | ||
ResourceGroupName string `tfschema:"resource_group_name"` | ||
Description string `tfschema:"description"` | ||
Location string `tfschema:"location"` | ||
Tags map[string]string `tfschema:"tags"` | ||
} | ||
|
||
type StorageMoverResource struct{} | ||
|
||
var _ sdk.ResourceWithUpdate = StorageMoverResource{} | ||
|
||
func (r StorageMoverResource) ResourceType() string { | ||
return "azurerm_storage_mover" | ||
} | ||
|
||
func (r StorageMoverResource) ModelObject() interface{} { | ||
return &StorageMoverModel{} | ||
} | ||
|
||
func (r StorageMoverResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { | ||
return storagemovers.ValidateStorageMoverID | ||
} | ||
|
||
func (r StorageMoverResource) Arguments() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{ | ||
"name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
|
||
"resource_group_name": commonschema.ResourceGroupName(), | ||
|
||
"description": { | ||
Type: pluginsdk.TypeString, | ||
Optional: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
|
||
"location": commonschema.Location(), | ||
|
||
"tags": commonschema.Tags(), | ||
} | ||
} | ||
|
||
func (r StorageMoverResource) Attributes() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{} | ||
} | ||
|
||
func (r StorageMoverResource) Create() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
var model StorageMoverModel | ||
if err := metadata.Decode(&model); err != nil { | ||
return fmt.Errorf("decoding: %+v", err) | ||
} | ||
|
||
client := metadata.Client.StorageMover.StorageMoversClient | ||
subscriptionId := metadata.Client.Account.SubscriptionId | ||
|
||
id := storagemovers.NewStorageMoverID(subscriptionId, model.ResourceGroupName, model.Name) | ||
|
||
existing, err := client.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) | ||
} | ||
|
||
properties := &storagemovers.StorageMover{ | ||
Location: location.Normalize(model.Location), | ||
Properties: &storagemovers.StorageMoverProperties{}, | ||
Tags: &model.Tags, | ||
} | ||
|
||
if model.Description != "" { | ||
properties.Properties.Description = &model.Description | ||
} | ||
|
||
if _, err := client.CreateOrUpdate(ctx, id, *properties); err != nil { | ||
return fmt.Errorf("creating %s: %+v", id, err) | ||
} | ||
|
||
metadata.SetID(id) | ||
|
||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func (r StorageMoverResource) Update() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.StorageMover.StorageMoversClient | ||
|
||
id, err := storagemovers.ParseStorageMoverID(metadata.ResourceData.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var model StorageMoverModel | ||
if err := metadata.Decode(&model); err != nil { | ||
return fmt.Errorf("decoding: %+v", err) | ||
} | ||
|
||
resp, err := client.Get(ctx, *id) | ||
if err != nil { | ||
return fmt.Errorf("retrieving %s: %+v", *id, err) | ||
} | ||
|
||
properties := resp.Model | ||
if properties == nil { | ||
return fmt.Errorf("retrieving %s: model was nil", *id) | ||
} | ||
|
||
if metadata.ResourceData.HasChange("description") { | ||
properties.Properties.Description = &model.Description | ||
} | ||
|
||
if metadata.ResourceData.HasChange("tags") { | ||
properties.Tags = &model.Tags | ||
} | ||
|
||
if _, err := client.CreateOrUpdate(ctx, *id, *properties); err != nil { | ||
return fmt.Errorf("updating %s: %+v", *id, err) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func (r StorageMoverResource) Read() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 5 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.StorageMover.StorageMoversClient | ||
|
||
id, err := storagemovers.ParseStorageMoverID(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) | ||
} | ||
|
||
model := resp.Model | ||
if model == nil { | ||
return fmt.Errorf("retrieving %s: model was nil", *id) | ||
} | ||
|
||
state := StorageMoverModel{ | ||
Name: id.StorageMoverName, | ||
ResourceGroupName: id.ResourceGroupName, | ||
Location: location.Normalize(model.Location), | ||
} | ||
|
||
description := "" | ||
if properties := model.Properties; properties != nil { | ||
if properties.Description != nil { | ||
description = *properties.Description | ||
} | ||
} | ||
state.Description = description | ||
|
||
if model.Tags != nil { | ||
state.Tags = *model.Tags | ||
} | ||
|
||
return metadata.Encode(&state) | ||
}, | ||
} | ||
} | ||
|
||
func (r StorageMoverResource) Delete() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.StorageMover.StorageMoversClient | ||
|
||
id, err := storagemovers.ParseStorageMoverID(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 | ||
}, | ||
} | ||
} |
Oops, something went wrong.