Skip to content

Commit

Permalink
new resource azurerm_resource_management_private_link (#23098)
Browse files Browse the repository at this point in the history
* new resource azurerm_resource_management_private_link

* fix review comment

* fix doc

* fix test
  • Loading branch information
teowa authored Sep 25, 2023
1 parent 66ac616 commit 6f8b827
Show file tree
Hide file tree
Showing 19 changed files with 909 additions and 18 deletions.
45 changes: 27 additions & 18 deletions internal/services/resource/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ 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/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"
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2022-09-01/providers"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
DeploymentsClient *resources.DeploymentsClient
DeploymentScriptsClient *deploymentscripts.DeploymentScriptsClient
FeaturesClient *features.FeaturesClient
GroupsClient *resources.GroupsClient
LocksClient *managementlocks.ManagementLocksClient
ResourceProvidersClient *providers.ProvidersClient
ResourcesClient *resources.Client
TagsClient *resources.TagsClient
TemplateSpecsVersionsClient *templatespecs.VersionsClient
DeploymentsClient *resources.DeploymentsClient
DeploymentScriptsClient *deploymentscripts.DeploymentScriptsClient
FeaturesClient *features.FeaturesClient
GroupsClient *resources.GroupsClient
LocksClient *managementlocks.ManagementLocksClient
ResourceManagementPrivateLinkClient *resourcemanagementprivatelink.ResourceManagementPrivateLinkClient
ResourceProvidersClient *providers.ProvidersClient
ResourcesClient *resources.Client
TagsClient *resources.TagsClient
TemplateSpecsVersionsClient *templatespecs.VersionsClient

options *common.ClientOptions
}
Expand Down Expand Up @@ -54,6 +56,12 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
}
o.Configure(locksClient.Client, o.Authorizers.ResourceManager)

resourceManagementPrivateLinkClient, err := resourcemanagementprivatelink.NewResourceManagementPrivateLinkClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building ResourceManagementPrivateLink client: %+v", err)
}
o.Configure(resourceManagementPrivateLinkClient.Client, o.Authorizers.ResourceManager)

resourceProvidersClient, err := providers.NewProvidersClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building Providers client: %+v", err)
Expand All @@ -70,15 +78,16 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
o.ConfigureClient(&tagsClient.Client, o.ResourceManagerAuthorizer)

return &Client{
GroupsClient: &groupsClient,
DeploymentsClient: &deploymentsClient,
DeploymentScriptsClient: deploymentScriptsClient,
FeaturesClient: featuresClient,
LocksClient: locksClient,
ResourceProvidersClient: resourceProvidersClient,
ResourcesClient: &resourcesClient,
TagsClient: &tagsClient,
TemplateSpecsVersionsClient: &templatespecsVersionsClient,
GroupsClient: &groupsClient,
DeploymentsClient: &deploymentsClient,
DeploymentScriptsClient: deploymentScriptsClient,
FeaturesClient: featuresClient,
LocksClient: locksClient,
ResourceManagementPrivateLinkClient: resourceManagementPrivateLinkClient,
ResourceProvidersClient: resourceProvidersClient,
ResourcesClient: &resourcesClient,
TagsClient: &tagsClient,
TemplateSpecsVersionsClient: &templatespecsVersionsClient,

options: o,
}, nil
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 @@ -64,6 +64,7 @@ func (r Registration) DataSources() []sdk.DataSource {
func (r Registration) Resources() []sdk.Resource {
return []sdk.Resource{
ResourceProviderRegistrationResource{},
ResourceManagementPrivateLinkResource{},
ResourceDeploymentScriptAzurePowerShellResource{},
ResourceDeploymentScriptAzureCliResource{},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package resource

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/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 = ResourceManagementPrivateLinkResource{}

type ResourceManagementPrivateLinkResource struct{}

func (r ResourceManagementPrivateLinkResource) ModelObject() interface{} {
return &ResourceManagementPrivateLinkResourceSchema{}
}

type ResourceManagementPrivateLinkResourceSchema struct {
Location string `tfschema:"location"`
Name string `tfschema:"name"`
ResourceGroupName string `tfschema:"resource_group_name"`
}

func (r ResourceManagementPrivateLinkResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return resourcemanagementprivatelink.ValidateResourceManagementPrivateLinkID
}

func (r ResourceManagementPrivateLinkResource) ResourceType() string {
return "azurerm_resource_management_private_link"
}

func (r ResourceManagementPrivateLinkResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
ForceNew: true,
Required: true,
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
"resource_group_name": commonschema.ResourceGroupName(),
"location": commonschema.Location(),
}
}

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

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

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

subscriptionId := metadata.Client.Account.SubscriptionId

id := resourcemanagementprivatelink.NewResourceManagementPrivateLinkID(subscriptionId, config.ResourceGroupName, config.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 := resourcemanagementprivatelink.ResourceManagementPrivateLinkLocation{
Location: pointer.To(location.Normalize(config.Location)),
}

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

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

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

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

schema := ResourceManagementPrivateLinkResourceSchema{
Name: id.ResourceManagementPrivateLinkName,
ResourceGroupName: id.ResourceGroupName,
}

if model := resp.Model; model != nil {
schema.Location = location.NormalizeNilable(model.Location)
}

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

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

id, err := resourcemanagementprivatelink.ParseResourceManagementPrivateLinkID(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
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package resource_test

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/resourcemanagementprivatelink"
"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 ResourceManagementPrivateLinkTestResource struct{}

func TestAccResourceManagementPrivateLink_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_resource_management_private_link", "test")
r := ResourceManagementPrivateLinkTestResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccResourceManagementPrivateLink_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_resource_management_private_link", "test")
r := ResourceManagementPrivateLinkTestResource{}

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 ResourceManagementPrivateLinkTestResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := resourcemanagementprivatelink.ParseResourceManagementPrivateLinkID(state.ID)
if err != nil {
return nil, err
}

resp, err := clients.Resource.ResourceManagementPrivateLinkClient.Get(ctx, *id)
if err != nil {
return nil, fmt.Errorf("reading %s: %+v", *id, err)
}

return utils.Bool(resp.Model != nil), nil
}

func (r ResourceManagementPrivateLinkTestResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
%s
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
}
`, r.template(data))
}

func (r ResourceManagementPrivateLinkTestResource) requiresImport(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_resource_management_private_link" "import" {
location = azurerm_resource_management_private_link.test.location
name = azurerm_resource_management_private_link.test.name
resource_group_name = azurerm_resource_management_private_link.test.resource_group_name
}
`, r.basic(data))
}

func (r ResourceManagementPrivateLinkTestResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
variable "primary_location" {
default = %q
}
variable "random_integer" {
default = %d
}
variable "random_string" {
default = %q
}
resource "azurerm_resource_group" "test" {
name = "acctestrg-${var.random_integer}"
location = var.primary_location
}
`, data.Locations.Primary, data.RandomInteger, data.RandomString)
}
Loading

0 comments on commit 6f8b827

Please sign in to comment.