forked from hashicorp/terraform-provider-azurerm
/
data_source_api_management_product.go
98 lines (78 loc) · 2.77 KB
/
data_source_api_management_product.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package apimanagement
import (
"fmt"
"time"
"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2019-12-01/apimanagement"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func dataSourceApiManagementProduct() *schema.Resource {
return &schema.Resource{
Read: dataSourceApiManagementProductRead,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"product_id": azure.SchemaApiManagementChildDataSourceName(),
"api_management_name": azure.SchemaApiManagementDataSourceName(),
"resource_group_name": azure.SchemaResourceGroupName(),
"display_name": {
Type: schema.TypeString,
Computed: true,
},
"subscription_required": {
Type: schema.TypeBool,
Computed: true,
},
"approval_required": {
Type: schema.TypeBool,
Computed: true,
},
"published": {
Type: schema.TypeBool,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"terms": {
Type: schema.TypeString,
Computed: true,
},
"subscriptions_limit": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func dataSourceApiManagementProductRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).ApiManagement.ProductsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
resourceGroup := d.Get("resource_group_name").(string)
serviceName := d.Get("api_management_name").(string)
productId := d.Get("product_id").(string)
resp, err := client.Get(ctx, resourceGroup, serviceName, productId)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Product %q was not found in API Management Service %q / Resource Group %q", productId, serviceName, resourceGroup)
}
return fmt.Errorf("making Read request on Product %q (API Management Service %q / Resource Group %q): %+v", productId, serviceName, resourceGroup, err)
}
d.SetId(*resp.ID)
if props := resp.ProductContractProperties; props != nil {
d.Set("approval_required", props.ApprovalRequired)
d.Set("description", props.Description)
d.Set("display_name", props.DisplayName)
d.Set("published", props.State == apimanagement.Published)
d.Set("subscriptions_limit", props.SubscriptionsLimit)
d.Set("subscription_required", props.SubscriptionRequired)
d.Set("terms", props.Terms)
}
return nil
}