-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Data Source:
azurerm_api_management_subscription
(#27824)
* Adds new data source, including test and docs. * Empty line * gofumpt-ing * Apply feedback and update docs accordingly. * Fix format * Fix test and missing field in the data source.
- Loading branch information
1 parent
d40831d
commit 4c98155
Showing
4 changed files
with
304 additions
and
2 deletions.
There are no files selected for viewing
169 changes: 169 additions & 0 deletions
169
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,169 @@ | ||
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-sdk/resource-manager/apimanagement/2022-08-01/api" | ||
"github.com/hashicorp/go-azure-sdk/resource-manager/apimanagement/2022-08-01/apimanagementservice" | ||
"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 { | ||
ApiManagementId string `tfschema:"api_management_id"` | ||
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_id": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: apimanagementservice.ValidateServiceID, | ||
}, | ||
|
||
"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, | ||
}, | ||
|
||
"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, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.ApiManagement.SubscriptionsClient | ||
|
||
var state ApiManagementSubscriptionDataSourceModel | ||
|
||
if err := metadata.Decode(&state); err != nil { | ||
return fmt.Errorf("decoding: %+v", err) | ||
} | ||
|
||
api_management_id, err := apimanagementservice.ParseServiceID(state.ApiManagementId) | ||
if err != nil { | ||
return fmt.Errorf("parsing: %+v", err) | ||
} | ||
|
||
id := subscription.NewSubscriptions2ID(api_management_id.SubscriptionId, api_management_id.ResourceGroupName, api_management_id.ServiceName, 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 { | ||
// 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 { | ||
state.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) | ||
} | ||
state.ApiId = parsedApiId.ID() | ||
} | ||
} | ||
state.AllowTracing = pointer.From(props.AllowTracing) | ||
state.DisplayName = pointer.From(props.DisplayName) | ||
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 Primary and Secondary Keys for %s: %+v", id, err) | ||
} | ||
if model := keyResp.Model; model != nil { | ||
state.SecondaryKey = pointer.From(model.SecondaryKey) | ||
state.PrimaryKey = pointer.From(model.PrimaryKey) | ||
} | ||
|
||
return metadata.Encode(&state) | ||
}, | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
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,67 @@ | ||
// 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 = "[email protected]" | ||
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" { | ||
api_management_id = azurerm_api_management.test.id | ||
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,60 @@ | ||
--- | ||
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" { | ||
api_management_id = "example-apim" | ||
subscription_id = "example-subscription-id" | ||
} | ||
output "id" { | ||
value = data.azurerm_api_management_subscription.example.subscription_id | ||
} | ||
``` | ||
|
||
## Arguments Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `api_management_id` - (Required) The ID of the API Management Service in which this Subscription 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. |