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_dashboard_grafana_managed_private_endpoint #27781

Merged
merged 11 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
13 changes: 11 additions & 2 deletions internal/services/dashboard/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"fmt"

"github.com/hashicorp/go-azure-sdk/resource-manager/dashboard/2023-09-01/grafanaresource"
"github.com/hashicorp/go-azure-sdk/resource-manager/dashboard/2023-09-01/managedprivateendpoints"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
GrafanaResourceClient *grafanaresource.GrafanaResourceClient
GrafanaResourceClient *grafanaresource.GrafanaResourceClient
ManagedPrivateEndpointsClient *managedprivateendpoints.ManagedPrivateEndpointsClient
}

func NewClient(o *common.ClientOptions) (*Client, error) {
Expand All @@ -21,7 +23,14 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
}
o.Configure(grafanaResourceClient.Client, o.Authorizers.ResourceManager)

managedPrivateEndpointsClient, err := managedprivateendpoints.NewManagedPrivateEndpointsClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building ManagedPrivateEndpoints client: %+v", err)
}

o.Configure(managedPrivateEndpointsClient.Client, o.Authorizers.ResourceManager)
return &Client{
GrafanaResourceClient: grafanaResourceClient,
GrafanaResourceClient: grafanaResourceClient,
ManagedPrivateEndpointsClient: managedPrivateEndpointsClient,
}, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
package dashboard

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

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-sdk/resource-manager/dashboard/2023-09-01/managedprivateendpoints"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"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 ManagedPrivateEndpointResource struct{}

type ManagedPrivateEndpointModel struct {
Name string `tfschema:"name"`
Location string `tfschema:"location"`
ResourceGroup string `tfschema:"resource_group_name"`
GrafanaName string `tfschema:"grafana_name"`
djryanj marked this conversation as resolved.
Show resolved Hide resolved
PrivateLinkResourceId string `tfschema:"private_link_resource_id"`
PrivateLinkResourceRegion string `tfschema:"private_link_resource_region"`
Tags map[string]string `tfschema:"tags"`
GroupIds []string `tfschema:"group_ids"`
RequestMessage string `tfschema:"request_message"`
}

func (r ManagedPrivateEndpointResource) ModelObject() interface{} {
return &ManagedPrivateEndpointModel{}
}

func (r ManagedPrivateEndpointResource) ResourceType() string {
return "azurerm_dashboard_grafana_managed_private_endpoint"
}

func (r ManagedPrivateEndpointResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return managedprivateendpoints.ValidateManagedPrivateEndpointID
}

func (r ManagedPrivateEndpointResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(`\A([a-zA-Z]{1}[a-zA-Z0-9\-]{1,19}[a-zA-Z0-9]{1})\z`),
`Name length can only consist of alphanumeric characters or dashes, and must be between 2 and 20 characters long. It must begin with a letter and end with a letter or digit.`,
),
},

"resource_group_name": commonschema.ResourceGroupName(),
djryanj marked this conversation as resolved.
Show resolved Hide resolved

"location": commonschema.Location(),

"tags": commonschema.Tags(),

"grafana_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
djryanj marked this conversation as resolved.
Show resolved Hide resolved

"private_link_resource_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},

"group_ids": {
Type: pluginsdk.TypeList,
Optional: true,
ForceNew: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
// no validation here because an empty group id is valid for a generic private link resource
},

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

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

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

func (r ManagedPrivateEndpointResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var model ManagedPrivateEndpointModel
if err := metadata.Decode(&model); err != nil {
return err
}

client := metadata.Client.Dashboard.ManagedPrivateEndpointsClient
subscriptionId := metadata.Client.Account.SubscriptionId

id := managedprivateendpoints.NewManagedPrivateEndpointID(subscriptionId, model.ResourceGroup, model.GrafanaName, model.Name)

existing, err := client.Get(ctx, id)
if err != nil && !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
if !response.WasNotFound(existing.HttpResponse) {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

props := managedprivateendpoints.ManagedPrivateEndpointModel{
Location: model.Location,
djryanj marked this conversation as resolved.
Show resolved Hide resolved
Name: &model.Name,
Properties: &managedprivateendpoints.ManagedPrivateEndpointModelProperties{
GroupIds: &model.GroupIds,
PrivateLinkResourceId: &model.PrivateLinkResourceId,
PrivateLinkResourceRegion: &model.PrivateLinkResourceRegion,
RequestMessage: &model.RequestMessage,
},
Tags: &model.Tags,
}

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

metadata.SetID(id)

return nil
},
}
}

func (r ManagedPrivateEndpointResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Dashboard.ManagedPrivateEndpointsClient
id, err := managedprivateendpoints.ParseManagedPrivateEndpointID(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("reading %s: %+v", *id, err)
djryanj marked this conversation as resolved.
Show resolved Hide resolved
}

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

state := ManagedPrivateEndpointModel{
Name: id.ManagedPrivateEndpointName,
Location: model.Location,
GrafanaName: id.GrafanaName,
ResourceGroup: id.ResourceGroupName,
}

if props := model.Properties; props != nil {
if props.GroupIds != nil {
state.GroupIds = *props.GroupIds
}

if props.PrivateLinkResourceId != nil {
state.PrivateLinkResourceId = *props.PrivateLinkResourceId
}

if props.PrivateLinkResourceRegion != nil {
state.PrivateLinkResourceRegion = *props.PrivateLinkResourceRegion
}

if props.RequestMessage != nil {
state.RequestMessage = *props.RequestMessage
}
}

if model.Tags != nil {
state.Tags = *model.Tags
}
djryanj marked this conversation as resolved.
Show resolved Hide resolved

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

func (r ManagedPrivateEndpointResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Dashboard.ManagedPrivateEndpointsClient
id, err := managedprivateendpoints.ParseManagedPrivateEndpointID(metadata.ResourceData.Id())
if err != nil {
return err
}

metadata.Logger.Infof("deleting %s", *id)

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

return nil
},
}
}

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

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

var model ManagedPrivateEndpointModel
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: properties was nil", id)
}
djryanj marked this conversation as resolved.
Show resolved Hide resolved

if metadata.ResourceData.HasChange("tags") {
properties.Tags = &model.Tags
}

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

return nil
},
}
}
Loading
Loading