forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
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_target_endpoint
and `azurerm_s…
…torage_mover_source_endpoint` (hashicorp#21449)
- Loading branch information
Showing
27 changed files
with
2,156 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
259 changes: 259 additions & 0 deletions
259
internal/services/storagemover/storage_mover_source_endpoint_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,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 | ||
}, | ||
} | ||
} |
Oops, something went wrong.