Skip to content

Commit

Permalink
New Resource: azurerm_application_load_balancer (#22517)
Browse files Browse the repository at this point in the history
  • Loading branch information
ziyeqf authored Sep 27, 2023
1 parent b2d11f0 commit e1eab1a
Show file tree
Hide file tree
Showing 63 changed files with 3,242 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/labeler-issue-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,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 @@ -312,6 +312,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 @@ -112,6 +112,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
3 changes: 3 additions & 0 deletions .teamcity/components/settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ var serviceTestConfigurationOverrides = mapOf(
// Synapse is only available in certain locations
"synapse" to testConfiguration(locationOverride = LocationConfiguration("westeurope", "francecentral", "eastus2", false)),

// ServiceNetworking is available in certain locations
"servicenetworking" to testConfiguration(locationOverride = LocationConfiguration("eastus","westus","westeurope", false)),

// Currently, we have insufficient quota to actually run these, but there are a few nodes in West Europe, so we'll pin it there for now
"vmware" to testConfiguration(parallelism = 3, locationOverride = LocationConfiguration("westeurope", "westus2", "eastus2", false)),

Expand Down
6 changes: 6 additions & 0 deletions internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
fluidrelay_2022_05_26 "github.com/hashicorp/go-azure-sdk/resource-manager/fluidrelay/2022-05-26"
nginx2 "github.com/hashicorp/go-azure-sdk/resource-manager/nginx/2022-08-01"
redis_v2023_04_01 "github.com/hashicorp/go-azure-sdk/resource-manager/redis/2023-04-01"
servicenetworking_v2023_05_01_preview "github.com/hashicorp/go-azure-sdk/resource-manager/servicenetworking/2023-05-01-preview"
storagecache_2023_05_01 "github.com/hashicorp/go-azure-sdk/resource-manager/storagecache/2023-05-01"
timeseriesinsights_v2020_05_15 "github.com/hashicorp/go-azure-sdk/resource-manager/timeseriesinsights/2020-05-15"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
Expand Down Expand Up @@ -130,6 +131,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 @@ -263,6 +265,7 @@ type Client struct {
ServiceConnector *serviceConnector.Client
ServiceFabric *serviceFabric.Client
ServiceFabricManaged *serviceFabricManaged.Client
ServiceNetworking *servicenetworking_v2023_05_01_preview.Client
SignalR *signalr.Client
Storage *storage.Client
StorageMover *storageMover.Client
Expand Down Expand Up @@ -574,6 +577,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 @@ -115,6 +115,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 @@ -192,6 +193,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,196 @@
package servicenetworking

import (
"context"
"fmt"
"regexp"
"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"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)

type ApplicationLoadBalancerResource struct{}

type ApplicationLoadBalancerModel struct {
Name string `tfschema:"name"`
ResourceGroupName string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
PrimaryConfigurationEndpoints string `tfschema:"primary_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,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_.-]{0,62}[a-zA-Z0-9]$`), "the name must begin with a letter or number, end with a letter, number or underscore, and may contain only letters, numbers, underscores, periods, or hyphens. The value must be 1-64 characters long."),
},

"resource_group_name": commonschema.ResourceGroupName(),

"location": commonschema.Location(),

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

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

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 {
client := metadata.Client.ServiceNetworking.TrafficControllerInterface
subscriptionId := metadata.Client.Account.SubscriptionId

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

id := trafficcontrollerinterface.NewTrafficControllerID(subscriptionId, config.ResourceGroupName, config.Name)
existing, err := client.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)
}

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

if err := client.CreateOrUpdateThenPoll(ctx, id, payload); 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.TrafficControllerInterface

id, err := trafficcontrollerinterface.ParseTrafficControllerID(metadata.ResourceData.Id())
if err != nil {
return fmt.Errorf("parsing %s: %+v", metadata.ResourceData.Id(), 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", 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 {
if endpoint := prop.ConfigurationEndpoints; endpoint != nil && len(*endpoint) > 0 {
state.PrimaryConfigurationEndpoints = (*endpoint)[0]
}
}
}

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

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

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

payload := trafficcontrollerinterface.TrafficControllerUpdate{
Tags: pointer.To(config.Tags),
}

if _, err := client.Update(ctx, *id, payload); 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 {
client := metadata.Client.ServiceNetworking.TrafficControllerInterface

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

return nil
},
}
}
Loading

0 comments on commit e1eab1a

Please sign in to comment.