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_express_route_port_authorization #20736

Merged
merged 2 commits into from
Mar 14, 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
5 changes: 5 additions & 0 deletions internal/services/network/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Client struct {
ExpressRouteGatewaysClient *network.ExpressRouteGatewaysClient
ExpressRoutePeeringsClient *network.ExpressRouteCircuitPeeringsClient
ExpressRoutePortsClient *network.ExpressRoutePortsClient
ExpressRoutePortAuthorizationsClient *network.ExpressRoutePortAuthorizationsClient
FlowLogsClient *network.FlowLogsClient
HubRouteTableClient *network.HubRouteTablesClient
HubVirtualNetworkConnectionClient *network.HubVirtualNetworkConnectionsClient
Expand Down Expand Up @@ -115,6 +116,9 @@ func NewClient(o *common.ClientOptions) *Client {
ExpressRoutePortsClient := network.NewExpressRoutePortsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ExpressRoutePortsClient.Client, o.ResourceManagerAuthorizer)

ExpressRoutePortAuthorizationsClient := network.NewExpressRoutePortAuthorizationsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ExpressRoutePortAuthorizationsClient.Client, o.ResourceManagerAuthorizer)

FlowLogsClient := network.NewFlowLogsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&FlowLogsClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -291,6 +295,7 @@ func NewClient(o *common.ClientOptions) *Client {
ExpressRouteGatewaysClient: &ExpressRouteGatewaysClient,
ExpressRoutePeeringsClient: &ExpressRoutePeeringsClient,
ExpressRoutePortsClient: &ExpressRoutePortsClient,
ExpressRoutePortAuthorizationsClient: &ExpressRoutePortAuthorizationsClient,
FlowLogsClient: &FlowLogsClient,
HubRouteTableClient: &HubRouteTableClient,
HubVirtualNetworkConnectionClient: &HubVirtualNetworkConnectionClient,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package network

import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
"github.com/tombuildsstuff/kermit/sdk/network/2022-07-01/network"
)

func resourceExpressRoutePortAuthorization() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceExpressRoutePortAuthorizationCreate,
Read: resourceExpressRoutePortAuthorizationRead,
Delete: resourceExpressRoutePortAuthorizationDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.ExpressRoutePortAuthorizationID(id)
return err
}),

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(30 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"resource_group_name": commonschema.ResourceGroupName(),

"express_route_port_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"authorization_key": {
Type: pluginsdk.TypeString,
Computed: true,
Sensitive: true,
},

"authorization_use_status": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
}
}

func resourceExpressRoutePortAuthorizationCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.ExpressRoutePortAuthorizationsClient
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
defer cancel()

id := parse.NewExpressRoutePortAuthorizationID(subscriptionId, d.Get("resource_group_name").(string), d.Get("express_route_port_name").(string), d.Get("name").(string))

if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.ExpressRoutePortName, id.AuthorizationName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
}
}

if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_express_route_port_authorization", id.ID())
}
}

properties := network.ExpressRoutePortAuthorization{
ExpressRoutePortAuthorizationPropertiesFormat: &network.ExpressRoutePortAuthorizationPropertiesFormat{},
}

future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.ExpressRoutePortName, id.AuthorizationName, properties)
if err != nil {
return fmt.Errorf("Creating/Updating %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for %s to finish creating/updating: %+v", id, err)
}

d.SetId(id.ID())

return resourceExpressRoutePortAuthorizationRead(d, meta)
}

func resourceExpressRoutePortAuthorizationRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.ExpressRoutePortAuthorizationsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ExpressRoutePortAuthorizationID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.ExpressRoutePortName, id.AuthorizationName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", id.AuthorizationName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("express_route_port_name", id.ExpressRoutePortName)

if props := resp.ExpressRoutePortAuthorizationPropertiesFormat; props != nil {
d.Set("authorization_key", props.AuthorizationKey)
d.Set("authorization_use_status", string(props.AuthorizationUseStatus))
}

return nil
}

func resourceExpressRoutePortAuthorizationDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.ExpressRoutePortAuthorizationsClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ExpressRoutePortAuthorizationID(d.Id())
if err != nil {
return err
}

future, err := client.Delete(ctx, id.ResourceGroup, id.ExpressRoutePortName, id.AuthorizationName)
if err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for %s to be deleted: %+v", *id, err)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package network_test

import (
"context"
"fmt"
"testing"

"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/services/network/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type ExpressRoutePortAuthorizationResource struct{}

func TestAccExpressRoutePortAuthorization_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_express_route_port_authorization", "test")
r := ExpressRoutePortAuthorizationResource{}

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

func TestAccExpressRoutePortAuthorization_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_express_route_port_authorization", "test")
r := ExpressRoutePortAuthorizationResource{}

data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("authorization_key").Exists(),
),
},
{
Config: r.requiresImport(data),
ExpectError: acceptance.RequiresImportError("azurerm_express_route_port_authorization"),
},
})
}

func TestAccExpressRoutePortAuthorization_multiple(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_express_route_port_authorization", "test1")
r := ExpressRoutePortAuthorizationResource{}
secondResourceName := "azurerm_express_route_port_authorization.test2"

data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: r.multiple(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("authorization_key").Exists(),
acceptance.TestCheckResourceAttrSet(secondResourceName, "authorization_key"),
),
},
})
}

func (r ExpressRoutePortAuthorizationResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.ExpressRoutePortAuthorizationID(state.ID)
if err != nil {
return nil, err
}

resp, err := clients.Network.ExpressRoutePortAuthorizationsClient.Get(ctx, id.ResourceGroup, id.ExpressRoutePortName, id.AuthorizationName)
if err != nil {
return nil, fmt.Errorf("reading %s: %+v", *id, err)
}

return utils.Bool(resp.ID != nil), nil
}

func (r ExpressRoutePortAuthorizationResource) basic(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
%s

resource "azurerm_express_route_port_authorization" "test" {
name = "acctestauth%[2]d"
express_route_port_name = azurerm_express_route_port.test.name
resource_group_name = azurerm_resource_group.test.name
}
`, template, data.RandomInteger)
}

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

resource "azurerm_express_route_port_authorization" "import" {
name = azurerm_express_route_port_authorization.test.name
express_route_port_name = azurerm_express_route_port_authorization.test.express_route_port_name
resource_group_name = azurerm_express_route_port_authorization.test.resource_group_name
}
`, r.basic(data))
}

func (r ExpressRoutePortAuthorizationResource) multiple(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
%s

resource "azurerm_express_route_port_authorization" "test1" {
name = "acctestauth1%[2]d"
express_route_port_name = azurerm_express_route_port.test.name
resource_group_name = azurerm_resource_group.test.name
}

resource "azurerm_express_route_port_authorization" "test2" {
name = "acctestauth2%[2]d"
express_route_port_name = azurerm_express_route_port.test.name
resource_group_name = azurerm_resource_group.test.name
}
`, template, data.RandomInteger)
}

func (ExpressRoutePortAuthorizationResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%[1]d"
location = "%[2]s"
}

resource "azurerm_express_route_port" "test" {
name = "acctestERP-%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
peering_location = "Airtel-Chennai2-CLS"
bandwidth_in_gbps = 10
encapsulation = "Dot1Q"
billing_type = "MeteredData"
tags = {
ENV = "Test"
}
}`, data.RandomInteger, data.Locations.Primary)
}
Loading