This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
/
groups_data_source.go
149 lines (132 loc) · 3.82 KB
/
groups_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
/*
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 GroupsDataSourceType struct {
}
type GroupsDataSource struct {
logger logging.Logger
collection *cmv1.ClustersClient
}
func (t *GroupsDataSourceType) GetSchema(ctx context.Context) (result tfsdk.Schema,
diags diag.Diagnostics) {
result = tfsdk.Schema{
Description: "List of groups.",
Attributes: map[string]tfsdk.Attribute{
"cluster": {
Description: "Identifier of the cluster.",
Type: types.StringType,
Required: true,
},
"items": {
Description: "Items of the list.",
Attributes: t.itemSchema(),
Computed: true,
},
},
}
return
}
func (t *GroupsDataSourceType) itemSchema() tfsdk.NestedAttributes {
return tfsdk.ListNestedAttributes(
map[string]tfsdk.Attribute{
"id": {
Description: "Unique identifier of the group. This is what " +
"should be used when referencing the group from other " +
"places, for example in the 'group' attribute of the " +
"user resource.",
Type: types.StringType,
Computed: true,
},
"name": {
Description: "Short name of the group for example " +
"'dedicated-admins'.",
Type: types.StringType,
Computed: true,
},
},
tfsdk.ListNestedAttributesOptions{},
)
}
func (t *GroupsDataSourceType) 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().Clusters()
// Create the resource:
result = &GroupsDataSource{
logger: parent.logger,
collection: collection,
}
return
}
func (s *GroupsDataSource) Read(ctx context.Context, request tfsdk.ReadDataSourceRequest,
response *tfsdk.ReadDataSourceResponse) {
// Get the state:
state := &GroupsState{}
diags := request.Config.Get(ctx, state)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}
// Fetch the complete list of groups of the cluster:
var listItems []*cmv1.Group
listSize := 10
listPage := 1
listRequest := s.collection.Cluster(state.Cluster.Value).Groups().List()
for {
listResponse, err := listRequest.SendContext(ctx)
if err != nil {
response.Diagnostics.AddError(
"Can't list groups",
err.Error(),
)
return
}
if listItems == nil {
listItems = make([]*cmv1.Group, 0, listResponse.Total())
}
listResponse.Items().Each(func(listItem *cmv1.Group) bool {
listItems = append(listItems, listItem)
return true
})
if listResponse.Size() < listSize {
break
}
listPage++
listRequest.Page(listPage)
}
// Populate the state:
state.Items = make([]*GroupState, len(listItems))
for i, listItem := range listItems {
state.Items[i] = &GroupState{
ID: types.String{
Value: listItem.ID(),
},
Name: types.String{
Value: listItem.ID(),
},
}
}
// Save the state:
diags = response.State.Set(ctx, state)
response.Diagnostics.Append(diags...)
}