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_private_link_endpoint New Data Source: azurerm_private_link_endpoint_connection and expose attibute in azurerm_subnet #4493

Merged
merged 45 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
64bb60e
[WIP] Private Endpoint
WodansSon Oct 3, 2019
d445539
[WIP] Update datasource to network_interface_ids
WodansSon Oct 3, 2019
cf252b9
[WIP] Update documentation with correct value
WodansSon Oct 3, 2019
266fa1b
Rename resource and flatten schema
WodansSon Oct 8, 2019
3840307
Merge branch 'master' of https://github.com/terraform-providers/terra…
WodansSon Oct 9, 2019
7ce24b8
Update for terraform-plugin-sdk
WodansSon Oct 9, 2019
71133ca
Gofmt and some PR comment updates
WodansSon Oct 9, 2019
f965560
Some documentation updates
WodansSon Oct 9, 2019
87cb012
More doc updates
WodansSon Oct 10, 2019
3a4b263
Merge branch 'master' of https://github.com/terraform-providers/terra…
WodansSon Oct 12, 2019
fe67c8f
Update to include new terraform-plugin-sdk
WodansSon Oct 12, 2019
bd69625
Fixed lint error
WodansSon Oct 12, 2019
5ee333d
Update to endpoint
WodansSon Oct 23, 2019
57ce715
Refactor progress
WodansSon Oct 25, 2019
52fa425
Added private_ip_address as computed output
WodansSon Oct 26, 2019
61624f4
The resource is done, working on tests
WodansSon Oct 29, 2019
d5e8155
Merge branch 'master' into nr_private-end-point
WodansSon Oct 30, 2019
07452f5
A few more updates
WodansSon Oct 30, 2019
b24ac15
Working new data source
WodansSon Nov 7, 2019
0d8dcd9
Merge branch 'master' into nr_private-end-point
WodansSon Nov 7, 2019
39eef7d
Fixed documentation
WodansSon Nov 7, 2019
69bb2c0
Merge branch 'nr_private-end-point' of https://github.com/terraform-p…
WodansSon Nov 7, 2019
f0f071d
gofmt
WodansSon Nov 7, 2019
e9300b9
Fix lint errors
WodansSon Nov 7, 2019
a676a2f
Partial update for consistency
WodansSon Nov 12, 2019
255957d
Update subnet and documentation
WodansSon Nov 12, 2019
06990fc
Merge branch 'master' of https://github.com/terraform-providers/terra…
WodansSon Nov 21, 2019
227ebc9
Some fixes
WodansSon Nov 21, 2019
e0fde44
Commit changes for rebase with master
WodansSon Nov 22, 2019
c0a3c8e
Merge branch 'master' of https://github.com/terraform-providers/terra…
WodansSon Nov 22, 2019
4ba6e4e
Updates per PR comments
WodansSon Nov 22, 2019
817ca0f
Remove dead code
WodansSon Nov 22, 2019
33e7fdd
Updates per PR comments
WodansSon Nov 22, 2019
e73e10e
Merge branch 'nr_private-end-point' of https://github.com/terraform-p…
WodansSon Nov 22, 2019
b209b87
Updates per PR comments
WodansSon Nov 27, 2019
bb8a136
Remove endpoint data source from provider
WodansSon Nov 28, 2019
ae82096
Update service endpoint connections
WodansSon Nov 28, 2019
7ab6a68
Update to documentation
WodansSon Nov 28, 2019
261c704
Fix sample code
WodansSon Nov 28, 2019
541694f
data source update
WodansSon Nov 28, 2019
6d6a38f
Merge branch 'master' of https://github.com/terraform-providers/terra…
WodansSon Dec 2, 2019
d818bc6
Removed service connections datasource
WodansSon Dec 3, 2019
017b56c
Add the service connections datasource back in
WodansSon Dec 3, 2019
a8e0eac
Update test case
WodansSon Dec 3, 2019
527a2f1
make fmt and adjust comment location
katbyte Dec 3, 2019
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
81 changes: 81 additions & 0 deletions azurerm/data_source_private_link_endpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package azurerm

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmPrivateLinkEndpoint() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmPrivateLinkEndpointRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we do a regex validation here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

