Skip to content

Commit

Permalink
New Resource : azurerm_storage_mover (#21000)
Browse files Browse the repository at this point in the history
Co-authored-by: kt <kt@katbyte.me>
  • Loading branch information
sinbai and katbyte authored Apr 1, 2023
1 parent ac610ca commit a69813b
Show file tree
Hide file tree
Showing 28 changed files with 1,339 additions and 0 deletions.
1 change: 1 addition & 0 deletions .teamcity/components/generated/services.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ var services = mapOf(
"signalr" to "SignalR",
"springcloud" to "Spring Cloud",
"storage" to "Storage",
"storagemover" to "Storage Mover",
"streamanalytics" to "Stream Analytics",
"subscription" to "Subscription",
"synapse" to "Synapse",
Expand Down
2 changes: 2 additions & 0 deletions .teamcity/components/settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ var serviceTestConfigurationOverrides = mapOf(
// SQL has quota available in certain locations
"sql" to testConfiguration(locationOverride = LocationConfiguration("westeurope", "francecentral", "eastus2", false), useDevTestSubscription = true),

"storagemover" to testConfiguration(locationOverride = LocationConfiguration("eastus", "eastus2", "westus3", false), useDevTestSubscription = true),

// StreamAnalytics has quota available in certain locations
"streamanalytics" to testConfiguration(locationOverride = LocationConfiguration("westeurope", "francecentral", "eastus2", false), useDevTestSubscription = true),

Expand Down
5 changes: 5 additions & 0 deletions internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ import (
appPlatform "github.com/hashicorp/terraform-provider-azurerm/internal/services/springcloud/client"
sql "github.com/hashicorp/terraform-provider-azurerm/internal/services/sql/client"
storage "github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/client"
storageMover "github.com/hashicorp/terraform-provider-azurerm/internal/services/storagemover/client"
streamAnalytics "github.com/hashicorp/terraform-provider-azurerm/internal/services/streamanalytics/client"
subscription "github.com/hashicorp/terraform-provider-azurerm/internal/services/subscription/client"
synapse "github.com/hashicorp/terraform-provider-azurerm/internal/services/synapse/client"
Expand Down Expand Up @@ -241,6 +242,7 @@ type Client struct {
ServiceFabricManaged *serviceFabricManaged.Client
SignalR *signalr.Client
Storage *storage.Client
StorageMover *storageMover.Client
StreamAnalytics *streamAnalytics.Client
Subscription *subscription.Client
Sql *sql.Client
Expand Down Expand Up @@ -387,6 +389,9 @@ func (client *Client) Build(ctx context.Context, o *common.ClientOptions) error
}
client.Sql = sql.NewClient(o)
client.Storage = storage.NewClient(o)
if client.StorageMover, err = storageMover.NewClient(o); err != nil {
return fmt.Errorf("building clients for StorageMover: %+v", err)
}
client.StreamAnalytics = streamAnalytics.NewClient(o)
client.Subscription = subscription.NewClient(o)
client.Synapse = synapse.NewClient(o)
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/services/springcloud"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/sql"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/storage"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/storagemover"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/streamanalytics"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/subscription"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/synapse"
Expand Down Expand Up @@ -165,6 +166,7 @@ func SupportedTypedServices() []sdk.TypedServiceRegistration {
serviceconnector.Registration{},
servicefabricmanaged.Registration{},
storage.Registration{},
storagemover.Registration{},
signalr.Registration{},
orbital.Registration{},
streamanalytics.Registration{},
Expand Down
1 change: 1 addition & 0 deletions internal/resourceproviders/required.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func Required() map[string]struct{} {
"Microsoft.ServiceFabric": {},
"Microsoft.Sql": {},
"Microsoft.Storage": {},
"Microsoft.StorageMover": {},
"Microsoft.StreamAnalytics": {},
"Microsoft.TimeSeriesInsights": {},
"Microsoft.Web": {},
Expand Down
24 changes: 24 additions & 0 deletions internal/services/storagemover/client/client.go
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
}
35 changes: 35 additions & 0 deletions internal/services/storagemover/registration.go
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 internal/services/storagemover/storage_mover_resource.go
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
},
}
}
Loading

0 comments on commit a69813b

Please sign in to comment.