-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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_data_network
; New DataSource: azurerm_mobile_network_data_network
#20338
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5c070d4
New Resource: `azurerm_mobile_network_data_network`; New DataSource: …
ziyeqf a829033
remove unused forcenew
ziyeqf c222e16
Merge branch 'main' into tengzh/mn_dn
ziyeqf e5c7fd8
Merge branch 'main' into tengzh/mn_dn
ziyeqf b7c24bf
update code
ziyeqf 8ac5f43
update doc
ziyeqf 885a9f5
Merge branch 'main' into tengzh/mn_dn
ziyeqf 48f4a34
update per comments
ziyeqf 0c5cf5d
Merge remote-tracking branch 'hashi/main' into tengzh/mn_dn
ziyeqf 59f49cf
add comment
ziyeqf 1f5bddc
Merge remote-tracking branch 'hashi/main' into tengzh/mn_dn
ziyeqf 122583c
update code per comments
ziyeqf 4693303
update Update timeout
ziyeqf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
internal/services/mobilenetwork/mobile_network_data_network_data_source.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
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/datanetwork" | ||
"github.com/hashicorp/go-azure-sdk/resource-manager/mobilenetwork/2022-11-01/mobilenetwork" | ||
"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 DataNetworkDataSource struct{} | ||
|
||
var _ sdk.DataSource = DataNetworkDataSource{} | ||
|
||
func (r DataNetworkDataSource) ResourceType() string { | ||
return "azurerm_mobile_network_data_network" | ||
} | ||
|
||
func (r DataNetworkDataSource) ModelObject() interface{} { | ||
return &DataNetworkModel{} | ||
} | ||
|
||
func (r DataNetworkDataSource) IDValidationFunc() pluginsdk.SchemaValidateFunc { | ||
return datanetwork.ValidateDataNetworkID | ||
} | ||
|
||
func (r DataNetworkDataSource) 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 DataNetworkDataSource) Attributes() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{ | ||
"description": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"location": commonschema.LocationComputed(), | ||
|
||
"tags": commonschema.TagsDataSource(), | ||
} | ||
} | ||
|
||
func (r DataNetworkDataSource) Read() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 5 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
var inputModel DataNetworkModel | ||
if err := metadata.Decode(&inputModel); err != nil { | ||
return fmt.Errorf("decoding: %+v", err) | ||
} | ||
|
||
client := metadata.Client.MobileNetwork.DataNetworkClient | ||
mobileNetworkId, err := mobilenetwork.ParseMobileNetworkID(inputModel.MobileNetworkMobileNetworkId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
id := datanetwork.NewDataNetworkID(mobileNetworkId.SubscriptionId, mobileNetworkId.ResourceGroupName, mobileNetworkId.MobileNetworkName, inputModel.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 := DataNetworkModel{ | ||
Name: id.DataNetworkName, | ||
MobileNetworkMobileNetworkId: mobilenetwork.NewMobileNetworkID(id.SubscriptionId, id.ResourceGroupName, id.MobileNetworkName).ID(), | ||
Location: location.Normalize(model.Location), | ||
} | ||
|
||
if properties := model.Properties; properties != nil { | ||
if properties.Description != nil { | ||
state.Description = *properties.Description | ||
} | ||
} | ||
if model.Tags != nil { | ||
state.Tags = *model.Tags | ||
} | ||
|
||
metadata.SetID(id) | ||
return metadata.Encode(&state) | ||
}, | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
internal/services/mobilenetwork/mobile_network_data_network_data_source_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package mobilenetwork_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" | ||
) | ||
|
||
type MobileNetworkDataNetworkDataSource struct{} | ||
|
||
func TestAccMobileNetworkDataNetworkDataSource_complete(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "azurerm_mobile_network_data_network", "test") | ||
// Limited regional availability for Mobile Network | ||
data.Locations.Primary = "eastus" | ||
|
||
d := MobileNetworkDataNetworkDataSource{} | ||
data.DataSourceTest(t, []acceptance.TestStep{ | ||
{ | ||
Config: d.complete(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).Key(`description`).HasValue("my favourite data network"), | ||
check.That(data.ResourceName).Key("tags.%").HasValue("1"), | ||
), | ||
}, | ||
}) | ||
} | ||
|
||
func (r MobileNetworkDataNetworkDataSource) complete(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
%s | ||
|
||
data "azurerm_mobile_network_data_network" "test" { | ||
name = azurerm_mobile_network_data_network.test.name | ||
mobile_network_id = azurerm_mobile_network_data_network.test.mobile_network_id | ||
} | ||
`, MobileNetworkDataNetworkResource{}.complete(data)) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't something we need to change now, but I wanted to call out that
hashicorp/go-azure-sdk
supports Meta Clients for each API Version - so if an entire Service is using a single API Version (as is the case here) - rather than defining the Client and the Resources to use inline (as we're doing here) we can instead use the Meta Client as perdatadog
to automatically have access to all clients, rather than defining these by hand.Again, this isn't something we need to change now, but should make it easier to import the Client one-time (since we try and stick to a single API Version for each Service where possible) rather than adding these across different PR's :)