As before, could we do some better validation here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed... I was hoping that the private link service was going to be merged before this one so I can re-use the validation in that PR. However, I have added the validation in this one as well.

},

"location": azure.SchemaLocationForDataSource(),

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"network_interface_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"subnet_id": {
Type: schema.TypeString,
Computed: true,
},

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

func dataSourceArmPrivateLinkEndpointRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Network.PrivateEndpointClient
ctx := meta.(*ArmClient).StopContext

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

resp, err := client.Get(ctx, resourceGroup, name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Private Endpoint %q (Resource Group %q) was not found", name, resourceGroup)
}
return fmt.Errorf("Error reading Private Endpoint %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("API returns a nil/empty id on Private Link Endpoint %q (Resource Group %q): %+v", name, resourceGroup, err)
}
d.SetId(*resp.ID)

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
if props := resp.PrivateEndpointProperties; props != nil {
if err := d.Set("network_interfaces", flattenArmPrivateLinkEndpointInterface(props.NetworkInterfaces)); err != nil {
return fmt.Errorf("Error setting `network_interfaces`: %+v", err)
}
if subnet := props.Subnet; subnet != nil {
d.Set("subnet_id", subnet.ID)
}
}

return tags.FlattenAndSet(d, resp.Tags)
}
73 changes: 73 additions & 0 deletions azurerm/data_source_private_link_endpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
)

func TestAccDataSourceAzureRMPrivateEndpoint_basic(t *testing.T) {
dataSourceName := "data.azurerm_private_link_endpoint.test"
ri := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourcePrivateEndpoint_basic(ri, location),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "subnet_id"),
),
},
},
})
}

func TestAccDataSourceAzureRMPrivateEndpoint_complete(t *testing.T) {
dataSourceName := "data.azurerm_private_link_endpoint.test"
ri := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourcePrivateEndpoint_complete(ri, location),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "subnet_id"),
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(dataSourceName, "tags.env", "test"),
),
},
},
})
}

func testAccDataSourcePrivateEndpoint_basic(rInt int, location string) string {
config := testAccAzureRMPrivateEndpoint_basic(rInt, location)
return fmt.Sprintf(`
%s

data "azurerm_private_link_endpoint" "test" {
resource_group_name = "${azurerm_private_link_endpoint.test.resource_group_name}"
name = "${azurerm_private_link_endpoint.test.name}"
}
`, config)
}

func testAccDataSourcePrivateEndpoint_complete(rInt int, location string) string {
config := testAccAzureRMPrivateEndpoint_complete(rInt, location)
return fmt.Sprintf(`
%s

data "azurerm_private_link_endpoint" "test" {
resource_group_name = "${azurerm_private_link_endpoint.test.resource_group_name}"
name = "${azurerm_private_link_endpoint.test.name}"
}
`, config)
}
11 changes: 11 additions & 0 deletions azurerm/data_source_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ func dataSourceArmSubnet() *schema.Resource {
Type: schema.TypeString,
},
},

"disable_private_link_endpoint_network_policies": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -92,6 +97,12 @@ func dataSourceArmSubnetRead(d *schema.ResourceData, meta interface{}) error {
if props := resp.SubnetPropertiesFormat; props != nil {
d.Set("address_prefix", props.AddressPrefix)

if privateEndpointNetworkPolicies := props.PrivateEndpointNetworkPolicies; privateEndpointNetworkPolicies != nil {
if err := d.Set("disable_private_link_endpoint_network_policies", *privateEndpointNetworkPolicies == "Disabled"); err != nil {
return err
}
}

if props.NetworkSecurityGroup != nil {
d.Set("network_security_group_id", props.NetworkSecurityGroup.ID)
} else {
Expand Down
24 changes: 24 additions & 0 deletions azurerm/helpers/validate/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,27 @@ func NoEmptyStrings(i interface{}, k string) ([]string, []error) {

return nil, nil
}

// PrivateLinkEnpointRequestMessage validates that the Private Link Enpoint Request Message is less than 140 characters
func PrivateLinkEnpointRequestMessage(i interface{}, k string) (_ []string, errors []error) {
return stringMaxLength(140)(i, k)
}

func stringMaxLength(maxLength int) func(i interface{}, k string) (_ []string, errors []error) {
return func(i interface{}, k string) (_ []string, errors []error) {
v, ok := i.(string)
if !ok {
return nil, []error{fmt.Errorf("expected type of %q to be string", k)}
}

if len(v) > maxLength {
return nil, []error{fmt.Errorf("%q must not be longer than %d characters, got %d", k, maxLength, len(v))}
}

if strings.TrimSpace(v) == "" {
return nil, []error{fmt.Errorf("%q must not be empty", k)}
}

return
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

We have validation.StringLenBetween that should work instead?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same comment

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

stringLenBetween is a private function which is why I wrote the wrapper. I didn't want to make it public out of fear of breaking other code.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It is? looking at the code:
ValidateFunc: validation.StringLenBetween(1, 1024),

5 changes: 5 additions & 0 deletions azurerm/internal/services/network/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Client struct {
LocalNetworkGatewaysClient *network.LocalNetworkGatewaysClient
ProfileClient *network.ProfilesClient
PacketCapturesClient *network.PacketCapturesClient
PrivateEndpointClient *network.PrivateEndpointsClient
PublicIPsClient *network.PublicIPAddressesClient
PublicIPPrefixesClient *network.PublicIPPrefixesClient
RoutesClient *network.RoutesClient
Expand Down Expand Up @@ -82,6 +83,9 @@ func BuildClient(o *common.ClientOptions) *Client {
PacketCapturesClient := network.NewPacketCapturesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&PacketCapturesClient.Client, o.ResourceManagerAuthorizer)

PrivateEndpointClient := network.NewPrivateEndpointsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&PrivateEndpointClient.Client, o.ResourceManagerAuthorizer)

VnetPeeringsClient := network.NewVirtualNetworkPeeringsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&VnetPeeringsClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -136,6 +140,7 @@ func BuildClient(o *common.ClientOptions) *Client {
LocalNetworkGatewaysClient: &LocalNetworkGatewaysClient,
ProfileClient: &ProfileClient,
PacketCapturesClient: &PacketCapturesClient,
PrivateEndpointClient: &PrivateEndpointClient,
PublicIPsClient: &PublicIPsClient,
PublicIPPrefixesClient: &PublicIPPrefixesClient,
RoutesClient: &RoutesClient,
Expand Down
23 changes: 23 additions & 0 deletions azurerm/internal/services/network/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package network

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func ValidatePrivateLinkEndpointSettings(d *schema.ResourceData) error {
privateServiceConnections := d.Get("private_service_connection").([]interface{})

for _, psc := range privateServiceConnections {
privateServiceConnection := psc.(map[string]interface{})
name := privateServiceConnection["name"].(string)

// If this is not a manule connection and the message is set return an error since this does not make sense.
if !privateServiceConnection["is_manual_connection"].(bool) && privateServiceConnection["request_message"].(string) != "" {
return fmt.Errorf(`"private_service_connection":%q is invalid, the "request_message" attribute cannot be set if the "is_manual_connection" attribute is "false"`, name)
}
}

return nil
}
2 changes: 2 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_network_watcher": dataSourceArmNetworkWatcher(),
"azurerm_notification_hub_namespace": dataSourceNotificationHubNamespace(),
"azurerm_notification_hub": dataSourceNotificationHub(),
"azurerm_private_link_endpoint": dataSourceArmPrivateLinkEndpoint(),
"azurerm_platform_image": dataSourceArmPlatformImage(),
"azurerm_policy_definition": dataSourceArmPolicyDefinition(),
"azurerm_proximity_placement_group": dataSourceArmProximityPlacementGroup(),
Expand Down Expand Up @@ -370,6 +371,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_private_dns_zone": resourceArmPrivateDnsZone(),
"azurerm_private_dns_a_record": resourceArmPrivateDnsARecord(),
"azurerm_private_dns_cname_record": resourceArmPrivateDnsCNameRecord(),
"azurerm_private_link_endpoint": resourceArmPrivateLinkEndpoint(),
"azurerm_private_dns_zone_virtual_network_link": resourceArmPrivateDnsZoneVirtualNetworkLink(),
"azurerm_proximity_placement_group": resourceArmProximityPlacementGroup(),
"azurerm_public_ip": resourceArmPublicIp(),
Expand Down
Loading