-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
(#23098)
* new resource azurerm_resource_management_private_link * fix review comment * fix doc * fix test
- Loading branch information
Showing
19 changed files
with
909 additions
and
18 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
147 changes: 147 additions & 0 deletions
147
internal/services/resource/resource_management_private_link_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,147 @@ | ||
package resource | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hashicorp/go-azure-helpers/lang/pointer" | ||
"github.com/hashicorp/go-azure-helpers/lang/response" | ||
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" | ||
"github.com/hashicorp/go-azure-helpers/resourcemanager/location" | ||
"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 = ResourceManagementPrivateLinkResource{} | ||
|
||
type ResourceManagementPrivateLinkResource struct{} | ||
|
||
func (r ResourceManagementPrivateLinkResource) ModelObject() interface{} { | ||
return &ResourceManagementPrivateLinkResourceSchema{} | ||
} | ||
|
||
type ResourceManagementPrivateLinkResourceSchema struct { | ||
Location string `tfschema:"location"` | ||
Name string `tfschema:"name"` | ||
ResourceGroupName string `tfschema:"resource_group_name"` | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { | ||
return resourcemanagementprivatelink.ValidateResourceManagementPrivateLinkID | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkResource) ResourceType() string { | ||
return "azurerm_resource_management_private_link" | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkResource) Arguments() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{ | ||
"name": { | ||
ForceNew: true, | ||
Required: true, | ||
Type: pluginsdk.TypeString, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
"resource_group_name": commonschema.ResourceGroupName(), | ||
"location": commonschema.Location(), | ||
} | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkResource) Attributes() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{} | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkResource) Create() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.Resource.ResourceManagementPrivateLinkClient | ||
|
||
var config ResourceManagementPrivateLinkResourceSchema | ||
if err := metadata.Decode(&config); err != nil { | ||
return fmt.Errorf("decoding: %+v", err) | ||
} | ||
|
||
subscriptionId := metadata.Client.Account.SubscriptionId | ||
|
||
id := resourcemanagementprivatelink.NewResourceManagementPrivateLinkID(subscriptionId, config.ResourceGroupName, config.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 := resourcemanagementprivatelink.ResourceManagementPrivateLinkLocation{ | ||
Location: pointer.To(location.Normalize(config.Location)), | ||
} | ||
|
||
if _, err := client.Put(ctx, id, payload); err != nil { | ||
return fmt.Errorf("creating %s: %+v", id, err) | ||
} | ||
|
||
metadata.SetID(id) | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkResource) Read() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 5 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.Resource.ResourceManagementPrivateLinkClient | ||
|
||
id, err := resourcemanagementprivatelink.ParseResourceManagementPrivateLinkID(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 := ResourceManagementPrivateLinkResourceSchema{ | ||
Name: id.ResourceManagementPrivateLinkName, | ||
ResourceGroupName: id.ResourceGroupName, | ||
} | ||
|
||
if model := resp.Model; model != nil { | ||
schema.Location = location.NormalizeNilable(model.Location) | ||
} | ||
|
||
return metadata.Encode(&schema) | ||
}, | ||
} | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkResource) Delete() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.Resource.ResourceManagementPrivateLinkClient | ||
|
||
id, err := resourcemanagementprivatelink.ParseResourceManagementPrivateLinkID(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 | ||
}, | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
internal/services/resource/resource_management_private_link_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,108 @@ | ||
package resource_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-05-01/resourcemanagementprivatelink" | ||
"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 ResourceManagementPrivateLinkTestResource struct{} | ||
|
||
func TestAccResourceManagementPrivateLink_basic(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "azurerm_resource_management_private_link", "test") | ||
r := ResourceManagementPrivateLinkTestResource{} | ||
|
||
data.ResourceTest(t, r, []acceptance.TestStep{ | ||
{ | ||
Config: r.basic(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
data.ImportStep(), | ||
}) | ||
} | ||
|
||
func TestAccResourceManagementPrivateLink_requiresImport(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "azurerm_resource_management_private_link", "test") | ||
r := ResourceManagementPrivateLinkTestResource{} | ||
|
||
data.ResourceTest(t, r, []acceptance.TestStep{ | ||
{ | ||
Config: r.basic(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
data.RequiresImportErrorStep(r.requiresImport), | ||
}) | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkTestResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { | ||
id, err := resourcemanagementprivatelink.ParseResourceManagementPrivateLinkID(state.ID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := clients.Resource.ResourceManagementPrivateLinkClient.Get(ctx, *id) | ||
if err != nil { | ||
return nil, fmt.Errorf("reading %s: %+v", *id, err) | ||
} | ||
|
||
return utils.Bool(resp.Model != nil), nil | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkTestResource) basic(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
%s | ||
resource "azurerm_resource_management_private_link" "test" { | ||
location = azurerm_resource_group.test.location | ||
name = "acctestrmpl-${var.random_string}" | ||
resource_group_name = azurerm_resource_group.test.name | ||
} | ||
`, r.template(data)) | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkTestResource) requiresImport(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
%s | ||
resource "azurerm_resource_management_private_link" "import" { | ||
location = azurerm_resource_management_private_link.test.location | ||
name = azurerm_resource_management_private_link.test.name | ||
resource_group_name = azurerm_resource_management_private_link.test.resource_group_name | ||
} | ||
`, r.basic(data)) | ||
} | ||
|
||
func (r ResourceManagementPrivateLinkTestResource) template(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
variable "primary_location" { | ||
default = %q | ||
} | ||
variable "random_integer" { | ||
default = %d | ||
} | ||
variable "random_string" { | ||
default = %q | ||
} | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestrg-${var.random_integer}" | ||
location = var.primary_location | ||
} | ||
`, data.Locations.Primary, data.RandomInteger, data.RandomString) | ||
} |
Oops, something went wrong.