Skip to content

Commit

Permalink
New Resource: azurerm_network_function_azure_traffic_collector (#22274
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ms-zhenhua authored Jul 25, 2023
1 parent 3b9d58b commit b282d23
Show file tree
Hide file tree
Showing 29 changed files with 1,498 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/labeler-pull-request-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ service/netapp:
service/network:
- internal/services/network/**/*

service/network-function:
- internal/services/networkfunction/**/*

service/nginx:
- internal/services/nginx/**/*

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 @@ -86,6 +86,7 @@ var services = mapOf(
"mysql" to "MySQL",
"netapp" to "NetApp",
"network" to "Network",
"networkfunction" to "Network Function",
"newrelic" to "New Relic",
"nginx" to "Nginx",
"notificationhub" to "Notification Hub",
Expand Down
3 changes: 3 additions & 0 deletions .teamcity/components/settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ var serviceTestConfigurationOverrides = mapOf(
// Orbital is only available in certain locations
"orbital" to testConfiguration(locationOverride = LocationConfiguration("eastus", "southcentralus", "westus2", false)),

// Network Function is only available in certain locations
"networkfunction" to testConfiguration(locationOverride = LocationConfiguration("westus2", "eastus2", "westeurope", false)),

"policy" to testConfiguration(useAltSubscription = true),

// Private DNS Resolver is only available in certain locations
Expand Down
5 changes: 5 additions & 0 deletions internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ import (
mysql "github.com/hashicorp/terraform-provider-azurerm/internal/services/mysql/client"
netapp "github.com/hashicorp/terraform-provider-azurerm/internal/services/netapp/client"
network "github.com/hashicorp/terraform-provider-azurerm/internal/services/network/client"
networkfunction "github.com/hashicorp/terraform-provider-azurerm/internal/services/networkfunction/client"
newrelic "github.com/hashicorp/terraform-provider-azurerm/internal/services/newrelic/client"
nginx "github.com/hashicorp/terraform-provider-azurerm/internal/services/nginx/client"
notificationhub "github.com/hashicorp/terraform-provider-azurerm/internal/services/notificationhub/client"
Expand Down Expand Up @@ -230,6 +231,7 @@ type Client struct {
MySQL *mysql.Client
NetApp *netapp.Client
Network *network.Client
NetworkFunction *networkfunction.Client
NewRelic *newrelic.Client
Nginx *nginx2.Client
NotificationHubs *notificationhub.Client
Expand Down Expand Up @@ -457,6 +459,9 @@ func (client *Client) Build(ctx context.Context, o *common.ClientOptions) error
if client.Network, err = network.NewClient(o); err != nil {
return fmt.Errorf("building clients for Network: %+v", err)
}
if client.NetworkFunction, err = networkfunction.NewClient(o); err != nil {
return fmt.Errorf("building clients for NetworkFunction: %+v", err)
}
if client.NewRelic, err = newrelic.NewClient(o); err != nil {
return fmt.Errorf("building clients for NewRelic: %+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 @@ -88,6 +88,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/services/mysql"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/netapp"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/networkfunction"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/newrelic"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/nginx"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/notificationhub"
Expand Down Expand Up @@ -174,6 +175,7 @@ func SupportedTypedServices() []sdk.TypedServiceRegistration {
mysql.Registration{},
network.Registration{},
netapp.Registration{},
networkfunction.Registration{},
newrelic.Registration{},
nginx.Registration{},
policy.Registration{},
Expand Down
23 changes: 23 additions & 0 deletions internal/services/networkfunction/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package client

import (
"github.com/hashicorp/go-azure-sdk/resource-manager/networkfunction/2022-11-01/azuretrafficcollectors"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
AzureTrafficCollectorsClient *azuretrafficcollectors.AzureTrafficCollectorsClient
}

func NewClient(o *common.ClientOptions) (*Client, error) {
azureTrafficCollectorsClient, err := azuretrafficcollectors.NewAzureTrafficCollectorsClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, err
}

o.Configure(azureTrafficCollectorsClient.Client, o.Authorizers.ResourceManager)

return &Client{
AzureTrafficCollectorsClient: azureTrafficCollectorsClient,
}, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
package networkfunction

import (
"context"
"fmt"
"regexp"
"time"

"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/networkfunction/2022-11-01/azuretrafficcollectors"
"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 NetworkFunctionAzureTrafficCollectorModel struct {
Name string `tfschema:"name"`
ResourceGroupName string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
Tags map[string]string `tfschema:"tags"`
CollectorPolicies []string `tfschema:"collector_policy_ids"`
VirtualHub []string `tfschema:"virtual_hub_id"`
}

type NetworkFunctionAzureTrafficCollectorResource struct{}

var _ sdk.ResourceWithUpdate = NetworkFunctionAzureTrafficCollectorResource{}

func (r NetworkFunctionAzureTrafficCollectorResource) ResourceType() string {
return "azurerm_network_function_azure_traffic_collector"
}

func (r NetworkFunctionAzureTrafficCollectorResource) ModelObject() interface{} {
return &NetworkFunctionAzureTrafficCollectorModel{}
}

func (r NetworkFunctionAzureTrafficCollectorResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return azuretrafficcollectors.ValidateAzureTrafficCollectorID
}

func (r NetworkFunctionAzureTrafficCollectorResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile("^[a-zA-Z0-9]([-._a-zA-Z0-9]{0,78}[a-zA-Z0-9_])?$"),
"The name can contain only letters, numbers, periods (.), hyphens (-),and underscores (_), up to 80 characters, and it must begin with a letter or number and end with a letter, number or underscore.",
),
},

"resource_group_name": commonschema.ResourceGroupName(),

"location": commonschema.Location(),

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

func (r NetworkFunctionAzureTrafficCollectorResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"collector_policy_ids": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"virtual_hub_id": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
}
}

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

client := metadata.Client.NetworkFunction.AzureTrafficCollectorsClient
subscriptionId := metadata.Client.Account.SubscriptionId
id := azuretrafficcollectors.NewAzureTrafficCollectorID(subscriptionId, model.ResourceGroupName, model.Name)
existing, err := client.Get(ctx, id)
if err != nil && !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for existing %s: %+v", id, err)
}

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

properties := &azuretrafficcollectors.AzureTrafficCollector{
Location: location.Normalize(model.Location),
Properties: &azuretrafficcollectors.AzureTrafficCollectorPropertiesFormat{},
Tags: &model.Tags,
}

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

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

func (r NetworkFunctionAzureTrafficCollectorResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.NetworkFunction.AzureTrafficCollectorsClient

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

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

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

properties := resp.Model
if properties == nil {
return fmt.Errorf("retrieving %s: properties was nil", id)
}

if metadata.ResourceData.HasChange("tags") {
properties.Tags = &model.Tags
}

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

return nil
},
}
}

func (r NetworkFunctionAzureTrafficCollectorResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.NetworkFunction.AzureTrafficCollectorsClient

id, err := azuretrafficcollectors.ParseAzureTrafficCollectorID(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", *id, err)
}

state := NetworkFunctionAzureTrafficCollectorModel{
Name: id.AzureTrafficCollectorName,
ResourceGroupName: id.ResourceGroupName,
}

if model := resp.Model; model != nil {
state.Location = location.Normalize(model.Location)
if properties := model.Properties; properties != nil {
state.CollectorPolicies = flattenCollectorPolicyModelArray(properties.CollectorPolicies)
state.VirtualHub = flattenVirtualHubModel(properties.VirtualHub)
}

if model.Tags != nil {
state.Tags = *model.Tags
}
}

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

func (r NetworkFunctionAzureTrafficCollectorResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.NetworkFunction.AzureTrafficCollectorsClient

id, err := azuretrafficcollectors.ParseAzureTrafficCollectorID(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
},
}
}

func flattenCollectorPolicyModelArray(inputList *[]azuretrafficcollectors.ResourceReference) []string {
var outputList []string
if inputList == nil {
return outputList
}

for _, input := range *inputList {
if input.Id != nil {
outputList = append(outputList, *input.Id)
}
}

return outputList
}

func flattenVirtualHubModel(input *azuretrafficcollectors.ResourceReference) []string {
var outputList []string
if input != nil && input.Id != nil {
outputList = append(outputList, *input.Id)
}

return outputList
}
Loading

0 comments on commit b282d23

Please sign in to comment.