forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from hashicorp/main
Fork Sync: Update from parent repository
- Loading branch information
Showing
64 changed files
with
3,246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
internal/services/servicenetworking/application_load_balancer_resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
} | ||
} |
Oops, something went wrong.