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_site; New Data Source: azurerm_mobile_network_site #20334

Merged
merged 9 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
6 changes: 6 additions & 0 deletions internal/services/mobilenetwork/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ package client

import (
"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"
)

type Client struct {
MobileNetworkClient *mobilenetwork.MobileNetworkClient
SiteClient *site.SiteClient
}

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

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

return &Client{
MobileNetworkClient: &mobileNetworkClient,
SiteClient: &siteClient,
}
}
44 changes: 44 additions & 0 deletions internal/services/mobilenetwork/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package mobilenetwork

import (
"context"
"fmt"
"net/http"
"strconv"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

// a workaround for that some child resources may still exist for seconds before it fully deleted.
// 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()
stateConf := &pluginsdk.StateChangeConf{
Pending: []string{"200"},
Target: []string{"404"},
Refresh: func() (interface{}, string, error) {
resp, err := getFunction()
if err != nil {
if response.WasNotFound(resp) {
return resp, strconv.Itoa(resp.StatusCode), nil
}

return nil, strconv.Itoa(resp.StatusCode), fmt.Errorf("polling for %s: %+v", id, err)
}

return resp, strconv.Itoa(resp.StatusCode), nil
},
MinTimeout: 10 * time.Second,
PollInterval: 10 * time.Second,
ContinuousTargetOccurence: 6,
Timeout: time.Until(deadline),
}

if _, err := stateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for deleting %s: %+v", id, err)
ziyeqf marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}
117 changes: 117 additions & 0 deletions internal/services/mobilenetwork/mobile_network_site_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
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/mobilenetwork"
"github.com/hashicorp/go-azure-sdk/resource-manager/mobilenetwork/2022-11-01/site"
"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 SiteDataSource struct{}

var _ sdk.DataSource = SiteDataSource{}

func (r SiteDataSource) ResourceType() string {
return "azurerm_mobile_network_site"
}

func (r SiteDataSource) ModelObject() interface{} {
return &SiteModel{}
}

func (r SiteDataSource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return site.ValidateSiteID
}

func (r SiteDataSource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ziyeqf marked this conversation as resolved.
Show resolved Hide resolved
ValidateFunc: validation.StringIsNotEmpty,
},

"mobile_network_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ziyeqf marked this conversation as resolved.
Show resolved Hide resolved
ValidateFunc: mobilenetwork.ValidateMobileNetworkID,
},
}
}

func (r SiteDataSource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"location": commonschema.LocationComputed(),

"tags": commonschema.TagsDataSource(),

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

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

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

id := site.NewSiteID(mobileNetworkId.SubscriptionId, mobileNetworkId.ResourceGroupName, mobileNetworkId.MobileNetworkName, metaModel.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)
}

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

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

if properties := model.Properties; properties != nil {
state.NetworkFunctions = flattenSubResourceModel(properties.NetworkFunctions)
}
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,40 @@
package mobilenetwork_test

import (
"fmt"
"testing"

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

type MobileNetworkSiteDataSource struct{}

func TestAccMobileNetworkSiteDataSource_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mobile_network_site", "test")

// Limited regional availability for Mobile Network
data.Locations.Primary = "eastus"

d := MobileNetworkSiteDataSource{}
data.DataSourceTest(t, []acceptance.TestStep{
{
Config: d.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key(`location`).Exists(),
check.That(data.ResourceName).Key("tags.%").HasValue("1"),
),
},
})
}

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

data "azurerm_mobile_network_site" "test" {
name = azurerm_mobile_network_site.test.name
mobile_network_id = azurerm_mobile_network_site.test.mobile_network_id
}
`, MobileNetworkSiteResource{}.complete(data))
}
Loading