Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new resource azurerm_kusto_cluster_managed_private_endpoint #17667

Merged
merged 9 commits into from
Jul 29, 2022
5 changes: 5 additions & 0 deletions internal/services/kusto/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type Client struct {
AttachedDatabaseConfigurationsClient *kusto.AttachedDatabaseConfigurationsClient
ClustersClient *kusto.ClustersClient
ClusterManagedPrivateEndpointClient *kusto.ManagedPrivateEndpointsClient
ClusterPrincipalAssignmentsClient *kusto.ClusterPrincipalAssignmentsClient
DatabasesClient *kusto.DatabasesClient
DataConnectionsClient *kusto.DataConnectionsClient
Expand All @@ -19,6 +20,9 @@ func NewClient(o *common.ClientOptions) *Client {
ClustersClient := kusto.NewClustersClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ClustersClient.Client, o.ResourceManagerAuthorizer)

ClusterManagedPrivateEndpointClient := kusto.NewManagedPrivateEndpointsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ClusterManagedPrivateEndpointClient.Client, o.ResourceManagerAuthorizer)

ClusterPrincipalAssignmentsClient := kusto.NewClusterPrincipalAssignmentsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ClusterPrincipalAssignmentsClient.Client, o.ResourceManagerAuthorizer)

Expand All @@ -39,6 +43,7 @@ func NewClient(o *common.ClientOptions) *Client {
return &Client{
AttachedDatabaseConfigurationsClient: &AttachedDatabaseConfigurationsClient,
ClustersClient: &ClustersClient,
ClusterManagedPrivateEndpointClient: &ClusterManagedPrivateEndpointClient,
ClusterPrincipalAssignmentsClient: &ClusterPrincipalAssignmentsClient,
DatabasesClient: &DatabasesClient,
DataConnectionsClient: &DataConnectionsClient,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package kusto

import (
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/kusto/mgmt/2022-02-01/kusto"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/kusto/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/kusto/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func resourceKustoClusterManagedPrivateEndpoint() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceKustoClusterManagedPrivateEndpointCreateUpdate,
Read: resourceKustoClusterManagedPrivateEndpointRead,
Update: resourceKustoClusterManagedPrivateEndpointCreateUpdate,
Delete: resourceKustoClusterManagedPrivateEndpointDelete,

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.ManagedPrivateEndpointsID(id)
return err
}),

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(60 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(60 * time.Minute),
Delete: pluginsdk.DefaultTimeout(60 * time.Minute),
},

Schema: map[string]*pluginsdk.Schema{
"resource_group_name": azure.SchemaResourceGroupName(),

"cluster_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.ClusterName,
},

"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

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

"group_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

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

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

func resourceKustoClusterManagedPrivateEndpointCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Kusto.ClusterManagedPrivateEndpointClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewManagedPrivateEndpointsID(subscriptionId, d.Get("resource_group_name").(string), d.Get("cluster_name").(string), d.Get("name").(string))
if d.IsNewResource() {
managedPrivateEndpoint, err := client.Get(ctx, id.ResourceGroup, id.ClusterName, id.ManagedPrivateEndpointName)
if err != nil {
if !utils.ResponseWasNotFound(managedPrivateEndpoint.Response) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}

if !utils.ResponseWasNotFound(managedPrivateEndpoint.Response) {
return tf.ImportAsExistsError("azurerm_kusto_cluster_managed_private_endpoint", id.ID())
}
}

managedPrivateEndpoint := kusto.ManagedPrivateEndpoint{
ManagedPrivateEndpointProperties: &kusto.ManagedPrivateEndpointProperties{
PrivateLinkResourceID: utils.String(d.Get("private_link_resource_id").(string)),
GroupID: utils.String(d.Get("group_id").(string)),
},
}

if v, ok := d.GetOk("private_link_resource_region"); ok {
managedPrivateEndpoint.PrivateLinkResourceRegion = utils.String(v.(string))
}

if v, ok := d.GetOk("request_message"); ok {
managedPrivateEndpoint.RequestMessage = utils.String(v.(string))
}

future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.ClusterName, id.ManagedPrivateEndpointName, managedPrivateEndpoint)
if err != nil {
return fmt.Errorf("creating/updating %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation/update of %s: %+v", id, err)
}

