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 #22517

Merged
merged 13 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
3 changes: 3 additions & 0 deletions .github/labeler-issue-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ service/service-fabric:
service/service-fabric-managed-cluster:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_service_fabric_managed_cluster((.|\n)*)###'

service/service-networking:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_application_load_balancer((.|\n)*)###'

service/signalr:
- '### (|New or )Affected Resource\(s\)\/Data Source\(s\)((.|\n)*)azurerm_(signalr_s|web_pubsub)((.|\n)*)###'

Expand Down
3 changes: 3 additions & 0 deletions .github/labeler-pull-request-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ service/service-fabric:
service/service-fabric-managed-cluster:
- internal/services/servicefabricmanaged/**/*

service/service-networking:
- internal/services/servicenetworking/**/*

service/signalr:
- internal/services/signalr/**/*

Expand Down
1 change: 1 addition & 0 deletions .teamcity/components/generated/services.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ var services = mapOf(
"sentinel" to "Sentinel",
"servicefabric" to "Service Fabric",
"servicefabricmanaged" to "Service Fabric Managed Clusters",
"servicenetworking" to "Service Networking",
"servicebus" to "ServiceBus",
"serviceconnector" to "ServiceConnector",
"signalr" to "SignalR",
Expand Down
5 changes: 5 additions & 0 deletions internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ import (
serviceConnector "github.com/hashicorp/terraform-provider-azurerm/internal/services/serviceconnector/client"
serviceFabric "github.com/hashicorp/terraform-provider-azurerm/internal/services/servicefabric/client"
serviceFabricManaged "github.com/hashicorp/terraform-provider-azurerm/internal/services/servicefabricmanaged/client"
serviceNetworking "github.com/hashicorp/terraform-provider-azurerm/internal/services/servicenetworking/client"
signalr "github.com/hashicorp/terraform-provider-azurerm/internal/services/signalr/client"
appPlatform "github.com/hashicorp/terraform-provider-azurerm/internal/services/springcloud/client"
sql "github.com/hashicorp/terraform-provider-azurerm/internal/services/sql/client"
Expand Down Expand Up @@ -261,6 +262,7 @@ type Client struct {
ServiceConnector *serviceConnector.Client
ServiceFabric *serviceFabric.Client
ServiceFabricManaged *serviceFabricManaged.Client
ServiceNetworking *serviceNetworking.Client
SignalR *signalr.Client
Storage *storage.Client
StorageMover *storageMover.Client
Expand Down Expand Up @@ -565,6 +567,9 @@ func (client *Client) Build(ctx context.Context, o *common.ClientOptions) error
client.ServiceConnector = serviceConnector.NewClient(o)
client.ServiceFabric = serviceFabric.NewClient(o)
client.ServiceFabricManaged = serviceFabricManaged.NewClient(o)
if client.ServiceNetworking, err = serviceNetworking.NewClient(o); err != nil {
return fmt.Errorf("building clients for ServiceNetworking: %+v", err)
}
if client.SignalR, err = signalr.NewClient(o); err != nil {
return fmt.Errorf("building clients for SignalR: %+v", err)
}
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/services/serviceconnector"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/servicefabric"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/servicefabricmanaged"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/servicenetworking"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/signalr"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/springcloud"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/sql"
Expand Down Expand Up @@ -189,6 +190,7 @@ func SupportedTypedServices() []sdk.TypedServiceRegistration {
sentinel.Registration{},
serviceconnector.Registration{},
servicefabricmanaged.Registration{},
servicenetworking.Registration{},
storage.Registration{},
storagemover.Registration{},
signalr.Registration{},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
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/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/servicenetworking/2023-05-01-preview/trafficcontrollerinterface"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type ApplicationLoadBalancerResource struct{}

type ApplicationLoadBalancerModel struct {
Name string `tfschema:"name"`
ResourceGroupName string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
ConfigurationEndpoints []string `tfschema:"configuration_endpoint"`
Tags map[string]string `tfschema:"tags"`
}

var _ sdk.ResourceWithUpdate = ApplicationLoadBalancerResource{}

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

"resource_group_name": commonschema.ResourceGroupName(),

"location": commonschema.Location(),

"tags": tags.Schema(),
}
}

func (t ApplicationLoadBalancerResource) Attributes() map[string]*schema.Schema {
return map[string]*schema.Schema{
"configuration_endpoint": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
}
}

func (t ApplicationLoadBalancerResource) ModelObject() interface{} {
return &ApplicationLoadBalancerModel{}
}

func (t ApplicationLoadBalancerResource) ResourceType() string {
return "azurerm_application_load_balancer"
}

func (t ApplicationLoadBalancerResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return trafficcontrollerinterface.ValidateTrafficControllerID
}

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

client := metadata.Client.ServiceNetworking.ServiceNetworkingClient
SubscriptionId := metadata.Client.Account.SubscriptionId

id := trafficcontrollerinterface.NewTrafficControllerID(SubscriptionId, plan.ResourceGroupName, plan.Name)

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

if !response.WasNotFound(existing.HttpResponse) {
return metadata.ResourceRequiresImport(t.ResourceType(), id)
}

controller := trafficcontrollerinterface.TrafficController{
Location: location.Normalize(plan.Location),
Tags: pointer.To(plan.Tags),
}

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

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

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

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

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

state := ApplicationLoadBalancerModel{
Name: id.TrafficControllerName,
ResourceGroupName: id.ResourceGroupName,
}

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

if prop := model.Properties; prop != nil {
state.ConfigurationEndpoints = pointer.From(prop.ConfigurationEndpoints)
}
}

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

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

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

existing, err := client.TrafficControllerInterface.Get(ctx, *id)
if err != nil {
return fmt.Errorf("retreiving %s: %+v", id, err)
}

if existing.Model == nil {
return fmt.Errorf("existing Traffic Controller %s has no model", id)
}

controller := existing.Model

if metadata.ResourceData.HasChange("tags") {
if len(plan.Tags) > 0 {
controller.Tags = pointer.To(plan.Tags)
} else {
controller.Tags = nil
}
}

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

return nil
},
}
}

func (t ApplicationLoadBalancerResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
id, err := trafficcontrollerinterface.ParseTrafficControllerID(metadata.ResourceData.Id())
if err != nil {
return fmt.Errorf("parsing %s: %+v", metadata.ResourceData.Id(), err)
}

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

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

return nil
},
}
}
Loading