forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_management_gateway_data_source.go
103 lines (85 loc) · 2.85 KB
/
api_management_gateway_data_source.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
99
100
101
102
103
package apimanagement
import (
"fmt"
"time"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/clients"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/apimanagement/parse"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/apimanagement/schemaz"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/apimanagement/validate"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/pluginsdk"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/timeouts"
"github.com/kevinklinger/terraform-provider-azurerm/v2/utils"
)
func dataSourceApiManagementGateway() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceApiManagementGatewayRead,
Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*pluginsdk.Schema{
"name": schemaz.SchemaApiManagementChildDataSourceName(),
"api_management_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.ApiManagementID,
},
"description": {
Type: pluginsdk.TypeString,
Computed: true,
},
"location_data": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"city": {
Type: pluginsdk.TypeString,
Computed: true,
},
"district": {
Type: pluginsdk.TypeString,
Computed: true,
},
"region": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceApiManagementGatewayRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).ApiManagement.GatewayClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
apimId, err := parse.ApiManagementID(d.Get("api_management_id").(string))
if err != nil {
return fmt.Errorf("parsing `api_management_id`: %v", err)
}
id := parse.NewGatewayID(apimId.SubscriptionId, apimId.ResourceGroup, apimId.ServiceName, d.Get("name").(string))
resp, err := client.Get(ctx, id.ResourceGroup, id.ServiceName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("making read request %s: %+v", id, err)
}
_, err = parse.GatewayID(*resp.ID)
if err != nil {
return fmt.Errorf("parsing Gateway ID %q", *resp.ID)
}
d.SetId(id.ID())
d.Set("name", resp.Name)
d.Set("api_management_id", apimId.ID())
if properties := resp.GatewayContractProperties; properties != nil {
d.Set("description", properties.Description)
d.Set("location_data", flattenApiManagementGatewayLocationData(properties.LocationData))
}
return nil
}