-
Notifications
You must be signed in to change notification settings - Fork 641
/
data_source_ibm_mqcloud_application.go
167 lines (142 loc) · 4.57 KB
/
data_source_ibm_mqcloud_application.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
// Copyright IBM Corp. 2023 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package mqcloud
import (
"context"
"fmt"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM/mqcloud-go-sdk/mqcloudv1"
)
func DataSourceIbmMqcloudApplication() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIbmMqcloudApplicationRead,
Schema: map[string]*schema.Schema{
"service_instance_guid": {
Type: schema.TypeString,
Required: true,
Description: "The GUID that uniquely identifies the MQ on Cloud service instance.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the application - conforming to MQ rules.",
},
"applications": {
Type: schema.TypeList,
Computed: true,
Description: "List of applications.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the application which was allocated on creation, and can be used for delete calls.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the application - conforming to MQ rules.",
},
"create_api_key_uri": {
Type: schema.TypeString,
Computed: true,
Description: "The URI to create a new apikey for the application.",
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this application.",
},
},
},
},
},
}
}
func dataSourceIbmMqcloudApplicationRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
mqcloudClient, err := meta.(conns.ClientSession).MqcloudV1()
if err != nil {
return diag.FromErr(err)
}
err = checkSIPlan(d, meta)
if err != nil {
return diag.FromErr(fmt.Errorf("Read Application failed %s", err))
}
serviceInstanceGuid := d.Get("service_instance_guid").(string)
// Support for pagination
offset := int64(0)
limit := int64(25)
allItems := []mqcloudv1.ApplicationDetails{}
for {
listApplicationsOptions := &mqcloudv1.ListApplicationsOptions{
ServiceInstanceGuid: &serviceInstanceGuid,
Limit: &limit,
Offset: &offset,
}
result, response, err := mqcloudClient.ListApplicationsWithContext(context, listApplicationsOptions)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error Getting Applications %s\n%s", err, response))
}
if result == nil {
return diag.FromErr(fmt.Errorf("List Applications returned nil"))
}
allItems = append(allItems, result.Applications...)
// Check if the number of returned records is less than the limit
if int64(len(result.Applications)) < limit {
break
}
offset += limit
}
// Use the provided filter argument and construct a new list with only the requested resource(s)
var matchApplications []mqcloudv1.ApplicationDetails
var name string
var suppliedFilter bool
if v, ok := d.GetOk("name"); ok {
name = v.(string)
suppliedFilter = true
for _, data := range allItems {
if *data.Name == name {
matchApplications = append(matchApplications, data)
}
}
} else {
matchApplications = allItems
}
allItems = matchApplications
if suppliedFilter {
if len(allItems) == 0 {
return diag.FromErr(fmt.Errorf("No Application found with name: \"%s\"", name))
}
d.SetId(name)
} else {
d.SetId(dataSourceIbmMqcloudApplicationID(d))
}
mapSlice := []map[string]interface{}{}
for _, modelItem := range allItems {
modelItem := modelItem
modelMap, err := dataSourceIbmMqcloudApplicationApplicationDetailsToMap(&modelItem)
if err != nil {
return diag.FromErr(err)
}
mapSlice = append(mapSlice, modelMap)
}
if err = d.Set("applications", mapSlice); err != nil {
return diag.FromErr(fmt.Errorf("Error setting applications: %s", err))
}
return nil
}
// dataSourceIbmMqcloudApplicationID returns a reasonable ID for the list.
func dataSourceIbmMqcloudApplicationID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceIbmMqcloudApplicationApplicationDetailsToMap(model *mqcloudv1.ApplicationDetails) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["id"] = model.ID
modelMap["name"] = model.Name
modelMap["create_api_key_uri"] = model.CreateApiKeyURI
modelMap["href"] = model.Href
return modelMap, nil
}