This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
/
cloud_providers_data_source.go
173 lines (156 loc) · 4.67 KB
/
cloud_providers_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
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
/*
Copyright (c) 2021 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package provider
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/openshift-online/ocm-sdk-go/logging"
)
type CloudProvidersDataSourceType struct {
}
type CloudProvidersDataSource struct {
logger logging.Logger
collection *cmv1.CloudProvidersClient
}
func (t *CloudProvidersDataSourceType) GetSchema(ctx context.Context) (result tfsdk.Schema,
diags diag.Diagnostics) {
result = tfsdk.Schema{
Description: "List of cloud providers.",
Attributes: map[string]tfsdk.Attribute{
"search": {
Description: "Search criteria.",
Type: types.StringType,
Optional: true,
},
"order": {
Description: "Order criteria.",
Type: types.StringType,
Optional: true,
},
"item": {
Description: "Content of the list when there is exactly one item.",
Attributes: tfsdk.SingleNestedAttributes(t.itemAttributes()),
Computed: true,
},
"items": {
Description: "Content of the list.",
Attributes: tfsdk.ListNestedAttributes(
t.itemAttributes(),
tfsdk.ListNestedAttributesOptions{},
),
Computed: true,
},
},
}
return
}
func (t *CloudProvidersDataSourceType) itemAttributes() map[string]tfsdk.Attribute {
return map[string]tfsdk.Attribute{
"id": {
Description: "Unique identifier of the cloud provider. This is what " +
"should be used when referencing the cloud provider from other " +
"places, for example in the 'cloud_provider' attribute " +
"of the cluster resource.",
Type: types.StringType,
Computed: true,
},
"name": {
Description: "Short name of the cloud provider, for example 'aws' " +
"or 'gcp'.",
Type: types.StringType,
Computed: true,
},
"display_name": {
Description: "Human friendly name of the cloud provider, for example " +
"'AWS' or 'GCP'",
Type: types.StringType,
Computed: true,
},
}
}
func (t *CloudProvidersDataSourceType) NewDataSource(ctx context.Context,
p tfsdk.Provider) (result tfsdk.DataSource, diags diag.Diagnostics) {
// Cast the provider interface to the specific implementation:
parent := p.(*Provider)
// Get the collection of clusters:
collection := parent.connection.ClustersMgmt().V1().CloudProviders()
// Create the resource:
result = &CloudProvidersDataSource{
logger: parent.logger,
collection: collection,
}
return
}
func (s *CloudProvidersDataSource) Read(ctx context.Context, request tfsdk.ReadDataSourceRequest,
response *tfsdk.ReadDataSourceResponse) {
// Get the state:
state := &CloudProvidersState{}
diags := request.Config.Get(ctx, state)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}
// Fetch the complete list of cloud providers:
var listItems []*cmv1.CloudProvider
listSize := 100
listPage := 1
listRequest := s.collection.List().Size(listSize)
if !state.Search.Unknown && !state.Search.Null {
listRequest.Search(state.Search.Value)
}
if !state.Order.Unknown && !state.Order.Null {
listRequest.Order(state.Order.Value)
}
for {
listResponse, err := listRequest.SendContext(ctx)
if err != nil {
response.Diagnostics.AddError(
"Can't list cloud providers",
err.Error(),
)
return
}
if listItems == nil {
listItems = make([]*cmv1.CloudProvider, 0, listResponse.Total())
}
listResponse.Items().Each(func(listItem *cmv1.CloudProvider) bool {
listItems = append(listItems, listItem)
return true
})
if listResponse.Size() < listSize {
break
}
listPage++
listRequest.Page(listPage)
}
// Populate the state:
state.Items = make([]*CloudProviderState, len(listItems))
for i, listItem := range listItems {
state.Items[i] = &CloudProviderState{
ID: listItem.ID(),
Name: listItem.Name(),
DisplayName: listItem.DisplayName(),
}
}
if len(state.Items) == 1 {
state.Item = state.Items[0]
} else {
state.Item = nil
}
// Save the state:
diags = response.State.Set(ctx, state)
response.Diagnostics.Append(diags...)
}