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_service; New DataSource: azurerm_mobile_network_service #20337

Merged
merged 14 commits into from
Feb 16, 2023
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,24 +2,30 @@ 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/service"
"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
ServiceClient *service.ServiceClient
SiteClient *site.SiteClient
}

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

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

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

return &Client{
MobileNetworkClient: &mobileNetworkClient,
ServiceClient: &serviceClient,
SiteClient: &siteClient,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
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/service"
"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 MobileNetworkServiceDataSource struct{}

var _ sdk.DataSource = MobileNetworkServiceDataSource{}

func (r MobileNetworkServiceDataSource) ResourceType() string {
return "azurerm_mobile_network_service"
}

func (r MobileNetworkServiceDataSource) ModelObject() interface{} {
return &ServiceModel{}
}

func (r MobileNetworkServiceDataSource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return service.ValidateServiceID
}

func (r MobileNetworkServiceDataSource) 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 MobileNetworkServiceDataSource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{

"location": commonschema.LocationComputed(),

"pcc_rule": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},

"precedence": {
Type: pluginsdk.TypeInt,
Computed: true,
},

"qos_policy": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"allocation_and_retention_priority_level": {
Type: pluginsdk.TypeInt,
Computed: true,
},

"qos_indicator": {
Type: pluginsdk.TypeInt,
Computed: true,
},

"guaranteed_bit_rate": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"downlink": {
Type: pluginsdk.TypeString,
Computed: true,
},

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

"maximum_bit_rate": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"downlink": {
Type: pluginsdk.TypeString,
Computed: true,
},

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

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

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

"service_data_flow_template": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"direction": {
Type: pluginsdk.TypeString,
Computed: true,
},

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

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

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

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

"traffic_control_enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},
},
},
},

"service_precedence": {
Type: pluginsdk.TypeInt,
Computed: true,
},

"service_qos_policy": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"allocation_and_retention_priority_level": {
Type: pluginsdk.TypeInt,
Computed: true,
},

"qos_indicator": {
Type: pluginsdk.TypeInt,
Computed: true,
},

"maximum_bit_rate": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"downlink": {
Type: pluginsdk.TypeString,
Computed: true,
},

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

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

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

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

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

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

id := service.NewServiceID(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)
ziyeqf marked this conversation as resolved.
Show resolved Hide resolved
}

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 := ServiceModel{
Name: id.ServiceName,
MobileNetworkMobileNetworkId: mobilenetwork.NewMobileNetworkID(id.SubscriptionId, id.ResourceGroupName, id.MobileNetworkName).ID(),
Location: location.Normalize(model.Location),
}

properties := model.Properties

state.PccRules = flattenPccRuleConfigurationModel(properties.PccRules)

state.ServicePrecedence = properties.ServicePrecedence

state.ServiceQosPolicy = flattenQosPolicyModel(properties.ServiceQosPolicy)

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,61 @@
package mobilenetwork_test

import (
"fmt"
"testing"

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

type MobileNetworkServiceDataSource struct{}

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

d := MobileNetworkServiceDataSource{}
data.DataSourceTest(t, []acceptance.TestStep{
{
Config: d.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key(`location`).Exists(),
check.That(data.ResourceName).Key(`service_precedence`).HasValue("0"),
check.That(data.ResourceName).Key(`pcc_rule.0.name`).HasValue("default-rule"),
check.That(data.ResourceName).Key(`pcc_rule.0.precedence`).HasValue("1"),
check.That(data.ResourceName).Key(`pcc_rule.0.traffic_control_enabled`).HasValue("true"),
check.That(data.ResourceName).Key(`pcc_rule.0.qos_policy.0.allocation_and_retention_priority_level`).HasValue("9"),
check.That(data.ResourceName).Key(`pcc_rule.0.qos_policy.0.qos_indicator`).HasValue("9"),
check.That(data.ResourceName).Key(`pcc_rule.0.qos_policy.0.preemption_capability`).HasValue("NotPreempt"),
check.That(data.ResourceName).Key(`pcc_rule.0.qos_policy.0.preemption_vulnerability`).HasValue("Preemptable"),
check.That(data.ResourceName).Key(`pcc_rule.0.qos_policy.0.guaranteed_bit_rate.0.downlink`).HasValue("100 Mbps"),
check.That(data.ResourceName).Key(`pcc_rule.0.qos_policy.0.guaranteed_bit_rate.0.uplink`).HasValue("10 Mbps"),
check.That(data.ResourceName).Key(`pcc_rule.0.qos_policy.0.maximum_bit_rate.0.downlink`).HasValue("1 Gbps"),
check.That(data.ResourceName).Key(`pcc_rule.0.qos_policy.0.maximum_bit_rate.0.uplink`).HasValue("100 Mbps"),
check.That(data.ResourceName).Key(`pcc_rule.0.service_data_flow_template.0.direction`).HasValue("Uplink"),
check.That(data.ResourceName).Key(`pcc_rule.0.service_data_flow_template.0.name`).HasValue("IP-to-server"),
check.That(data.ResourceName).Key(`pcc_rule.0.service_data_flow_template.0.protocol.0`).HasValue("ip"),
check.That(data.ResourceName).Key(`pcc_rule.0.service_data_flow_template.0.remote_ip_list.0`).HasValue("10.3.4.0/24"),
check.That(data.ResourceName).Key(`service_qos_policy.0.allocation_and_retention_priority_level`).HasValue("9"),
check.That(data.ResourceName).Key(`service_qos_policy.0.qos_indicator`).HasValue("9"),
check.That(data.ResourceName).Key(`service_qos_policy.0.preemption_capability`).HasValue("NotPreempt"),
check.That(data.ResourceName).Key(`service_qos_policy.0.preemption_vulnerability`).HasValue("Preemptable"),
check.That(data.ResourceName).Key(`service_qos_policy.0.maximum_bit_rate.0.downlink`).HasValue("1 Gbps"),
check.That(data.ResourceName).Key(`service_qos_policy.0.maximum_bit_rate.0.uplink`).HasValue("100 Mbps"),
check.That(data.ResourceName).Key("tags.%").HasValue("1"),
),
},
})
}

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

data "azurerm_mobile_network_service" "test" {
name = azurerm_mobile_network_service.test.name
mobile_network_id = azurerm_mobile_network_service.test.mobile_network_id
}
`, MobileNetworkServiceResource{}.complete(data))
}
Loading