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_synapse_private_link_hub" #12495

Merged
merged 5 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions azurerm/internal/services/synapse/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

type Client struct {
FirewallRulesClient *synapse.IPFirewallRulesClient
PrivateLinkHubsClient *synapse.PrivateLinkHubsClient
SparkPoolClient *synapse.BigDataPoolsClient
SqlPoolClient *synapse.SQLPoolsClient
SqlPoolTransparentDataEncryptionClient *synapse.SQLPoolTransparentDataEncryptionsClient
Expand All @@ -26,6 +27,9 @@ func NewClient(o *common.ClientOptions) *Client {
firewallRuleClient := synapse.NewIPFirewallRulesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&firewallRuleClient.Client, o.ResourceManagerAuthorizer)

privateLinkHubsClient := synapse.NewPrivateLinkHubsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&privateLinkHubsClient.Client, o.ResourceManagerAuthorizer)

// the service team hopes to rename it to sparkPool, so rename the sdk here
sparkPoolClient := synapse.NewBigDataPoolsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&sparkPoolClient.Client, o.ResourceManagerAuthorizer)
Expand All @@ -47,6 +51,7 @@ func NewClient(o *common.ClientOptions) *Client {

return &Client{
FirewallRulesClient: &firewallRuleClient,
PrivateLinkHubsClient: &privateLinkHubsClient,
SparkPoolClient: &sparkPoolClient,
SqlPoolClient: &sqlPoolClient,
SqlPoolTransparentDataEncryptionClient: &sqlPoolTransparentDataEncryptionClient,
Expand Down
69 changes: 69 additions & 0 deletions azurerm/internal/services/synapse/parse/private_link_hub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"fmt"
"strings"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type PrivateLinkHubId struct {
SubscriptionId string
ResourceGroup string
Name string
}

func NewPrivateLinkHubID(subscriptionId, resourceGroup, name string) PrivateLinkHubId {
return PrivateLinkHubId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
Name: name,
}
}

func (id PrivateLinkHubId) String() string {
segments := []string{
fmt.Sprintf("Name %q", id.Name),
fmt.Sprintf("Resource Group %q", id.ResourceGroup),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("%s: (%s)", "Private Link Hub", segmentsStr)
}

func (id PrivateLinkHubId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Synapse/privateLinkHubs/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.Name)
}

// PrivateLinkHubID parses a Private Link Hub ID into an PrivateLinkHubId struct
func PrivateLinkHubID(input string) (*PrivateLinkHubId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

resourceId := PrivateLinkHubId{
SubscriptionId: id.SubscriptionID,
ResourceGroup: id.ResourceGroup,
}

if resourceId.SubscriptionId == "" {
return nil, fmt.Errorf("ID was missing the 'subscriptions' element")
}

if resourceId.ResourceGroup == "" {
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element")
}

if resourceId.Name, err = id.PopSegment("privateLinkHubs"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &resourceId, nil
}
112 changes: 112 additions & 0 deletions azurerm/internal/services/synapse/parse/private_link_hub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid"
)

var _ resourceid.Formatter = PrivateLinkHubId{}

func TestPrivateLinkHubIDFormatter(t *testing.T) {
actual := NewPrivateLinkHubID("12345678-1234-9876-4563-123456789012", "resGroup1", "privateLinkHub1").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/privateLinkHubs/privateLinkHub1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestPrivateLinkHubID(t *testing.T) {
testData := []struct {
Input string
Error bool
Expected *PrivateLinkHubId
}{

{
// empty
Input: "",
Error: true,
},

{
// missing SubscriptionId
Input: "/",
Error: true,
},

{
// missing value for SubscriptionId
Input: "/subscriptions/",
Error: true,
},

{
// missing ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/",
Error: true,
},

{
// missing value for ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/",
Error: true,
},

{
// missing Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/",
Error: true,
},

{
// missing value for Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/privateLinkHubs/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Synapse/privateLinkHubs/privateLinkHub1",
Expected: &PrivateLinkHubId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
Name: "privateLinkHub1",
},
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.SYNAPSE/PRIVATELINKHUBS/PRIVATELINKHUB1",
Error: true,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Input)

actual, err := PrivateLinkHubID(v.Input)
if err != nil {
if v.Error {
continue
}

t.Fatalf("Expect a value but got an error: %s", err)
}
if v.Error {
t.Fatal("Expect an error but didn't get one")
}

if actual.SubscriptionId != v.Expected.SubscriptionId {
t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId)
}
if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup)
}
if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}
}
}
1 change: 1 addition & 0 deletions azurerm/internal/services/synapse/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (r Registration) SupportedDataSources() map[string]*pluginsdk.Resource {
func (r Registration) SupportedResources() map[string]*pluginsdk.Resource {
return map[string]*pluginsdk.Resource{
"azurerm_synapse_firewall_rule": resourceSynapseFirewallRule(),
"azurerm_synapse_private_link_hub": resourceSynapsePrivateLinkHub(),
"azurerm_synapse_managed_private_endpoint": resourceSynapseManagedPrivateEndpoint(),
"azurerm_synapse_role_assignment": resourceSynapseRoleAssignment(),
"azurerm_synapse_spark_pool": resourceSynapseSparkPool(),
Expand Down
167 changes: 167 additions & 0 deletions azurerm/internal/services/synapse/synapse_private_link_hub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package synapse

import (
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/synapse/mgmt/2021-03-01/synapse"
"github.com/hashicorp/go-azure-helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/synapse/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/synapse/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceSynapsePrivateLinkHub() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceSynapsePrivateLinkHubCreate,
Read: resourceSynapsePrivateLinkHubRead,
Update: resourceSynapsePrivateLinkHubUpdate,
Delete: resourceSynapsePrivateLinkHubDelete,

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

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

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.PrivateLinkHubName,
},

"resource_group_name": azure.SchemaResourceGroupName(),

"location": azure.SchemaLocation(),

"tags": tags.Schema(),
},
}
}

func resourceSynapsePrivateLinkHubCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Synapse.PrivateLinkHubsClient
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)

existing, err := client.Get(ctx, resourceGroup, name)
owenfarrell marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for present of existing Synapse Private Link Hub %q (Resource Group %q): %+v", name, resourceGroup, err)
owenfarrell marked this conversation as resolved.
Show resolved Hide resolved
}
}
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_synapse_private_link_hub", *existing.ID)
owenfarrell marked this conversation as resolved.
Show resolved Hide resolved
}

privateLinkHubInfo := synapse.PrivateLinkHub{
Location: utils.String(location.Normalize(d.Get("location").(string))),
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

_, err = client.CreateOrUpdate(ctx, privateLinkHubInfo, resourceGroup, name)
owenfarrell marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("creating Synapse Private Link Hub %q (Resource Group %q): %+v", name, resourceGroup, err)
owenfarrell marked this conversation as resolved.
Show resolved Hide resolved
}

subscriptionId := meta.(*clients.Client).Account.SubscriptionId
id := parse.NewPrivateLinkHubID(subscriptionId, resourceGroup, name)
owenfarrell marked this conversation as resolved.
Show resolved Hide resolved
d.SetId(id.ID())

return resourceSynapsePrivateLinkHubRead(d, meta)
}

func resourceSynapsePrivateLinkHubRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Synapse.PrivateLinkHubsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

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

resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] synapse %q does not exist - removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("retrieving Synapse Private Link Hub %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
owenfarrell marked this conversation as resolved.
Show resolved Hide resolved
}

d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("location", location.NormalizeNilable(resp.Location))

return tags.FlattenAndSet(d, resp.Tags)
}

func resourceSynapsePrivateLinkHubUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Synapse.PrivateLinkHubsClient
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

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

if d.HasChanges("tags") {
owenfarrell marked this conversation as resolved.
Show resolved Hide resolved
privateLinkHubPatchInfo := synapse.PrivateLinkHubPatchInfo{
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

_, err := client.Update(ctx, privateLinkHubPatchInfo, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("updating Synapse Private Link Hub %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
owenfarrell marked this conversation as resolved.
Show resolved Hide resolved
}
}

return resourceSynapsePrivateLinkHubRead(d, meta)
}

func resourceSynapsePrivateLinkHubDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Synapse.PrivateLinkHubsClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

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

future, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("deleting Synapse Private Link Hub %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
owenfarrell marked this conversation as resolved.
Show resolved Hide resolved
}

// sometimes the waitForCompletion rest api will return 404
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
if !response.WasNotFound(future.Response()) {
return fmt.Errorf("waiting for Synapse Private Link Hub %q (Resource Group %q) to be deleted: %+v", id.Name, id.ResourceGroup, err)
owenfarrell marked this conversation as resolved.
Show resolved Hide resolved
}
}

return nil
}
Loading