From 526971b888776643223d13053bf558c53ac8add0 Mon Sep 17 00:00:00 2001 From: teowa <104055472+teowa@users.noreply.github.com> Date: Fri, 13 Oct 2023 03:25:09 +0000 Subject: [PATCH 1/6] new resource private_link_association --- internal/services/resource/client/client.go | 9 + .../private_link_association_resource.go | 210 ++++++++++++++++++ .../private_link_association_resource_test.go | 120 ++++++++++ internal/services/resource/registration.go | 1 + .../privatelinkassociation/README.md | 89 ++++++++ .../privatelinkassociation/client.go | 26 +++ .../privatelinkassociation/constants.go | 51 +++++ .../id_privatelinkassociation.go | 116 ++++++++++ .../privatelinkassociation/method_delete.go | 47 ++++ .../privatelinkassociation/method_get.go | 51 +++++ .../privatelinkassociation/method_list.go | 53 +++++ .../privatelinkassociation/method_put.go | 56 +++++ .../model_privatelinkassociation.go | 11 + .../model_privatelinkassociationgetresult.go | 8 + .../model_privatelinkassociationobject.go | 8 + .../model_privatelinkassociationproperties.go | 9 + ...rivatelinkassociationpropertiesexpanded.go | 11 + .../privatelinkassociation/version.go | 12 + vendor/modules.txt | 1 + .../r/private_link_association.html.markdown | 81 +++++++ 20 files changed, 970 insertions(+) create mode 100644 internal/services/resource/private_link_association_resource.go create mode 100644 internal/services/resource/private_link_association_resource_test.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/README.md create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/client.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/constants.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/id_privatelinkassociation.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/method_delete.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/method_get.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/method_list.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/method_put.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociation.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociationgetresult.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociationobject.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociationproperties.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociationpropertiesexpanded.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/version.go create mode 100644 website/docs/r/private_link_association.html.markdown diff --git a/internal/services/resource/client/client.go b/internal/services/resource/client/client.go index 149b2c55076d..5f1cb279f283 100644 --- a/internal/services/resource/client/client.go +++ b/internal/services/resource/client/client.go @@ -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" @@ -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 @@ -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) @@ -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, diff --git a/internal/services/resource/private_link_association_resource.go b/internal/services/resource/private_link_association_resource.go new file mode 100644 index 000000000000..7f1831f25695 --- /dev/null +++ b/internal/services/resource/private_link_association_resource.go @@ -0,0 +1,210 @@ +package resource + +import ( + "context" + "fmt" + "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" + "time" +) + +var _ sdk.Resource = PrivateLinkAssociationResource{} + +type PrivateLinkAssociationResource struct{} + +func (r PrivateLinkAssociationResource) ModelObject() interface{} { + return &PrivateLinkAssociationResourceSchema{} +} + +type PrivateLinkAssociationResourceSchema struct { + ManagementGroupId string `tfschema:"management_group_id"` + Name string `tfschema:"name"` + PrivateLinkId string `tfschema:"private_link_id"` + PublicNetworkAccessEnabled bool `tfschema:"public_network_access_enabled"` + Scope string `tfschema:"scope"` + TenantID string `tfschema:"tenant_id"` +} + +func (r PrivateLinkAssociationResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { + return privatelinkassociation.ValidatePrivateLinkAssociationID +} + +func (r PrivateLinkAssociationResource) ResourceType() string { + return "azurerm_private_link_association" +} + +func (r PrivateLinkAssociationResource) Arguments() map[string]*pluginsdk.Schema { + return map[string]*pluginsdk.Schema{ + "name": { + ForceNew: true, + Optional: true, + Computed: true, + Type: pluginsdk.TypeString, + ValidateFunc: validation.IsUUID, + }, + "management_group_id": { + ForceNew: true, + Required: true, + Type: pluginsdk.TypeString, + ValidateFunc: commonids.ValidateManagementGroupID, + }, + "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 PrivateLinkAssociationResource) Attributes() map[string]*pluginsdk.Schema { + return map[string]*pluginsdk.Schema{ + "scope": { + Computed: true, + Type: pluginsdk.TypeString, + }, + "tenant_id": { + Computed: true, + Type: pluginsdk.TypeString, + }, + } +} + +func (r PrivateLinkAssociationResource) 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 PrivateLinkAssociationResourceSchema + 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) + } + + 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.PrivateLinkId), + 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 PrivateLinkAssociationResource) 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 := PrivateLinkAssociationResourceSchema{} + + 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.Scope = pointer.From(prop.Scope) + schema.TenantID = pointer.From(prop.TenantID) + schema.PrivateLinkId = pointer.From(prop.PrivateLink) + } + } + + return metadata.Encode(&schema) + }, + } +} + +func (r PrivateLinkAssociationResource) 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 PrivateLinkAssociationResource) expandPublicNetworkAccess(input bool) *privatelinkassociation.PublicNetworkAccessOptions { + output := privatelinkassociation.PublicNetworkAccessOptionsEnabled + if !input { + output = privatelinkassociation.PublicNetworkAccessOptionsDisabled + } + + return &output +} + +func (r PrivateLinkAssociationResource) flattenPublicNetworkAccess(input *privatelinkassociation.PublicNetworkAccessOptions) bool { + if input == nil || *input == privatelinkassociation.PublicNetworkAccessOptionsEnabled { + return true + } + + return false +} diff --git a/internal/services/resource/private_link_association_resource_test.go b/internal/services/resource/private_link_association_resource_test.go new file mode 100644 index 000000000000..69bbb0458627 --- /dev/null +++ b/internal/services/resource/private_link_association_resource_test.go @@ -0,0 +1,120 @@ +package resource_test + +import ( + "context" + "fmt" + "testing" + + "github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation" + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" + "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" + "github.com/hashicorp/terraform-provider-azurerm/utils" +) + +type PrivateLinkAssociationTestResource struct{} + +func TestAccPrivateLinkAssociation_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_private_link_association", "test") + r := PrivateLinkAssociationTestResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func TestAccPrivateLinkAssociation_requiresImport(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_private_link_association", "test") + r := PrivateLinkAssociationTestResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.RequiresImportErrorStep(r.requiresImport), + }) +} + +func (r PrivateLinkAssociationTestResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { + id, err := privatelinkassociation.ParsePrivateLinkAssociationID(state.ID) + if err != nil { + return nil, err + } + + resp, err := clients.Resource.PrivateLinkAssociationClient.Get(ctx, *id) + if err != nil { + return nil, fmt.Errorf("reading %s: %+v", *id, err) + } + + return utils.Bool(resp.Model != nil), nil +} + +func (r PrivateLinkAssociationTestResource) basic(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +resource "azurerm_private_link_association" "test" { + management_group_id = data.azurerm_management_group.test.id + private_link_id = azurerm_resource_management_private_link.test.id + public_network_access_enabled = true +} +`, r.template(data)) +} + +func (r PrivateLinkAssociationTestResource) requiresImport(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +resource "azurerm_private_link_association" "import" { + name = azurerm_private_link_association.test.name + management_group_id = azurerm_private_link_association.test.management_group_id + private_link_id = azurerm_private_link_association.test.private_link_id + public_network_access_enabled = azurerm_private_link_association.test.public_network_access_enabled +} +`, r.basic(data)) +} + +func (r PrivateLinkAssociationTestResource) template(data acceptance.TestData) string { + return fmt.Sprintf(` +variable "primary_location" { + default = %q +} +variable "random_string" { + default = %q +} +variable "random_integer" { + default = %d +} + +provider "azurerm" { + features {} +} + +data "azurerm_client_config" "test" {} + +data "azurerm_management_group" "test" { + name = data.azurerm_client_config.test.tenant_id +} + +resource "azurerm_resource_group" "test" { + name = "acctestrg-${var.random_integer}" + location = var.primary_location +} + +resource "azurerm_resource_management_private_link" "test" { + location = azurerm_resource_group.test.location + name = "acctestrmpl-${var.random_string}" + resource_group_name = azurerm_resource_group.test.name +} +`, data.Locations.Primary, data.RandomString, data.RandomInteger) +} diff --git a/internal/services/resource/registration.go b/internal/services/resource/registration.go index aaec2742104c..d3980a039c1c 100644 --- a/internal/services/resource/registration.go +++ b/internal/services/resource/registration.go @@ -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{ + PrivateLinkAssociationResource{}, ResourceProviderRegistrationResource{}, ResourceManagementPrivateLinkResource{}, ResourceDeploymentScriptAzurePowerShellResource{}, diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/README.md b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/README.md new file mode 100644 index 000000000000..a77321d1c848 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/README.md @@ -0,0 +1,89 @@ + +## `github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation` Documentation + +The `privatelinkassociation` SDK allows for interaction with the Azure Resource Manager Service `resources` (API Version `2020-05-01`). + +This readme covers example usages, but further information on [using this SDK can be found in the project root](https://github.com/hashicorp/go-azure-sdk/tree/main/docs). + +### Import Path + +```go +import "github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation" +``` + + +### Client Initialization + +```go +client := privatelinkassociation.NewPrivateLinkAssociationClientWithBaseURI("https://management.azure.com") +client.Client.Authorizer = authorizer +``` + + +### Example Usage: `PrivateLinkAssociationClient.Delete` + +```go +ctx := context.TODO() +id := privatelinkassociation.NewPrivateLinkAssociationID("groupIdValue", "plaIdValue") + +read, err := client.Delete(ctx, id) +if err != nil { + // handle the error +} +if model := read.Model; model != nil { + // do something with the model/response object +} +``` + + +### Example Usage: `PrivateLinkAssociationClient.Get` + +```go +ctx := context.TODO() +id := privatelinkassociation.NewPrivateLinkAssociationID("groupIdValue", "plaIdValue") + +read, err := client.Get(ctx, id) +if err != nil { + // handle the error +} +if model := read.Model; model != nil { + // do something with the model/response object +} +``` + + +### Example Usage: `PrivateLinkAssociationClient.List` + +```go +ctx := context.TODO() +id := privatelinkassociation.NewManagementGroupID("groupIdValue") + +read, err := client.List(ctx, id) +if err != nil { + // handle the error +} +if model := read.Model; model != nil { + // do something with the model/response object +} +``` + + +### Example Usage: `PrivateLinkAssociationClient.Put` + +```go +ctx := context.TODO() +id := privatelinkassociation.NewPrivateLinkAssociationID("groupIdValue", "plaIdValue") + +payload := privatelinkassociation.PrivateLinkAssociationObject{ + // ... +} + + +read, err := client.Put(ctx, id, payload) +if err != nil { + // handle the error +} +if model := read.Model; model != nil { + // do something with the model/response object +} +``` diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/client.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/client.go new file mode 100644 index 000000000000..7eda06e6d3d3 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/client.go @@ -0,0 +1,26 @@ +package privatelinkassociation + +import ( + "fmt" + + "github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager" + sdkEnv "github.com/hashicorp/go-azure-sdk/sdk/environments" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type PrivateLinkAssociationClient struct { + Client *resourcemanager.Client +} + +func NewPrivateLinkAssociationClientWithBaseURI(sdkApi sdkEnv.Api) (*PrivateLinkAssociationClient, error) { + client, err := resourcemanager.NewResourceManagerClient(sdkApi, "privatelinkassociation", defaultApiVersion) + if err != nil { + return nil, fmt.Errorf("instantiating PrivateLinkAssociationClient: %+v", err) + } + + return &PrivateLinkAssociationClient{ + Client: client, + }, nil +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/constants.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/constants.go new file mode 100644 index 000000000000..0b4150bc2204 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/constants.go @@ -0,0 +1,51 @@ +package privatelinkassociation + +import ( + "encoding/json" + "fmt" + "strings" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type PublicNetworkAccessOptions string + +const ( + PublicNetworkAccessOptionsDisabled PublicNetworkAccessOptions = "Disabled" + PublicNetworkAccessOptionsEnabled PublicNetworkAccessOptions = "Enabled" +) + +func PossibleValuesForPublicNetworkAccessOptions() []string { + return []string{ + string(PublicNetworkAccessOptionsDisabled), + string(PublicNetworkAccessOptionsEnabled), + } +} + +func (s *PublicNetworkAccessOptions) UnmarshalJSON(bytes []byte) error { + var decoded string + if err := json.Unmarshal(bytes, &decoded); err != nil { + return fmt.Errorf("unmarshaling: %+v", err) + } + out, err := parsePublicNetworkAccessOptions(decoded) + if err != nil { + return fmt.Errorf("parsing %q: %+v", decoded, err) + } + *s = *out + return nil +} + +func parsePublicNetworkAccessOptions(input string) (*PublicNetworkAccessOptions, error) { + vals := map[string]PublicNetworkAccessOptions{ + "disabled": PublicNetworkAccessOptionsDisabled, + "enabled": PublicNetworkAccessOptionsEnabled, + } + if v, ok := vals[strings.ToLower(input)]; ok { + return &v, nil + } + + // otherwise presume it's an undefined value and best-effort it + out := PublicNetworkAccessOptions(input) + return &out, nil +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/id_privatelinkassociation.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/id_privatelinkassociation.go new file mode 100644 index 000000000000..4b93814fbc4c --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/id_privatelinkassociation.go @@ -0,0 +1,116 @@ +package privatelinkassociation + +import ( + "fmt" + "strings" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +var _ resourceids.ResourceId = PrivateLinkAssociationId{} + +// PrivateLinkAssociationId is a struct representing the Resource ID for a Private Link Association +type PrivateLinkAssociationId struct { + GroupId string + PlaId string +} + +// NewPrivateLinkAssociationID returns a new PrivateLinkAssociationId struct +func NewPrivateLinkAssociationID(groupId string, plaId string) PrivateLinkAssociationId { + return PrivateLinkAssociationId{ + GroupId: groupId, + PlaId: plaId, + } +} + +// ParsePrivateLinkAssociationID parses 'input' into a PrivateLinkAssociationId +func ParsePrivateLinkAssociationID(input string) (*PrivateLinkAssociationId, error) { + parser := resourceids.NewParserFromResourceIdType(PrivateLinkAssociationId{}) + parsed, err := parser.Parse(input, false) + if err != nil { + return nil, fmt.Errorf("parsing %q: %+v", input, err) + } + + var ok bool + id := PrivateLinkAssociationId{} + + if id.GroupId, ok = parsed.Parsed["groupId"]; !ok { + return nil, resourceids.NewSegmentNotSpecifiedError(id, "groupId", *parsed) + } + + if id.PlaId, ok = parsed.Parsed["plaId"]; !ok { + return nil, resourceids.NewSegmentNotSpecifiedError(id, "plaId", *parsed) + } + + return &id, nil +} + +// ParsePrivateLinkAssociationIDInsensitively parses 'input' case-insensitively into a PrivateLinkAssociationId +// note: this method should only be used for API response data and not user input +func ParsePrivateLinkAssociationIDInsensitively(input string) (*PrivateLinkAssociationId, error) { + parser := resourceids.NewParserFromResourceIdType(PrivateLinkAssociationId{}) + parsed, err := parser.Parse(input, true) + if err != nil { + return nil, fmt.Errorf("parsing %q: %+v", input, err) + } + + var ok bool + id := PrivateLinkAssociationId{} + + if id.GroupId, ok = parsed.Parsed["groupId"]; !ok { + return nil, resourceids.NewSegmentNotSpecifiedError(id, "groupId", *parsed) + } + + if id.PlaId, ok = parsed.Parsed["plaId"]; !ok { + return nil, resourceids.NewSegmentNotSpecifiedError(id, "plaId", *parsed) + } + + return &id, nil +} + +// ValidatePrivateLinkAssociationID checks that 'input' can be parsed as a Private Link Association ID +func ValidatePrivateLinkAssociationID(input interface{}, key string) (warnings []string, errors []error) { + v, ok := input.(string) + if !ok { + errors = append(errors, fmt.Errorf("expected %q to be a string", key)) + return + } + + if _, err := ParsePrivateLinkAssociationID(v); err != nil { + errors = append(errors, err) + } + + return +} + +// ID returns the formatted Private Link Association ID +func (id PrivateLinkAssociationId) ID() string { + fmtString := "/providers/Microsoft.Management/managementGroups/%s/providers/Microsoft.Authorization/privateLinkAssociations/%s" + return fmt.Sprintf(fmtString, id.GroupId, id.PlaId) +} + +// Segments returns a slice of Resource ID Segments which comprise this Private Link Association ID +func (id PrivateLinkAssociationId) Segments() []resourceids.Segment { + return []resourceids.Segment{ + resourceids.StaticSegment("staticProviders", "providers", "providers"), + resourceids.ResourceProviderSegment("staticMicrosoftManagement", "Microsoft.Management", "Microsoft.Management"), + resourceids.StaticSegment("staticManagementGroups", "managementGroups", "managementGroups"), + resourceids.UserSpecifiedSegment("groupId", "groupIdValue"), + resourceids.StaticSegment("staticProviders2", "providers", "providers"), + resourceids.ResourceProviderSegment("staticMicrosoftAuthorization", "Microsoft.Authorization", "Microsoft.Authorization"), + resourceids.StaticSegment("staticPrivateLinkAssociations", "privateLinkAssociations", "privateLinkAssociations"), + resourceids.UserSpecifiedSegment("plaId", "plaIdValue"), + } +} + +// String returns a human-readable description of this Private Link Association ID +func (id PrivateLinkAssociationId) String() string { + components := []string{ + fmt.Sprintf("Group: %q", id.GroupId), + fmt.Sprintf("Pla: %q", id.PlaId), + } + return fmt.Sprintf("Private Link Association (%s)", strings.Join(components, "\n")) +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/method_delete.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/method_delete.go new file mode 100644 index 000000000000..1b27d0d39c08 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/method_delete.go @@ -0,0 +1,47 @@ +package privatelinkassociation + +import ( + "context" + "net/http" + + "github.com/hashicorp/go-azure-sdk/sdk/client" + "github.com/hashicorp/go-azure-sdk/sdk/odata" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type DeleteOperationResponse struct { + HttpResponse *http.Response + OData *odata.OData +} + +// Delete ... +func (c PrivateLinkAssociationClient) Delete(ctx context.Context, id PrivateLinkAssociationId) (result DeleteOperationResponse, err error) { + opts := client.RequestOptions{ + ContentType: "application/json; charset=utf-8", + ExpectedStatusCodes: []int{ + http.StatusNoContent, + http.StatusOK, + }, + HttpMethod: http.MethodDelete, + Path: id.ID(), + } + + req, err := c.Client.NewRequest(ctx, opts) + if err != nil { + return + } + + var resp *client.Response + resp, err = req.Execute(ctx) + if resp != nil { + result.OData = resp.OData + result.HttpResponse = resp.Response + } + if err != nil { + return + } + + return +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/method_get.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/method_get.go new file mode 100644 index 000000000000..74bfa7ef8381 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/method_get.go @@ -0,0 +1,51 @@ +package privatelinkassociation + +import ( + "context" + "net/http" + + "github.com/hashicorp/go-azure-sdk/sdk/client" + "github.com/hashicorp/go-azure-sdk/sdk/odata" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type GetOperationResponse struct { + HttpResponse *http.Response + OData *odata.OData + Model *PrivateLinkAssociation +} + +// Get ... +func (c PrivateLinkAssociationClient) Get(ctx context.Context, id PrivateLinkAssociationId) (result GetOperationResponse, err error) { + opts := client.RequestOptions{ + ContentType: "application/json; charset=utf-8", + ExpectedStatusCodes: []int{ + http.StatusOK, + }, + HttpMethod: http.MethodGet, + Path: id.ID(), + } + + req, err := c.Client.NewRequest(ctx, opts) + if err != nil { + return + } + + var resp *client.Response + resp, err = req.Execute(ctx) + if resp != nil { + result.OData = resp.OData + result.HttpResponse = resp.Response + } + if err != nil { + return + } + + if err = resp.Unmarshal(&result.Model); err != nil { + return + } + + return +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/method_list.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/method_list.go new file mode 100644 index 000000000000..0e98c75e06a8 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/method_list.go @@ -0,0 +1,53 @@ +package privatelinkassociation + +import ( + "context" + "fmt" + "net/http" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" + "github.com/hashicorp/go-azure-sdk/sdk/client" + "github.com/hashicorp/go-azure-sdk/sdk/odata" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type ListOperationResponse struct { + HttpResponse *http.Response + OData *odata.OData + Model *PrivateLinkAssociationGetResult +} + +// List ... +func (c PrivateLinkAssociationClient) List(ctx context.Context, id commonids.ManagementGroupId) (result ListOperationResponse, err error) { + opts := client.RequestOptions{ + ContentType: "application/json; charset=utf-8", + ExpectedStatusCodes: []int{ + http.StatusOK, + }, + HttpMethod: http.MethodGet, + Path: fmt.Sprintf("%s/providers/Microsoft.Authorization/privateLinkAssociations", id.ID()), + } + + req, err := c.Client.NewRequest(ctx, opts) + if err != nil { + return + } + + var resp *client.Response + resp, err = req.Execute(ctx) + if resp != nil { + result.OData = resp.OData + result.HttpResponse = resp.Response + } + if err != nil { + return + } + + if err = resp.Unmarshal(&result.Model); err != nil { + return + } + + return +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/method_put.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/method_put.go new file mode 100644 index 000000000000..1622db6c243f --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/method_put.go @@ -0,0 +1,56 @@ +package privatelinkassociation + +import ( + "context" + "net/http" + + "github.com/hashicorp/go-azure-sdk/sdk/client" + "github.com/hashicorp/go-azure-sdk/sdk/odata" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type PutOperationResponse struct { + HttpResponse *http.Response + OData *odata.OData + Model *PrivateLinkAssociation +} + +// Put ... +func (c PrivateLinkAssociationClient) Put(ctx context.Context, id PrivateLinkAssociationId, input PrivateLinkAssociationObject) (result PutOperationResponse, err error) { + opts := client.RequestOptions{ + ContentType: "application/json; charset=utf-8", + ExpectedStatusCodes: []int{ + http.StatusCreated, + http.StatusOK, + }, + HttpMethod: http.MethodPut, + Path: id.ID(), + } + + req, err := c.Client.NewRequest(ctx, opts) + if err != nil { + return + } + + if err = req.Marshal(input); err != nil { + return + } + + var resp *client.Response + resp, err = req.Execute(ctx) + if resp != nil { + result.OData = resp.OData + result.HttpResponse = resp.Response + } + if err != nil { + return + } + + if err = resp.Unmarshal(&result.Model); err != nil { + return + } + + return +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociation.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociation.go new file mode 100644 index 000000000000..ffb33c738b46 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociation.go @@ -0,0 +1,11 @@ +package privatelinkassociation + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type PrivateLinkAssociation struct { + Id *string `json:"id,omitempty"` + Name *string `json:"name,omitempty"` + Properties *PrivateLinkAssociationPropertiesExpanded `json:"properties,omitempty"` + Type *string `json:"type,omitempty"` +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociationgetresult.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociationgetresult.go new file mode 100644 index 000000000000..a196acfb9c0e --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociationgetresult.go @@ -0,0 +1,8 @@ +package privatelinkassociation + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type PrivateLinkAssociationGetResult struct { + Value *[]PrivateLinkAssociation `json:"value,omitempty"` +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociationobject.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociationobject.go new file mode 100644 index 000000000000..a9c635335a53 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociationobject.go @@ -0,0 +1,8 @@ +package privatelinkassociation + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type PrivateLinkAssociationObject struct { + Properties *PrivateLinkAssociationProperties `json:"properties,omitempty"` +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociationproperties.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociationproperties.go new file mode 100644 index 000000000000..b63c5d5cebbe --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociationproperties.go @@ -0,0 +1,9 @@ +package privatelinkassociation + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type PrivateLinkAssociationProperties struct { + PrivateLink *string `json:"privateLink,omitempty"` + PublicNetworkAccess *PublicNetworkAccessOptions `json:"publicNetworkAccess,omitempty"` +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociationpropertiesexpanded.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociationpropertiesexpanded.go new file mode 100644 index 000000000000..2f25e217f9d5 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/model_privatelinkassociationpropertiesexpanded.go @@ -0,0 +1,11 @@ +package privatelinkassociation + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type PrivateLinkAssociationPropertiesExpanded struct { + PrivateLink *string `json:"privateLink,omitempty"` + PublicNetworkAccess *PublicNetworkAccessOptions `json:"publicNetworkAccess,omitempty"` + Scope *string `json:"scope,omitempty"` + TenantID *string `json:"tenantID,omitempty"` +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/version.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/version.go new file mode 100644 index 000000000000..91aa6cdb35fc --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation/version.go @@ -0,0 +1,12 @@ +package privatelinkassociation + +import "fmt" + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +const defaultApiVersion = "2020-05-01" + +func userAgent() string { + return fmt.Sprintf("hashicorp/go-azure-sdk/privatelinkassociation/%s", defaultApiVersion) +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 6e45f4273d68..f6a7b5b6cd40 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -836,6 +836,7 @@ github.com/hashicorp/go-azure-sdk/resource-manager/relay/2021-11-01/hybridconnec github.com/hashicorp/go-azure-sdk/resource-manager/relay/2021-11-01/namespaces github.com/hashicorp/go-azure-sdk/resource-manager/resourceconnector/2022-10-27/appliances 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 diff --git a/website/docs/r/private_link_association.html.markdown b/website/docs/r/private_link_association.html.markdown new file mode 100644 index 000000000000..12a9b4073ae5 --- /dev/null +++ b/website/docs/r/private_link_association.html.markdown @@ -0,0 +1,81 @@ +--- +subcategory: "Management" +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_private_link_association" +description: |- + Manages a Private Link Association. +--- + +# azurerm_private_link_association + +Manages a Private Link Association. + +## Example Usage + +```hcl +data "azurerm_client_config" "example" {} + +data "azurerm_management_group" "example" { + name = data.azurerm_client_config.example.tenant_id +} + +resource "azurerm_resource_group" "example" { + name = "example" + location = "West Europe" +} + +resource "azurerm_resource_management_private_link" "example" { + name = "example" + resource_group_name = azurerm_resource_group.example.name + location = azurerm_resource_group.example.location +} + +resource "azurerm_private_link_association" "example" { + management_group_id = azurerm_management_group.example.id + private_link_id = azurerm_resource_management_private_link.example.id + public_network_access_enabled = true +} + +``` + +## Arguments Reference + +The following arguments are supported: + +* `management_group_id` - (Required) Specifies the Management Group ID within which this Private Link Association should exist. Changing this forces a new Private Link Association to be created. + +**Note:** For now, `management_group_id` must be the ID of [Root Management Group](https://learn.microsoft.com/en-us/azure/governance/management-groups/overview#root-management-group-for-each-directory). + +* `private_link_id` - (Required) The Resource ID of Resource Management Private Link. Changing this forces a new Private Link Association to be created. + +* `public_network_access_enabled` - (Required) Whether public network access is allowed. Changing this forces a new Private Link Association to be created. + +* `name` - (Optional) Specifies the name of this Private Link Association, which should be a UUID. A new UUID will be generated if not provided. Changing this forces a new Private Link Association to be created. + +## Attributes Reference + +In addition to the Arguments listed above - the following Attributes are exported: + +* `id` - The ID of the Private Link Association. + +* `scope` - The scope of the private link association. + +* `tenant_id` - The TenantID. + +--- + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: + +* `create` - (Defaults to 30 minutes) Used when creating this Private Link Association. +* `delete` - (Defaults to 30 minutes) Used when deleting this Private Link Association. +* `read` - (Defaults to 5 minutes) Used when retrieving this Private Link Association. + +## Import + +An existing Private Link Association can be imported into Terraform using the `resource id`, e.g. + +```shell +terraform import azurerm_private_link_association.example /providers/Microsoft.Management/managementGroups/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/privateLinkAssociations/00000000-0000-0000-0000-000000000000 +``` From 173eca87b1b2cee14bb3251198836c2a76e899eb Mon Sep 17 00:00:00 2001 From: teowa <104055472+teowa@users.noreply.github.com> Date: Fri, 13 Oct 2023 06:14:57 +0000 Subject: [PATCH 2/6] new resource azurerm_resource_management_private_link_association --- .../private_link_association_resource_test.go | 120 -------------- internal/services/resource/registration.go | 2 +- ...ment_private_link_association_resource.go} | 87 ++++++---- ..._private_link_association_resource_test.go | 152 ++++++++++++++++++ ...nt_private_link_association.html.markdown} | 24 ++- 5 files changed, 219 insertions(+), 166 deletions(-) delete mode 100644 internal/services/resource/private_link_association_resource_test.go rename internal/services/resource/{private_link_association_resource.go => resource_management_private_link_association_resource.go} (62%) create mode 100644 internal/services/resource/resource_management_private_link_association_resource_test.go rename website/docs/r/{private_link_association.html.markdown => resource_management_private_link_association.html.markdown} (67%) diff --git a/internal/services/resource/private_link_association_resource_test.go b/internal/services/resource/private_link_association_resource_test.go deleted file mode 100644 index 69bbb0458627..000000000000 --- a/internal/services/resource/private_link_association_resource_test.go +++ /dev/null @@ -1,120 +0,0 @@ -package resource_test - -import ( - "context" - "fmt" - "testing" - - "github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation" - "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" - "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" - "github.com/hashicorp/terraform-provider-azurerm/internal/clients" - "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" - "github.com/hashicorp/terraform-provider-azurerm/utils" -) - -type PrivateLinkAssociationTestResource struct{} - -func TestAccPrivateLinkAssociation_basic(t *testing.T) { - data := acceptance.BuildTestData(t, "azurerm_private_link_association", "test") - r := PrivateLinkAssociationTestResource{} - - data.ResourceTest(t, r, []acceptance.TestStep{ - { - Config: r.basic(data), - Check: acceptance.ComposeTestCheckFunc( - check.That(data.ResourceName).ExistsInAzure(r), - ), - }, - data.ImportStep(), - }) -} - -func TestAccPrivateLinkAssociation_requiresImport(t *testing.T) { - data := acceptance.BuildTestData(t, "azurerm_private_link_association", "test") - r := PrivateLinkAssociationTestResource{} - - data.ResourceTest(t, r, []acceptance.TestStep{ - { - Config: r.basic(data), - Check: acceptance.ComposeTestCheckFunc( - check.That(data.ResourceName).ExistsInAzure(r), - ), - }, - data.RequiresImportErrorStep(r.requiresImport), - }) -} - -func (r PrivateLinkAssociationTestResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := privatelinkassociation.ParsePrivateLinkAssociationID(state.ID) - if err != nil { - return nil, err - } - - resp, err := clients.Resource.PrivateLinkAssociationClient.Get(ctx, *id) - if err != nil { - return nil, fmt.Errorf("reading %s: %+v", *id, err) - } - - return utils.Bool(resp.Model != nil), nil -} - -func (r PrivateLinkAssociationTestResource) basic(data acceptance.TestData) string { - return fmt.Sprintf(` -%s - -resource "azurerm_private_link_association" "test" { - management_group_id = data.azurerm_management_group.test.id - private_link_id = azurerm_resource_management_private_link.test.id - public_network_access_enabled = true -} -`, r.template(data)) -} - -func (r PrivateLinkAssociationTestResource) requiresImport(data acceptance.TestData) string { - return fmt.Sprintf(` -%s - -resource "azurerm_private_link_association" "import" { - name = azurerm_private_link_association.test.name - management_group_id = azurerm_private_link_association.test.management_group_id - private_link_id = azurerm_private_link_association.test.private_link_id - public_network_access_enabled = azurerm_private_link_association.test.public_network_access_enabled -} -`, r.basic(data)) -} - -func (r PrivateLinkAssociationTestResource) template(data acceptance.TestData) string { - return fmt.Sprintf(` -variable "primary_location" { - default = %q -} -variable "random_string" { - default = %q -} -variable "random_integer" { - default = %d -} - -provider "azurerm" { - features {} -} - -data "azurerm_client_config" "test" {} - -data "azurerm_management_group" "test" { - name = data.azurerm_client_config.test.tenant_id -} - -resource "azurerm_resource_group" "test" { - name = "acctestrg-${var.random_integer}" - location = var.primary_location -} - -resource "azurerm_resource_management_private_link" "test" { - location = azurerm_resource_group.test.location - name = "acctestrmpl-${var.random_string}" - resource_group_name = azurerm_resource_group.test.name -} -`, data.Locations.Primary, data.RandomString, data.RandomInteger) -} diff --git a/internal/services/resource/registration.go b/internal/services/resource/registration.go index d3980a039c1c..1a8afa890f16 100644 --- a/internal/services/resource/registration.go +++ b/internal/services/resource/registration.go @@ -63,7 +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{ - PrivateLinkAssociationResource{}, + ResourceManagementPrivateLinkAssociationResource{}, ResourceProviderRegistrationResource{}, ResourceManagementPrivateLinkResource{}, ResourceDeploymentScriptAzurePowerShellResource{}, diff --git a/internal/services/resource/private_link_association_resource.go b/internal/services/resource/resource_management_private_link_association_resource.go similarity index 62% rename from internal/services/resource/private_link_association_resource.go rename to internal/services/resource/resource_management_private_link_association_resource.go index 7f1831f25695..4f5a3f7dd00f 100644 --- a/internal/services/resource/private_link_association_resource.go +++ b/internal/services/resource/resource_management_private_link_association_resource.go @@ -3,6 +3,8 @@ 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" @@ -12,35 +14,33 @@ import ( "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" - "time" ) -var _ sdk.Resource = PrivateLinkAssociationResource{} +var _ sdk.Resource = ResourceManagementPrivateLinkAssociationResource{} -type PrivateLinkAssociationResource struct{} +type ResourceManagementPrivateLinkAssociationResource struct{} -func (r PrivateLinkAssociationResource) ModelObject() interface{} { - return &PrivateLinkAssociationResourceSchema{} +func (r ResourceManagementPrivateLinkAssociationResource) ModelObject() interface{} { + return &ResourceManagementPrivateLinkAssociationResourceSchema{} } -type PrivateLinkAssociationResourceSchema struct { - ManagementGroupId string `tfschema:"management_group_id"` - Name string `tfschema:"name"` - PrivateLinkId string `tfschema:"private_link_id"` - PublicNetworkAccessEnabled bool `tfschema:"public_network_access_enabled"` - Scope string `tfschema:"scope"` - TenantID string `tfschema:"tenant_id"` +type ResourceManagementPrivateLinkAssociationResourceSchema struct { + 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 PrivateLinkAssociationResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { +func (r ResourceManagementPrivateLinkAssociationResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { return privatelinkassociation.ValidatePrivateLinkAssociationID } -func (r PrivateLinkAssociationResource) ResourceType() string { - return "azurerm_private_link_association" +func (r ResourceManagementPrivateLinkAssociationResource) ResourceType() string { + return "azurerm_resource_management_private_link_association" } -func (r PrivateLinkAssociationResource) Arguments() map[string]*pluginsdk.Schema { +func (r ResourceManagementPrivateLinkAssociationResource) Arguments() map[string]*pluginsdk.Schema { return map[string]*pluginsdk.Schema{ "name": { ForceNew: true, @@ -55,7 +55,7 @@ func (r PrivateLinkAssociationResource) Arguments() map[string]*pluginsdk.Schema Type: pluginsdk.TypeString, ValidateFunc: commonids.ValidateManagementGroupID, }, - "private_link_id": { + "resource_management_private_link_id": { ForceNew: true, Required: true, Type: pluginsdk.TypeString, @@ -69,12 +69,8 @@ func (r PrivateLinkAssociationResource) Arguments() map[string]*pluginsdk.Schema } } -func (r PrivateLinkAssociationResource) Attributes() map[string]*pluginsdk.Schema { +func (r ResourceManagementPrivateLinkAssociationResource) Attributes() map[string]*pluginsdk.Schema { return map[string]*pluginsdk.Schema{ - "scope": { - Computed: true, - Type: pluginsdk.TypeString, - }, "tenant_id": { Computed: true, Type: pluginsdk.TypeString, @@ -82,13 +78,13 @@ func (r PrivateLinkAssociationResource) Attributes() map[string]*pluginsdk.Schem } } -func (r PrivateLinkAssociationResource) Create() sdk.ResourceFunc { +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 PrivateLinkAssociationResourceSchema + var config ResourceManagementPrivateLinkAssociationResourceSchema if err := metadata.Decode(&config); err != nil { return fmt.Errorf("decoding: %+v", err) } @@ -121,7 +117,7 @@ func (r PrivateLinkAssociationResource) Create() sdk.ResourceFunc { payload := privatelinkassociation.PrivateLinkAssociationObject{ Properties: &privatelinkassociation.PrivateLinkAssociationProperties{ - PrivateLink: pointer.To(config.PrivateLinkId), + PrivateLink: pointer.To(config.ResourceManagementPrivateLinkId), PublicNetworkAccess: r.expandPublicNetworkAccess(config.PublicNetworkAccessEnabled), }, } @@ -136,12 +132,12 @@ func (r PrivateLinkAssociationResource) Create() sdk.ResourceFunc { } } -func (r PrivateLinkAssociationResource) Read() sdk.ResourceFunc { +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 := PrivateLinkAssociationResourceSchema{} + schema := ResourceManagementPrivateLinkAssociationResourceSchema{} id, err := privatelinkassociation.ParsePrivateLinkAssociationID(metadata.ResourceData.Id()) if err != nil { @@ -161,9 +157,8 @@ func (r PrivateLinkAssociationResource) Read() sdk.ResourceFunc { schema.Name = id.PlaId if prop := model.Properties; prop != nil { schema.PublicNetworkAccessEnabled = r.flattenPublicNetworkAccess(prop.PublicNetworkAccess) - schema.Scope = pointer.From(prop.Scope) schema.TenantID = pointer.From(prop.TenantID) - schema.PrivateLinkId = pointer.From(prop.PrivateLink) + schema.ResourceManagementPrivateLinkId = pointer.From(prop.PrivateLink) } } @@ -172,7 +167,7 @@ func (r PrivateLinkAssociationResource) Read() sdk.ResourceFunc { } } -func (r PrivateLinkAssociationResource) Delete() sdk.ResourceFunc { +func (r ResourceManagementPrivateLinkAssociationResource) Delete() sdk.ResourceFunc { return sdk.ResourceFunc{ Timeout: 30 * time.Minute, Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { @@ -187,12 +182,40 @@ func (r PrivateLinkAssociationResource) Delete() sdk.ResourceFunc { return fmt.Errorf("deleting %s: %+v", *id, err) } + deadline, ok := ctx.Deadline() + if !ok { + return fmt.Errorf("internal-error: context had no deadline") + } + + state := &pluginsdk.StateChangeConf{ + Delay: 10 * time.Second, + Pending: []string{"Found"}, + Target: []string{"NotFound"}, + Timeout: time.Until(deadline), + Refresh: func() (interface{}, string, error) { + resp, err := client.Get(ctx, *id) + if err != nil { + if response.WasNotFound(resp.HttpResponse) { + return resp, "NotFound", nil + } + + return resp, "Error", fmt.Errorf("making Read request on %s: %+v", *id, err) + } + + return resp, "Found", nil + }, + } + + if _, err := state.WaitForStateContext(ctx); err != nil { + return fmt.Errorf("waiting for the Deployment %s: %+v", *id, err) + } + return nil }, } } -func (r PrivateLinkAssociationResource) expandPublicNetworkAccess(input bool) *privatelinkassociation.PublicNetworkAccessOptions { +func (r ResourceManagementPrivateLinkAssociationResource) expandPublicNetworkAccess(input bool) *privatelinkassociation.PublicNetworkAccessOptions { output := privatelinkassociation.PublicNetworkAccessOptionsEnabled if !input { output = privatelinkassociation.PublicNetworkAccessOptionsDisabled @@ -201,7 +224,7 @@ func (r PrivateLinkAssociationResource) expandPublicNetworkAccess(input bool) *p return &output } -func (r PrivateLinkAssociationResource) flattenPublicNetworkAccess(input *privatelinkassociation.PublicNetworkAccessOptions) bool { +func (r ResourceManagementPrivateLinkAssociationResource) flattenPublicNetworkAccess(input *privatelinkassociation.PublicNetworkAccessOptions) bool { if input == nil || *input == privatelinkassociation.PublicNetworkAccessOptionsEnabled { return true } diff --git a/internal/services/resource/resource_management_private_link_association_resource_test.go b/internal/services/resource/resource_management_private_link_association_resource_test.go new file mode 100644 index 000000000000..54c1f61dd4ba --- /dev/null +++ b/internal/services/resource/resource_management_private_link_association_resource_test.go @@ -0,0 +1,152 @@ +package resource_test + +import ( + "context" + "fmt" + "testing" + + "github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation" + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" + "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" + "github.com/hashicorp/terraform-provider-azurerm/utils" +) + +type ResourceManagementPrivateLinkAssociationTestResource struct{} + +func TestAccResourceManagementPrivateLinkAssociation_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_resource_management_private_link_association", "test") + r := ResourceManagementPrivateLinkAssociationTestResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func TestAccResourceManagementPrivateLinkAssociation_name(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_resource_management_private_link_association", "test") + r := ResourceManagementPrivateLinkAssociationTestResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.name(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func TestAccResourceManagementPrivateLinkAssociation_requiresImport(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_resource_management_private_link_association", "test") + r := ResourceManagementPrivateLinkAssociationTestResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.RequiresImportErrorStep(r.requiresImport), + }) +} + +func (r ResourceManagementPrivateLinkAssociationTestResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { + id, err := privatelinkassociation.ParsePrivateLinkAssociationID(state.ID) + if err != nil { + return nil, err + } + + resp, err := clients.Resource.PrivateLinkAssociationClient.Get(ctx, *id) + if err != nil { + return nil, fmt.Errorf("reading %s: %+v", *id, err) + } + + return utils.Bool(resp.Model != nil), nil +} + +func (r ResourceManagementPrivateLinkAssociationTestResource) basic(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +%s + +resource "azurerm_resource_management_private_link_association" "test" { + management_group_id = data.azurerm_management_group.test.id + resource_management_private_link_id = azurerm_resource_management_private_link.test.id + public_network_access_enabled = true +} +`, r.template(data)) +} + +func (r ResourceManagementPrivateLinkAssociationTestResource) name(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +%s + +resource "azurerm_resource_management_private_link_association" "test" { + name = "12345678-1234-1234-1234-123456789012" + management_group_id = data.azurerm_management_group.test.id + resource_management_private_link_id = azurerm_resource_management_private_link.test.id + public_network_access_enabled = true +} +`, r.template(data)) +} + +func (r ResourceManagementPrivateLinkAssociationTestResource) requiresImport(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +resource "azurerm_resource_management_private_link_association" "import" { + name = azurerm_resource_management_private_link_association.test.name + management_group_id = azurerm_resource_management_private_link_association.test.management_group_id + resource_management_private_link_id = azurerm_resource_management_private_link_association.test.resource_management_private_link_id + public_network_access_enabled = azurerm_resource_management_private_link_association.test.public_network_access_enabled +} +`, r.basic(data)) +} + +func (r ResourceManagementPrivateLinkAssociationTestResource) template(data acceptance.TestData) string { + return fmt.Sprintf(` +variable "primary_location" { + default = %q +} +variable "random_string" { + default = %q +} +variable "random_integer" { + default = %d +} + +data "azurerm_client_config" "test" {} + +data "azurerm_management_group" "test" { + name = data.azurerm_client_config.test.tenant_id +} + +resource "azurerm_resource_group" "test" { + name = "acctestrg-${var.random_integer}" + location = var.primary_location +} + +resource "azurerm_resource_management_private_link" "test" { + location = azurerm_resource_group.test.location + name = "acctestrmpl-${var.random_string}" + resource_group_name = azurerm_resource_group.test.name +} +`, data.Locations.Primary, data.RandomString, data.RandomInteger) +} diff --git a/website/docs/r/private_link_association.html.markdown b/website/docs/r/resource_management_private_link_association.html.markdown similarity index 67% rename from website/docs/r/private_link_association.html.markdown rename to website/docs/r/resource_management_private_link_association.html.markdown index 12a9b4073ae5..01f8ba11c046 100644 --- a/website/docs/r/private_link_association.html.markdown +++ b/website/docs/r/resource_management_private_link_association.html.markdown @@ -1,14 +1,14 @@ --- subcategory: "Management" layout: "azurerm" -page_title: "Azure Resource Manager: azurerm_private_link_association" +page_title: "Azure Resource Manager: azurerm_resource_management_private_link_association" description: |- - Manages a Private Link Association. + Manages a Resource Management Private Link Association. --- -# azurerm_private_link_association +# azurerm_resource_management_private_link_association -Manages a Private Link Association. +Manages a Resource Management Private Link Association. ## Example Usage @@ -30,10 +30,10 @@ resource "azurerm_resource_management_private_link" "example" { location = azurerm_resource_group.example.location } -resource "azurerm_private_link_association" "example" { - management_group_id = azurerm_management_group.example.id - private_link_id = azurerm_resource_management_private_link.example.id - public_network_access_enabled = true +resource "azurerm_resource_management_private_link_association" "example" { + management_group_id = azurerm_management_group.example.id + resource_management_private_link_id = azurerm_resource_management_private_link.example.id + public_network_access_enabled = true } ``` @@ -46,7 +46,7 @@ The following arguments are supported: **Note:** For now, `management_group_id` must be the ID of [Root Management Group](https://learn.microsoft.com/en-us/azure/governance/management-groups/overview#root-management-group-for-each-directory). -* `private_link_id` - (Required) The Resource ID of Resource Management Private Link. Changing this forces a new Private Link Association to be created. +* `resource_management_private_link_id` - (Required) The Resource ID of Resource Management Private Link. Changing this forces a new Private Link Association to be created. * `public_network_access_enabled` - (Required) Whether public network access is allowed. Changing this forces a new Private Link Association to be created. @@ -58,9 +58,7 @@ In addition to the Arguments listed above - the following Attributes are exporte * `id` - The ID of the Private Link Association. -* `scope` - The scope of the private link association. - -* `tenant_id` - The TenantID. +* `tenant_id` - The Tenant ID. --- @@ -77,5 +75,5 @@ The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/d An existing Private Link Association can be imported into Terraform using the `resource id`, e.g. ```shell -terraform import azurerm_private_link_association.example /providers/Microsoft.Management/managementGroups/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/privateLinkAssociations/00000000-0000-0000-0000-000000000000 +terraform import azurerm_resource_management_private_link_association.example /providers/Microsoft.Management/managementGroups/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/privateLinkAssociations/00000000-0000-0000-0000-000000000000 ``` From f067dd0e496f21fa2c436dbc51146c85471aa8d5 Mon Sep 17 00:00:00 2001 From: teowa <104055472+teowa@users.noreply.github.com> Date: Fri, 13 Oct 2023 06:23:26 +0000 Subject: [PATCH 3/6] fix --- ...ement_private_link_association_resource.go | 28 ------------------- 1 file changed, 28 deletions(-) diff --git a/internal/services/resource/resource_management_private_link_association_resource.go b/internal/services/resource/resource_management_private_link_association_resource.go index 4f5a3f7dd00f..7fc3997b9fa4 100644 --- a/internal/services/resource/resource_management_private_link_association_resource.go +++ b/internal/services/resource/resource_management_private_link_association_resource.go @@ -182,34 +182,6 @@ func (r ResourceManagementPrivateLinkAssociationResource) Delete() sdk.ResourceF return fmt.Errorf("deleting %s: %+v", *id, err) } - deadline, ok := ctx.Deadline() - if !ok { - return fmt.Errorf("internal-error: context had no deadline") - } - - state := &pluginsdk.StateChangeConf{ - Delay: 10 * time.Second, - Pending: []string{"Found"}, - Target: []string{"NotFound"}, - Timeout: time.Until(deadline), - Refresh: func() (interface{}, string, error) { - resp, err := client.Get(ctx, *id) - if err != nil { - if response.WasNotFound(resp.HttpResponse) { - return resp, "NotFound", nil - } - - return resp, "Error", fmt.Errorf("making Read request on %s: %+v", *id, err) - } - - return resp, "Found", nil - }, - } - - if _, err := state.WaitForStateContext(ctx); err != nil { - return fmt.Errorf("waiting for the Deployment %s: %+v", *id, err) - } - return nil }, } From e7018d9f89e5e2c51674e63fd57a66714fad8c77 Mon Sep 17 00:00:00 2001 From: teowa <104055472+teowa@users.noreply.github.com> Date: Tue, 17 Oct 2023 03:29:33 +0000 Subject: [PATCH 4/6] fix --- ...gement_private_link_association_resource.go | 2 +- ...t_private_link_association_resource_test.go | 18 ++++-------------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/internal/services/resource/resource_management_private_link_association_resource.go b/internal/services/resource/resource_management_private_link_association_resource.go index 7fc3997b9fa4..d0f71a589ba6 100644 --- a/internal/services/resource/resource_management_private_link_association_resource.go +++ b/internal/services/resource/resource_management_private_link_association_resource.go @@ -100,7 +100,7 @@ func (r ResourceManagementPrivateLinkAssociationResource) Create() sdk.ResourceF managementGroupId, err := commonids.ParseManagementGroupID(config.ManagementGroupId) if err != nil { - return fmt.Errorf("parse management group id: %+v", err) + return err } id := privatelinkassociation.NewPrivateLinkAssociationID(managementGroupId.GroupId, name) diff --git a/internal/services/resource/resource_management_private_link_association_resource_test.go b/internal/services/resource/resource_management_private_link_association_resource_test.go index 54c1f61dd4ba..ab5ce7cf83f9 100644 --- a/internal/services/resource/resource_management_private_link_association_resource_test.go +++ b/internal/services/resource/resource_management_private_link_association_resource_test.go @@ -122,16 +122,6 @@ resource "azurerm_resource_management_private_link_association" "import" { func (r ResourceManagementPrivateLinkAssociationTestResource) template(data acceptance.TestData) string { return fmt.Sprintf(` -variable "primary_location" { - default = %q -} -variable "random_string" { - default = %q -} -variable "random_integer" { - default = %d -} - data "azurerm_client_config" "test" {} data "azurerm_management_group" "test" { @@ -139,14 +129,14 @@ data "azurerm_management_group" "test" { } resource "azurerm_resource_group" "test" { - name = "acctestrg-${var.random_integer}" - location = var.primary_location + name = "acctestrg-%d" + location = "%s" } resource "azurerm_resource_management_private_link" "test" { location = azurerm_resource_group.test.location - name = "acctestrmpl-${var.random_string}" + name = "acctestrmpl-%[1]d" resource_group_name = azurerm_resource_group.test.name } -`, data.Locations.Primary, data.RandomString, data.RandomInteger) +`, data.RandomInteger, data.Locations.Primary) } From 8270cd61906f9b30c44dd3109e63ba6afbcc0674 Mon Sep 17 00:00:00 2001 From: teowa <104055472+teowa@users.noreply.github.com> Date: Fri, 20 Oct 2023 03:41:35 +0000 Subject: [PATCH 5/6] change name as required --- ...ement_private_link_association_resource.go | 20 +++------- ..._private_link_association_resource_test.go | 37 ++----------------- ...ent_private_link_association.html.markdown | 8 +++- 3 files changed, 15 insertions(+), 50 deletions(-) diff --git a/internal/services/resource/resource_management_private_link_association_resource.go b/internal/services/resource/resource_management_private_link_association_resource.go index d0f71a589ba6..51639b954ee2 100644 --- a/internal/services/resource/resource_management_private_link_association_resource.go +++ b/internal/services/resource/resource_management_private_link_association_resource.go @@ -5,7 +5,6 @@ import ( "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" @@ -44,8 +43,7 @@ func (r ResourceManagementPrivateLinkAssociationResource) Arguments() map[string return map[string]*pluginsdk.Schema{ "name": { ForceNew: true, - Optional: true, - Computed: true, + Required: true, Type: pluginsdk.TypeString, ValidateFunc: validation.IsUUID, }, @@ -89,21 +87,12 @@ func (r ResourceManagementPrivateLinkAssociationResource) Create() sdk.ResourceF 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 err } - id := privatelinkassociation.NewPrivateLinkAssociationID(managementGroupId.GroupId, name) + id := privatelinkassociation.NewPrivateLinkAssociationID(managementGroupId.GroupId, config.Name) existing, err := client.Get(ctx, id) if err != nil { @@ -152,9 +141,10 @@ func (r ResourceManagementPrivateLinkAssociationResource) Read() sdk.ResourceFun return fmt.Errorf("retrieving %s: %+v", *id, err) } + schema.ManagementGroupId = commonids.NewManagementGroupID(id.GroupId).ID() + schema.Name = id.PlaId + 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) diff --git a/internal/services/resource/resource_management_private_link_association_resource_test.go b/internal/services/resource/resource_management_private_link_association_resource_test.go index ab5ce7cf83f9..3948dbae7158 100644 --- a/internal/services/resource/resource_management_private_link_association_resource_test.go +++ b/internal/services/resource/resource_management_private_link_association_resource_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation" + "github.com/hashicorp/go-uuid" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" @@ -30,21 +31,6 @@ func TestAccResourceManagementPrivateLinkAssociation_basic(t *testing.T) { }) } -func TestAccResourceManagementPrivateLinkAssociation_name(t *testing.T) { - data := acceptance.BuildTestData(t, "azurerm_resource_management_private_link_association", "test") - r := ResourceManagementPrivateLinkAssociationTestResource{} - - data.ResourceTest(t, r, []acceptance.TestStep{ - { - Config: r.name(data), - Check: acceptance.ComposeTestCheckFunc( - check.That(data.ResourceName).ExistsInAzure(r), - ), - }, - data.ImportStep(), - }) -} - func TestAccResourceManagementPrivateLinkAssociation_requiresImport(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_resource_management_private_link_association", "test") r := ResourceManagementPrivateLinkAssociationTestResource{} @@ -75,6 +61,7 @@ func (r ResourceManagementPrivateLinkAssociationTestResource) Exists(ctx context } func (r ResourceManagementPrivateLinkAssociationTestResource) basic(data acceptance.TestData) string { + randomUUID, _ := uuid.GenerateUUID() return fmt.Sprintf(` provider "azurerm" { features {} @@ -83,28 +70,12 @@ provider "azurerm" { %s resource "azurerm_resource_management_private_link_association" "test" { + name = "%s" management_group_id = data.azurerm_management_group.test.id resource_management_private_link_id = azurerm_resource_management_private_link.test.id public_network_access_enabled = true } -`, r.template(data)) -} - -func (r ResourceManagementPrivateLinkAssociationTestResource) name(data acceptance.TestData) string { - return fmt.Sprintf(` -provider "azurerm" { - features {} -} - -%s - -resource "azurerm_resource_management_private_link_association" "test" { - name = "12345678-1234-1234-1234-123456789012" - management_group_id = data.azurerm_management_group.test.id - resource_management_private_link_id = azurerm_resource_management_private_link.test.id - public_network_access_enabled = true -} -`, r.template(data)) +`, r.template(data), randomUUID) } func (r ResourceManagementPrivateLinkAssociationTestResource) requiresImport(data acceptance.TestData) string { diff --git a/website/docs/r/resource_management_private_link_association.html.markdown b/website/docs/r/resource_management_private_link_association.html.markdown index 01f8ba11c046..159b8d288b0d 100644 --- a/website/docs/r/resource_management_private_link_association.html.markdown +++ b/website/docs/r/resource_management_private_link_association.html.markdown @@ -30,7 +30,11 @@ resource "azurerm_resource_management_private_link" "example" { location = azurerm_resource_group.example.location } +resource "random_uuid" "example" { +} + resource "azurerm_resource_management_private_link_association" "example" { + name = random_uuid.example.result management_group_id = azurerm_management_group.example.id resource_management_private_link_id = azurerm_resource_management_private_link.example.id public_network_access_enabled = true @@ -42,6 +46,8 @@ resource "azurerm_resource_management_private_link_association" "example" { The following arguments are supported: +* `name` - (Required) Specifies the name of this Private Link Association, which should be a UUID. A new UUID will be generated if not provided. Changing this forces a new Private Link Association to be created. + * `management_group_id` - (Required) Specifies the Management Group ID within which this Private Link Association should exist. Changing this forces a new Private Link Association to be created. **Note:** For now, `management_group_id` must be the ID of [Root Management Group](https://learn.microsoft.com/en-us/azure/governance/management-groups/overview#root-management-group-for-each-directory). @@ -50,8 +56,6 @@ The following arguments are supported: * `public_network_access_enabled` - (Required) Whether public network access is allowed. Changing this forces a new Private Link Association to be created. -* `name` - (Optional) Specifies the name of this Private Link Association, which should be a UUID. A new UUID will be generated if not provided. Changing this forces a new Private Link Association to be created. - ## Attributes Reference In addition to the Arguments listed above - the following Attributes are exported: From d397c06f71693bc51f1f94e22abf4ffed5e61a82 Mon Sep 17 00:00:00 2001 From: teowa <104055472+teowa@users.noreply.github.com> Date: Tue, 24 Oct 2023 02:35:51 +0000 Subject: [PATCH 6/6] change to optional --- ...ement_private_link_association_resource.go | 14 ++++++-- ..._private_link_association_resource_test.go | 34 +++++++++++++++++++ ...ent_private_link_association.html.markdown | 16 +++++++-- 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/internal/services/resource/resource_management_private_link_association_resource.go b/internal/services/resource/resource_management_private_link_association_resource.go index 51639b954ee2..68a724d71a07 100644 --- a/internal/services/resource/resource_management_private_link_association_resource.go +++ b/internal/services/resource/resource_management_private_link_association_resource.go @@ -5,6 +5,7 @@ import ( "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" @@ -43,7 +44,7 @@ func (r ResourceManagementPrivateLinkAssociationResource) Arguments() map[string return map[string]*pluginsdk.Schema{ "name": { ForceNew: true, - Required: true, + Optional: true, Type: pluginsdk.TypeString, ValidateFunc: validation.IsUUID, }, @@ -92,7 +93,16 @@ func (r ResourceManagementPrivateLinkAssociationResource) Create() sdk.ResourceF return err } - id := privatelinkassociation.NewPrivateLinkAssociationID(managementGroupId.GroupId, config.Name) + var name string + if config.Name != "" { + name = config.Name + } + + if name == "" { + name = uuid.New().String() + } + + id := privatelinkassociation.NewPrivateLinkAssociationID(managementGroupId.GroupId, name) existing, err := client.Get(ctx, id) if err != nil { diff --git a/internal/services/resource/resource_management_private_link_association_resource_test.go b/internal/services/resource/resource_management_private_link_association_resource_test.go index 3948dbae7158..d8c726ca6e8a 100644 --- a/internal/services/resource/resource_management_private_link_association_resource_test.go +++ b/internal/services/resource/resource_management_private_link_association_resource_test.go @@ -46,6 +46,21 @@ func TestAccResourceManagementPrivateLinkAssociation_requiresImport(t *testing.T }) } +func TestAccResourceManagementPrivateLinkAssociation_generateName(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_resource_management_private_link_association", "test") + r := ResourceManagementPrivateLinkAssociationTestResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.generateName(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + func (r ResourceManagementPrivateLinkAssociationTestResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { id, err := privatelinkassociation.ParsePrivateLinkAssociationID(state.ID) if err != nil { @@ -78,6 +93,25 @@ resource "azurerm_resource_management_private_link_association" "test" { `, r.template(data), randomUUID) } +func (r ResourceManagementPrivateLinkAssociationTestResource) generateName(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +%s + +resource "azurerm_resource_management_private_link_association" "test" { + management_group_id = data.azurerm_management_group.test.id + resource_management_private_link_id = azurerm_resource_management_private_link.test.id + public_network_access_enabled = true + lifecycle { + ignore_changes = [name] + } +} +`, r.template(data)) +} + func (r ResourceManagementPrivateLinkAssociationTestResource) requiresImport(data acceptance.TestData) string { return fmt.Sprintf(` %s diff --git a/website/docs/r/resource_management_private_link_association.html.markdown b/website/docs/r/resource_management_private_link_association.html.markdown index 159b8d288b0d..4a7755b03344 100644 --- a/website/docs/r/resource_management_private_link_association.html.markdown +++ b/website/docs/r/resource_management_private_link_association.html.markdown @@ -46,8 +46,6 @@ resource "azurerm_resource_management_private_link_association" "example" { The following arguments are supported: -* `name` - (Required) Specifies the name of this Private Link Association, which should be a UUID. A new UUID will be generated if not provided. Changing this forces a new Private Link Association to be created. - * `management_group_id` - (Required) Specifies the Management Group ID within which this Private Link Association should exist. Changing this forces a new Private Link Association to be created. **Note:** For now, `management_group_id` must be the ID of [Root Management Group](https://learn.microsoft.com/en-us/azure/governance/management-groups/overview#root-management-group-for-each-directory). @@ -55,6 +53,20 @@ The following arguments are supported: * `resource_management_private_link_id` - (Required) The Resource ID of Resource Management Private Link. Changing this forces a new Private Link Association to be created. * `public_network_access_enabled` - (Required) Whether public network access is allowed. Changing this forces a new Private Link Association to be created. + +* `name` - (Optional) Specifies the name of this Private Link Association, which should be a UUID. If `name` is not provided, a UUID will be generated, you should use [the `ignore_changes` attribute to ignore changes to this field](https://www.terraform.io/language/meta-arguments/lifecycle#ignore_changess). Changing this forces a new Private Link Association to be created. + +```hcl +resource "azurerm_resource_management_private_link_association" "example" { + management_group_id = azurerm_management_group.example.id + resource_management_private_link_id = azurerm_resource_management_private_link.example.id + public_network_access_enabled = true + lifecycle { + ignore_changes = [name] + } +} + +``` ## Attributes Reference