Skip to content

Commit

Permalink
New Resource : azurerm_storage_mover_target_endpoint and `azurerm_s…
Browse files Browse the repository at this point in the history
…torage_mover_source_endpoint` (hashicorp#21449)
  • Loading branch information
sinbai authored Apr 20, 2023
1 parent fd5d264 commit 748b561
Show file tree
Hide file tree
Showing 27 changed files with 2,156 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/services/storagemover/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"fmt"

"github.com/hashicorp/go-azure-sdk/resource-manager/storagemover/2023-03-01/agents"
"github.com/hashicorp/go-azure-sdk/resource-manager/storagemover/2023-03-01/endpoints"
"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
AgentsClient *agents.AgentsClient
EndpointsClient *endpoints.EndpointsClient
}

func NewClient(o *common.ClientOptions) (*Client, error) {
Expand All @@ -26,8 +28,15 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
}
o.Configure(agentsClient.Client, o.Authorizers.ResourceManager)

endpointsClient, err := endpoints.NewEndpointsClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building endpoints client: %+v", err)
}
o.Configure(endpointsClient.Client, o.Authorizers.ResourceManager)

return &Client{
StorageMoversClient: storageMoversClient,
AgentsClient: agentsClient,
EndpointsClient: endpointsClient,
}, nil
}
2 changes: 2 additions & 0 deletions internal/services/storagemover/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ func (r Registration) Resources() []sdk.Resource {
return []sdk.Resource{
StorageMoverResource{},
StorageMoverAgentResource{},
StorageMoverSourceEndpointResource{},
StorageMoverTargetEndpointResource{},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
package storagemover

import (
"context"
"fmt"
"regexp"
"time"

"github.com/hashicorp/terraform-provider-azurerm/utils"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/storagemover/2023-03-01/endpoints"
"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 StorageMoverSourceEndpointModel struct {
Name string `tfschema:"name"`
StorageMoverId string `tfschema:"storage_mover_id"`
Export string `tfschema:"export"`
Host string `tfschema:"host"`
NfsVersion endpoints.NfsVersion `tfschema:"nfs_version"`
Description string `tfschema:"description"`
}

type StorageMoverSourceEndpointResource struct{}

var _ sdk.ResourceWithUpdate = StorageMoverSourceEndpointResource{}

func (r StorageMoverSourceEndpointResource) ResourceType() string {
return "azurerm_storage_mover_source_endpoint"
}

func (r StorageMoverSourceEndpointResource) ModelObject() interface{} {
return &StorageMoverSourceEndpointModel{}
}

func (r StorageMoverSourceEndpointResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return endpoints.ValidateEndpointID
}

func (r StorageMoverSourceEndpointResource) 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,63}$`),
`The name must be between 1 and 64 characters in length, begin with a letter or number, and may contain letters, numbers, dashes and underscore.`,
),
},

"storage_mover_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: storagemovers.ValidateStorageMoverID,
},

"host": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"export": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"nfs_version": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
Default: string(endpoints.NfsVersionNFSauto),
ValidateFunc: validation.StringInSlice([]string{
string(endpoints.NfsVersionNFSauto),
string(endpoints.NfsVersionNFSvFour),
string(endpoints.NfsVersionNFSvThree),
}, false),
},

"description": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
}
}

func (r StorageMoverSourceEndpointResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{}
}

func (r StorageMoverSourceEndpointResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var model StorageMoverSourceEndpointModel
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

client := metadata.Client.StorageMover.EndpointsClient
storageMoverId, err := storagemovers.ParseStorageMoverID(model.StorageMoverId)
if err != nil {
return err
}

id := endpoints.NewEndpointID(storageMoverId.SubscriptionId, storageMoverId.ResourceGroupName, storageMoverId.StorageMoverName, 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 := endpoints.Endpoint{
Properties: endpoints.NfsMountEndpointProperties{
Export: model.Export,
Host: model.Host,
NfsVersion: &model.NfsVersion,
},
}

if model.Description != "" {
if v, ok := properties.Properties.(endpoints.NfsMountEndpointProperties); ok {
v.Description = utils.String(model.Description)
properties.Properties = v
}

}

if _, err := client.CreateOrUpdate(ctx, id, properties); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

metadata.SetID(id)
return nil
},
}
}

func (r StorageMoverSourceEndpointResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.StorageMover.EndpointsClient

id, err := endpoints.ParseEndpointID(metadata.ResourceData.Id())
if err != nil {
return err
}

var model StorageMoverSourceEndpointModel
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") {
if v, ok := properties.Properties.(endpoints.NfsMountEndpointProperties); ok {
v.Description = utils.String(model.Description)
properties.Properties = v
}
}

if _, err := client.CreateOrUpdate(ctx, *id, *properties); err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
}

return nil
},
}
}

func (r StorageMoverSourceEndpointResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.StorageMover.EndpointsClient

id, err := endpoints.ParseEndpointID(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 := StorageMoverSourceEndpointModel{
Name: id.EndpointName,
StorageMoverId: storagemovers.NewStorageMoverID(id.SubscriptionId, id.ResourceGroupName, id.StorageMoverName).ID(),
}

if model := resp.Model; model != nil {
if v, ok := model.Properties.(endpoints.NfsMountEndpointProperties); ok {
state.Export = v.Export
state.Host = v.Host

if v := v.NfsVersion; v != nil {
state.NfsVersion = *v
}

des := ""
if v.Description != nil {
des = *v.Description
}
state.Description = des
}
}

return metadata.Encode(&state)
},
}
}

func (r StorageMoverSourceEndpointResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.StorageMover.EndpointsClient

id, err := endpoints.ParseEndpointID(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 748b561

Please sign in to comment.