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/service"
"github.com/hashicorp/go-azure-sdk/resource-manager/mobilenetwork/2022-11-01/simgroup"
Comment on lines +4 to 7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't something we need to change now, but I wanted to call out that hashicorp/go-azure-sdk supports Meta Clients for each API Version - so if an entire Service is using a single API Version (as is the case here) - rather than defining the Client and the Resources to use inline (as we're doing here) we can instead use the Meta Client as per datadog to automatically have access to all clients, rather than defining these by hand.

Again, this isn't something we need to change now, but should make it easier to import the Client one-time (since we try and stick to a single API Version for each Service where possible) rather than adding these across different PR's :)

Expand All @@ -15,12 +16,16 @@ type Client struct {
SIMGroupClient *simgroup.SIMGroupClient
SliceClient *slice.SliceClient
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)

serviceClient := service.NewServiceClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&serviceClient.Client, o.ResourceManagerAuthorizer)

Expand All @@ -35,6 +40,7 @@ func NewClient(o *common.ClientOptions) *Client {

return &Client{
MobileNetworkClient: &mobileNetworkClient,
DataNetworkClient: &dataNetworkClient,
ServiceClient: &serviceClient,
SIMGroupClient: &simGroupClient,
SiteClient: &siteClient,
Expand Down
1 change: 1 addition & 0 deletions internal/services/mobilenetwork/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

// a workaround for that some child resources may still exist for seconds before it fully deleted.
// tracked on https://github.com/Azure/azure-rest-api-specs/issues/22691
// it will cause the error "Can not delete resource before nested resources are deleted."
func resourceMobileNetworkChildWaitForDeletion(ctx context.Context, id string, getFunction func() (*http.Response, error)) error {
deadline, _ := ctx.Deadline()
Expand Down
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