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,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/service"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
MobileNetworkClient *mobilenetwork.MobileNetworkClient
ServiceClient *service.ServiceClient
}

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)

return &Client{
MobileNetworkClient: &mobileNetworkClient,
ServiceClient: &serviceClient,
}
}
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)
catriona-m marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
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)
}

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

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