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_resource_management_private_link_association #23546

Merged
merged 6 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions internal/services/resource/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/preview/resources/mgmt/2019-06-01-preview/templatespecs" // nolint: staticcheck
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-06-01/resources" // nolint: staticcheck
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/managementlocks"
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation"
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/resourcemanagementprivatelink"
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-10-01/deploymentscripts"
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2021-07-01/features"
Expand All @@ -22,6 +23,7 @@ type Client struct {
FeaturesClient *features.FeaturesClient
GroupsClient *resources.GroupsClient
LocksClient *managementlocks.ManagementLocksClient
PrivateLinkAssociationClient *privatelinkassociation.PrivateLinkAssociationClient
ResourceManagementPrivateLinkClient *resourcemanagementprivatelink.ResourceManagementPrivateLinkClient
ResourceProvidersClient *providers.ProvidersClient
ResourcesClient *resources.Client
Expand Down Expand Up @@ -56,6 +58,12 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
}
o.Configure(locksClient.Client, o.Authorizers.ResourceManager)

privateLinkAssociationClient, err := privatelinkassociation.NewPrivateLinkAssociationClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building PrivateLinkAssociation client: %+v", err)
}
o.Configure(privateLinkAssociationClient.Client, o.Authorizers.ResourceManager)

resourceManagementPrivateLinkClient, err := resourcemanagementprivatelink.NewResourceManagementPrivateLinkClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building ResourceManagementPrivateLink client: %+v", err)
Expand Down Expand Up @@ -83,6 +91,7 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
DeploymentScriptsClient: deploymentScriptsClient,
FeaturesClient: featuresClient,
LocksClient: locksClient,
PrivateLinkAssociationClient: privateLinkAssociationClient,
ResourceManagementPrivateLinkClient: resourceManagementPrivateLinkClient,
ResourceProvidersClient: resourceProvidersClient,
ResourcesClient: &resourcesClient,
Expand Down
1 change: 1 addition & 0 deletions internal/services/resource/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (r Registration) DataSources() []sdk.DataSource {
// Resources returns a list of Resources supported by this Service
func (r Registration) Resources() []sdk.Resource {
return []sdk.Resource{
ResourceManagementPrivateLinkAssociationResource{},
ResourceProviderRegistrationResource{},
ResourceManagementPrivateLinkResource{},
ResourceDeploymentScriptAzurePowerShellResource{},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
package resource

import (
"context"
"fmt"
"time"

"github.com/google/uuid"
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation"
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/resourcemanagementprivatelink"
"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"
)

var _ sdk.Resource = ResourceManagementPrivateLinkAssociationResource{}

type ResourceManagementPrivateLinkAssociationResource struct{}

func (r ResourceManagementPrivateLinkAssociationResource) ModelObject() interface{} {
return &ResourceManagementPrivateLinkAssociationResourceSchema{}
}

type ResourceManagementPrivateLinkAssociationResourceSchema struct {
catriona-m marked this conversation as resolved.
Show resolved Hide resolved
ManagementGroupId string `tfschema:"management_group_id"`
Name string `tfschema:"name"`
ResourceManagementPrivateLinkId string `tfschema:"resource_management_private_link_id"`
PublicNetworkAccessEnabled bool `tfschema:"public_network_access_enabled"`
TenantID string `tfschema:"tenant_id"`
}

func (r ResourceManagementPrivateLinkAssociationResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return privatelinkassociation.ValidatePrivateLinkAssociationID
}

func (r ResourceManagementPrivateLinkAssociationResource) ResourceType() string {
return "azurerm_resource_management_private_link_association"
}

func (r ResourceManagementPrivateLinkAssociationResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
ForceNew: true,
Optional: true,
Computed: true,
Type: pluginsdk.TypeString,
ValidateFunc: validation.IsUUID,
},
Copy link
Member

Choose a reason for hiding this comment

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

Given that this is only used as part of the resource id, would it make sense to remove this entirely and rely on the generated uuid here? In any case, as outlined in the contributors guide here we are no longer adding Optional and Computed to new fields.

Copy link
Contributor Author

@teowa teowa Oct 17, 2023

Choose a reason for hiding this comment

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

If remove name entirely, will get test error:

=== CONT  TestAccResourceManagementPrivateLinkAssociation_requiresImport
    testcase.go:113: Step 2/2, expected an error but got none

Copy link
Contributor Author

@teowa teowa Oct 20, 2023

Choose a reason for hiding this comment

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

Can I make name as required to solve this? but in this way user needs to manually set a uuid.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks @teowa. On second thought, we should set this to just Optional and add a note in the docs that the user should use ignore_changes on name if they want to use the generated uuid

"management_group_id": {
ForceNew: true,
Required: true,
Type: pluginsdk.TypeString,
ValidateFunc: commonids.ValidateManagementGroupID,
},
"resource_management_private_link_id": {
ForceNew: true,
Required: true,
Type: pluginsdk.TypeString,
ValidateFunc: resourcemanagementprivatelink.ValidateResourceManagementPrivateLinkID,
},
"public_network_access_enabled": {
ForceNew: true,
Required: true,
Type: pluginsdk.TypeBool,
},
}
}

func (r ResourceManagementPrivateLinkAssociationResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"tenant_id": {
Computed: true,
Type: pluginsdk.TypeString,
},
}
}

