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_frontend #23411

Merged
merged 13 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
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/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/servicenetworking/2023-05-01-preview/frontendsinterface"
"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/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 FrontendsResource struct{}

type FrontendsModel struct {
Name string `tfschema:"name"`
ApplicationLoadBalancerId string `tfschema:"application_load_balancer_id"`
Fqdn string `tfschema:"fully_qualified_domain_name"`
Tags map[string]interface{} `tfschema:"tags"`
}

var _ sdk.Resource = FrontendsResource{}

func (f FrontendsResource) 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: frontendsinterface.ValidateTrafficControllerID,
},

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

func (f FrontendsResource) Attributes() map[string]*schema.Schema {
return map[string]*schema.Schema{
"fully_qualified_domain_name": {
Type: pluginsdk.TypeString,
Computed: true,
},
}
}

func (f FrontendsResource) ModelObject() interface{} {
return &FrontendsModel{}
}

func (f FrontendsResource) ResourceType() string {
return "azurerm_application_load_balancer_frontend"
}

func (f FrontendsResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return frontendsinterface.ValidateFrontendID
}

func (f FrontendsResource) Create() sdk.ResourceFunc {
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.FrontendsInterface

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

trafficControllerId, err := frontendsinterface.ParseTrafficControllerID(config.ApplicationLoadBalancerId)
if err != nil {
return err
}

controllerId := trafficcontrollerinterface.NewTrafficControllerID(trafficControllerId.SubscriptionId, trafficControllerId.ResourceGroupName, trafficControllerId.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

id := frontendsinterface.NewFrontendID(trafficControllerId.SubscriptionId, trafficControllerId.ResourceGroupName, trafficControllerId.TrafficControllerName, config.Name)
resp, err := client.Get(ctx, id)
if err != nil {
if !response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}

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

frontend := frontendsinterface.Frontend{
Location: loc,
Properties: &frontendsinterface.FrontendProperties{},
Tags: tags.Expand(config.Tags),
}

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

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

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

id, err := frontendsinterface.ParseFrontendID(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("retrieving %s: %+v", metadata.ResourceData.Id(), err)
}

trafficControllerId := frontendsinterface.NewTrafficControllerID(id.SubscriptionId, id.ResourceGroupName, id.TrafficControllerName)
state := FrontendsModel{
Name: id.FrontendName,
ApplicationLoadBalancerId: trafficControllerId.ID(),
}

if model := resp.Model; model != nil {
state.Tags = tags.Flatten(model.Tags)

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

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

func (f FrontendsResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.ServiceNetworking.FrontendsInterface

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

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

update := frontendsinterface.FrontendUpdate{}

if metadata.ResourceData.HasChange("tags") {
update.Tags = tags.Expand(config.Tags)
}
if _, err := client.Update(ctx, *id, update); err != nil {
return fmt.Errorf("updating `azurerm_application_load_balancer_frontend` %s: %+v", *id, err)
}

return nil
},
}
}

func (f FrontendsResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.ServiceNetworking.FrontendsInterface

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

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

return nil
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
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/frontendsinterface"
"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 ApplicationLoadBalancerFrontendResource struct{}

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

resp, err := clients.ServiceNetworking.FrontendsInterface.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 TestAccApplicationLoadBalancerFrontend_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_application_load_balancer_frontend", "test")

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

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

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

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

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

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

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

func (r ApplicationLoadBalancerFrontendResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestrg-alb-%[1]d"
location = "%[2]s"
}

resource "azurerm_application_load_balancer" "test" {
name = "acctestalb-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
}
`, data.RandomInteger, data.Locations.Primary)
}

func (r ApplicationLoadBalancerFrontendResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {
}
}

%s

resource "azurerm_application_load_balancer_frontend" "test" {
name = "acct-frnt-%d"
application_load_balancer_id = azurerm_application_load_balancer.test.id
}
`, r.template(data), data.RandomInteger)
}

func (r ApplicationLoadBalancerFrontendResource) complete(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {
}
}

%s

resource "azurerm_application_load_balancer_frontend" "test" {
name = "acct-frnt-%d"
application_load_balancer_id = azurerm_application_load_balancer.test.id
tags = {
"tag1" = "value1"
}
}
`, r.template(data), data.RandomInteger)
}

func (r ApplicationLoadBalancerFrontendResource) requiresImport(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

resource "azurerm_application_load_balancer_frontend" "import" {
name = azurerm_application_load_balancer_frontend.test.name
application_load_balancer_id = azurerm_application_load_balancer_frontend.test.application_load_balancer_id
}
`, r.basic(data))
}
Loading
Loading