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_monitor_workspace #21598

Merged
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/monitor/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2022-06-01/datacollectionendpoints"
"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2022-06-01/datacollectionruleassociations"
"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2022-06-01/datacollectionrules"
"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2023-04-03/azuremonitorworkspaces"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

Expand Down Expand Up @@ -50,6 +51,7 @@ type Client struct {
PrivateLinkScopedResourcesClient *privatelinkscopedresources.PrivateLinkScopedResourcesClient
ScheduledQueryRulesClient *scheduledqueryrules2018.ScheduledQueryRulesClient
ScheduledQueryRulesV2Client *scheduledqueryrules.ScheduledQueryRulesClient
WorkspacesClient *azuremonitorworkspaces.AzureMonitorWorkspacesClient
}

func NewClient(o *common.ClientOptions) *Client {
Expand Down Expand Up @@ -113,6 +115,9 @@ func NewClient(o *common.ClientOptions) *Client {
ScheduledQueryRulesV2Client := scheduledqueryrules.NewScheduledQueryRulesClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&ScheduledQueryRulesV2Client.Client, o.ResourceManagerAuthorizer)

WorkspacesClient := azuremonitorworkspaces.NewAzureMonitorWorkspacesClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&WorkspacesClient.Client, o.ResourceManagerAuthorizer)

return &Client{
AADDiagnosticSettingsClient: &AADDiagnosticSettingsClient,
AutoscaleSettingsClient: &AutoscaleSettingsClient,
Expand All @@ -134,5 +139,6 @@ func NewClient(o *common.ClientOptions) *Client {
PrivateLinkScopedResourcesClient: &PrivateLinkScopedResourcesClient,
ScheduledQueryRulesClient: &ScheduledQueryRulesClient,
ScheduledQueryRulesV2Client: &ScheduledQueryRulesV2Client,
WorkspacesClient: &WorkspacesClient,
}
}
192 changes: 192 additions & 0 deletions internal/services/monitor/monitor_workspace_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package monitor

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"

"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/insights/2023-04-03/azuremonitorworkspaces"
"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 WorkspaceResourceModel struct {
Name string `tfschema:"name"`
ResourceGroupName string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
Tags map[string]string `tfschema:"tags"`
}

type WorkspaceResource struct{}

var _ sdk.ResourceWithUpdate = WorkspaceResource{}

func (r WorkspaceResource) ResourceType() string {
return "azurerm_monitor_workspace"
}

func (r WorkspaceResource) ModelObject() interface{} {
return &WorkspaceResourceModel{}
}

func (r WorkspaceResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return azuremonitorworkspaces.ValidateAccountID
}

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

"location": commonschema.Location(),

"tags": commonschema.Tags(),
}
}

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

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

client := metadata.Client.Monitor.WorkspacesClient
subscriptionId := metadata.Client.Account.SubscriptionId
id := azuremonitorworkspaces.NewAccountID(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 := azuremonitorworkspaces.AzureMonitorWorkspaceResource{
Location: location.Normalize(model.Location),
Properties: &azuremonitorworkspaces.AzureMonitorWorkspace{},
Tags: &model.Tags,
}

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

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

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

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

var model WorkspaceResourceModel
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 resp.Model == nil {
return fmt.Errorf("retrieving %s: model was nil", *id)
}

if metadata.ResourceData.HasChange("tags") {
resp.Model.Tags = pointer.To(model.Tags)
}

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

return nil
},
}
}

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

id, err := azuremonitorworkspaces.ParseAccountID(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 := WorkspaceResourceModel{
Name: id.AccountName,
ResourceGroupName: id.ResourceGroupName,
}

if model := resp.Model; model != nil {
state.Tags = pointer.From(model.Tags)
state.Location = location.Normalize(model.Location)
}

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

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

id, err := azuremonitorworkspaces.ParseAccountID(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