/
data_source_ibm_api_gateway.go
193 lines (185 loc) · 5.96 KB
/
data_source_ibm_api_gateway.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package ibm
import (
"encoding/json"
"fmt"
"strings"
apigatewaysdk "github.com/IBM/apigateway-go-sdk"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceIBMApiGateway() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMApiGatewayRead,
Schema: map[string]*schema.Schema{
"service_instance_crn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Api Gateway Service Instance Crn",
},
"endpoints": {
Type: schema.TypeList,
Computed: true,
Description: "List of all endpoints of an instance",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"endpoint_id": {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint ID",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint name",
},
"routes": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Description: "Invokable routes for an endpoint",
},
"managed": {
Type: schema.TypeBool,
Computed: true,
Description: "Managed indicates if endpoint is online or offline.",
},
"shared": {
Type: schema.TypeBool,
Computed: true,
Description: "The Shared status of an endpoint",
},
"base_path": {
Type: schema.TypeString,
Computed: true,
Description: " Base path of an endpoint",
},
"managed_url": {
Type: schema.TypeString,
Computed: true,
Description: "Managed url for an endpoint",
},
"alias_url": {
Type: schema.TypeString,
Computed: true,
Description: "Alias Url for an endpoint",
},
"open_api_doc": {
Type: schema.TypeString,
Computed: true,
Description: "API document that represents endpoint",
},
"subscriptions": {
Type: schema.TypeList,
Computed: true,
Description: "List of all subscription of an endpoint",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"client_id": {
Type: schema.TypeString,
Computed: true,
Description: "Subscription Id",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Subscription name",
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: "Subscription type",
},
"secret_provided": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates if client secret is provided to subscription or not",
},
},
},
},
},
},
},
},
}
}
func dataSourceIBMApiGatewayRead(d *schema.ResourceData, meta interface{}) error {
sess, err := meta.(ClientSession).BluemixSession()
if err != nil {
return err
}
endpointservice, err := meta.(ClientSession).APIGateway()
if err != nil {
return err
}
payload := &apigatewaysdk.GetAllEndpointsOptions{}
oauthtoken := sess.Config.IAMAccessToken
oauthtoken = strings.Replace(oauthtoken, "Bearer ", "", -1)
serviceInstanceCrn := d.Get("service_instance_crn").(string)
payload.Authorization = &oauthtoken
payload.ServiceInstanceCrn = &serviceInstanceCrn
allendpoints, response, err := endpointservice.GetAllEndpoints(payload)
if err != nil {
return fmt.Errorf("Error Getting All Endpoint: %s,%d", err, response.StatusCode)
}
endpointsMap := make([]map[string]interface{}, 0, len(*allendpoints))
for _, endpoint := range *allendpoints {
ArtifactID := endpoint.ArtifactID
swaggerPayload := &apigatewaysdk.GetEndpointSwaggerOptions{}
swaggerPayload.Authorization = &oauthtoken
swaggerPayload.ID = ArtifactID
swaggerPayload.ServiceInstanceCrn = &serviceInstanceCrn
swagger, err := endpointservice.GetEndpointSwagger(swaggerPayload)
if err != nil {
return fmt.Errorf("Error Getting All Endpoint: %s,%s", err, swagger)
}
doc := swagger.Result
str, err := json.Marshal(doc)
if err != nil {
fmt.Printf("error while json Marshal: %v", err)
}
swagger_document := string(str)
SubscriptionPayload := &apigatewaysdk.GetAllSubscriptionsOptions{}
SubscriptionPayload.ArtifactID = ArtifactID
SubscriptionPayload.Authorization = &oauthtoken
if v, ok := d.GetOk("type"); ok && v != nil {
Type := v.(string)
if Type == "internal" {
Type = "bluemix"
}
SubscriptionPayload.Type = &Type
}
allsubscriptions, response, err := endpointservice.GetAllSubscriptions(SubscriptionPayload)
if err != nil {
return fmt.Errorf("Error Getting All Endpoint: %s %s", err, response)
}
subscriptionMap := make([]map[string]interface{}, 0, len(*allsubscriptions))
for _, subscription := range *allsubscriptions {
allsubscription := make(map[string]interface{})
allsubscription["name"] = *subscription.Name
allsubscription["client_id"] = subscription.ClientID
if *subscription.Type == "bluemix" {
*subscription.Type = "internal"
}
allsubscription["type"] = subscription.Type
allsubscription["secret_provided"] = subscription.SecretProvided
subscriptionMap = append(subscriptionMap, allsubscription)
}
result := make(map[string]interface{})
result["endpoint_id"] = *endpoint.ArtifactID
result["name"] = *endpoint.Name
result["managed"] = endpoint.Managed
result["shared"] = endpoint.Shared
result["routes"] = endpoint.Routes
result["managed_url"] = *endpoint.ManagedURL
result["base_path"] = endpoint.BasePath
result["alias_url"] = endpoint.AliasURL
result["open_api_doc"] = swagger_document
result["subscriptions"] = subscriptionMap
endpointsMap = append(endpointsMap, result)
}
d.SetId(serviceInstanceCrn)
d.Set("service_instance_crn", serviceInstanceCrn)
d.Set("endpoints", endpointsMap)
return nil
}