Skip to content
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

New Resource : azurerm_storage_mover_project #21477

Merged
merged 11 commits into from
Apr 21, 2023
9 changes: 9 additions & 0 deletions internal/services/storagemover/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"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/projects"
"github.com/hashicorp/go-azure-sdk/resource-manager/storagemover/2023-03-01/storagemovers"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)
Expand All @@ -13,6 +14,7 @@ type Client struct {
StorageMoversClient *storagemovers.StorageMoversClient
AgentsClient *agents.AgentsClient
EndpointsClient *endpoints.EndpointsClient
ProjectsClient *projects.ProjectsClient
}

func NewClient(o *common.ClientOptions) (*Client, error) {
Expand All @@ -34,9 +36,16 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
}
o.Configure(endpointsClient.Client, o.Authorizers.ResourceManager)

projectsClient, err := projects.NewProjectsClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building Projects client: %+v", err)
}
o.Configure(projectsClient.Client, o.Authorizers.ResourceManager)

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

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-sdk/resource-manager/storagemover/2023-03-01/projects"
"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 StorageMoverProjectResourceModel struct {
Name string `tfschema:"name"`
StorageMoverId string `tfschema:"storage_mover_id"`
Description string `tfschema:"description"`
}

type StorageMoverProjectResource struct{}

var _ sdk.ResourceWithUpdate = StorageMoverProjectResource{}

func (r StorageMoverProjectResource) ResourceType() string {
return "azurerm_storage_mover_project"
}

func (r StorageMoverProjectResource) ModelObject() interface{} {
return &StorageMoverProjectResourceModel{}
}

func (r StorageMoverProjectResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return projects.ValidateProjectID
}

func (r StorageMoverProjectResource) 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,
},

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

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

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

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

id := projects.NewProjectID(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 := &projects.Project{
Properties: &projects.ProjectProperties{},
}

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 StorageMoverProjectResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.StorageMover.ProjectsClient

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

var model StorageMoverProjectResourceModel
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 != nil {
properties.Properties.Description = pointer.To(model.Description)
}

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

return nil
},
}
}

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

id, err := projects.ParseProjectID(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 := StorageMoverProjectResourceModel{
Name: id.ProjectName,
StorageMoverId: storagemovers.NewStorageMoverID(id.SubscriptionId, id.ResourceGroupName, id.StorageMoverName).ID(),
}

if resp.Model != nil {
if properties := resp.Model.Properties; properties != nil {
state.Description = pointer.From(properties.Description)
}
}

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

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

id, err := projects.ParseProjectID(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