Skip to content

Commit

Permalink
new resource: azurerm_application_load_balancer_association
Browse files Browse the repository at this point in the history
Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com>
  • Loading branch information
ziyeqf committed Aug 29, 2023
1 parent 95c0fd3 commit 2b27747
Show file tree
Hide file tree
Showing 4 changed files with 468 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/services/servicenetworking/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (r Registration) Resources() []sdk.Resource {
return []sdk.Resource{
ApplicationLoadBalancerResource{},
FrontendsResource{},
AssociationResource{},
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
package servicenetworking

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/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/servicenetworking/2023-05-01-preview/associationsinterface"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"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"
)

type AssociationResource struct{}

type AssociationModel struct {
Name string `tfschema:"name"`
ApplicationLoadBalancerId string `tfschema:"application_load_balancer_id"`
SubnetId string `tfschema:"subnet_id"`
Location string `tfschema:"location"`
Tags map[string]string `tfschema:"tags"`
}

var _ sdk.ResourceWithUpdate = AssociationResource{}

func (t AssociationResource) Arguments() map[string]*schema.Schema {
return map[string]*schema.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"application_load_balancer_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: associationsinterface.ValidateTrafficControllerID,
},

"subnet_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: commonids.ValidateSubnetID,
},

"location": commonschema.Location(),

"tags": commonschema.Tags(),
}
}

func (t AssociationResource) Attributes() map[string]*schema.Schema {
return map[string]*schema.Schema{}
}

func (t AssociationResource) ModelObject() interface{} {
return &AssociationModel{}
}

func (t AssociationResource) ResourceType() string {
return "azurerm_application_load_balancer_association"
}

func (t AssociationResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return associationsinterface.ValidateAssociationID
}
func (t AssociationResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var plan AssociationModel
if err := metadata.Decode(&plan); err != nil {
return fmt.Errorf("decoding %v", err)
}

client := metadata.Client.ServiceNetworking.ServiceNetworkingClient.AssociationsInterface

parsedTrafficControllerId, err := associationsinterface.ParseTrafficControllerID(plan.ApplicationLoadBalancerId)
if err != nil {
return fmt.Errorf("parsing traffic controller id %v", err)
}

id := associationsinterface.NewAssociationID(parsedTrafficControllerId.SubscriptionId, parsedTrafficControllerId.ResourceGroupName, parsedTrafficControllerId.TrafficControllerName, plan.Name)

existing, err := client.Get(ctx, id)
if err != nil {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of exisiting %s: %+v", id, err)
}
}

if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError(t.ResourceType(), id.ID())
}

association := associationsinterface.Association{
Location: location.Normalize(plan.Location),
Properties: &associationsinterface.AssociationProperties{
Subnet: &associationsinterface.AssociationSubnet{
Id: plan.SubnetId,
},
AssociationType: associationsinterface.AssociationTypeSubnets,
},
}

if len(plan.Tags) > 0 {
association.Tags = &plan.Tags
}

if err := client.CreateOrUpdateThenPoll(ctx, id, association); err != nil {
return fmt.Errorf("creating %v", err)
}

metadata.SetID(id)
return nil
},
}
}

func (t AssociationResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.ServiceNetworking.ServiceNetworkingClient.AssociationsInterface

id, err := associationsinterface.ParseAssociationID(metadata.ResourceData.Id())
if err != nil {
return fmt.Errorf("parsing id %v", err)
}

resp, err := client.Get(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return metadata.MarkAsGone(id)
}
return fmt.Errorf("retreiving %s: %v", id.ID(), err)
}

trafficControllerId := associationsinterface.NewTrafficControllerID(id.SubscriptionId, id.ResourceGroupName, id.TrafficControllerName)
state := AssociationModel{
Name: id.AssociationName,
ApplicationLoadBalancerId: trafficControllerId.ID(),
}

if model := resp.Model; model != nil {
state.Tags = pointer.From(model.Tags)
state.Location = location.Normalize(model.Location)

if prop := model.Properties; prop != nil {
if prop.Subnet != nil {
state.SubnetId = prop.Subnet.Id
}
}
}

return metadata.Encode(&state)
},
}
}

func (t AssociationResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var plan AssociationModel
if err := metadata.Decode(&plan); err != nil {
return fmt.Errorf("decoding %v", err)
}

id, err := associationsinterface.ParseAssociationID(metadata.ResourceData.Id())
if err != nil {
return fmt.Errorf("parsing id %v", err)
}

client := metadata.Client.ServiceNetworking.ServiceNetworkingClient.AssociationsInterface
resp, err := client.Get(ctx, *id)
if err != nil {
return fmt.Errorf("retrieving %s: %v", id.ID(), err)
}

if resp.Model == nil {
return fmt.Errorf("retrieving %s: model is nil", id.ID())
}
association := *resp.Model

if metadata.ResourceData.HasChange("tags") {
association.Tags = &plan.Tags
}

if err = client.CreateOrUpdateThenPoll(ctx, *id, association); err != nil {
return fmt.Errorf("updating %s: %v", id.ID(), err)
}
return nil
},
}
}

func (t AssociationResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.ServiceNetworking.ServiceNetworkingClient.AssociationsInterface

id, err := associationsinterface.ParseAssociationID(metadata.ResourceData.Id())
if err != nil {
return fmt.Errorf("parsing id %s: %v", metadata.ResourceData.Id(), err)
}

if err = client.DeleteThenPoll(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %v", id.ID(), err)
}

return nil
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package servicenetworking_test

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/servicenetworking/2023-05-01-preview/associationsinterface"
"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"
)

type AssociationResource struct{}

func (r AssociationResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := associationsinterface.ParseAssociationID(state.ID)
if err != nil {
return nil, fmt.Errorf("while parsing resource ID: %+v", err)
}

resp, err := clients.ServiceNetworking.ServiceNetworkingClient.AssociationsInterface.Get(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return pointer.To(false), nil
}
return nil, fmt.Errorf("while checking existence for %q: %+v", id.String(), err)
}
return pointer.To(resp.Model != nil), nil
}

func TestAccServiceNetworkingALBAssociation_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_application_load_balancer_association", "test")

r := AssociationResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccServiceNetworkingALBAssociation_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_application_load_balancer_association", "test")

r := AssociationResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccServiceNetworkingALBAssociation_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_application_load_balancer_association", "test")

r := AssociationResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (r AssociationResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s
resource "azurerm_virtual_network" "test" {
name = "acctestvnet%[2]d"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
}
resource "azurerm_subnet" "test" {
name = "acctestsubnet%[2]d"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefixes = ["10.0.1.0/24"]
delegation {
name = "delegation"
service_delegation {
name = "Microsoft.ServiceNetworking/trafficControllers"
actions = ["Microsoft.Network/virtualNetworks/subnets/join/action"]
}
}
}
`, ApplicationLoadBalancerResource{}.basic(data), data.RandomInteger)
}

func (r AssociationResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_application_load_balancer_association" "test" {
name = "acct-%d"
application_load_balancer_id = azurerm_application_load_balancer.test.id
subnet_id = azurerm_subnet.test.id
location = azurerm_alb.test.location
}
`, r.template(data), data.RandomInteger)
}

func (r AssociationResource) complete(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_application_load_balancer_association" "test" {
name = "acct-%d"
alb_id = azurerm_application_load_balancer.test.id
subnet_id = azurerm_subnet.test.id
location = azurerm_application_load_balancer.test.location
tags = {
key = "value"
}
}
`, r.template(data), data.RandomInteger)
}
Loading

0 comments on commit 2b27747

Please sign in to comment.