Skip to content

Commit

Permalink
New Resource: `azurerm_private_endpoint_application_security_group_as…
Browse files Browse the repository at this point in the history
…sociation` (#19825)

Co-authored-by: kt <[email protected]>
Fixes #17665
  • Loading branch information
liuwuliuyun authored Jan 10, 2023
1 parent 4a84c78 commit c3e143f
Show file tree
Hide file tree
Showing 6 changed files with 916 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package parse

import (
"fmt"
"strings"

"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
)

var _ resourceids.Id = PrivateEndpointApplicationSecurityGroupAssociationId{}

type PrivateEndpointApplicationSecurityGroupAssociationId struct {
PrivateEndpointId PrivateEndpointId
ApplicationSecurityGroupId ApplicationSecurityGroupId
}

func (p PrivateEndpointApplicationSecurityGroupAssociationId) ID() string {
return fmt.Sprintf("%s|%s", p.PrivateEndpointId.ID(), p.ApplicationSecurityGroupId.ID())
}

func (p PrivateEndpointApplicationSecurityGroupAssociationId) String() string {
components := []string{
fmt.Sprintf("PrivateEndpointId %s", p.PrivateEndpointId.ID()),
fmt.Sprintf("ApplicationSecurityGroupId %s", p.ApplicationSecurityGroupId.ID()),
}
return fmt.Sprintf("Private Endpoint Application Security Group Association: %s", strings.Join(components, " / "))
}

func NewPrivateEndpointApplicationSecurityGroupAssociationId(endpointId PrivateEndpointId, securityGroupId ApplicationSecurityGroupId) PrivateEndpointApplicationSecurityGroupAssociationId {
return PrivateEndpointApplicationSecurityGroupAssociationId{
PrivateEndpointId: endpointId,
ApplicationSecurityGroupId: securityGroupId,
}
}

func PrivateEndpointApplicationSecurityGroupAssociationID(input string) (PrivateEndpointApplicationSecurityGroupAssociationId, error) {
splitId := strings.Split(input, "|")
if len(splitId) != 2 {
return PrivateEndpointApplicationSecurityGroupAssociationId{}, fmt.Errorf("expected ID to be in the format {PrivateEndpointId}|{ApplicationSecurityGroupId} but got %q", input)
}

endpointId, err := PrivateEndpointID(splitId[0])
if err != nil {
return PrivateEndpointApplicationSecurityGroupAssociationId{}, err
}

securityGroupId, err := ApplicationSecurityGroupID(splitId[1])
if err != nil {
return PrivateEndpointApplicationSecurityGroupAssociationId{}, err
}

if endpointId == nil || securityGroupId == nil {
return PrivateEndpointApplicationSecurityGroupAssociationId{}, fmt.Errorf("parse error, both PrivateEndpointId and ApplicationSecurityGroupId should not be nil")
}

return PrivateEndpointApplicationSecurityGroupAssociationId{
PrivateEndpointId: *endpointId,
ApplicationSecurityGroupId: *securityGroupId,
}, nil
}

func PrivateEndpointApplicationSecurityGroupAssociationIDValidation(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 := PrivateEndpointApplicationSecurityGroupAssociationID(v); err != nil {
errors = append(errors, err)
}

return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package parse

import (
"testing"
)

func TestPrivateEndpointApplicationSecurityGroupAssociationID(t *testing.T) {
testData := []struct {
Name string
Input string
Expect *PrivateEndpointApplicationSecurityGroupAssociationId
Error bool
}{
{
Name: "Empty",
Input: "",
Error: true,
},
{
Name: "One Segment",
Input: "hello",
Error: true,
},
{
Name: "Two Segments Invalid ID's",
Input: "hello|world",
Error: true,
},
{
Name: "Missing ASG Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Network/privateEndpoints/endpoints1",
Error: true,
},
{
Name: "Private Endpoint Id",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Network/privateEndpoints/endpoints1",
Error: true,
},
{
Name: "Application Security Group ID",
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Network/applicationSecurityGroups/securityGroup1",
Error: true,
},
{
Name: "Nat Gateway / Public IP Association ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Network/privateEndpoints/endpoints1|/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.Network/applicationSecurityGroups/securityGroup1",
Error: false,
Expect: &PrivateEndpointApplicationSecurityGroupAssociationId{
ApplicationSecurityGroupId: ApplicationSecurityGroupId{
ResourceGroup: "mygroup1",
SubscriptionId: "00000000-0000-0000-0000-000000000000",
Name: "securityGroup1",
},
PrivateEndpointId: PrivateEndpointId{
ResourceGroup: "group1",
SubscriptionId: "00000000-0000-0000-0000-000000000000",
Name: "endpoints1",
},
},
},
}
for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

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

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.PrivateEndpointId.Name != v.Expect.PrivateEndpointId.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expect.PrivateEndpointId.Name, actual.PrivateEndpointId.Name)
}

if actual.ApplicationSecurityGroupId.ResourceGroup != v.Expect.ApplicationSecurityGroupId.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expect.ApplicationSecurityGroupId.ResourceGroup, actual.ApplicationSecurityGroupId.ResourceGroup)
}
}
}
Loading

0 comments on commit c3e143f

Please sign in to comment.