Skip to content

Commit

Permalink
New Resource: azurerm_mobile_network_service; New DataSource: `azur…
Browse files Browse the repository at this point in the history
…erm_mobile_network_service` (#20337)
  • Loading branch information
ziyeqf authored Feb 16, 2023
1 parent 48923b0 commit b9b3ba4
Show file tree
Hide file tree
Showing 26 changed files with 2,621 additions and 0 deletions.
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,6 +2,7 @@ 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/simgroup"
"github.com/hashicorp/go-azure-sdk/resource-manager/mobilenetwork/2022-11-01/site"
"github.com/hashicorp/go-azure-sdk/resource-manager/mobilenetwork/2022-11-01/slice"
Expand All @@ -10,6 +11,7 @@ import (

type Client struct {
MobileNetworkClient *mobilenetwork.MobileNetworkClient
ServiceClient *service.ServiceClient
SIMGroupClient *simgroup.SIMGroupClient
SliceClient *slice.SliceClient
SiteClient *site.SiteClient
Expand All @@ -19,6 +21,9 @@ 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)

simGroupClient := simgroup.NewSIMGroupClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&simGroupClient.Client, o.ResourceManagerAuthorizer)

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

return &Client{
MobileNetworkClient: &mobileNetworkClient,
ServiceClient: &serviceClient,
SIMGroupClient: &simGroupClient,
SiteClient: &siteClient,
SliceClient: &sliceClient,
Expand Down
289 changes: 289 additions & 0 deletions internal/services/mobilenetwork/mobile_network_service_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
package mobilenetwork

import (
"context"
"fmt"
"time"

"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 {
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

0 comments on commit b9b3ba4

Please sign in to comment.