-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Resource
azurerm_resource_management_private_link_association
(#…
…23546) * new resource private_link_association * new resource azurerm_resource_management_private_link_association * fix * fix * change name as required * change to optional
- Loading branch information
Showing
20 changed files
with
1,006 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
205 changes: 205 additions & 0 deletions
205
internal/services/resource/resource_management_private_link_association_resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
package resource | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/hashicorp/go-azure-helpers/lang/pointer" | ||
"github.com/hashicorp/go-azure-helpers/lang/response" | ||
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" | ||
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation" | ||
"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 = ResourceManagementPrivateLinkAssociationResource{} | ||
|
||
type ResourceManagementPrivateLinkAssociationResource struct{} | ||
|
||
func (r ResourceManagementPrivateLinkAssociationResource) ModelObject() interface{} { | ||
return &ResourceManagementPrivateLinkAssociationResourceSchema{} | ||
} | ||
|
||
type ResourceManagementPrivateLinkAssociationResourceSchema struct { | ||
ManagementGroupId string `tfschema:"management_group_id"` | ||
Name string `tfschema:"name"` | ||
ResourceManagementPrivateLinkId string `tfschema:"resource_management_private_link_id"` | ||
PublicNetworkAccessEnabled bool `tfschema:"public_network_access_enabled"` | ||
TenantID string `tfschema:"tenant_id"` | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkAssociationResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { | ||
return privatelinkassociation.ValidatePrivateLinkAssociationID | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkAssociationResource) ResourceType() string { | ||
return "azurerm_resource_management_private_link_association" | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkAssociationResource) Arguments() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{ | ||
"name": { | ||
ForceNew: true, | ||
Optional: true, | ||
Type: pluginsdk.TypeString, | ||
ValidateFunc: validation.IsUUID, | ||
}, | ||
"management_group_id": { | ||
ForceNew: true, | ||
Required: true, | ||
Type: pluginsdk.TypeString, | ||
ValidateFunc: commonids.ValidateManagementGroupID, | ||
}, | ||
"resource_management_private_link_id": { | ||
ForceNew: true, | ||
Required: true, | ||
Type: pluginsdk.TypeString, | ||
ValidateFunc: resourcemanagementprivatelink.ValidateResourceManagementPrivateLinkID, | ||
}, | ||
"public_network_access_enabled": { | ||
ForceNew: true, | ||
Required: true, | ||
Type: pluginsdk.TypeBool, | ||
}, | ||
} | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkAssociationResource) Attributes() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{ | ||
"tenant_id": { | ||
Computed: true, | ||
Type: pluginsdk.TypeString, | ||
}, | ||
} | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkAssociationResource) Create() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.Resource.PrivateLinkAssociationClient | ||
|
||
var config ResourceManagementPrivateLinkAssociationResourceSchema | ||
if err := metadata.Decode(&config); err != nil { | ||
return fmt.Errorf("decoding: %+v", err) | ||
} | ||
|
||
managementGroupId, err := commonids.ParseManagementGroupID(config.ManagementGroupId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var name string | ||
if config.Name != "" { | ||
name = config.Name | ||
} | ||
|
||
if name == "" { | ||
name = uuid.New().String() | ||
} | ||
|
||
id := privatelinkassociation.NewPrivateLinkAssociationID(managementGroupId.GroupId, 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 := privatelinkassociation.PrivateLinkAssociationObject{ | ||
Properties: &privatelinkassociation.PrivateLinkAssociationProperties{ | ||
PrivateLink: pointer.To(config.ResourceManagementPrivateLinkId), | ||
PublicNetworkAccess: r.expandPublicNetworkAccess(config.PublicNetworkAccessEnabled), | ||
}, | ||
} | ||
|
||
if _, err := client.Put(ctx, id, payload); err != nil { | ||
return fmt.Errorf("creating %s: %+v", id, err) | ||
} | ||
|
||
metadata.SetID(id) | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkAssociationResource) Read() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 5 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.Resource.PrivateLinkAssociationClient | ||
schema := ResourceManagementPrivateLinkAssociationResourceSchema{} | ||
|
||
id, err := privatelinkassociation.ParsePrivateLinkAssociationID(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.ManagementGroupId = commonids.NewManagementGroupID(id.GroupId).ID() | ||
schema.Name = id.PlaId | ||
|
||
if model := resp.Model; model != nil { | ||
if prop := model.Properties; prop != nil { | ||
schema.PublicNetworkAccessEnabled = r.flattenPublicNetworkAccess(prop.PublicNetworkAccess) | ||
schema.TenantID = pointer.From(prop.TenantID) | ||
schema.ResourceManagementPrivateLinkId = pointer.From(prop.PrivateLink) | ||
} | ||
} | ||
|
||
return metadata.Encode(&schema) | ||
}, | ||
} | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkAssociationResource) Delete() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.Resource.PrivateLinkAssociationClient | ||
|
||
id, err := privatelinkassociation.ParsePrivateLinkAssociationID(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 | ||
}, | ||
} | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkAssociationResource) expandPublicNetworkAccess(input bool) *privatelinkassociation.PublicNetworkAccessOptions { | ||
output := privatelinkassociation.PublicNetworkAccessOptionsEnabled | ||
if !input { | ||
output = privatelinkassociation.PublicNetworkAccessOptionsDisabled | ||
} | ||
|
||
return &output | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkAssociationResource) flattenPublicNetworkAccess(input *privatelinkassociation.PublicNetworkAccessOptions) bool { | ||
if input == nil || *input == privatelinkassociation.PublicNetworkAccessOptionsEnabled { | ||
return true | ||
} | ||
|
||
return false | ||
} |
147 changes: 147 additions & 0 deletions
147
internal/services/resource/resource_management_private_link_association_resource_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package resource_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/privatelinkassociation" | ||
"github.com/hashicorp/go-uuid" | ||
"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 ResourceManagementPrivateLinkAssociationTestResource struct{} | ||
|
||
func TestAccResourceManagementPrivateLinkAssociation_basic(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "azurerm_resource_management_private_link_association", "test") | ||
r := ResourceManagementPrivateLinkAssociationTestResource{} | ||
|
||
data.ResourceTest(t, r, []acceptance.TestStep{ | ||
{ | ||
Config: r.basic(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
data.ImportStep(), | ||
}) | ||
} | ||
|
||
func TestAccResourceManagementPrivateLinkAssociation_requiresImport(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "azurerm_resource_management_private_link_association", "test") | ||
r := ResourceManagementPrivateLinkAssociationTestResource{} | ||
|
||
data.ResourceTest(t, r, []acceptance.TestStep{ | ||
{ | ||
Config: r.basic(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
data.RequiresImportErrorStep(r.requiresImport), | ||
}) | ||
} | ||
|
||
func TestAccResourceManagementPrivateLinkAssociation_generateName(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "azurerm_resource_management_private_link_association", "test") | ||
r := ResourceManagementPrivateLinkAssociationTestResource{} | ||
|
||
data.ResourceTest(t, r, []acceptance.TestStep{ | ||
{ | ||
Config: r.generateName(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
data.ImportStep(), | ||
}) | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkAssociationTestResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { | ||
id, err := privatelinkassociation.ParsePrivateLinkAssociationID(state.ID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := clients.Resource.PrivateLinkAssociationClient.Get(ctx, *id) | ||
if err != nil { | ||
return nil, fmt.Errorf("reading %s: %+v", *id, err) | ||
} | ||
|
||
return utils.Bool(resp.Model != nil), nil | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkAssociationTestResource) basic(data acceptance.TestData) string { | ||
randomUUID, _ := uuid.GenerateUUID() | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
%s | ||
resource "azurerm_resource_management_private_link_association" "test" { | ||
name = "%s" | ||
management_group_id = data.azurerm_management_group.test.id | ||
resource_management_private_link_id = azurerm_resource_management_private_link.test.id | ||
public_network_access_enabled = true | ||
} | ||
`, r.template(data), randomUUID) | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkAssociationTestResource) generateName(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
%s | ||
resource "azurerm_resource_management_private_link_association" "test" { | ||
management_group_id = data.azurerm_management_group.test.id | ||
resource_management_private_link_id = azurerm_resource_management_private_link.test.id | ||
public_network_access_enabled = true | ||
lifecycle { | ||
ignore_changes = [name] | ||
} | ||
} | ||
`, r.template(data)) | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkAssociationTestResource) requiresImport(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
%s | ||
resource "azurerm_resource_management_private_link_association" "import" { | ||
name = azurerm_resource_management_private_link_association.test.name | ||
management_group_id = azurerm_resource_management_private_link_association.test.management_group_id | ||
resource_management_private_link_id = azurerm_resource_management_private_link_association.test.resource_management_private_link_id | ||
public_network_access_enabled = azurerm_resource_management_private_link_association.test.public_network_access_enabled | ||
} | ||
`, r.basic(data)) | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkAssociationTestResource) template(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
data "azurerm_client_config" "test" {} | ||
data "azurerm_management_group" "test" { | ||
name = data.azurerm_client_config.test.tenant_id | ||
} | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestrg-%d" | ||
location = "%s" | ||
} | ||
resource "azurerm_resource_management_private_link" "test" { | ||
location = azurerm_resource_group.test.location | ||
name = "acctestrmpl-%[1]d" | ||
resource_group_name = azurerm_resource_group.test.name | ||
} | ||
`, data.RandomInteger, data.Locations.Primary) | ||
} |
Oops, something went wrong.