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 10 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,277 @@
package dashboard

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-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"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/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"`
GrafanaId string `tfschema:"grafana_id"`
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"`
}

type ManagedPrivateEndpointId struct {
SubscriptionId string
ResourceGroupName string
GrafanaName string
ManagedPrivateEndpointName string
}

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.`,
),
},

"location": commonschema.Location(),

"tags": commonschema.Tags(),

"grafana_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: grafanaresource.ValidateGrafanaID,
},

"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
grafanaId, err := grafanaresource.ParseGrafanaID(model.GrafanaId)
if err != nil {
return fmt.Errorf("parsing %q: %+v", model.GrafanaId, err)
djryanj marked this conversation as resolved.
Show resolved Hide resolved
}
id := managedprivateendpoints.NewManagedPrivateEndpointID(subscriptionId, grafanaId.ResourceGroupName, grafanaId.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: location.Normalize(model.Location),
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
}

grafanaId, err := GetGrafanaIdFromManagedPrivateEndpointId(id.ID())
if err != nil {
return err
}
djryanj marked this conversation as resolved.
Show resolved Hide resolved

state := ManagedPrivateEndpointModel{
Name: id.ManagedPrivateEndpointName,
GrafanaId: grafanaId.ID(),
}

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

if props := model.Properties; props != nil {
state.GroupIds = pointer.From(props.GroupIds)
state.PrivateLinkResourceId = pointer.From(props.PrivateLinkResourceId)
state.PrivateLinkResourceRegion = pointer.From(props.PrivateLinkResourceRegion)
state.RequestMessage = pointer.From(props.RequestMessage)
}
}

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)
}

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

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)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In nearly all cases the Model has a field called Properties which is why we follow the naming convention of model := resp.Model and props := model.Properties. For clarity and consistency can you please update this to

Suggested change
properties := resp.Model
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)
}
model := resp.Model
if metadata.ResourceData.HasChange("tags") {
model.Tags = &model.Tags
}
if err := client.CreateThenPoll(ctx, *id, *model); err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephybun I believe I have resolved this, but please double check; I had to modify the variable assigned to the model created in line 235. I hope this maintains consistency.


return nil
},
}
}

// GetGrafanaIdFromManagedPrivateEndpointId parses 'input' into a GrafanaId
func GetGrafanaIdFromManagedPrivateEndpointId(input string) (*grafanaresource.GrafanaId, error) {

mpe_id, err := managedprivateendpoints.ParseManagedPrivateEndpointID(input)

if err != nil {
return nil, fmt.Errorf("parsing %q: %+v", input, err)
}

id := grafanaresource.NewGrafanaID(mpe_id.SubscriptionId, mpe_id.ResourceGroupName, mpe_id.GrafanaName)

return &id, nil
}
djryanj marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading