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_mobile_network_data_network; New DataSource: azurerm_mobile_network_data_network #20338

Merged
merged 13 commits into from
Feb 28, 2023
6 changes: 6 additions & 0 deletions internal/services/mobilenetwork/client/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"github.com/hashicorp/go-azure-sdk/resource-manager/mobilenetwork/2022-11-01/datanetwork"
"github.com/hashicorp/go-azure-sdk/resource-manager/mobilenetwork/2022-11-01/mobilenetwork"
"github.com/hashicorp/go-azure-sdk/resource-manager/mobilenetwork/2022-11-01/site"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
Expand All @@ -9,17 +10,22 @@ import (
type Client struct {
MobileNetworkClient *mobilenetwork.MobileNetworkClient
SiteClient *site.SiteClient
DataNetworkClient *datanetwork.DataNetworkClient
}

func NewClient(o *common.ClientOptions) *Client {
mobileNetworkClient := mobilenetwork.NewMobileNetworkClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&mobileNetworkClient.Client, o.ResourceManagerAuthorizer)

dataNetworkClient := datanetwork.NewDataNetworkClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&dataNetworkClient.Client, o.ResourceManagerAuthorizer)

siteClient := site.NewSiteClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&siteClient.Client, o.ResourceManagerAuthorizer)

return &Client{
MobileNetworkClient: &mobileNetworkClient,
DataNetworkClient: &dataNetworkClient,
SiteClient: &siteClient,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package mobilenetwork

import (
"context"
"fmt"
"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/mobilenetwork/2022-11-01/datanetwork"
"github.com/hashicorp/go-azure-sdk/resource-manager/mobilenetwork/2022-11-01/mobilenetwork"
"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 DataNetworkDataSource struct{}

var _ sdk.DataSource = DataNetworkDataSource{}

func (r DataNetworkDataSource) ResourceType() string {
return "azurerm_mobile_network_data_network"
}

func (r DataNetworkDataSource) ModelObject() interface{} {
return &DataNetworkModel{}
}

func (r DataNetworkDataSource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return datanetwork.ValidateDataNetworkID
}

func (r DataNetworkDataSource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"mobile_network_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: mobilenetwork.ValidateMobileNetworkID,
},
}
}

func (r DataNetworkDataSource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"description": {
Type: pluginsdk.TypeString,
Computed: true,
},

"location": commonschema.LocationComputed(),

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

func (r DataNetworkDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var inputModel DataNetworkModel
if err := metadata.Decode(&inputModel); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

client := metadata.Client.MobileNetwork.DataNetworkClient
mobileNetworkId, err := mobilenetwork.ParseMobileNetworkID(inputModel.MobileNetworkMobileNetworkId)
if err != nil {
return err
}

id := datanetwork.NewDataNetworkID(mobileNetworkId.SubscriptionId, mobileNetworkId.ResourceGroupName, mobileNetworkId.MobileNetworkName, inputModel.Name)

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)
}

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

model := *resp.Model

state := DataNetworkModel{
Name: id.DataNetworkName,
MobileNetworkMobileNetworkId: mobilenetwork.NewMobileNetworkID(id.SubscriptionId, id.ResourceGroupName, id.MobileNetworkName).ID(),
Location: location.Normalize(model.Location),
}

if properties := model.Properties; properties != nil {
if properties.Description != nil {
state.Description = *properties.Description
}
}
if model.Tags != nil {
state.Tags = *model.Tags
}

metadata.SetID(id)
return metadata.Encode(&state)
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package mobilenetwork_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)

type MobileNetworkDataNetworkDataSource struct{}

func TestAccMobileNetworkDataNetworkDataSource_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mobile_network_data_network", "test")
// Limited regional availability for Mobile Network
data.Locations.Primary = "eastus"

d := MobileNetworkDataNetworkDataSource{}
data.DataSourceTest(t, []acceptance.TestStep{
{
Config: d.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key(`description`).HasValue("my favourite data network"),
check.That(data.ResourceName).Key("tags.%").HasValue("1"),
),
},
})
}

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

data "azurerm_mobile_network_data_network" "test" {
name = azurerm_mobile_network_data_network.test.name
mobile_network_id = azurerm_mobile_network_data_network.test.mobile_network_id
}
`, MobileNetworkDataNetworkResource{}.complete(data))
}
Loading