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_sim_group; New DataSource: azurerm_mobile_network_sim_group #20339

Merged
merged 9 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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,13 +2,15 @@ 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/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"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
MobileNetworkClient *mobilenetwork.MobileNetworkClient
SIMGroupClient *simgroup.SIMGroupClient
SliceClient *slice.SliceClient
SiteClient *site.SiteClient
}
Expand All @@ -17,6 +19,9 @@ func NewClient(o *common.ClientOptions) *Client {
mobileNetworkClient := mobilenetwork.NewMobileNetworkClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&mobileNetworkClient.Client, o.ResourceManagerAuthorizer)

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

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

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

return &Client{
MobileNetworkClient: &mobileNetworkClient,
SIMGroupClient: &simGroupClient,
SiteClient: &siteClient,
SliceClient: &sliceClient,
}
Expand Down
39 changes: 39 additions & 0 deletions internal/services/mobilenetwork/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

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

Expand Down Expand Up @@ -42,3 +43,41 @@ func resourceMobileNetworkChildWaitForDeletion(ctx context.Context, id string, g

return nil
}

// some resources defined both systemAssigned and userAssigned Identity type in Swagger but only support userAssigned Identity,
// so add a workaround to convert type here.
Comment on lines +47 to +48
Copy link
Contributor

Choose a reason for hiding this comment

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

@ziyeqf is there a Swagger bug for this?

Copy link
Contributor Author

@ziyeqf ziyeqf Feb 15, 2023

Choose a reason for hiding this comment

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

just opened one Azure/azure-rest-api-specs#22634
and added comments to code on this PR #20474

func expandMobileNetworkLegacyToUserAssignedIdentity(input []identity.ModelUserAssigned) (*identity.LegacySystemAndUserAssignedMap, error) {
if len(input) == 0 {
return nil, nil
}

identityValue, err := identity.ExpandUserAssignedMapFromModel(input)
if err != nil {
return nil, fmt.Errorf("expanding `identity`: %+v", err)
}

output := identity.LegacySystemAndUserAssignedMap{
Type: identityValue.Type,
IdentityIds: identityValue.IdentityIds,
}

return &output, nil
}

func flattenMobileNetworkUserAssignedToNetworkLegacyIdentity(input *identity.LegacySystemAndUserAssignedMap) ([]identity.ModelUserAssigned, error) {
if input == nil {
return nil, nil
}

tmp := identity.UserAssignedMap{
Type: input.Type,
IdentityIds: input.IdentityIds,
}

output, err := identity.FlattenUserAssignedMapToModel(&tmp)
if err != nil {
return nil, fmt.Errorf("expanding `identity`: %+v", err)
}

return *output, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
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/identity"
"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/simgroup"
"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 SimGroupDataSource struct{}

var _ sdk.DataSource = SimGroupDataSource{}

func (r SimGroupDataSource) ResourceType() string {
return "azurerm_mobile_network_sim_group"
}

func (r SimGroupDataSource) ModelObject() interface{} {
return &SimGroupModel{}
}

func (r SimGroupDataSource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return simgroup.ValidateSimGroupID
}

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

"encryption_key_url": { // needs UserAssignedIdentity
Type: pluginsdk.TypeString,
Computed: true,
},

"identity": commonschema.SystemOrUserAssignedIdentityComputed(),

"location": commonschema.LocationComputed(),

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

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

client := metadata.Client.MobileNetwork.SIMGroupClient
parsedMobileNetworkId, err := mobilenetwork.ParseMobileNetworkID(metaModel.MobileNetworkId)
if err != nil {
return fmt.Errorf("parsing `mobile_network_id`: %+v", err)
}
id := simgroup.NewSimGroupID(parsedMobileNetworkId.SubscriptionId, parsedMobileNetworkId.ResourceGroupName, 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)
}

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

model := *resp.Model

state := SimGroupModel{
Name: id.SimGroupName,
MobileNetworkId: metaModel.MobileNetworkId,
Location: location.Normalize(model.Location),
}

identityValue, err := identity.FlattenLegacySystemAndUserAssignedMap(model.Identity)
if err != nil {
return fmt.Errorf("flattening `identity`: %+v", err)
}

if err := metadata.ResourceData.Set("identity", identityValue); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}

properties := model.Properties

if properties.EncryptionKey != nil && properties.EncryptionKey.KeyUrl != nil {
state.EncryptionKeyUrl = *properties.EncryptionKey.KeyUrl
}

if properties.MobileNetwork != nil {
state.MobileNetworkId = properties.MobileNetwork.Id
}

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

import (
"fmt"
"testing"

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

type MobileNetworkSimGroupDataSource struct{}

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

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

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

data "azurerm_mobile_network_sim_group" "test" {
name = azurerm_mobile_network_sim_group.test.name
mobile_network_id = azurerm_mobile_network.test.id
}
`, MobileNetworkSimGroupResource{}.complete(data))
}
Loading