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_application_load_balancer_subnet_association #23628

Merged
merged 10 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
package servicenetworking
Copy link
Collaborator

Choose a reason for hiding this comment

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

This filename needs to be updated to reflect the new name?


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/go-azure-sdk/resource-manager/servicenetworking/2023-05-01-preview/trafficcontrollerinterface"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
magodo marked this conversation as resolved.
Show resolved Hide resolved
"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{}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we rename it something like ApplicationLoadBalancerAssociationResource or even ApplicationLoadBalancerAssociationSubnetResource to be more specific?


type AssociationModel struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is specific to the Resource, can we make this:

Suggested change
type AssociationModel struct {
type AssociationResourceModel struct {

jackofallops marked this conversation as resolved.
Show resolved Hide resolved
Name string `tfschema:"name"`
ApplicationLoadBalancerId string `tfschema:"application_load_balancer_id"`
SubnetId string `tfschema:"subnet_id"`
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,
magodo marked this conversation as resolved.
Show resolved Hide resolved
},

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

Choose a reason for hiding this comment

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

FWIW the commonschema package has a ResourceIDReference schema property for this purpose:

Suggested change
"application_load_balancer_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: associationsinterface.ValidateTrafficControllerID,
},
"application_load_balancer_id": commonschema.ResourceIDReferenceForceNew(associationsinterface.TrafficControllerId{}),


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

Choose a reason for hiding this comment

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

(same here)

Suggested change
"subnet_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: commonids.ValidateSubnetID,
},
"subnet_id": commonschema.ResourceIDReferenceForceNew(commonids.SubnetId{}),


"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 {
jackofallops marked this conversation as resolved.
Show resolved Hide resolved
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
trafficControllerClient := metadata.Client.ServiceNetworking.TrafficControllerInterface
client := metadata.Client.ServiceNetworking.AssociationsInterface

var config AssociationModel
if err := metadata.Decode(&config); err != nil {
return fmt.Errorf("decoding %v", err)
}

parsedTrafficControllerId, err := associationsinterface.ParseTrafficControllerID(config.ApplicationLoadBalancerId)
magodo marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

controllerId := trafficcontrollerinterface.NewTrafficControllerID(parsedTrafficControllerId.SubscriptionId, parsedTrafficControllerId.ResourceGroupName, parsedTrafficControllerId.TrafficControllerName)
controller, err := trafficControllerClient.Get(ctx, controllerId)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", controllerId, err)
}

if controller.Model == nil {
return fmt.Errorf("retrieving %s: Model was nil", controllerId)
}

loc := controller.Model.Location
magodo marked this conversation as resolved.
Show resolved Hide resolved

id := associationsinterface.NewAssociationID(parsedTrafficControllerId.SubscriptionId, parsedTrafficControllerId.ResourceGroupName, parsedTrafficControllerId.TrafficControllerName, config.Name)
existing, err := client.Get(ctx, id)
magodo marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
if !response.WasNotFound(existing.HttpResponse) {
magodo marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("checking for presence of exisiting %s: %+v", id, err)
}
}

if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError(t.ResourceType(), id.ID())
magodo marked this conversation as resolved.
Show resolved Hide resolved
}
Copy link
Contributor

Choose a reason for hiding this comment

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

here we're assuming we're getting a model back:

Suggested change
}
}
if controller.Model == nil {
return fmt.Errorf("retrieving parent %s: model was nil", *albId)
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

@ziyeqf could you address this comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I have already updated code per this comment?


association := associationsinterface.Association{
Location: location.Normalize(loc),
Properties: &associationsinterface.AssociationProperties{
Subnet: &associationsinterface.AssociationSubnet{
Id: config.SubnetId,
},
AssociationType: associationsinterface.AssociationTypeSubnets,
katbyte marked this conversation as resolved.
Show resolved Hide resolved
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
},
Tags: tags.FromTypedObject(config.Tags),
},

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I cant use it because it returns map[string]*string, while sdk needs *map[string]string

Copy link
Contributor

Choose a reason for hiding this comment

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

There's two imports for tags - the legacy one within this provider (./internal/tags) and the one within go-azure-helpers (github.com/hashicorp/go-azure-helpers/resourcemanager/tags) - updating the reference to use go-azure-helpers should solve that?

Copy link
Collaborator

Choose a reason for hiding this comment

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

please follow toms advice here @ziyeqf

Copy link
Contributor Author

@ziyeqf ziyeqf Nov 10, 2023

Choose a reason for hiding this comment

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

I'm already using "github.com/hashicorp/go-azure-helpers/resourcemanager/tags" now, is it correct?

}

if len(config.Tags) > 0 {
association.Tags = &config.Tags
}
Copy link
Contributor

Choose a reason for hiding this comment

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

we should always be setting tags here, else you can't remove them?


if err := client.CreateOrUpdateThenPoll(ctx, id, association); err != nil {
return fmt.Errorf("creating %s: %+v", id, 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.AssociationsInterface

id, err := associationsinterface.ParseAssociationID(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("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)

if prop := model.Properties; prop != nil {
if prop.Subnet != nil {
state.SubnetId = prop.Subnet.Id
magodo marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

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 {
client := metadata.Client.ServiceNetworking.AssociationsInterface

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

There's no need to wrap this one:

Suggested change
return fmt.Errorf("parsing id %v", err)
return err

}

// thought `AssociationSubnetUpdate` is defined in the SDK, per testing the subnet id can not be updated.
magodo marked this conversation as resolved.
Show resolved Hide resolved
associationUpdate := associationsinterface.AssociationUpdate{}

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

if _, err = client.Update(ctx, *id, associationUpdate); err != nil {
return fmt.Errorf("updating `azurerm_application_load_balancer_association` %s: %v", id.ID(), err)
magodo marked this conversation as resolved.
Show resolved Hide resolved
}

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.AssociationsInterface

id, err := associationsinterface.ParseAssociationID(metadata.ResourceData.Id())
if err != nil {
return err
}

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

Choose a reason for hiding this comment

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

this should be using the Resource ID itself, since the .String() methods is then automatically called:

Suggested change
return fmt.Errorf("deleting %s: %v", id.ID(), err)
return fmt.Errorf("deleting %s: %v", *id, err)

(rather than this which outputs the ID, which is less descriptive)

}

return nil
},
}
}
Loading