func (r ResourceManagementPrivateLinkAssociationResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Resource.PrivateLinkAssociationClient

var config ResourceManagementPrivateLinkAssociationResourceSchema
if err := metadata.Decode(&config); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

var name string
if config.Name != "" {
name = config.Name
}

if name == "" {
name = uuid.New().String()
}

managementGroupId, err := commonids.ParseManagementGroupID(config.ManagementGroupId)
if err != nil {
return fmt.Errorf("parse management group id: %+v", err)
Copy link
Member

@catriona-m catriona-m Oct 16, 2023

Choose a reason for hiding this comment

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

Suggested change
return fmt.Errorf("parse management group id: %+v", err)
return 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.

fixed, thanks.

}

id := privatelinkassociation.NewPrivateLinkAssociationID(managementGroupId.GroupId, name)

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

payload := privatelinkassociation.PrivateLinkAssociationObject{
Properties: &privatelinkassociation.PrivateLinkAssociationProperties{
PrivateLink: pointer.To(config.ResourceManagementPrivateLinkId),
PublicNetworkAccess: r.expandPublicNetworkAccess(config.PublicNetworkAccessEnabled),
},
}

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

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

func (r ResourceManagementPrivateLinkAssociationResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Resource.PrivateLinkAssociationClient
schema := ResourceManagementPrivateLinkAssociationResourceSchema{}

id, err := privatelinkassociation.ParsePrivateLinkAssociationID(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)
}

if model := resp.Model; model != nil {
schema.ManagementGroupId = commonids.NewManagementGroupID(id.GroupId).ID()
schema.Name = id.PlaId
if prop := model.Properties; prop != nil {
schema.PublicNetworkAccessEnabled = r.flattenPublicNetworkAccess(prop.PublicNetworkAccess)
schema.TenantID = pointer.From(prop.TenantID)
schema.ResourceManagementPrivateLinkId = pointer.From(prop.PrivateLink)
}
}

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

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

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

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

return nil
},
}
}

func (r ResourceManagementPrivateLinkAssociationResource) expandPublicNetworkAccess(input bool) *privatelinkassociation.PublicNetworkAccessOptions {
output := privatelinkassociation.PublicNetworkAccessOptionsEnabled
if !input {
output = privatelinkassociation.PublicNetworkAccessOptionsDisabled
}

return &output
}

func (r ResourceManagementPrivateLinkAssociationResource) flattenPublicNetworkAccess(input *privatelinkassociation.PublicNetworkAccessOptions) bool {
if input == nil || *input == privatelinkassociation.PublicNetworkAccessOptionsEnabled {
return true
}

return false
}
Loading