d.SetId(id.ID())
return resourceKustoClusterManagedPrivateEndpointRead(d, meta)
}

func resourceKustoClusterManagedPrivateEndpointRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Kusto.ClusterManagedPrivateEndpointClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ManagedPrivateEndpointsID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.ClusterName, id.ManagedPrivateEndpointName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", id.ManagedPrivateEndpointName)
d.Set("cluster_name", id.ClusterName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("private_link_resource_id", resp.PrivateLinkResourceID)
d.Set("group_id", resp.GroupID)

if resp.PrivateLinkResourceRegion != nil {
d.Set("private_link_resource_region", resp.PrivateLinkResourceRegion)
}

if resp.RequestMessage != nil {
d.Set("request_message", resp.RequestMessage)
}

return nil
}

func resourceKustoClusterManagedPrivateEndpointDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Kusto.ClusterManagedPrivateEndpointClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ManagedPrivateEndpointsID(d.Id())
if err != nil {
return err
}

future, err := client.Delete(ctx, id.ResourceGroup, id.ClusterName, id.ManagedPrivateEndpointName)

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

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of %s: %+v", *id, err)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package kusto_test

import (
"context"
"fmt"
"testing"

"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/services/kusto/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type KustoClusterManagedPrivateEndpointResource struct{}

func TestAccKustoClusterManagedPrivateEndpoint_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_kusto_cluster_managed_private_endpoint", "test")
r := KustoClusterManagedPrivateEndpointResource{}

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

func TestAccKustoClusterManagedPrivateEndpoint_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_kusto_cluster_managed_private_endpoint", "test")
r := KustoClusterManagedPrivateEndpointResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("group_id").HasValue("blob"),
check.That(data.ResourceName).Key("request_message").HasValue("Please Approve"),
),
},
data.ImportStep()},
)
}

func (KustoClusterManagedPrivateEndpointResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.ManagedPrivateEndpointsID(state.ID)
if err != nil {
return nil, err
}

resp, err := clients.Kusto.ClusterManagedPrivateEndpointClient.Get(ctx, id.ResourceGroup, id.ClusterName, id.ManagedPrivateEndpointName)
if err != nil {
return nil, fmt.Errorf("retrieving %s: %v", id.String(), err)
}

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

func (r KustoClusterManagedPrivateEndpointResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "rg" {
name = "acctestRG-kusto-%d"
location = "%s"
}

resource "azurerm_kusto_cluster" "test" {
name = "acctestkc%s"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

sku {
name = "Dev(No SLA)_Standard_D11_v2"
capacity = 1
}
}

resource "azurerm_storage_account" "test" {
name = "acctestsa%s"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
account_tier = "Standard"
account_replication_type = "LRS"
}

resource "azurerm_kusto_cluster_managed_private_endpoint" "test" {
name = "acctestmpe%d"
resource_group_name = azurerm_resource_group.rg.name
cluster_name = azurerm_kusto_cluster.test.name
private_link_resource_id = azurerm_storage_account.test.id
group_id = "blob"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger)
}

func (r KustoClusterManagedPrivateEndpointResource) complete(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "rg" {
name = "acctestRG-kusto-%d"
location = "%s"
}

resource "azurerm_kusto_cluster" "test" {
name = "acctestkc%s"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

sku {
name = "Dev(No SLA)_Standard_D11_v2"
capacity = 1
}
}

resource "azurerm_storage_account" "test" {
name = "acctestsa%s"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
account_tier = "Standard"
account_replication_type = "LRS"
}

resource "azurerm_kusto_cluster_managed_private_endpoint" "test" {
name = "acctestmpe%d"
resource_group_name = azurerm_resource_group.rg.name
cluster_name = azurerm_kusto_cluster.test.name
private_link_resource_id = azurerm_storage_account.test.id
private_link_resource_region = azurerm_storage_account.test.location
group_id = "blob"
request_message = "Please Approve"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger)
}
Loading