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 Data Source: azurerm_api_management_subscription #27824

Merged
merged 8 commits into from
Jan 21, 2025
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
2 changes: 1 addition & 1 deletion contributing/topics/guide-new-data-source.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (ResourceGroupExampleDataSource) Attributes() map[string]*pluginsdk.Schema
}

func (ResourceGroupExampleDataSource) ModelObject() interface{} {
return &ResourceGroupExampleDataSourceModel
return &ResourceGroupExampleDataSourceModel{}
}

func (ResourceGroupExampleDataSource) ResourceType() string {
Expand Down
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,
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,
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)
}

CorrenSoft marked this conversation as resolved.
Show resolved Hide resolved
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)
},
}
}
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 = "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" {
api_management_id = azurerm_api_management.test.id
subscription_id = azurerm_api_management_subscription.test.subscription_id
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
10 changes: 8 additions & 2 deletions internal/services/apimanagement/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (

type Registration struct{}

var _ sdk.UntypedServiceRegistrationWithAGitHubLabel = Registration{}
var (
_ sdk.TypedServiceRegistration = Registration{}
_ sdk.UntypedServiceRegistrationWithAGitHubLabel = Registration{}
)

func (r Registration) AssociatedGitHubLabel() string {
return "service/api-management"
Expand Down Expand Up @@ -93,8 +96,11 @@ func (r Registration) SupportedResources() map[string]*pluginsdk.Resource {
}
}

// DataSources returns a list of Data Sources supported by this Service
func (r Registration) DataSources() []sdk.DataSource {
return []sdk.DataSource{}
return []sdk.DataSource{
ApiManagementSubscriptionDataSource{},
}
}

func (r Registration) Resources() []sdk.Resource {
Expand Down
60 changes: 60 additions & 0 deletions website/docs/d/api_management_subscription.html.markdown
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.
Loading