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 10 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
3 changes: 3 additions & 0 deletions .teamcity/components/settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,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
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
26 changes: 26 additions & 0 deletions internal/services/servicenetworking/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package client

import (
"fmt"

servicenetworking_v2023_05_01_preview "github.com/hashicorp/go-azure-sdk/resource-manager/servicenetworking/2023-05-01-preview"
"github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
ServiceNetworkingClient *servicenetworking_v2023_05_01_preview.Client
}

func NewClient(o *common.ClientOptions) (*Client, error) {
serviceNetworkingClient, err := servicenetworking_v2023_05_01_preview.NewClientWithBaseURI(o.Environment.ResourceManager, func(c *resourcemanager.Client) {
o.Configure(c, o.Authorizers.ResourceManager)
})
if err != nil {
return nil, fmt.Errorf("building service netowkriing client: %+v", err)
}

return &Client{
ServiceNetworkingClient: serviceNetworkingClient,
}, nil
}
jackofallops marked this conversation as resolved.
Show resolved Hide resolved
35 changes: 35 additions & 0 deletions internal/services/servicenetworking/registration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package servicenetworking

import (
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
)

type Registration struct{}

var _ sdk.TypedServiceRegistration = Registration{}

func (r Registration) AssociatedGitHubLabel() string {
return "service/service-networking"
}

func (r Registration) DataSources() []sdk.DataSource {
return []sdk.DataSource{}
}

func (r Registration) Resources() []sdk.Resource {
return []sdk.Resource{
ApplicationLoadBalancerResource{},
}
}

// Name is the name of this Service
func (r Registration) Name() string {
return "Service Networking"
}

// WebsiteCategories returns a list of categories which can be used for the sidebar
func (r Registration) WebsiteCategories() []string {
return []string{
"Service Networking",
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
package servicenetworking
jackofallops marked this conversation as resolved.
Show resolved Hide resolved

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"`
jackofallops marked this conversation as resolved.
Show resolved Hide resolved
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,
Copy link
Contributor

Choose a reason for hiding this comment

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

can we add validation for this field?

Copy link
Member

Choose a reason for hiding this comment

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

Hi @ziyeqf - I think @tombuildsstuff was looking for specific validation here, not just that the string is not empty. The portal reports the following validation to be required for this property
image
Can you check and update as appropriate?

},

"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)
}
jackofallops marked this conversation as resolved.
Show resolved Hide resolved

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
jackofallops marked this conversation as resolved.
Show resolved Hide resolved

id, err := trafficcontrollerinterface.ParseTrafficControllerID(metadata.ResourceData.Id())
if err != nil {
return fmt.Errorf("parsing %s: %+v", metadata.ResourceData.Id(), err)
jackofallops marked this conversation as resolved.
Show resolved Hide resolved
}

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

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)
}
jackofallops marked this conversation as resolved.
Show resolved Hide resolved

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)
}
jackofallops marked this conversation as resolved.
Show resolved Hide resolved

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)
}
jackofallops marked this conversation as resolved.
Show resolved Hide resolved

return nil
},
}
}
Loading