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_lab_service_user #19957

Merged
merged 5 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/services/labservice/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package client
import (
"github.com/hashicorp/go-azure-sdk/resource-manager/labservices/2022-08-01/lab"
"github.com/hashicorp/go-azure-sdk/resource-manager/labservices/2022-08-01/labplan"
"github.com/hashicorp/go-azure-sdk/resource-manager/labservices/2022-08-01/user"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
LabClient *lab.LabClient
LabPlanClient *labplan.LabPlanClient
UserClient *user.UserClient
}

func NewClient(o *common.ClientOptions) *Client {
Expand All @@ -18,8 +20,12 @@ func NewClient(o *common.ClientOptions) *Client {
LabPlanClient := labplan.NewLabPlanClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&LabPlanClient.Client, o.ResourceManagerAuthorizer)

UserClient := user.NewUserClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&UserClient.Client, o.ResourceManagerAuthorizer)

return &Client{
LabClient: &LabClient,
LabPlanClient: &LabPlanClient,
UserClient: &UserClient,
}
}
226 changes: 226 additions & 0 deletions internal/services/labservice/lab_service_user_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
package labservice

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/labservices/2022-08-01/lab"
"github.com/hashicorp/go-azure-sdk/resource-manager/labservices/2022-08-01/user"
azValidate "github.com/hashicorp/terraform-provider-azurerm/helpers/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/labservice/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type LabServiceUserModel struct {
Name string `tfschema:"name"`
LabId string `tfschema:"lab_id"`
Email string `tfschema:"email"`
AdditionalUsageQuota string `tfschema:"additional_usage_quota"`
}

type LabServiceUserResource struct{}

var _ sdk.ResourceWithUpdate = LabServiceUserResource{}

func (r LabServiceUserResource) ResourceType() string {
return "azurerm_lab_service_user"
}

func (r LabServiceUserResource) ModelObject() interface{} {
return &LabServiceUserModel{}
}

func (r LabServiceUserResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return user.ValidateUserID
}

func (r LabServiceUserResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"lab_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: lab.ValidateLabID,
},

"email": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.Email,
},

"additional_usage_quota": {
Type: pluginsdk.TypeString,
Optional: true,
Default: "PT0S",
ValidateFunc: azValidate.ISO8601Duration,
},
}
}

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

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

client := metadata.Client.LabService.UserClient
labId, err := lab.ParseLabID(model.LabId)
if err != nil {
return err
}

id := user.NewUserID(labId.SubscriptionId, labId.ResourceGroupName, labId.LabName, 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 := &user.User{
Properties: user.UserProperties{
Email: model.Email,
},
}

if model.AdditionalUsageQuota != "" {
properties.Properties.AdditionalUsageQuota = &model.AdditionalUsageQuota
}

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

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

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

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

var model LabServiceUserModel
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)
}

props := resp.Model
if props == nil {
return fmt.Errorf("retrieving %s: properties was nil", id)
}

if metadata.ResourceData.HasChange("additional_usage_quota") {
props.Properties.AdditionalUsageQuota = utils.String(model.AdditionalUsageQuota)
}

if metadata.ResourceData.HasChange("email") {
props.Properties.Email = model.Email
}

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

return nil
},
}
}

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

id, err := user.ParseUserID(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 := LabServiceUserModel{
Name: id.UserName,
LabId: lab.NewLabID(id.SubscriptionId, id.ResourceGroupName, id.LabName).ID(),
}

properties := &model.Properties
state.Email = properties.Email

if properties.AdditionalUsageQuota != nil {
state.AdditionalUsageQuota = *properties.AdditionalUsageQuota
}

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

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

id, err := user.ParseUserID(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