-
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 Data Source: azurerm_api_management_subscription
#27824
Merged
+305
−3
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ba18f95
Adds new data source, including test and docs.
CorrenSoft 2e32f19
Merge remote-tracking branch 'remotes/origin/main' into feature/20575
CorrenSoft 9b62fb6
Merge branch 'main' into feature/20575
CorrenSoft ff040f7
Empty line
CorrenSoft bdeebf0
gofumpt-ing
CorrenSoft 4717d5c
Apply feedback and update docs accordingly.
CorrenSoft 8000de7
Fix format
CorrenSoft 4df2569
Fix test and missing field in the data source.
CorrenSoft 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
170 changes: 170 additions & 0 deletions
170
internal/services/apimanagement/api_management_subscription_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,170 @@ | ||
package apimanagement | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/hashicorp/go-azure-helpers/lang/pointer" | ||
"github.com/hashicorp/go-azure-helpers/lang/response" | ||
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" | ||
"github.com/hashicorp/go-azure-sdk/resource-manager/apimanagement/2022-08-01/api" | ||
"github.com/hashicorp/go-azure-sdk/resource-manager/apimanagement/2022-08-01/product" | ||
"github.com/hashicorp/go-azure-sdk/resource-manager/apimanagement/2022-08-01/subscription" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/schemaz" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||
) | ||
|
||
var _ sdk.DataSource = ApiManagementSubscriptionDataSource{} | ||
|
||
type ApiManagementSubscriptionDataSource struct{} | ||
|
||
type ApiManagementSubscriptionDataSourceModel struct { | ||
ApiManagementName string `tfschema:"api_management_name"` | ||
ResourceGroupName string `tfschema:"resource_group_name"` | ||
SubscriptionId string `tfschema:"subscription_id"` | ||
AllowTracing bool `tfschema:"allow_tracing"` | ||
ApiId string `tfschema:"api_id"` | ||
DisplayName string `tfschema:"display_name"` | ||
PrimaryKey string `tfschema:"primary_key"` | ||
ProductId string `tfschema:"product_id"` | ||
SecondaryKey string `tfschema:"secondary_key"` | ||
State string `tfschema:"state"` | ||
UserId string `tfschema:"user_id"` | ||
} | ||
|
||
func (ApiManagementSubscriptionDataSource) Arguments() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{ | ||
"api_management_name": schemaz.SchemaApiManagementDataSourceName(), | ||
|
||
"resource_group_name": commonschema.ResourceGroupName(), | ||
|
||
"subscription_id": schemaz.SchemaApiManagementChildDataSourceName(), | ||
} | ||
} | ||
|
||
func (ApiManagementSubscriptionDataSource) Attributes() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{ | ||
|
||
"allow_tracing": { | ||
Type: pluginsdk.TypeBool, | ||
Computed: true, | ||
}, | ||
|
||
"api_id": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"display_name": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"primary_key": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
Sensitive: true, | ||
CorrenSoft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
|
||
"product_id": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"secondary_key": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
|
||
"state": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
"user_id": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
} | ||
} | ||
|
||
func (ApiManagementSubscriptionDataSource) ModelObject() interface{} { | ||
return &ApiManagementSubscriptionDataSourceModel{} | ||
} | ||
|
||
func (ApiManagementSubscriptionDataSource) ResourceType() string { | ||
return "azurerm_api_management_subscription" | ||
} | ||
|
||
func (ApiManagementSubscriptionDataSource) Read() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
|
||
Timeout: 5 * time.Minute, | ||
|
||
CorrenSoft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.ApiManagement.SubscriptionsClient | ||
subscriptionId := metadata.Client.Account.SubscriptionId | ||
|
||
var state ApiManagementSubscriptionDataSourceModel | ||
|
||
if err := metadata.Decode(&state); err != nil { | ||
return fmt.Errorf("decoding: %+v", err) | ||
} | ||
|
||
CorrenSoft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
id := subscription.NewSubscriptions2ID(subscriptionId, state.ResourceGroupName, state.ApiManagementName, state.SubscriptionId) | ||
|
||
resp, err := client.Get(ctx, id) | ||
if err != nil { | ||
if response.WasNotFound(resp.HttpResponse) { | ||
return fmt.Errorf("%s was not found", id) | ||
} | ||
|
||
return fmt.Errorf("retrieving %s: %+v", id, err) | ||
} | ||
|
||
metadata.SetID(id) | ||
|
||
if model := resp.Model; model != nil { | ||
if props := model.Properties; props != nil { | ||
productId := "" | ||
apiId := "" | ||
// check if the subscription is for all apis or a specific product/ api | ||
if props.Scope != "" && !strings.HasSuffix(props.Scope, "/apis") { | ||
// the scope is either a product or api id | ||
parseId, err := product.ParseProductIDInsensitively(props.Scope) | ||
if err == nil { | ||
productId = parseId.ID() | ||
} else { | ||
parsedApiId, err := api.ParseApiIDInsensitively(props.Scope) | ||
if err != nil { | ||
return fmt.Errorf("parsing scope into product/ api id %q: %+v", props.Scope, err) | ||
} | ||
apiId = parsedApiId.ID() | ||
} | ||
} | ||
state.AllowTracing = pointer.From(props.AllowTracing) | ||
state.ApiId = apiId | ||
state.DisplayName = pointer.From(props.DisplayName) | ||
state.ProductId = productId | ||
CorrenSoft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
state.State = string(props.State) | ||
state.UserId = pointer.From(props.OwnerId) | ||
} | ||
} | ||
|
||
// Primary and secondary keys must be got from this additional api | ||
keyResp, err := client.ListSecrets(ctx, id) | ||
if err != nil { | ||
return fmt.Errorf("listing Subscription %q Primary and Secondary Keys (API Management Service %q / Resource Group %q): %+v", id.SubscriptionId, id.ServiceName, id.ResourceGroupName, err) | ||
CorrenSoft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
if model := keyResp.Model; model != nil { | ||
state.SecondaryKey = pointer.From(model.SecondaryKey) | ||
state.PrimaryKey = pointer.From(model.PrimaryKey) | ||
} | ||
|
||
return metadata.Encode(&state) | ||
}, | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
internal/services/apimanagement/api_management_subscription_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,68 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package apimanagement_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" | ||
) | ||
|
||
type ApiManagementSubscriptionDataSource struct{} | ||
|
||
func TestAccDataSourceApiManagementSubscription_basic(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "data.azurerm_api_management_subscription", "test") | ||
r := ApiManagementSubscriptionDataSource{} | ||
|
||
data.DataSourceTest(t, []acceptance.TestStep{ | ||
{ | ||
Config: r.basic(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).Key("subscription_id").HasValue("test-subscription"), | ||
check.That(data.ResourceName).Key("display_name").HasValue("Test Subscription"), | ||
check.That(data.ResourceName).Key("allow_tracing").HasValue("true"), | ||
check.That(data.ResourceName).Key("state").HasValue("active"), | ||
), | ||
}, | ||
}) | ||
} | ||
|
||
func (ApiManagementSubscriptionDataSource) basic(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
|
||
resource "azurerm_resource_group" "test" { | ||
name = "accTestRG-%d" | ||
location = "%s" | ||
} | ||
|
||
resource "azurerm_api_management" "test" { | ||
name = "acctestAM-%d" | ||
publisher_name = "pub1" | ||
publisher_email = "pub1@email.com" | ||
sku_name = "Consumption_0" | ||
location = azurerm_resource_group.test.location | ||
resource_group_name = azurerm_resource_group.test.name | ||
} | ||
|
||
resource "azurerm_api_management_subscription" "test" { | ||
resource_group_name = azurerm_resource_group.test.name | ||
api_management_name = azurerm_api_management.test.name | ||
subscription_id = "test-subscription" | ||
allow_tracing = true | ||
display_name = "Test Subscription" | ||
state = "active" | ||
} | ||
|
||
data "azurerm_api_management_subscription" "test" { | ||
resource_group_name = azurerm_api_management_subscription.test.resource_group_name | ||
api_management_name = azurerm_api_management_subscription.test.api_management_name | ||
subscription_id = azurerm_api_management_subscription.test.subscription_id | ||
} | ||
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
subcategory: "API Management" | ||
layout: "azurerm" | ||
page_title: "Azure Resource Manager: Data Source: azurerm_api_management_subscription" | ||
description: |- | ||
Gets information about an existing API Management Subscription. | ||
--- | ||
|
||
# Data Source: azurerm_api_management_subscription | ||
|
||
Use this data source to access information about an existing API Management Subscription. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "azurerm_api_management_subscription" "example" { | ||
resource_group_name = "example-rg" | ||
subscription_id = "example-subscription" | ||
api_management_name = "example-apim" | ||
} | ||
|
||
output "id" { | ||
value = data.azurerm_api_management_subscription.example.subscription_id | ||
} | ||
``` | ||
|
||
## Arguments Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `api_management_name` - (Required) The Name of the API Management Service in which this Subscription exists. | ||
|
||
* `resource_group_name` - (Required) The Name of the Resource Group in which the API Management Service exists. | ||
|
||
* `subscription_id` - (Required) The Identifier for the API Management Subscription. | ||
|
||
## Attributes Reference | ||
|
||
In addition to the Arguments listed above - the following Attributes are exported: | ||
|
||
* `id` - The ID of the API Management Subscription. | ||
|
||
* `allow_tracing` - Indicates whether tracing is enabled. | ||
|
||
* `api_id` - The ID of the API assigned to this Subscription. | ||
|
||
* `display_name` - The display name of this Subscription. | ||
|
||
* `primary_key` - The primary key for this subscription. | ||
|
||
* `product_id` - The ID of the Product assigned to this Subscription. | ||
|
||
* `secondary_key` - The secondary key for this subscription. | ||
|
||
* `state` - The state of this Subscription. | ||
|
||
* `user_id` - The ID of the User assigned to this Subscription. | ||
|
||
## Timeouts | ||
|
||
The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions: | ||
|
||
* `read` - (Defaults to 5 minutes) Used when retrieving the API Management Subscription. |
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.
Let's replace these with the resource's ID since we can infer the resource group from it
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.
Applied the changes, and also updated the doc.